Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp569918imn; Tue, 26 Jul 2022 03:52:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tqa0igDVFqFreip5fGFOaudNFR5bhbU9OXOXSCJ3LenspUxA/CuzS+jnTZ533pFZzu+zgJ X-Received: by 2002:aa7:c90b:0:b0:43c:86bc:10df with SMTP id b11-20020aa7c90b000000b0043c86bc10dfmr1127977edt.174.1658832770259; Tue, 26 Jul 2022 03:52:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658832770; cv=none; d=google.com; s=arc-20160816; b=ZVr5pT1VILu8iixBZpHq+ziqKBNmM/79LAV5Z9Qlq6FIWjLlzrS/zSPxIaOqfeoaVO IBIWVquSgu68FPwLQL/MtbXVP6WHX7sC7W6LQn/jByv/aBwMLLOSoTpbmNfBCmruceyo AcEV4rsSiL98+F1Qn31I/Di+L5yCcJ1kXpWlrdjvg484Or5tIQgX2UxrTyiOknongDSG O1IGfNI1mUDMoJyfdm9nEtZpblW864KOsQfSnC65RRDvmEdCOJb5deELtsehQdRYQ0E+ +h4xkb5oGItDDXGMFgLpTveBGNKoWDI5KNv6CIj5PXil9W+LutUOyEv975el0OOOqo9b PpFQ== 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=s5T7GLuLEawFWgu0Mrm68OBBezaPyZ8IkeuWW81SFHc=; b=H5uy9r9/ePR2eois2BmO5HpeJR27LfJ1VhttQ/YdETUgrMi+RlhYbhgcPne2AAmIsr 5OSJwg0HXpPntVXODxNLXdESevhbx6uOkcNFSwZFUNRd2CD6b2LR5yF1rQvI1Sy2UP0/ U+1NUT6ZouSv3clm567FKfzyteHHDGsWQYJuRtwMnkdVS5gR8pYeKXW+u3cg2yQPMCzy cO5neTdDamun2vspBdr/W1Xxvsx4xvBt/B4uQBw3EDAZN8NXxRqR0dI8mqJHBd1O4Tqi E6E8BfHLIXMsodamaRigsyO2PtLTPVErsqiYmIRdjy8+UuL2qje/C2aTG3469FRRbPFK Ihmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b="h/n1pjMJ"; 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 sd25-20020a1709076e1900b00726b8c59accsi15917378ejc.787.2022.07.26.03.52.14; Tue, 26 Jul 2022 03:52:50 -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="h/n1pjMJ"; 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 S238483AbiGZKlW (ORCPT + 99 others); Tue, 26 Jul 2022 06:41:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232504AbiGZKlV (ORCPT ); Tue, 26 Jul 2022 06:41:21 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81886E0F1 for ; Tue, 26 Jul 2022 03:41:20 -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 ams.source.kernel.org (Postfix) with ESMTPS id 4A419B812A2 for ; Tue, 26 Jul 2022 10:41:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 631C9C341C0; Tue, 26 Jul 2022 10:41:17 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="h/n1pjMJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1658832075; 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=s5T7GLuLEawFWgu0Mrm68OBBezaPyZ8IkeuWW81SFHc=; b=h/n1pjMJedGhQPGsBbzcJJwlGt/cWArUG/rvHGdZbtCE4jnhf64bJ2WYc/YNLXOfdVwg8K Re5WMVRccu8IDj7uQicPMhGgSIhz/6YBp14ae4m0Rg0ffDqDnWAqT6Rk7PQ7d+xcKOSr6d lzYegFb8a1Zw+ZxijkwqQnf1xx1IcwM= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 5d45f5c3 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Tue, 26 Jul 2022 10:41:15 +0000 (UTC) Date: Tue, 26 Jul 2022 12:41:13 +0200 From: "Jason A. Donenfeld" To: Mark Harris Cc: libc-alpha@sourceware.org, Florian Weimer , 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> 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 Hi Mark, On Mon, Jul 25, 2022 at 06:10:06PM -0700, Mark Harris wrote: > Jason A. Donenfeld wrote: > > + l = __getrandom_nocancel (p, n, 0); > > + if (l > 0) > > + { > > + if ((size_t) l == n) > > + return; /* Done reading, success. */ > > + p = (uint8_t *) p + l; > > + n -= l; > > + continue; /* Interrupted by a signal; keep going. */ > > + } > > + else if (l == 0) > > + arc4random_getrandom_failure (); /* Weird, should never happen. */ > > + else if (errno == ENOSYS) > > + { > > + have_getrandom = false; > > + break; /* No syscall, so fallback to /dev/urandom. */ > > + } > > + arc4random_getrandom_failure (); /* Unknown error, should never happen. */ > > Isn't EINTR also possible? Aborting in that case does not seem reasonable. Not in current kernels, where it always returns at least PAGE_SIZE bytes before checking for pending signals. In older kernels, if there was a signal pending at the top, it would do no work and return -ERESTARTSYS, which I believe should then get restarted by glibc's syscaller? I might be wrong about how restarts work though, so if you know better, please let me know. TEMP_FAILURE_RETRY relies on errno, so that's not what we want. I guess I can just add a case for it. > Also the __getrandom_nocancel function does not set errno on Linux; it > just returns INTERNAL_SYSCALL_CALL (getrandom, buf, buflen, flags). > So unless that is changed, it doesn't look like this ENOSYS check will > detect old Linux kernels. Thanks. It looks like INTERNAL_SYSCALL_CALL just returns the errno as-is as a return value, right? I'll adjust the code to account for that. > > + struct pollfd pfd = { .events = POLLIN }; > > + pfd.fd = TEMP_FAILURE_RETRY ( > > + __open64_nocancel ("/dev/random", O_RDONLY | O_CLOEXEC | O_NOCTTY)); > > + if (pfd.fd < 0) > > + arc4random_getrandom_failure (); > > + if (__poll (&pfd, 1, -1) < 0) > > + arc4random_getrandom_failure (); > > + if (__close_nocancel (pfd.fd) < 0) > > + arc4random_getrandom_failure (); > > The TEMP_FAILURE_RETRY handles EINTR on open, but __poll can also > result in EINTR. Thanks. I'll surround the __poll in TEMP_FAILURE_RETRY. Thank you for the review! v3 will have the above changes. Jason