From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from Atcsqr.andestech.com (60-248-80-70.hinet-ip.hinet.net [60.248.80.70]) by sourceware.org (Postfix) with ESMTPS id 9005D3858D28 for ; Thu, 29 Sep 2022 11:13:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9005D3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=andestech.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andestech.com Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 28TBDX7r073061; Thu, 29 Sep 2022 19:13:33 +0800 (+08) (envelope-from peterlin@andestech.com) Received: from atcfdc88.andestech.com (10.0.15.120) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Thu, 29 Sep 2022 19:13:32 +0800 From: Yu Chien Peter Lin To: CC: , , , , Yu Chien Peter Lin Subject: [PATCH v2] malloc: Fix clobbered errno when getrandom failed [BZ #29624] Date: Thu, 29 Sep 2022 19:13:23 +0800 Message-ID: <20220929111323.12670-1-peterlin@andestech.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.0.15.120] X-DNSRBL: X-MAIL:Atcsqr.andestech.com 28TBDX7r073061 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 --- 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