Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp275726imn; Mon, 25 Jul 2022 16:16:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1utSFEMN+o90q5AUYK/hlPs6buxwGVB6Cv1BDla3DzYuWdpK6jqwz+1xcnf/GoyG0gniK+J X-Received: by 2002:a65:694d:0:b0:412:a2f2:9f1e with SMTP id w13-20020a65694d000000b00412a2f29f1emr12487757pgq.541.1658790981500; Mon, 25 Jul 2022 16:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658790981; cv=none; d=google.com; s=arc-20160816; b=UV9FUWkKqF7bBOwfGMm0DnYaNIcBYK6dZ4/miB5USkNfkgwS7ouhkDk9bKtC/V2iBL Qb13idhhp/mFhjphskUX1HraFYgjJRUIUU/Y0iWexJQgmmr/Km6dTLRtvSVi34XIFXbP aycm6/N0LGmE3TdQIbI/5XOMsUQJmtXTi6Va/xRQkV//I+FGL/FNYDLdT+jePG8RmH2n vNsZuaVpoSqY9kIDVR5rsTTrJTkWc7zoOSofvo2NM6du5vCc5VULChuAnatvSiuro4zu tNrI9Z1KV3d9RksjevQAAVZ5XaT9eejTvKLLKw0lP4rWdrgeeaIWhEz1ubnsfFaszscM B7Ww== 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=zonKIOJ2fmlfI/iMb+LZK9aQ2EHiBAm9kqAGXEH1jK0=; b=pI7bkgAxQao0bKJkAJKzWmMD+3aG5Pt2gStDP80fHwyDByjJOXm2K6RPKpUG3jNJ+n pHjQQaJgOwV8DffO8AIwBF/VeEdAo1uNX810wIgJxgmiDNFG6HgAG+BghDLUoYvlKPxl ETIJ3K4svWRkDK4qrlTJ21SRxAXQ1T+qcoGT3J66gRQkxgQmxrOEprtThag4O0SwihKk K9vPO1ISJwbA+i/rofQHTojsdq1kdN1LMsi+3cfUpUn/S687Yxdb1dV8EY0dm4DHRwiJ 5DSxbIhL8B8VaZSFD3+9AQsaK8fKeVMK8i8PM2BQrngHVfZr+rm1olV4d2CFdJXoXK7t Tylg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=LBHOcs51; 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 s9-20020a17090b070900b001f061a4d77fsi17366846pjz.42.2022.07.25.16.16.07; Mon, 25 Jul 2022 16:16:21 -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=LBHOcs51; 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 S236866AbiGYXLf (ORCPT + 99 others); Mon, 25 Jul 2022 19:11:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237595AbiGYXLb (ORCPT ); Mon, 25 Jul 2022 19:11:31 -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 429262613E for ; Mon, 25 Jul 2022 16:11:30 -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 B9C2861444 for ; Mon, 25 Jul 2022 23:11:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79A57C341C6; Mon, 25 Jul 2022 23:11:28 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="LBHOcs51" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1658790687; 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=zonKIOJ2fmlfI/iMb+LZK9aQ2EHiBAm9kqAGXEH1jK0=; b=LBHOcs51Df5uhRvh0NDxL1bA8na9UDxXZUye1546ycyHT5ZbIlCZCSi2TUzzbuReIGACWj qgyVI/ZWg3Q2C6SVl8fOjJbhXzIN4I8mOQJfidmiM94ozETc7qZNAkWDERpm2M3t+ay3B/ gZP0bgxXYY2xBmMwlDIN6gWGRwbvJb0= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 0756cbc2 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Mon, 25 Jul 2022 23:11:26 +0000 (UTC) Date: Tue, 26 Jul 2022 01:11:24 +0200 From: "Jason A. Donenfeld" To: libc-alpha@sourceware.org Cc: Adhemerval Zanella Netto , Florian Weimer , Cristian =?utf-8?Q?Rodr=C3=ADguez?= , Paul Eggert , linux-crypto@vger.kernel.org Subject: Re: [PATCH] arc4random: simplify design for better safety Message-ID: References: <20220725225728.824128-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220725225728.824128-1-Jason@zx2c4.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 If you're just following along on the mailing list, without actively trying to apply this to a glibc tree, that diff might be hard to read. The meat of it is the below function implementation. Notably this is basically the same as systemd's crypto_random_bytes() (which I recently rewrote there). void __arc4random_buf (void *p, size_t n) { static bool have_getrandom = true, seen_initialized = false; int fd; if (n == 0) return; for (;;) { ssize_t l; if (!have_getrandom) break; 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 other error, should never happen. */ } if (!seen_initialized) { 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 (); seen_initialized = true; } fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOCTTY); if (fd < 0) arc4random_getrandom_failure (); while (n) { ssize_t l = TEMP_FAILURE_RETRY (__read_nocancel (fd, p, n)); if (l <= 0) arc4random_getrandom_failure (); p = (uint8_t *) p + l; n -= l; } if (__close_nocancel (fd) < 0) arc4random_getrandom_failure (); } libc_hidden_def (__arc4random_buf) weak_alias (__arc4random_buf, arc4random_buf)