Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp581131imn; Tue, 26 Jul 2022 04:10:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tA1RjonhH0huNL/ofy47zivcpO7+r3at39wwCdcsJd/8rDcgaVTFo0qTb8P/Y7uK8pifdv X-Received: by 2002:a17:907:948e:b0:72d:3fd2:5da0 with SMTP id dm14-20020a170907948e00b0072d3fd25da0mr13691027ejc.225.1658833859127; Tue, 26 Jul 2022 04:10:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658833859; cv=none; d=google.com; s=arc-20160816; b=M8mF9TsxFGdZ3Thea8kojeTSrAV9RWwsRxTr5YX1xrJa6IRbDzeYqdzwT4+OfDCoH9 UwQEiQOnjO1qUxb/YNVrmL2q3fO7kIncEwZbEdqz99FfuBgssE37zyFHiC0YOV++3MHX HEPB0V93SKHC7+Y2N85ZBgR1GTP5vAHC5GjEItG6hMZ9rO/mthQlFyz7bEDu/68LPY0h yl5wLUyVJcSmtPyMn7atqz8E9zqIfLI+3fVkzXwTskecDWYSroF4UKv1XOCjIGkjwi85 UO7qY7nrC9Mv2WW/a0xS3bCKGbrgTJKRDTYxZUMWXuRq4YG+4C55sdIf+Wal2AkxooCB ORBw== 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=g2RgjLln+xJNTK52Wm9pWE8jo2IoA4T7LjlDVKu/LMI=; b=ewbNVUVhUm/QGGOR0nd7YIdsIx0z2z//nWf5o+YfDpFCcH5msFNMWOzmb7YlB+/7Kg mU6zDi3+SIhUGk0a6rBL4ssbUpHx/6iPPZuNtozrCGUmvarAWl85OFOlrON8/pZURGJd gq1ew0vBvn/9BgZSbbMQ0lm6DFCg8xWIz023LY3slzLKU+R4Psrt0ogp5COGKxnGpc8D 2HUtNHdUZI8CWO4LXuc4pvgax0TINRp5Z6i4fOaKSdDjRLgShbdEGuOemB90UU01XNZ2 2QviTVYm5SWTA1OYu7CR/v/2wizfI89taMXTC8SxQe4nXc3B+TXBXLY1SBCbYY25bREy vCBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=jdPdZN3i; 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 ds9-20020a170907724900b00718d1cddac7si16758702ejc.2.2022.07.26.04.10.27; Tue, 26 Jul 2022 04:10:59 -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=jdPdZN3i; 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 S232674AbiGZLEd (ORCPT + 99 others); Tue, 26 Jul 2022 07:04:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231335AbiGZLEd (ORCPT ); Tue, 26 Jul 2022 07:04:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 306372F64B for ; Tue, 26 Jul 2022 04:04:32 -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 D5C58B811C5 for ; Tue, 26 Jul 2022 11:04:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C89B8C341C0; Tue, 26 Jul 2022 11:04:28 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="jdPdZN3i" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1658833467; 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=g2RgjLln+xJNTK52Wm9pWE8jo2IoA4T7LjlDVKu/LMI=; b=jdPdZN3irEWzI080wFpQAjuFadRXbl7f8c358SDNn5pgmT0QuybLMIAM+7QW8eo5VBgPWb R0KvtVVWurYuN8me5fyRgWws2CI09ZtNVY/bOabK6KI0pc5VuCeAQ8gMojS1qIhGHPCd9a T4RjpvjaSbJACAdrv0rijG+Wq33D/qs= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id fbf47f6c (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Tue, 26 Jul 2022 11:04:27 +0000 (UTC) Date: Tue, 26 Jul 2022 13:04:25 +0200 From: "Jason A. Donenfeld" To: Florian Weimer Cc: libc-alpha@sourceware.org, Adhemerval Zanella Netto , 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> <87k080i4fo.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <87k080i4fo.fsf@oldenburg.str.redhat.com> 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 Florian, On Tue, Jul 26, 2022 at 11:55:23AM +0200, Florian Weimer wrote: > * Jason A. Donenfeld: > > > + 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 (); > > What happens if /dev/random is actually /dev/urandom? Will the poll > call fail? Yes. I'm unsure if you're asking this because it'd be a nice simplification to only have to open one fd, or because you're worried about confusion. I don't think the confusion problem is one we should take too seriously, but if you're concerned, we can always fstat and check the maj/min. Seems a bit much, though. > I think we need a no-cancel variant of poll here, and we also need to > handle EINTR gracefully. Thanks for the note about poll nocancel. I'll try to add this. I don't totally know how to manage that pluming, but I'll give it my best shot. > Performance-wise, my 1000 element shuffle benchmark runs about 14 times > slower without userspace buffering. (For comparison, just removing > ChaCha20 while keeping a 256-byte buffer makes it run roughly 25% slower > than current master.) Our random() implementation is quite slow, so > arc4random() as a replacement call is competitive. The unbuffered > version, not so much. Yes, as mentioned, this is slower. But let's get something down first that's *correct*, and then after we can start optimizing it. Let's not prematurely optimize and create a problematic function that nobody should use. > Running the benchmark, I see 40% of the time spent in chacha_permute in > the kernel, that is really quite odd. Why doesn't the system call > overhead dominate? Huh, that is interesting. I guess if you're reading 4 bytes for an integer, it winds up computing a whole chacha block each time, with half of it doing fast key erasure and half of it being returnable to the caller. When we later figure out a safer way to buffer, ostensibly this will go away. But for now, we really should not prematurely optimize. I'll have v3 out shortly with your suggested fixes. Jason