[PATCH] Harden tcache double-free check
Adhemerval Zanella
adhemerval.zanella@linaro.org
Wed Jul 7 18:09:00 GMT 2021
On 07/07/2021 14:58, Siddhesh Poyarekar wrote:
> On 7/7/21 11:05 PM, Adhemerval Zanella via Libc-alpha wrote:
>>
>>
>> On 06/07/2021 22:29, Siddhesh Poyarekar via Libc-alpha wrote:
>>
>>> +/* Process-wide key to try and catch a double-free in the same thread. */
>>> +static uintptr_t tcache_key;
>>> +
>>> +/* The value of tcache_key does not really have to be a cryptographically
>>> + secure random number. It only needs to be arbitrary enough so that it does
>>> + not collide with values present in applications, which would be quite rare,
>>> + about 1 in 2^wordsize. */
>>> +static void
>>> +tcache_key_initialize (void)
>>> +{
>>> + if (__getrandom (&tcache_key, sizeof(tcache_key), GRND_NONBLOCK)
>>> + != sizeof (tcache_key))
>>> + {
>>> + tcache_key = random_bits ();
>>> +#if __WORDSIZE == 64
>>> + tcache_key = (tcache_key << 32) | random_bits ();
>>> +#endif
>>
>> The other usage for ramdom_bits at sysdeps/posix/tempname.c already uses
>
> tempname.c seems to have its own random_bits function (i.e. it just happens to have the same name); it doesn't use the one in random_bits.h AFAICT. All other users of random_bits() use 32-bit. Entropy isn't really a concern in the above use case, it's just a key to avoid collisions.
Yes, but it is essentially what you are doing here: query getrandom
with GRND_NONBLOCK a falling back to clock_gettime. And we also
have the random_bits from include/random-bits.h.
I think we would better to consolidate it with only one implementation.
More information about the Libc-alpha
mailing list