On Sat, May 03, 2025 at 11:02:57PM +0800, Herbert Xu wrote:
> On Sat, May 03, 2025 at 05:39:16PM +0300, Jarkko Sakkinen wrote:
> > On Wed, Apr 30, 2025 at 06:25:53PM +0300, Jarkko Sakkinen wrote:
> > > Rely only on the memory ordering of spin_unlock() when setting
> > > KEY_FLAG_FINAL_PUT under key->user->lock in key_put().
> > > 
> > > Signed-off-by: Jarkko Sakkinen <jar...@kernel.org>
> > > ---
> > >  security/keys/key.c | 6 ++++--
> > >  1 file changed, 4 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/security/keys/key.c b/security/keys/key.c
> > > index 7198cd2ac3a3..aecbd624612d 100644
> > > --- a/security/keys/key.c
> > > +++ b/security/keys/key.c
> > > @@ -656,10 +656,12 @@ void key_put(struct key *key)
> > >                           spin_lock_irqsave(&key->user->lock, flags);
> > >                           key->user->qnkeys--;
> > >                           key->user->qnbytes -= key->quotalen;
> > > +                         set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> > >                           spin_unlock_irqrestore(&key->user->lock, flags);
> > > +                 } else {
> > > +                         set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> > > +                         smp_mb(); /* key->user before FINAL_PUT set. */
> > >                   }
> > > -                 smp_mb(); /* key->user before FINAL_PUT set. */
> > > -                 set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> > 
> > Oops, my bad (order swap), sorry. Should have been:
> >     
> >                             spin_unlock_irqrestore(&key->user->lock, flags);
> >                     } else {
> >                             smp_mb(); /* key->user before FINAL_PUT set. */
> 
> You can use smp_mb__before_atomic here as it is equivalent to
> smp_mb in this situation.
> 
> >                     }
> >                     set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> > 
> > Should spin_lock()/unlock() be good enough or what good does smp_mb() do
> > in that branch? Just checking if I'm missing something before sending
> > fixed version.
> 
> I don't think spin_unlock alone is enough to replace an smp_mb.
> A spin_lock + spin_unlock would be enough though.
> 
> However, looking at the bigger picture this smp_mb looks bogus.
> What exactly is it protecting against?
> 
> The race condition that this is supposed to fix should have been
> dealt with by the set_bit/test_bit of FINAL_PUT alone.  I don't
> see any point in having this smb_mb at all.

smp_mb() there makes sure that key->user change don't spill between
key_put() and gc.

GC pairs smp_mb() in key_put() after FINAL_PUT to make sure that also
in its side key->user changes have been walled before moving the key
as part of unrefenced keys.

See also [1]. It cleared this up for me. Here user->lock easily misleads
to overlook the actual synchronization scheme.

> 
> Cheers,
> -- 
> Email: Herbert Xu <herb...@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

[1] https://lore.kernel.org/keyrings/1121543.1746310...@warthog.procyon.org.uk/

BR, Jarkko

Reply via email to