Different countries go by different laws I guess. Here in France the rule is "no plagiarism", and even if there is no legal definition of "plagiarism", the usual definition is "you must submit your own work and, when using something that is not your own work, you must quote it correctly."
If I'm asking you "write a program that solves this problem", or "write an essay about that topic", you can certainly find a solution online that has a very permissive licence letting you use it any way you want. Good for you, but that's still not your own work and will potentially put you in deep trouble if you use it. Ditto with anything produced by ChatGPT: not your work. You don't own that. You can write "according to ChatGPT, ..." (although you probably won't impress the teacher with that), or you can get inspiration from the output to produce your own work, but not use it as is and pretend you did it.
> And nobody is ever in "legal trouble" for a copyright violation in a typical school assignment... because unless the student work is publically published, the owner of the copyright is never going to know what you turned into your teacher.
Many univs automatically runs a plagiarism-checker on anything submitted by students. Sometimes one of them gets caught. In France, that's enough to be, worse case scenario (although that rarely happens), banned from taking any public exam or work for the government, for your whole life.