public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fw@deneb.enyo.de>
To: Yu Chien Peter Lin <peterlin@andestech.com>
Cc: <libc-alpha@sourceware.org>,
	 ycliang@andestech.com,  alankao@andestech.com
Subject: Re: [PATCH] malloc: Fix clobbered errno when getrandom failed [BZ #29624]
Date: Thu, 29 Sep 2022 12:00:48 +0200	[thread overview]
Message-ID: <87tu4q7b5b.fsf@mid.deneb.enyo.de> (raw)
In-Reply-To: <20220929083352.11890-1-peterlin@andestech.com> (Yu Chien Peter Lin's message of "Thu, 29 Sep 2022 16:33:52 +0800")

* Yu Chien Peter Lin:

> The patch resets errno when getrandom syscall failed, which 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 | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 953183e956..21f2bf5431 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -3140,6 +3140,7 @@ tcache_key_initialize (void)
>  #if __WORDSIZE == 64
>        tcache_key = (tcache_key << 32) | random_bits ();
>  #endif
> +      __set_errno(0);
>      }
>  }

Sorry, this is wrong for the dynamically linked case because we do not
call malloc before calling the main function.  The first call to
malloc will set errno to 0, which is observable by the application in
this case.  And such errno-setting behavior is not permitted by POSIX.

You need to save and restore errno instead.

  reply	other threads:[~2022-09-29 10:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29  8:33 Yu Chien Peter Lin
2022-09-29 10:00 ` Florian Weimer [this message]
2022-09-29 12:42 Wilco Dijkstra
2022-09-29 13:25 ` 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=87tu4q7b5b.fsf@mid.deneb.enyo.de \
    --to=fw@deneb.enyo.de \
    --cc=alankao@andestech.com \
    --cc=libc-alpha@sourceware.org \
    --cc=peterlin@andestech.com \
    --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).