[PATCHv3] malloc: Make sure tcache_key is odd enough

Samuel Thibault samuel.thibault@ens-lyon.org
Sun Aug 10 21:47:54 GMT 2025


Yann Droneaud, le dim. 10 août 2025 20:39:57 +0200, a ecrit:
> Le 04/08/2025 à 11:41, Samuel Thibault a écrit :
> > @@ -3097,6 +3100,19 @@ tcache_key_initialize (void)
> >     if (__getrandom_nocancel_nostatus_direct (&tcache_key, sizeof(tcache_key),
> >   					    GRND_NONBLOCK)
> >         != sizeof (tcache_key))
> > +    tcache_key = 0;
> > +
> > +  /* We need tcache_key to be non-zero (otherwise tcache_double_free_verify's
> > +     clearing of e->key would go unnoticed and it would loop getting called
> > +     through __libc_free), and we want tcache_key not to be a
> > +     commonly-occurring value in memory, so ensure a minimum amount of one and
> > +     zero bits.  */
> > +  int minimum_bits = __WORDSIZE / 4;
> > +  int maximum_bits = __WORDSIZE - minimum_bits;
> > +
> > +  while (labs (tcache_key) <= 0x1000000
> 
> 
> I'm surprised one need the absolute value of an uintptr_t variable.

It should have been explicitly cast to intptr_t indeed (though in
practice the implicit cast will have done it).

> > +      || stdc_count_ones (tcache_key) < minimum_bits
> > +      || stdc_count_ones (tcache_key) > maximum_bits)
> 
> 
> If this is really needed, why only random_bits() is subject to those
> constraints ?

The result of __getrandom_nocancel_nostatus_direct() is also tested.

Samuel


More information about the Libc-alpha mailing list