public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Yu Chien Peter Lin <peterlin@andestech.com>
To: <libc-alpha@sourceware.org>
Cc: <alankao@andestech.com>, <ycliang@andestech.com>,
	<fw@deneb.enyo.de>, <dylan@andestech.com>,
	Yu Chien Peter Lin <peterlin@andestech.com>
Subject: [PATCH v2] malloc: Fix clobbered errno when getrandom failed [BZ #29624]
Date: Thu, 29 Sep 2022 19:13:23 +0800	[thread overview]
Message-ID: <20220929111323.12670-1-peterlin@andestech.com> (raw)

Save and restore errno when getrandom failed. On failure it will result
in errno clobbered at statically linked program startup. This scenario
is possible if getrandom is called by tcache_key_initialize when crng is
not ready thus EAGAIN is returned.

Fixes bug 29624.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
---
 malloc/malloc.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 953183e956..823d454c99 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3133,9 +3133,11 @@ static uintptr_t tcache_key;
 static void
 tcache_key_initialize (void)
 {
+  int saved_errno = errno;
   if (__getrandom_nocancel (&tcache_key, sizeof(tcache_key), GRND_NONBLOCK)
       != sizeof (tcache_key))
     {
+      __set_errno(saved_errno);
       tcache_key = random_bits ();
 #if __WORDSIZE == 64
       tcache_key = (tcache_key << 32) | random_bits ();
-- 
2.34.1


             reply	other threads:[~2022-09-29 11:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 11:13 Yu Chien Peter Lin [this message]
2022-09-29 11:39 ` Adhemerval Zanella Netto
2022-09-29 13:31   ` Florian Weimer
2022-09-29 13:51     ` Adhemerval Zanella Netto

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=20220929111323.12670-1-peterlin@andestech.com \
    --to=peterlin@andestech.com \
    --cc=alankao@andestech.com \
    --cc=dylan@andestech.com \
    --cc=fw@deneb.enyo.de \
    --cc=libc-alpha@sourceware.org \
    --cc=ycliang@andestech.com \
    /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).