Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1309601rwi; Thu, 13 Oct 2022 11:50:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM519ik4dq4ogrAi+GYBIizsCvRXObpQWD0pwUKgDljOb3OSi/hYuAck7nTcwl5W5PFqbM53 X-Received: by 2002:a17:90b:3908:b0:20d:4151:1b73 with SMTP id ob8-20020a17090b390800b0020d41511b73mr1299620pjb.45.1665687029199; Thu, 13 Oct 2022 11:50:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665687029; cv=none; d=google.com; s=arc-20160816; b=KB5vhTITIeGEXTknIGMUs9EX8APZyVoXxCg0VoNEQTDdy4Y//ZDG8KayVTQE1398el qaC1+nM/4KO7MH2Rh1KOJYEbMPZrwk7yqPb7c1T/HyZgauaAKoWsAsc0SIfzYTuY/oYO 9ozN5+vvhTkSXWnJ4otTLdGG5c5rK4hPkSdbaaQ5RgeN+hxRemAIpwaGn2T+YfzKYfSu KtVnPj1QPvSGnix90jDt8mU+6ve9lSknFT7koehLmqGc5mK4d4H9pgv8cIAAv2sY9Uu1 wlLGd7G+GOCdmvLV/ioWp+PlkzZI/JJvdERXXHt5bbhjJ2Kk0MxFW2i/1VmNNmfxzrWN auiw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QCCP4Ow6nByYUD6+A0Ve+foJp3blDAILnckBc6Uq/uU=; b=KEytziOoUBnUQMxvwBd8wUdiMjnrO8hCPiTamrrVM+TlRZfK0NTUyStqFBiPOGxWmj cO0ZzwdfIu8lIZYLPOU83kJaac3/Yxf7YzNCzWO/6CeX3WzAjHR8O6RkhHdSr5c+nTjh yXRu2FyMY2GmVmOsT8RxDDccq6+91lWx+v55kVwNIGMzsMjGZXKFvAFfFoPoxkpSEpMG Z1Xzf06QLQW5FnwwcYSM7j5cR/SSDYzizWEJvr6roGeUVSCj4pJSBtt4QZgyXLRNeS29 LEa77dDW5+Go8XjgsTc2FPK7A4q+hY4YdG6EiB8qGi/riPVg1caB3m/011yONlVOv80b llIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=O4DlYXgo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s23-20020a056a00179700b00562d9cddbf9si206948pfg.13.2022.10.13.11.50.16; Thu, 13 Oct 2022 11:50:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=O4DlYXgo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229746AbiJMST3 (ORCPT + 99 others); Thu, 13 Oct 2022 14:19:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229769AbiJMSST (ORCPT ); Thu, 13 Oct 2022 14:18:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6928116C221; Thu, 13 Oct 2022 11:13:59 -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 B98B4B8204E; Thu, 13 Oct 2022 17:58:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 132D5C433D6; Thu, 13 Oct 2022 17:58:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1665683916; bh=LXvBMZgQGW5rg2p6e0YwUu2ffKqKQwDfqB6k048ZkgQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O4DlYXgoF4nGpiwBTbKJ0JGmy46s3yDEVNZ7r8cRvd81eAnvjmAN19q5k2/oMhw8B qNxv9xH4zb1LMFayxgHkmYGfX9jKqy6w4ApVqrz3M3t4WtB4K0bU43weqLKwKjnQms iRIZmjqpPd9v11yy9nQUU62ntXC+PVuBwcVLDqsQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guozihua , Zhongguohua , Al Viro , Theodore Tso , Andrew Lutomirski , "Jason A. Donenfeld" Subject: [PATCH 5.15 06/27] random: restore O_NONBLOCK support Date: Thu, 13 Oct 2022 19:52:35 +0200 Message-Id: <20221013175143.753815853@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221013175143.518476113@linuxfoundation.org> References: <20221013175143.518476113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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-kernel@vger.kernel.org From: Jason A. Donenfeld commit cd4f24ae9404fd31fc461066e57889be3b68641b upstream. 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 Signed-off-by: Greg Kroah-Hartman --- drivers/char/mem.c | 4 ++-- drivers/char/random.c | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -702,8 +702,8 @@ static const struct memdev { #endif [5] = { "zero", 0666, &zero_fops, 0 }, [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 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1298,6 +1298,11 @@ static ssize_t random_read_iter(struct k { 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;