From nobody Thu May 7 19:05:37 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 58B36C433EF for ; Fri, 20 May 2022 09:45:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347931AbiETJp0 (ORCPT ); Fri, 20 May 2022 05:45:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347916AbiETJpU (ORCPT ); Fri, 20 May 2022 05:45:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC22014B652 for ; Fri, 20 May 2022 02:45:18 -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 3A936B82297 for ; Fri, 20 May 2022 09:45:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F415C385A9; Fri, 20 May 2022 09:45:15 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="AVWjTQ02" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653039912; 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=2ksmISZlIi6nB2NESgItDN22QTvPuay/eWLB20U+Sbs=; b=AVWjTQ02CpZ+ErHAFozJunpPnNdFp1pk7iLPzMsRVYXHoRKeQg3D5NmbfFOR5Ovjil3piG BXu/VChBl6kvmGDhQ67zWmU45pijSM6lWVT0i3weyN7zRTQi7FYLrLnHyKmNHiZw/5Ugau PlulYfO1BYTLuJHjN5MfQuWWRRBTjSM= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id c62442fa (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 09:45:12 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , Theodore Ts'o , Christoph Hellwig , LKML , Al Viro Cc: "Jason A . Donenfeld" Subject: [PATCH v4 1/3] random: convert to using fops->read_iter() Date: Fri, 20 May 2022 11:44:57 +0200 Message-Id: <20220520094459.116240-2-Jason@zx2c4.com> In-Reply-To: <20220520094459.116240-1-Jason@zx2c4.com> References: <20220520094459.116240-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 wiring up splice() again for the random and urandom drivers. It also allows us to remove the INT_MAX check in getrandom(), because import_single_range() applies capping internally. Signed-off-by: Jens Axboe [Jason: rewrote get_random_bytes_user() to simplify and also incorporate additional suggestions from Al.] Cc: Al Viro Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 55 +++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 0958fa91a964..f8cd747acec2 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, copied; =20 - if (!len) + if (!iov_iter_count(to)) return 0; =20 /* @@ -417,8 +417,8 @@ static ssize_t get_random_bytes_user(void __user *ubuf,= 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], CHACHA_KEY_SIZE, to); goto out_zero_chacha; } =20 @@ -427,17 +427,9 @@ static ssize_t get_random_bytes_user(void __user *ubuf= , size_t len) 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, CHACHA_BLOCK_SIZE, to); + ret +=3D copied; + if (!iov_iter_count(to) || copied !=3D CHACHA_BLOCK_SIZE) break; =20 BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE !=3D 0); @@ -1248,6 +1240,10 @@ static void __cold try_to_generate_entropy(void) =20 SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int,= flags) { + struct iov_iter to; + struct iovec iov; + int ret; + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) return -EINVAL; =20 @@ -1258,19 +1254,18 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, siz= e_t, len, unsigned int, flags if ((flags & (GRND_INSECURE | GRND_RANDOM)) =3D=3D (GRND_INSECURE | GRND_= RANDOM)) return -EINVAL; =20 - if (len > INT_MAX) - len =3D INT_MAX; - if (!crng_ready() && !(flags & GRND_INSECURE)) { - int ret; - if (flags & GRND_NONBLOCK) return -EAGAIN; ret =3D wait_for_random_bytes(); if (unlikely(ret)) return ret; } - return get_random_bytes_user(ubuf, len); + + ret =3D import_single_range(READ, ubuf, len, &iov, &to); + if (unlikely(ret)) + return ret; + return get_random_bytes_user(&to); } =20 static __poll_t random_poll(struct file *file, poll_table *wait) @@ -1314,8 +1309,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 +1326,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 +1402,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 +1412,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:05:37 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 A5752C433EF for ; Fri, 20 May 2022 09:45:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347827AbiETJpp (ORCPT ); Fri, 20 May 2022 05:45:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347939AbiETJp1 (ORCPT ); Fri, 20 May 2022 05:45:27 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D68EF14AF63 for ; Fri, 20 May 2022 02:45:21 -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 0ADDCB82956 for ; Fri, 20 May 2022 09:45:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CE73C34113; Fri, 20 May 2022 09:45:18 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="Vv0CbJwB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653039917; 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=AjqxppiLVbK2RRhIoss04u4+2YUT3AE7xNSn/In95Hk=; b=Vv0CbJwBVd8C3Svpv1DhZ+an2yySZJ1EUfFMolbtu4ZGaXEtnTpdnLuAdwvKDGZSKgq01i Tp7ZvLnneVpjDb8HC3JoLB9zQivdiFa4B83FI/ecMfxI9V3lpqRv9HRaAGmi7Eb6lF42hr ssW5afDKU4lo2tSTEeLX64XF/RyAQks= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id f8dcb615 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 09:45:17 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , Theodore Ts'o , Christoph Hellwig , LKML , Al Viro Cc: "Jason A . Donenfeld" Subject: [PATCH v4 2/3] random: convert to using fops->write_iter() Date: Fri, 20 May 2022 11:44:58 +0200 Message-Id: <20220520094459.116240-3-Jason@zx2c4.com> In-Reply-To: <20220520094459.116240-1-Jason@zx2c4.com> References: <20220520094459.116240-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, simplify control flow of write_pool(), and incorporate suggestions from Al.] Cc: Al Viro Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 67 ++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index f8cd747acec2..831cafcd1034 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1274,39 +1274,31 @@ 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; + size_t copied; =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 (;;) { + copied =3D copy_from_iter(block, sizeof(block), from); + ret +=3D copied; + mix_pool_bytes(block, copied); + if (!iov_iter_count(from) || copied !=3D sizeof(block)) + 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) @@ -1345,9 +1337,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: @@ -1364,20 +1355,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 from; + 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; + + ret =3D import_single_range(WRITE, p, len, &iov, &from); + if (unlikely(ret)) + return ret; + ret =3D write_pool(&from); + if (unlikely(ret < 0)) + return ret; + if (unlikely(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. */ @@ -1403,7 +1406,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, @@ -1413,7 +1416,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:05:37 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 3E7EEC433EF for ; Fri, 20 May 2022 09:45:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347916AbiETJpw (ORCPT ); Fri, 20 May 2022 05:45:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347950AbiETJp3 (ORCPT ); Fri, 20 May 2022 05:45:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C66814B651 for ; Fri, 20 May 2022 02:45:25 -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 EBC3CB82297 for ; Fri, 20 May 2022 09:45:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F952C385A9; Fri, 20 May 2022 09:45:21 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="WJZNLD9u" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1653039919; 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=w2s4/EPOaBhwby3ZhNih+pgq+QSJUBTa6LO6++W+QMM=; b=WJZNLD9udT66Q2OJs1CSqfzg2tQeckWdFjOBMG0jOwYpMaU8HWSpEd3GptR/P3ZgocBIZ7 2/LsdcnPCeNI2vpeP1QZANzUvDqaYSN8vFureHCbybnHGQjudt6aQzW6hMIdEtVOf/R8KS 9o7o116SPhCByBPpmOYTMoCp3edEgB4= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id e6914df7 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 20 May 2022 09:45:19 +0000 (UTC) From: "Jason A. Donenfeld" To: Jens Axboe , Theodore Ts'o , Christoph Hellwig , LKML , Al Viro Cc: "Jason A . Donenfeld" Subject: [PATCH v4 3/3] random: wire up fops->splice_{read,write}_iter() Date: Fri, 20 May 2022 11:44:59 +0200 Message-Id: <20220520094459.116240-4-Jason@zx2c4.com> In-Reply-To: <20220520094459.116240-1-Jason@zx2c4.com> References: <20220520094459.116240-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. Note that sendfile() and such still does not work for read, though it does for write, because of the outdated file type restriction in splice_direct_to_actor(), which I'll address separately.] Cc: Al Viro 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 831cafcd1034..15a9e5ea1b3f 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1412,6 +1412,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 { @@ -1421,6 +1423,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