From patchwork Mon Oct 24 11:29:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8656 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp421685wru; Mon, 24 Oct 2022 05:21:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM76WgqJmLcbaXPdkmeuZhYY4Oc4sFecbqXjzYZwGuiiDR+hiYRMOiaFLXlGmUOoYLg/B+fB X-Received: by 2002:a17:907:728b:b0:7a1:b545:b39b with SMTP id dt11-20020a170907728b00b007a1b545b39bmr8810869ejc.661.1666614061866; Mon, 24 Oct 2022 05:21:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666614061; cv=none; d=google.com; s=arc-20160816; b=YsLhbwbAQXjbUGNLwH5mstCk3kHSpr4ImAVLnOGGrLNIpo4q5WOxmJgf3OYrVo3lpr /E7vOmG0KkETe8Y3seybv7Apx9ktTIsfqeA6sf1+PyuotDBXjsyk6gPXMXPMi+dvlLxL NYGtIpLgIgsgNW3+IaZczU82XjF3EVJJobSxi3JTysxna3Ie4Hwa+fZoFprtu2Xw8oZs jWro+Hl43l0PhwdCnE/D2oHvKQyYbnkwagx1jaQ4h7Jta6HnozX2jliYIqB4OOh8u3rF vsf08WepwU9AGZ9AHx2UPPfhr+qa4SKXMYgyW+ssNqPXUOclx3b7UiI2dOGmhpO1Eaa8 ALUw== 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=CQ67gAsOfhLp0OGuXrY/gH8htmq5Z1URGnt2rp2jegA=; b=IC9TTJAk8yyHf2JLsG2FqqGH+J64kcSTzYN6qY0osQvbkf8BoTpCHpTAwf7rSNCqXs qVd0AsODDe8f6epzWsGrB2bGlJjUSCo7PleASTEuyHIdRJTCc4JHJUznax3wGF31055R oYE2FZW75BTCiZhLNproDc5QMQ8A0co2gqX46uPQ3JPr2mYdGkrOFZfHi4KXnAVIF+60 wQh2Pyct8nCVzK4eoc2rwsnrcbsMgEPLzNtlqxyWtqse+V69MDxINLC/9w+beszBnjYK h3X/C4wkTP8yS34mUgmNuAzsp/J0l12iD49Z/Bmqw2FtKGUV+5GGP2JTmRQUBXSFgCVX ZaNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zKSG+gyF; 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 n19-20020a170906165300b0077c5ec87ec2si22167078ejd.297.2022.10.24.05.20.37; Mon, 24 Oct 2022 05:21:01 -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=zKSG+gyF; 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 S233145AbiJXMQm (ORCPT + 99 others); Mon, 24 Oct 2022 08:16:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233383AbiJXMO7 (ORCPT ); Mon, 24 Oct 2022 08:14:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F4BD7963A; Mon, 24 Oct 2022 04:55:47 -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 2F6BA612FE; Mon, 24 Oct 2022 11:55:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45502C433D7; Mon, 24 Oct 2022 11:55:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612539; bh=AtPuK4+6wjQ0bpEyIlTUi6fm79RRmk4Cm/SAy7YgL4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zKSG+gyFPC1GvqnUUe0SgPNnyrP5UrcMMRF/liRRU1sgP7IzXbUWhx8yZ4D5JASY1 PBuifkj6Og4st26s9lVfJP4AkNA2K9zXH9DS48/BPu4W8/fDhgjW8FppoQchrH6ZXA zGWzLmAk/jv600ClA/IfGzmt8fis2PgzKgFUyMxQ= 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.19 026/229] random: restore O_NONBLOCK support Date: Mon, 24 Oct 2022 13:29:05 +0200 Message-Id: <20221024112959.982158381@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@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?1747571506624127680?= X-GMAIL-MSGID: =?utf-8?q?1747571506624127680?= 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 @@ -893,8 +893,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;