From patchwork Thu Jan 4 00:54:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 83280 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 F373538582BF for ; Thu, 4 Jan 2024 00:56:12 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 8BF1538582BF for ; Thu, 4 Jan 2024 00:54:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BF1538582BF Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8BF1538582BF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704329674; cv=none; b=Rad36FOBr/TZwCIff+hagDk8W2gCx0RfzxEnbZ7oUElqyEvFleCFeyX/83nmwweJWznLTSgNNpyly0mgr23DLh/dB0R3Kcm3FNVJ+nNUlFMW4bZDHDWPEza57Oh08agucdxBBplypifhZnPVcXPRI4w7aZAjbvj8iIWsjVyWaQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704329674; c=relaxed/simple; bh=E0AaICR3a65kj8nLmAvMA/KzdU3AgB72csx6xMUbX6g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EhdTmmggN2HUJR3CaTimR+Q0OxchXM3kSUnlB07hwOdQbhrhvNp1C8ZkdoG7j9j+rv49ltlF2UskdcLUTFcgqIcnwskJgRKJHKMGvAE5FYmpG222ZuTzuAjlzaB1PFSOnZfv5TgUQQ2heB+CfwzqIxBDeI2fWteCxWpgd72W5Oo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1704329668; bh=E0AaICR3a65kj8nLmAvMA/KzdU3AgB72csx6xMUbX6g=; h=From:To:Cc:Subject:Date:From; b=cdTqDuxgEQDJ7qRrM5hggm5UMfAYwHhn5abqt83we+2szwrO7W5xPmWQlKTfm/RKO g6n+6tizguRVLfrA7wg52AphguWhlG+7JLPcUNdllUNHzlVuHQX0gZqG80c7ijWrDm Kbtk4huDBQMZSDroIi3ue4E/C461UuREbmEJ+mgE= Received: from stargazer.. (unknown [IPv6:240e:358:115d:3e00:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id B533F659CE; Wed, 3 Jan 2024 19:54:25 -0500 (EST) From: Xi Ruoyao To: libc-alpha@sourceware.org Cc: Adhemerval Zanella Netto , "Andreas K . Huettel" , Xi Ruoyao Subject: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY Date: Thu, 4 Jan 2024 08:54:12 +0800 Message-ID: <20240104005411.31500-2-xry111@xry111.site> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org The _nocancel functions returns errors as negative values, i. e. -EINTR for interrupted system call. But TEMP_FAILURE_RETRY expects the errors as errno, thus using TEMP_FAILURE_RETRY is incorrect here. Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of TEMP_FAILURE_RETRY to fix the issue. Signed-off-by: Xi Ruoyao --- stdlib/arc4random.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c index 3ae8fc1302..41f24c124c 100644 --- a/stdlib/arc4random.c +++ b/stdlib/arc4random.c @@ -30,6 +30,15 @@ arc4random_getrandom_failure (void) __libc_fatal ("Fatal glibc error: cannot get entropy for arc4random\n"); } +/* Special version of TEMP_FAILURE_RETRY for error values as negative + values. */ +#define TEMP_FAILURE_RETRY_NEG(expression) \ + (__extension__ \ + ({ long int __result; \ + do __result = (long int) (expression); \ + while (__result == -EINTR); \ + __result; })) + void __arc4random_buf (void *p, size_t n) { @@ -42,7 +51,7 @@ __arc4random_buf (void *p, size_t n) for (;;) { - l = TEMP_FAILURE_RETRY (__getrandom_nocancel (p, n, 0)); + l = TEMP_FAILURE_RETRY_NEG (__getrandom_nocancel (p, n, 0)); if (l > 0) { if ((size_t) l == n) @@ -60,24 +69,24 @@ __arc4random_buf (void *p, size_t n) { /* Poll /dev/random as an approximation of RNG initialization. */ struct pollfd pfd = { .events = POLLIN }; - pfd.fd = TEMP_FAILURE_RETRY ( + pfd.fd = TEMP_FAILURE_RETRY_NEG ( __open64_nocancel ("/dev/random", O_RDONLY | O_CLOEXEC | O_NOCTTY)); if (pfd.fd < 0) arc4random_getrandom_failure (); - if (TEMP_FAILURE_RETRY (__poll_infinity_nocancel (&pfd, 1)) < 0) + if (TEMP_FAILURE_RETRY_NEG (__poll_infinity_nocancel (&pfd, 1)) < 0) arc4random_getrandom_failure (); if (__close_nocancel (pfd.fd) < 0) arc4random_getrandom_failure (); atomic_store_relaxed (&seen_initialized, 1); } - fd = TEMP_FAILURE_RETRY ( + fd = TEMP_FAILURE_RETRY_NEG ( __open64_nocancel ("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOCTTY)); if (fd < 0) arc4random_getrandom_failure (); for (;;) { - l = TEMP_FAILURE_RETRY (__read_nocancel (fd, p, n)); + l = TEMP_FAILURE_RETRY_NEG (__read_nocancel (fd, p, n)); if (l <= 0) arc4random_getrandom_failure (); if ((size_t) l == n)