public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Siddhesh Poyarekar <siddhesh@sourceware.org>, libc-alpha@sourceware.org
Cc: fweimer@redhat.com, Eyal Itkin <eyalit@checkpoint.com>
Subject: Re: [PATCH] Harden tcache double-free check
Date: Wed, 7 Jul 2021 14:35:30 -0300	[thread overview]
Message-ID: <b61b0597-3c87-c648-e2dc-1c04ea7deeea@linaro.org> (raw)
In-Reply-To: <20210707012919.1298612-1-siddhesh@sourceware.org>



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
a uint_fast64_t, so I think it would be better to make it random_bits()
return 64-bit instead.

Also, calling random_bits() in a sequence will result in quite low
entropy, since it uses __clock_gettime64().  Maybe you could add
XOR the pid for the higher bits:

  static inline uint64_t
  random_bits (void)
  {
    struct __timespec64 tv;
    __clock_gettime64 (CLOCK_MONOTONIC, &tv);
    /* Shuffle the lower bits to minimize the clock bias.  */
    uint32_t ret_low = tv.tv_nsec ^ tv.tv_sec;
    ret_low ^= (ret_low << 24) | (ret_low >> 8);
    /* And add a lit more entropy for higher bits.  */
    uint32_t ret_high = ret_low ^ (uint32_t) __getpid ();
    return ((uint64_t) ret_high << 32) | ret_low;
  }

  parent reply	other threads:[~2021-07-07 17:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-07  1:29 Siddhesh Poyarekar
2021-07-07 17:17 ` Florian Weimer
2021-07-07 17:34   ` [PATCH v2] " Siddhesh Poyarekar
2021-07-07 17:35 ` Adhemerval Zanella [this message]
2021-07-07 17:58   ` [PATCH] " Siddhesh Poyarekar
2021-07-07 18:09     ` Adhemerval Zanella
2021-07-07 18:12       ` Siddhesh Poyarekar
2021-07-07 18:27         ` Adhemerval Zanella
2021-07-07 18:28           ` Siddhesh Poyarekar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b61b0597-3c87-c648-e2dc-1c04ea7deeea@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=eyalit@checkpoint.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=siddhesh@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).