Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp632114imn; Tue, 26 Jul 2022 05:33:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vVeYb0iXz6WqZ2YORy3UHGzx/bY7smJYfEWhj/ugM0ljWoH/HzQ7g4jW2+7EfDhaKGqQR1 X-Received: by 2002:a05:6a00:796:b0:52c:2d1:3ae0 with SMTP id g22-20020a056a00079600b0052c02d13ae0mr5839341pfu.52.1658838783904; Tue, 26 Jul 2022 05:33:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658838783; cv=none; d=google.com; s=arc-20160816; b=ReV4Y/rpqg5YnPb392IxbbOkFEUIDYfE0LPh6Ig9suE9mao8QI8LoXv+XzWrc+u9uu sLWQ7sf1cqVivdHIP1bB9dzwqmfH74hDWf+e/jOY6JMR24hJlQ0XiV9SnFyzL/bW2a7x bJgjTaLJlsYzvig8jFQUx8jjat+h9Kn6gXrjLvPk6eWwdmv4d+L45YH2irfzJm6IGEwO Zk5FfjaRhCzVp3VUCdmbrZi1yZQ/uNWSDUBGEyohvxl4HhG8o+/DtJxLz8n2lsXeoUNp vWq/Lkd6lzq3a38Z9vONgi9HRo07/R5Ki97rIRv1wowYSKD3NBYGV3bvZ7qbL3KE/ZsL pMSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=huAD2pZwlf6UsyFSUwFIRPTakCSMqmpOF7e8Vzl53bg=; b=R0am08TG9iDaN5CAhcb+DDOh9Ad1NUxGGli/YXBTY7fZkVdwR0A7GmWT6VVLXklsXS VTEvw43KBopDbenSlRVX53SJ4URLsTdqjFRA5O6/DU2cmXVqSlRX3DJiy2u0LWkgEoGt O2aVAyaFXanpfxrHBanxBgxziSKwdkOjlZaNc5oNiHOmNCeAcDlxdaUZ2WFU222RIdHy 9sa7UjS7H7R+7+vKBPwDOMZyI/JGfoueSvtoYdWxw7AU40mDMKDfAr4ZUuMbHdu3oQqI 6Ni4+sr91GTY/utf0eQdGpBVQj9DBCs0ElrZb7Oq8Y5+BFLg91cY2wCH3yEV2iZYTn7N ionA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=J+aPaSK1; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i189-20020a6387c6000000b0041b0cbde969si3692343pge.555.2022.07.26.05.32.40; Tue, 26 Jul 2022 05:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=J+aPaSK1; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233086AbiGZMUq (ORCPT + 99 others); Tue, 26 Jul 2022 08:20:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232662AbiGZMUp (ORCPT ); Tue, 26 Jul 2022 08:20:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7C4AB4B5 for ; Tue, 26 Jul 2022 05:20:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5CF186150B for ; Tue, 26 Jul 2022 12:20:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 117DEC341C8; Tue, 26 Jul 2022 12:20:42 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="J+aPaSK1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1658838041; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=huAD2pZwlf6UsyFSUwFIRPTakCSMqmpOF7e8Vzl53bg=; b=J+aPaSK1i4COjZ42HiTmo7U2jZ3SZ+B3vxIGl2l17S/Ae33VYSVmi/guELkE41cY0kVymp 4ElGAIzVd3FwQ3g2xaati1Smqmrh+xp9GP5XpFZVaG769W8iDOxI5GaOMT7tKtB7JQYJ4Y iUbAi+uCogzvOpXMUWRH3ls/F8thaJA= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 511d27c9 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Tue, 26 Jul 2022 12:20:40 +0000 (UTC) Date: Tue, 26 Jul 2022 14:20:38 +0200 From: "Jason A. Donenfeld" To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org, Florian Weimer , Cristian =?utf-8?Q?Rodr=C3=ADguez?= , Paul Eggert , linux-crypto@vger.kernel.org Subject: Re: [PATCH v2] arc4random: simplify design for better safety Message-ID: References: <20220725225728.824128-1-Jason@zx2c4.com> <20220725232810.843433-1-Jason@zx2c4.com> <9c576e6b-77c9-88c5-50a3-a43665ea5e93@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, Jul 26, 2022 at 01:54:23PM +0200, Jason A. Donenfeld wrote: > > Also, does it make sense to fallback if we build for a kernel that should > > always support getrandom? > > I guess only if syscall filtering is a concern. But if not, then maybe > yea? We could do this in a follow-up commit, or I could do this in v4. > Would `#if __LINUX_KERNEL_VERSION >` be the right mechanism to use here? > If so, I think the way I'd implement that would be: > > [...] > > And then arc4random_getrandom_failure() being a noreturn function would > make gcc optimize out the rest. > > Does that seem like a good approach? It actually winds up looking a bit more like the below. Let me know if you want that in v4. diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c index c0f132ea9b..8fcf41e7de 100644 --- a/stdlib/arc4random.c +++ b/stdlib/arc4random.c @@ -43,7 +43,7 @@ __arc4random_buf (void *p, size_t n) { ssize_t l; - if (!atomic_load_relaxed (&have_getrandom)) + if (!__ASSUME_GETRANDOM && !atomic_load_relaxed (&have_getrandom)) break; l = __getrandom_nocancel (p, n, 0); @@ -59,7 +59,7 @@ __arc4random_buf (void *p, size_t n) arc4random_getrandom_failure (); /* Weird, should never happen. */ else if (l == -EINTR) continue; /* Interrupted by a signal; keep going. */ - else if (l == -ENOSYS) + else if (!__ASSUME_GETRANDOM && l == -ENOSYS) { atomic_store_relaxed (&have_getrandom, false); break; /* No syscall, so fallback to /dev/urandom. */ diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 74adc3956b..75d5f953d4 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -236,4 +236,11 @@ # define __ASSUME_FUTEX_LOCK_PI2 0 #endif +/* The getrandom() syscall was added in 3.17. */ +#if __LINUX_KERNEL_VERSION >= 0x031100 +# define __ASSUME_GETRANDOM 1 +#else +# define __ASSUME_GETRANDOM 0 +#endif + #endif /* kernel-features.h */