From nobody Thu May 7 19:54:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17F44C433F5 for ; Fri, 20 May 2022 00:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344145AbiETAlp (ORCPT ); Thu, 19 May 2022 20:41:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344007AbiETAlR (ORCPT ); Thu, 19 May 2022 20:41:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CB233631B for ; Thu, 19 May 2022 17:41:09 -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 69076B828D4 for ; Fri, 20 May 2022 00:41:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A67B8C385B8; Fri, 20 May 2022 00:41:06 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="ZjqZvTex" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653007265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CjspdeBWDGhe4cGBMj1ukhgsSZgmcHVj6Y/pJcZHSEQ=; b=ZjqZvTexCihOozS+2vh5OtM6u94hIucBX0CQTjFccZA5d2kF6QStVNV2vihrxlBsyaEvN1 HrKWhA3bEbGMYPFBxzpsnJ8UBjdTamw7YYUBNwnvVs1/6smozCVJvGeUuCW316JeZg4NZN LFfl5a0+Mo4mjWn6Jss7Mc6Lf2ZGnEc= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 97d1d727 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 00:41:05 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , LKML Cc: "Jason A . Donenfeld" Subject: [PATCH v3 1/3] random: convert to using fops->read_iter() Date: Fri, 20 May 2022 02:40:56 +0200 Message-Id: <20220520004058.96691-2-Jason@zx2c4.com> In-Reply-To: <20220520004058.96691-1-Jason@zx2c4.com> References: <20220520004058.96691-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Jens Axboe This is a pre-requisite to writing up splice() again for the random and urandom drivers. Signed-off-by: Jens Axboe [Jason: simplify control flow a bit in get_random_bytes_user().] Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 49 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 0958fa91a964..2b2c3681f172 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -397,13 +397,13 @@ void get_random_bytes(void *buf, size_t len) } EXPORT_SYMBOL(get_random_bytes); =20 -static ssize_t get_random_bytes_user(void __user *ubuf, size_t len) +static ssize_t get_random_bytes_user(struct iov_iter *to) { - size_t block_len, left, ret =3D 0; u32 chacha_state[CHACHA_STATE_WORDS]; u8 output[CHACHA_BLOCK_SIZE]; + size_t ret =3D 0; =20 - if (!len) + if (!iov_iter_count(to)) return 0; =20 /* @@ -417,27 +417,21 @@ static ssize_t get_random_bytes_user(void __user *ubu= f, size_t len) * use chacha_state after, so we can simply return those bytes to * the user directly. */ - if (len <=3D CHACHA_KEY_SIZE) { - ret =3D len - copy_to_user(ubuf, &chacha_state[4], len); + if (iov_iter_count(to) <=3D CHACHA_KEY_SIZE) { + ret =3D copy_to_iter(&chacha_state[4], iov_iter_count(to), to); goto out_zero_chacha; } =20 for (;;) { + size_t copied, block_len =3D min_t(size_t, iov_iter_count(to), CHACHA_BL= OCK_SIZE); + chacha20_block(chacha_state, output); if (unlikely(chacha_state[12] =3D=3D 0)) ++chacha_state[13]; =20 - block_len =3D min_t(size_t, len, CHACHA_BLOCK_SIZE); - left =3D copy_to_user(ubuf, output, block_len); - if (left) { - ret +=3D block_len - left; - break; - } - - ubuf +=3D block_len; - ret +=3D block_len; - len -=3D block_len; - if (!len) + copied =3D copy_to_iter(output, block_len, to); + ret +=3D copied; + if (!iov_iter_count(to) || copied !=3D block_len) break; =20 BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE !=3D 0); @@ -1248,6 +1242,9 @@ static void __cold try_to_generate_entropy(void) =20 SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int,= flags) { + struct iovec iov =3D { .iov_base =3D ubuf }; + struct iov_iter iter; + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) return -EINVAL; =20 @@ -1270,7 +1267,9 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_= t, len, unsigned int, flags if (unlikely(ret)) return ret; } - return get_random_bytes_user(ubuf, len); + iov.iov_len =3D len; + iov_iter_init(&iter, READ, &iov, 1, len); + return get_random_bytes_user(&iter); } =20 static __poll_t random_poll(struct file *file, poll_table *wait) @@ -1314,8 +1313,7 @@ static ssize_t random_write(struct file *file, const = char __user *ubuf, return (ssize_t)len; } =20 -static ssize_t urandom_read(struct file *file, char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *to) { static int maxwarn =3D 10; =20 @@ -1332,22 +1330,21 @@ static ssize_t urandom_read(struct file *file, char= __user *ubuf, else if (ratelimit_disable || __ratelimit(&urandom_warning)) { --maxwarn; pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", - current->comm, len); + current->comm, iov_iter_count(to)); } } =20 - return get_random_bytes_user(ubuf, len); + return get_random_bytes_user(to); } =20 -static ssize_t random_read(struct file *file, char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *to) { int ret; =20 ret =3D wait_for_random_bytes(); if (ret !=3D 0) return ret; - return get_random_bytes_user(ubuf, len); + return get_random_bytes_user(to); } =20 static long random_ioctl(struct file *f, unsigned int cmd, unsigned long a= rg) @@ -1409,7 +1406,7 @@ static int random_fasync(int fd, struct file *filp, i= nt on) } =20 const struct file_operations random_fops =3D { - .read =3D random_read, + .read_iter =3D random_read_iter, .write =3D random_write, .poll =3D random_poll, .unlocked_ioctl =3D random_ioctl, @@ -1419,7 +1416,7 @@ const struct file_operations random_fops =3D { }; =20 const struct file_operations urandom_fops =3D { - .read =3D urandom_read, + .read_iter =3D urandom_read_iter, .write =3D random_write, .unlocked_ioctl =3D random_ioctl, .compat_ioctl =3D compat_ptr_ioctl, --=20 2.35.1 From nobody Thu May 7 19:54:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC12CC433F5 for ; Fri, 20 May 2022 00:41:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344103AbiETAlZ (ORCPT ); Thu, 19 May 2022 20:41:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344029AbiETAlP (ORCPT ); Thu, 19 May 2022 20:41:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 792973615A for ; Thu, 19 May 2022 17:41:11 -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 F0210B82954 for ; Fri, 20 May 2022 00:41:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62406C385AA; Fri, 20 May 2022 00:41:08 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="jpuZIX9k" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653007267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rn7LVTW5ffXKQvok9WzGgZaKZ49uE7a0y7KHzsCIXdc=; b=jpuZIX9kUDD/hamIBEep6W4O83hKRnoadMwK49EMoSV3InXt7FsWQ1Bez7QdDFvFDGuWL0 dP9PhpKlTBJqXfimpqtu7AEXS6BVyD9lHdhi2SZ9SyyjJY61XNz7G3fSPgHiqp1fm5hqYD T555BElS2a5WqM/BIQq9CXQoJfbgGH8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 9a9cdf4d (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 00:41:07 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , LKML Cc: "Jason A . Donenfeld" Subject: [PATCH v3 2/3] random: convert to using fops->write_iter() Date: Fri, 20 May 2022 02:40:57 +0200 Message-Id: <20220520004058.96691-3-Jason@zx2c4.com> In-Reply-To: <20220520004058.96691-1-Jason@zx2c4.com> References: <20220520004058.96691-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Jens Axboe Now that the read side has been converted to fix a regression with splice, convert the write side as well to have some symmetry in the interface used (and help deprecate ->write()). Signed-off-by: Jens Axboe [Jason: cleaned up random_ioctl a bit, require full writes in RNDADDENTROPY since it's crediting entropy, and minimize control flow of write_pool().] Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 68 +++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 2b2c3681f172..a664e2b89ae0 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1278,39 +1278,32 @@ static __poll_t random_poll(struct file *file, poll= _table *wait) return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM; } =20 -static int write_pool(const char __user *ubuf, size_t len) +static ssize_t write_pool(struct iov_iter *from) { - size_t block_len; - int ret =3D 0; u8 block[BLAKE2S_BLOCK_SIZE]; + ssize_t ret =3D 0; =20 - while (len) { - block_len =3D min(len, sizeof(block)); - if (copy_from_user(block, ubuf, block_len)) { - ret =3D -EFAULT; - goto out; - } - len -=3D block_len; - ubuf +=3D block_len; - mix_pool_bytes(block, block_len); + if (!iov_iter_count(from)) + return 0; + + for (;;) { + size_t copied, block_len =3D min(iov_iter_count(from), sizeof(block)); + + copied =3D copy_from_iter(block, block_len, from); + ret +=3D copied; + mix_pool_bytes(block, copied); + if (!iov_iter_count(from) || copied !=3D block_len) + break; cond_resched(); } =20 -out: memzero_explicit(block, sizeof(block)); - return ret; + return ret ? ret : -EFAULT; } =20 -static ssize_t random_write(struct file *file, const char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *fro= m) { - int ret; - - ret =3D write_pool(ubuf, len); - if (ret) - return ret; - - return (ssize_t)len; + return write_pool(from); } =20 static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *to) @@ -1349,9 +1342,8 @@ static ssize_t random_read_iter(struct kiocb *kiocb, = struct iov_iter *to) =20 static long random_ioctl(struct file *f, unsigned int cmd, unsigned long a= rg) { - int size, ent_count; int __user *p =3D (int __user *)arg; - int retval; + int ent_count; =20 switch (cmd) { case RNDGETENTCNT: @@ -1368,20 +1360,32 @@ static long random_ioctl(struct file *f, unsigned i= nt cmd, unsigned long arg) return -EINVAL; credit_init_bits(ent_count); return 0; - case RNDADDENTROPY: + case RNDADDENTROPY: { + struct iov_iter iter; + struct iovec iov; + ssize_t ret; + int len; + if (!capable(CAP_SYS_ADMIN)) return -EPERM; if (get_user(ent_count, p++)) return -EFAULT; if (ent_count < 0) return -EINVAL; - if (get_user(size, p++)) + if (get_user(len, p++)) + return -EFAULT; + + iov.iov_base =3D p; + iov.iov_len =3D len; + iov_iter_init(&iter, WRITE, &iov, 1, len); + ret =3D write_pool(&iter); + if (ret < 0) + return ret; + if (ret !=3D len) return -EFAULT; - retval =3D write_pool((const char __user *)p, size); - if (retval < 0) - return retval; credit_init_bits(ent_count); return 0; + } case RNDZAPENTCNT: case RNDCLEARPOOL: /* No longer has any effect. */ @@ -1407,7 +1411,7 @@ static int random_fasync(int fd, struct file *filp, i= nt on) =20 const struct file_operations random_fops =3D { .read_iter =3D random_read_iter, - .write =3D random_write, + .write_iter =3D random_write_iter, .poll =3D random_poll, .unlocked_ioctl =3D random_ioctl, .compat_ioctl =3D compat_ptr_ioctl, @@ -1417,7 +1421,7 @@ const struct file_operations random_fops =3D { =20 const struct file_operations urandom_fops =3D { .read_iter =3D urandom_read_iter, - .write =3D random_write, + .write_iter =3D random_write_iter, .unlocked_ioctl =3D random_ioctl, .compat_ioctl =3D compat_ptr_ioctl, .fasync =3D random_fasync, --=20 2.35.1 From nobody Thu May 7 19:54:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AA0FC433F5 for ; Fri, 20 May 2022 00:41:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344078AbiETAlT (ORCPT ); Thu, 19 May 2022 20:41:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343938AbiETAlP (ORCPT ); Thu, 19 May 2022 20:41:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 413BB35265 for ; Thu, 19 May 2022 17:41:12 -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 E568761AC8 for ; Fri, 20 May 2022 00:41:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07DE8C385B8; Fri, 20 May 2022 00:41:10 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="fwWP7O5j" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653007270; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vkxnEo4pirJjax+GcZ9VlzCPXywbCo9wzJe/TT1cyeM=; b=fwWP7O5j6wOB2ya+VD3CPI4mjd/9uFa4CBgbzIVHaQ8o3Nxw4MZBReUJ8UcWaIUkndsr59 AMMjoXW0qKsp2Oe8qusThwiDhQ7FRkrdJuUeuhnUYfUfO1Y/5VIvkYpsrb9OeFFwVJy5mb i8zNvQRFgKghurRm0onRg7SsRunFza8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 166ebe75 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 00:41:09 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , LKML Cc: "Jason A . Donenfeld" Subject: [PATCH v3 3/3] random: wire up fops->splice_{read,write}_iter() Date: Fri, 20 May 2022 02:40:58 +0200 Message-Id: <20220520004058.96691-4-Jason@zx2c4.com> In-Reply-To: <20220520004058.96691-1-Jason@zx2c4.com> References: <20220520004058.96691-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Jens Axboe Now that random/urandom is using {read,write}_iter, we can wire it up to using the generic splice handlers. Fixes: 36e2c7421f02 ("fs: don't allow splice read/write without explicit op= s") Signed-off-by: Jens Axboe [Jason: added the splice_write path.] Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/char/random.c b/drivers/char/random.c index a664e2b89ae0..90d779c5d1a1 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1417,6 +1417,8 @@ const struct file_operations random_fops =3D { .compat_ioctl =3D compat_ptr_ioctl, .fasync =3D random_fasync, .llseek =3D noop_llseek, + .splice_read =3D generic_file_splice_read, + .splice_write =3D iter_file_splice_write, }; =20 const struct file_operations urandom_fops =3D { @@ -1426,6 +1428,8 @@ const struct file_operations urandom_fops =3D { .compat_ioctl =3D compat_ptr_ioctl, .fasync =3D random_fasync, .llseek =3D noop_llseek, + .splice_read =3D generic_file_splice_read, + .splice_write =3D iter_file_splice_write, }; =20 =20 --=20 2.35.1