From patchwork Thu Sep 29 16:45:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 58174 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 7F0183857C60 for ; Thu, 29 Sep 2022 16:45:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F0183857C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664469941; bh=cy43f+OvRi9fNBf9+YtXVOc9024bZDn1Vsxlc60sYvk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sTCOr1pjArksSzsusVZQH27M6TnXV2QHCh8bKQ+z/tP4t0TxLLhf9Pclagu4R/1BD pqkQNiF5LGd2lRYqEDHRoy0fUwbJjiH1LSaRkPH4UvOm5y5RoY+QxaVvIO0ckktnXK SWLUdV/xxobqq2D2JLRvucgT7YCxU6YFIfT2Iy+I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa32.google.com (mail-vk1-xa32.google.com [IPv6:2607:f8b0:4864:20::a32]) by sourceware.org (Postfix) with ESMTPS id A48423858D39 for ; Thu, 29 Sep 2022 16:45:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A48423858D39 Received: by mail-vk1-xa32.google.com with SMTP id b15so978524vkp.2 for ; Thu, 29 Sep 2022 09:45:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=cy43f+OvRi9fNBf9+YtXVOc9024bZDn1Vsxlc60sYvk=; b=c49cLRSJzgudfqVJvMQG32R+1e73nf0n5XLN+/Y+zRIFVxhe7u3n50qfkP1e7Hg7ZX LmlhyOt4U7Ffk4iHl8ZYzzf6l2vYPlB/yCGiI4OeEzfaEaBrLuUgmG3f4cy9cwmdXfwC vqWAtAbvS7EOYvJfF9wcp/wZ3NVbdNnFcyry3oMKXLHCSsgzqsB7mVZMoqtfp6Fou2fO tgm/yJqcH23Ol4HG34WAgWLmygJEE3Sa9bL7LxaUL/lq4ejuYiiJMnHFDGcyYzSGS0gd 7+a6kJO9p9+RIzvjKVTTl1nU718dM9NTGMysVzd8mnJPOfwDQEccGvFOWcvT8gHAraXs HLfQ== X-Gm-Message-State: ACrzQf22Sp5rCQDcR2bu9XVP/+IknBk8IpbNB9OS7P/PULWmdcP6Itmc oeCWaaRDHR46PWxq61zGfgXT2Dhshsys5R2a X-Google-Smtp-Source: AMsMyM7XlzeZSwLrnvy5Hn1ugZQeqGooKdrOBN9rXSl6UB5bq54TtVo2bSdIkyIt9equOqN94mtTrA== X-Received: by 2002:a05:6122:1781:b0:3a3:9de2:1d7c with SMTP id o1-20020a056122178100b003a39de21d7cmr2130326vkf.19.1664469913785; Thu, 29 Sep 2022 09:45:13 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:f485:5f41:e13d:4b46]) by smtp.gmail.com with ESMTPSA id p16-20020a056102201000b0039ae99e4723sm1820584vsr.17.2022.09.29.09.45.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 09:45:13 -0700 (PDT) To: libc-alpha@sourceware.org, Wilco Dijkstra , Yu Chien Peter Lin Subject: [PATCH] malloc: Do not clobber errno on __getrandom_nocancel (BZ#29624) Date: Thu, 29 Sep 2022 13:45:10 -0300 Message-Id: <20220929164510.3454281-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" I am not sure if changing thread cache initialization to not use getrandom syscall will add much, on recent kernel getrandom should fast (although still a syscall) and I am not sure of the performance implications. --- Use INTERNAL_SYSCALL_CALL instead of INLINE_SYSCALL_CALL. This requires emulate the semantic for hurd call (so __arc4random_buf uses the fallback). Checked on x86_64-linux-gnu. --- stdlib/arc4random.c | 4 ++-- sysdeps/mach/hurd/not-cancel.h | 11 +++++++++-- sysdeps/unix/sysv/linux/not-cancel.h | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c index e417ef624d..20886e0445 100644 --- a/stdlib/arc4random.c +++ b/stdlib/arc4random.c @@ -34,7 +34,7 @@ void __arc4random_buf (void *p, size_t n) { static int seen_initialized; - size_t l; + int l; int fd; if (n == 0) @@ -51,7 +51,7 @@ __arc4random_buf (void *p, size_t n) n -= l; continue; /* Interrupted by a signal; keep going. */ } - else if (l < 0 && errno == ENOSYS) + else if (l < 0 && l == -ENOSYS) break; /* No syscall, so fallback to /dev/urandom. */ arc4random_getrandom_failure (); } diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h index ae58b734e3..f2cb9b60ba 100644 --- a/sysdeps/mach/hurd/not-cancel.h +++ b/sysdeps/mach/hurd/not-cancel.h @@ -27,6 +27,7 @@ #include #include #include +#include /* Non cancellable close syscall. */ __typeof (__close) __close_nocancel; @@ -75,8 +76,14 @@ __typeof (__fcntl) __fcntl_nocancel; #define __fcntl64_nocancel(...) \ __fcntl_nocancel (__VA_ARGS__) -#define __getrandom_nocancel(buf, size, flags) \ - __getrandom (buf, size, flags) +static inline int +__getrandom_nocancel (void *buf, size_t buflen, unsigned int flags) +{ + int save_errno = errno; + int r = __getrandom (buf, buflen, flags); + __set_errno (save_errno); + return r == -1 ? -save_errno : r; +} #define __poll_infinity_nocancel(fds, nfds) \ __poll (fds, nfds, -1) diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h index a263d294b1..00ab75a405 100644 --- a/sysdeps/unix/sysv/linux/not-cancel.h +++ b/sysdeps/unix/sysv/linux/not-cancel.h @@ -71,7 +71,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt) static inline int __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags) { - return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags); + return INTERNAL_SYSCALL_CALL (getrandom, buf, buflen, flags); } static inline int