The Compassionate Investigational New Drug program is based on the common law defence of necessity, that is, if you 'need' to possess a controlled substance in the relevant sense then you can't be convicted of an offence for possessing it.
The program might well have been illegal (clearly the United States thought it was illegal if they opted for settlement rather than just dropping the charges) but the nature of settlement is that you agree to things you don't agree with.
Whether the United States lets you do whatever or whether they decided to prosecute you is up to the relevant prosecutors, who have a discretion about whether to begin a prosecution at all and, if they have begun a prosecution, they have a discretion to abandon it or to agree to a settlement.
In subsequent years, the United States decided to push back against the medical necessity defence and won in the Supreme Court: United States v Oakland Buyers' Cooperative (2001) 532 US 483.
However, since the people receiving marijuana under the Compassionate Investigational New Drug program were parties to a settlement with the United States, the United States must continue the program for them despite the Supreme Court confirming its illegality (or, to be more precise, confirming that absent a settlement it would have been illegal).