From patchwork Thu Sep 29 11:13:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-Chien Peter Lin X-Patchwork-Id: 58164 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A802138582A2 for ; Thu, 29 Sep 2022 11:13:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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: 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 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fw@deneb.enyo.de, ycliang@andestech.com, dylan@andestech.com, alankao@andestech.com, Yu Chien Peter Lin Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 ();