From patchwork Mon Oct 24 11:29:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 10010 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp618205wru; Mon, 24 Oct 2022 12:33:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6lZgYnfSlXs6zputXAU3dp0mYfXXuih8ILVq8EQlssbVcgNWbk2NVVd+g3NczFncAXV8W9 X-Received: by 2002:a17:902:ccc4:b0:186:6fcb:3fcf with SMTP id z4-20020a170902ccc400b001866fcb3fcfmr21810237ple.100.1666639997677; Mon, 24 Oct 2022 12:33:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666639997; cv=none; d=google.com; s=arc-20160816; b=QXSedT/5lER8CWaUUXrqljK/yupeJYcNcGCFw+lsOI3t+M/aa95InyoGTuwQpkAhDx oJO1H31RRP5Kmm6BOTY/5nVQrXLAN/Ppd+Xfa34k/NK1JPbjI6LHYCiGdq9m22pf6P24 fELthz90WwcO/IfqAUNpVRrdXM9R1rMpvg/l4O6aabVR7WaE8v7rDcCELTFzbqhe23ZA esuserjR3bT/OSLWXWkKAFgaloQxYmuzYxlPt18FPFXTc0JGS0xKHgSzxriPuTohTBV0 GSYIk8CuiI+YbExJ3yWSSc0gAjAJ0E8qvZ/w4m+N0v657wL2AttwPbU9tMxZUXT/Xdbg cF9g== 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=5Pb2fNaaf06ZMsnWzUv8A8UZI+3EJnK07iuFaTV06Do=; b=urIrHP5R+ZuTlaHZcLCEH+DZbd3O0cJgn5cRmTDpoLCsqNsJqtbCGjjXs7CJMba4n+ G08ob04vxHqNBOrWZnuIJZ5EEIIJPa4mj6kuyNIPW6SugTmYs9VFpN70O24GPVLFL3n7 yPPglfrZ/bNzszUea2UWfJiO5kERm0R1Hejx3fZnf5t9qTihC4DwUqrgXopKDD+7nqF7 9A9IMJmeVVpsuGE2zz9DM5DRCc0TVv118G87Y+66UxS0gRMUI+7j2XWDag8G3Rvuam8t ZHjw4dOiftpjtCCgcnUTnJcXyNjtlYXLDXI8U3lBpKd4GFHlYauZAt41PiTGI5Gl8bZg zGGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ikt73LiH; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j189-20020a6380c6000000b00438b7cb3311si374087pgd.501.2022.10.24.12.33.04; Mon, 24 Oct 2022 12:33:17 -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=ikt73LiH; 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 S233049AbiJXTbU (ORCPT + 99 others); Mon, 24 Oct 2022 15:31:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233794AbiJXTaB (ORCPT ); Mon, 24 Oct 2022 15:30:01 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12EF91EEA3C; Mon, 24 Oct 2022 11:01:22 -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 sin.source.kernel.org (Postfix) with ESMTPS id CA06BCE1348; Mon, 24 Oct 2022 11:47:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0EFDC433C1; Mon, 24 Oct 2022 11:47:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612030; bh=4bL5Tuj/hi3U+nafDv72MeeMFAUY9IxSCW/j3e9tN8Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ikt73LiHr0W8jiaw/goOxobFFukTztjJvWdZqzd4rQQOgpbWnPcnTvRad50O1vTQV ory02BUF89lnLOUNKeehpNvR9kb8hrT6xQ9llkMOuBwi5AyaId1XOPMlPOPUl6SR5U jRLRYvxMnFlKDj7/D2bzxqwm94A3UOQ+bJVrN4aU= 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 4.14 044/210] random: restore O_NONBLOCK support Date: Mon, 24 Oct 2022 13:29:21 +0200 Message-Id: <20221024112958.426074294@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112956.797777597@linuxfoundation.org> References: <20221024112956.797777597@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747598702382161134?= X-GMAIL-MSGID: =?utf-8?q?1747598702382161134?= 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 @@ -887,8 +887,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 @@ -1294,6 +1294,11 @@ static ssize_t random_read_iter(struct k { int ret; + if (!crng_ready() && + ((kiocb->ki_flags & IOCB_NOWAIT) || + (kiocb->ki_filp->f_flags & O_NONBLOCK))) + return -EAGAIN; + ret = wait_for_random_bytes(); if (ret != 0) return ret;