Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2931107iog; Mon, 20 Jun 2022 07:44:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shFVpsIBNE9sbGcEkVQZKXIISIOPaizVLICRd2M6upJdQQdprEX96y5/+34uYduvT+/N9Y X-Received: by 2002:a17:902:ed8e:b0:16a:147b:9044 with SMTP id e14-20020a170902ed8e00b0016a147b9044mr11085402plj.85.1655736252256; Mon, 20 Jun 2022 07:44:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736252; cv=none; d=google.com; s=arc-20160816; b=VkqLcKfm5du2F9wNIXzMMctUVuXYlt4ssuswbUHu/D8JYfWD+NIUijIVFGQOewjWZQ vQkNNFoqU3eIi7rSaAs5yTkaKFjJ8JbnkF3rVsBCuYvZgZfnUtyWuAioy6aSS0JcXDA+ cTqNV9ANhFlSIBadVi1Xm9iwH/Vh8F5ZFzZDOq2FIqUjbQh5MlbsRYGXKHJiVcunkcK6 zswUVtYqUIkszDXoZ9/ZE/t6DTqiGl05NXcjRXmu/6HYh7cdv/8bRNHkvH6qIAoXMec9 /th54Qw6Y24JK6wvMO9lazTHIT7EUKQMokwyyPCOi8tBONtbbX+7rP5KS4pSOiZbyXAT /0pA== 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=Elo+mgWdwoZcCeMPQqoZhaqNL+Dcb1WK4TXMIDffryg=; b=Fcl2MUn7yy86iLbue7BbdHukg++AI39rC4jlpSiQsrZqxqZ62zt1D/dFa4oErnQ3uJ j5MbPBxLr2bfxzrPK5HstWlsb3syNmUgXB7LCw6bj7aG3FuLTxPeI5Fq+cCGdIyavPyR D7+OL9HbammDAClVhEpWmsz5B2fc4OmOWyiBQyiPLVQsrcj4wFPR8W1yk/sZQ3XwOl22 i/AQhVrzkm8JvFSPOIl92Q4GRiWW2fOD40z3OFj0ZKFUptP4KoBR6YvnK/d0YpQfl+bh bTdN3Bgbs7CnyEDGcp9C0EujOtu1g+pztyaB1vpRHb55jZN74OKtsEOhe1DnXb0fKUEa cs3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="yL/VlYcp"; 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 206-20020a6301d7000000b003fc51453576si16922065pgb.545.2022.06.20.07.43.58; Mon, 20 Jun 2022 07:44:12 -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="yL/VlYcp"; 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 S1347895AbiFTNpd (ORCPT + 99 others); Mon, 20 Jun 2022 09:45:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348324AbiFTNnp (ORCPT ); Mon, 20 Jun 2022 09:43:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA58F2B27C; Mon, 20 Jun 2022 06:15:48 -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 83903B811E1; Mon, 20 Jun 2022 13:15:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4B95C341C0; Mon, 20 Jun 2022 13:15:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655730924; bh=nBIrK65y4Z+lIqo3uyfFJGQ7Z+6nhhza/5yLAwPOjTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yL/VlYcp3J6yyubR/bInFm22m9Mow38sPD2n65xNCHQt+pqtLpE0fBKcs++UQkEXX 5BQ1zR0Gb0nG1CPZQ3Q/c5inWhsa8iS09DMuTBIG8miSDnv7w6YsRXHf1omXGQ+UlL 5kmJkipFhFO34A2NTAC44vKoNSVIObK8MbJ3OA+Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Eric Biggers , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 5.4 097/240] random: group userspace read/write functions Date: Mon, 20 Jun 2022 14:49:58 +0200 Message-Id: <20220620124741.827434754@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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,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-kernel@vger.kernel.org From: "Jason A. Donenfeld" commit a6adf8e7a605250b911e94793fd077933709ff9e upstream. This pulls all of the userspace read/write-focused functions into the fifth labeled section. No functional changes. Cc: Theodore Ts'o Reviewed-by: Eric Biggers Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 125 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 48 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1472,30 +1472,61 @@ static void try_to_generate_entropy(void mix_pool_bytes(&stack.now, sizeof(stack.now)); } -static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, - loff_t *ppos) + +/********************************************************************** + * + * Userspace reader/writer interfaces. + * + * getrandom(2) is the primary modern interface into the RNG and should + * be used in preference to anything else. + * + * Reading from /dev/random has the same functionality as calling + * getrandom(2) with flags=0. In earlier versions, however, it had + * vastly different semantics and should therefore be avoided, to + * prevent backwards compatibility issues. + * + * Reading from /dev/urandom has the same functionality as calling + * getrandom(2) with flags=GRND_INSECURE. Because it does not block + * waiting for the RNG to be ready, it should not be used. + * + * Writing to either /dev/random or /dev/urandom adds entropy to + * the input pool but does not credit it. + * + * Polling on /dev/random indicates when the RNG is initialized, on + * the read side, and when it wants new entropy, on the write side. + * + * Both /dev/random and /dev/urandom have the same set of ioctls for + * adding entropy, getting the entropy count, zeroing the count, and + * reseeding the crng. + * + **********************************************************************/ + +SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, + flags) { - static int maxwarn = 10; + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) + return -EINVAL; - if (!crng_ready() && maxwarn > 0) { - maxwarn--; - if (__ratelimit(&urandom_warning)) - pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", - current->comm, nbytes); - } + /* + * Requesting insecure and blocking randomness at the same time makes + * no sense. + */ + if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) + return -EINVAL; - return get_random_bytes_user(buf, nbytes); -} + if (count > INT_MAX) + count = INT_MAX; -static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, - loff_t *ppos) -{ - int ret; + if (!(flags & GRND_INSECURE) && !crng_ready()) { + int ret; - ret = wait_for_random_bytes(); - if (ret != 0) - return ret; - return get_random_bytes_user(buf, nbytes); + if (flags & GRND_NONBLOCK) + return -EAGAIN; + ret = wait_for_random_bytes(); + if (unlikely(ret)) + return ret; + } + return get_random_bytes_user(buf, count); } static __poll_t random_poll(struct file *file, poll_table *wait) @@ -1547,6 +1578,32 @@ static ssize_t random_write(struct file return (ssize_t)count; } +static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, + loff_t *ppos) +{ + static int maxwarn = 10; + + if (!crng_ready() && maxwarn > 0) { + maxwarn--; + if (__ratelimit(&urandom_warning)) + pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", + current->comm, nbytes); + } + + return get_random_bytes_user(buf, nbytes); +} + +static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, + loff_t *ppos) +{ + int ret; + + ret = wait_for_random_bytes(); + if (ret != 0) + return ret; + return get_random_bytes_user(buf, nbytes); +} + static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) { int size, ent_count; @@ -1555,7 +1612,7 @@ static long random_ioctl(struct file *f, switch (cmd) { case RNDGETENTCNT: - /* inherently racy, no point locking */ + /* Inherently racy, no point locking. */ if (put_user(input_pool.entropy_count, p)) return -EFAULT; return 0; @@ -1631,34 +1688,6 @@ const struct file_operations urandom_fop .llseek = noop_llseek, }; -SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, - flags) -{ - if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) - return -EINVAL; - - /* - * Requesting insecure and blocking randomness at the same time makes - * no sense. - */ - if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) - return -EINVAL; - - if (count > INT_MAX) - count = INT_MAX; - - if (!(flags & GRND_INSECURE) && !crng_ready()) { - int ret; - - if (flags & GRND_NONBLOCK) - return -EAGAIN; - ret = wait_for_random_bytes(); - if (unlikely(ret)) - return ret; - } - return get_random_bytes_user(buf, count); -} - /******************************************************************** * * Sysctl interface