Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1310000rwi; Thu, 13 Oct 2022 11:50:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7k7NdEc+/t87iENsf+1ruPUOmQpOkh6HIFb+RARwgFU256YYzCuV8p7AUSmwHQsxygsu1O X-Received: by 2002:a17:902:7897:b0:178:9292:57b9 with SMTP id q23-20020a170902789700b00178929257b9mr1404691pll.102.1665687050499; Thu, 13 Oct 2022 11:50:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665687050; cv=none; d=google.com; s=arc-20160816; b=jGc3XXdjL/KsjoaKkRmGbk3lRpF63IdS5K+W7xCa8X2rURc8TfEY/1ItXdIKx7qR5Y KVgAOgQOrzdOx3w46esv0eiPenDkj5z8TD7Jyup3Bq1eKuhWlasodK/wnGmDM093ewB2 wQl8V5wuGFX9khgxJt7Gp5pa4MH5jnDFin/zVfJ/56fLp5xMDnJzOhkn7Xy1zRmaY9GB 4dG1Vk/8NPSkuOI/vBaYSxmg1W3uwucER6gvxkT8uQr/817mUOG8lWhtE/sKVCZB2gSf nYvNb4aurDQeaJ92bgCNdbAEfXfcydevRSflDX2ssv1+kRHZkwUBMo/h+mnlzlhfbMtv y51g== 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=7Kr3diaaU4ux4/2fX592SmHV1ZD9xZqADetN1fmD+80=; b=yFhAhWXa7YQZPTZc3KdWYO8ITfVuUHNG8uGchvYHrQU4s/9lF2Z1ABqWZI70eqxF+M FEUZgRFv8qRAeduh7cIz0ox447o4fwB4vsHYmQzBwF/bVViSeDHRe7xkXQs0+QsasbOL VLsooOLG10qGPN+81aMKHk8h94+B9uVkFVVT08g7PefNGtzofe2o0WU1UNyhHqsNpHkj BJT3j9X2Dc0dU5Dj40Ox9BgOUUx+isAaC4LaFy9uFZfdKXuh6tMqMiYnbd2Pnbke8EwU EEnrPeni4oA3noUqqrDPmXNsqvYuor1oxNcEVGVGJ4mwQVWcuMyTlo7pf2pSM2flh4Q5 2/Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ej8ydLtD; 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 g5-20020a056a0023c500b00562040a507asi185899pfc.85.2022.10.13.11.50.39; Thu, 13 Oct 2022 11:50:50 -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=Ej8ydLtD; 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 S229852AbiJMRyp (ORCPT + 99 others); Thu, 13 Oct 2022 13:54:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229990AbiJMRyJ (ORCPT ); Thu, 13 Oct 2022 13:54:09 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A019B15789C; Thu, 13 Oct 2022 10:53:34 -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 04A89B82022; Thu, 13 Oct 2022 17:53:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BCF5C433D6; Thu, 13 Oct 2022 17:53:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1665683610; bh=JaS9pWfJgg7MfTSQcs82nTtNJD73Ta9aTXlVgOCy1lo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ej8ydLtDB5txaQ02C8CIW6BuniItj2L4ubeSx+0QKm1Fd/O1SFTxqJke4+RAkzAb9 jsE+poKjuslRyutaRunVBQGA2hUPrZp+0QvGTJVaoh6C782Gv2gWO/Fulwz3uUkJdC 3mAcVJoBMQ3Fwoz3AzQbS/7mGYqjicPYrSycAf6o= 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.4 27/38] random: restore O_NONBLOCK support Date: Thu, 13 Oct 2022 19:52:28 +0200 Message-Id: <20221013175145.161005886@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221013175144.245431424@linuxfoundation.org> References: <20221013175144.245431424@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 @@ -953,8 +953,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;