Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp686057rwn; Thu, 8 Sep 2022 07:28:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR6IXO42tg/UELCaxPSOjLrs+q4X1Ki8P9RlTuxaBsUTyvGKHd4xLgpkXSPJ7JTLbOOy9/2p X-Received: by 2002:a05:6512:1587:b0:481:1946:8e1c with SMTP id bp7-20020a056512158700b0048119468e1cmr3039201lfb.163.1662647301633; Thu, 08 Sep 2022 07:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662647301; cv=none; d=google.com; s=arc-20160816; b=JeK27WqhqREJxVIkSBr7fvgKYsVyKkOLmbIvD+9tmK7C5F36t4xVYc7myEiQ0bDFxk vDZ53Biem6L8/vgAmYU7zIDZL9mz0Q3J+qYvDVbw6Ak02l3GfH6FXrWP4DRRQWH9Sr90 fwPtl8sHiflAtqP0rw1iZz2KQH4Lp1IChO1hYuZXg1uLliQwe7eOYZkXorkFLNOg4qF9 3+rLh8c+YGS83Fgbq+urZEjhBaUTp8fqIngMpRvdrMg0/Liz31S2LsnG0Gx49iaWOO04 8nrsgbLBKxnS3jOitqb5FYaR1QX1zDK178g3x8b+8bYBeeaLwk3IZEK2k4IIiD356WZE bKIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tiDiqwWP8LXWHrwhahDSsAig51VVDPd45ZIiXF6w6yk=; b=B7tlV7SooDMvKavMyJI7HvXOjAg5g8vU8KulBQimSWU2Xrw1b1mhkVcbq6+TAqGjFh GW/5PaEen4/ude7Ku4EcY53XZgdCTT0MrtfXfnhhziMt4D2zNvJ1p3vdAkT6kEXZc6SG lTAx67iN0FdJJpv/FP2to74S2ljd5u6STNPHUDqPGLSB9y3r2+94lcWsoeoElNoUWDPP X38wJRG1hVZY6QwqtNIH6ugZpQXQz3mLegaLF/UVEScdS/XgXfZaIZganjif2Nf7F9W1 BLbM0/2UQmLjZXXEaa3F7lGaf0ykZrCOR6pU4+Ea9KZaHQ9rTYG6k23VUO9UmJ67RmPz qq4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=kTlTPKqb; 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 k25-20020a2ea279000000b00261ea546cc0si8452448ljm.473.2022.09.08.07.27.41; Thu, 08 Sep 2022 07:28: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=kTlTPKqb; 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 S232480AbiIHO0p (ORCPT + 99 others); Thu, 8 Sep 2022 10:26:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232456AbiIHO0m (ORCPT ); Thu, 8 Sep 2022 10:26:42 -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 924FFFB8C6; Thu, 8 Sep 2022 07:26:40 -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 0C15261D2E; Thu, 8 Sep 2022 14:26:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C666C433D6; Thu, 8 Sep 2022 14:26:38 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="kTlTPKqb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1662647195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tiDiqwWP8LXWHrwhahDSsAig51VVDPd45ZIiXF6w6yk=; b=kTlTPKqbw78HR6NYkp+aosPtLtoFplLAWPRDHp9faSihIFAlZqZxGoDuaCm1iP1gUozW72 rtMvop1KaPkxBP8Cy2dlO84kZ3drMWNGI6VZlmnBhmyyMWqRc5IX4/stXcurS7siw6PUYD YXp+46jhb3LmQBlP63hlqx5D2yeIR4w= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id abb9b42c (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 8 Sep 2022 14:26:35 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-fsdevel@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ebiggers@kernel.org, "Jason A. Donenfeld" , Guozihua , Zhongguohua , Al Viro , Theodore Ts'o , Andrew Lutomirski , stable@vger.kernel.org Subject: [PATCH] random: restore O_NONBLOCK support Date: Thu, 8 Sep 2022 16:26:13 +0200 Message-Id: <20220908142613.341294-1-Jason@zx2c4.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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,T_SCC_BODY_TEXT_LINE 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 Prior to 5.6, when /dev/random was opened with O_NONBLOCK, it would return -EAGAIN if there was no entropy. When the pools were unified in 5.6, this was lost. The post 5.6 behavior of blocking until the pool is initialized, and ignoring O_NONBLOCK in the process, went unnoticed, with no reports about the regression received for two and a half years. However, eventually this indeed did break somebody's userspace. So we restore the old behavior, by returning -EAGAIN if the pool is not initialized. Unlike the old /dev/random, this can only occur during early boot, after which it never blocks again. In order to make this O_NONBLOCK behavior consistent with other expectations, also respect users reading with preadv2(RWF_NOWAIT) and similar. Fixes: 30c08efec888 ("random: make /dev/random be almost like /dev/urandom") Reported-by: Guozihua Reported-by: Zhongguohua Cc: Al Viro Cc: Theodore Ts'o Cc: Andrew Lutomirski Cc: stable@vger.kernel.org Signed-off-by: Jason A. Donenfeld --- drivers/char/mem.c | 4 ++-- drivers/char/random.c | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 84ca98ed1dad..c2b37009b11e 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -706,8 +706,8 @@ static const struct memdev { #endif [5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT }, [7] = { "full", 0666, &full_fops, 0 }, - [8] = { "random", 0666, &random_fops, 0 }, - [9] = { "urandom", 0666, &urandom_fops, 0 }, + [8] = { "random", 0666, &random_fops, FMODE_NOWAIT }, + [9] = { "urandom", 0666, &urandom_fops, FMODE_NOWAIT }, #ifdef CONFIG_PRINTK [11] = { "kmsg", 0644, &kmsg_fops, 0 }, #endif diff --git a/drivers/char/random.c b/drivers/char/random.c index 79d7d4e4e582..c8cc23515568 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1347,6 +1347,11 @@ static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter) { int ret; + if (!crng_ready() && + ((kiocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) || + (kiocb->ki_filp->f_flags & O_NONBLOCK))) + return -EAGAIN; + ret = wait_for_random_bytes(); if (ret != 0) return ret; -- 2.37.3