It has to somehow know what a naked minor looks like.
Not necessarily
You need to feed it CSAM
You don't. You just need lists of other things, properly tagged. If you feed an AI a bunch of clothed adults and a bunch of naked adults, it will, in theory, "understand" the difference between being clothed and naked and create any of its clothed adults, naked.
With that initial set above, you feed it a bunch of clothed children. When you ask for a naked child, it will either produce a child head with naked adult body, or a "weird" naked child. It "understands" that adult and child are different things, that clothed and naked are different things, and tries to infer what "naked child" looks like from what it "knows".
So is it right to be using images of real children to train these AI?
This is the real question and one I don't know the answer to, because it will boil down to consent to being part of a training model, whether your own as an adult, or a child's parent, much like how it works for stock photos and videos.
"I consent to having my likeness used for AI training models, except for any use that involves NSFW content" - Fair enough. Good luck enforcing that.