Re: [PATCH v2] keys: Fix UAF in key_put()
From: David Howells
Date: Thu Mar 20 2025 - 12:39:36 EST
Jarkko Sakkinen <jarkko@xxxxxxxxxx> wrote:
> > + if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) {
> > + smp_mb(); /* Clobber key->user after FINAL_PUT seen. */
>
> test_bit() is already atomic.
Atomiticity doesn't apply to test_bit() - it only matters when it does two (or
more) accesses that must be perceptually indivisible (e.g. set_bit doing RMW).
But atomiticity isn't the issue here, hence the barrier. You need to be
looking at memory-barriers.txt, not atomic_bitops.txt.
We have two things to correctly order and set_bit() does not imply sufficient
barriering; test_and_set_bit() does, but not set_bit(), hence Linus's comment
about really wanting a set_bit_release().
> > + smp_mb(); /* key->user before FINAL_PUT set. */
> > + set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
>
> Ditto.
Ditto. ;-)
> Nit: I'm just thinking should the name imply more like that "now
> key_put() is actually done". E.g., even something like KEY_FLAG_PUT_DONE
> would be more self-descriptive.
KEY_FLAG_PUT_DONE isn't right. There can be lots of puts on a single key -
only the one that reduces it to 0 matters for this. You could call it
KEY_FLAG_CAN_NOW_GC or KEY_FLAG_GC_ABLE.
David