random: convert to using fops->read_iter()

commit 1b388e7765f2eaa137cf5d92b47ef5925ad83ced upstream.

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 <axboe@kernel.dk>
[Jason: rewrote get_random_bytes_user() to simplify and also incorporate
 additional suggestions from Al.]
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Jens Axboe
2022-05-19 17:31:36 -06:00
committed by Greg Kroah-Hartman
parent 4bb374a118
commit affa1ae522

View File

@@ -52,6 +52,7 @@
#include <linux/uuid.h> #include <linux/uuid.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/siphash.h> #include <linux/siphash.h>
#include <linux/uio.h>
#include <crypto/chacha.h> #include <crypto/chacha.h>
#include <crypto/blake2s.h> #include <crypto/blake2s.h>
#include <asm/processor.h> #include <asm/processor.h>
@@ -448,13 +449,13 @@ void get_random_bytes(void *buf, size_t len)
} }
EXPORT_SYMBOL(get_random_bytes); EXPORT_SYMBOL(get_random_bytes);
static ssize_t get_random_bytes_user(void __user *ubuf, size_t len) static ssize_t get_random_bytes_user(struct iov_iter *iter)
{ {
size_t block_len, left, ret = 0;
u32 chacha_state[CHACHA_STATE_WORDS]; u32 chacha_state[CHACHA_STATE_WORDS];
u8 output[CHACHA_BLOCK_SIZE]; u8 block[CHACHA_BLOCK_SIZE];
size_t ret = 0, copied;
if (!len) if (unlikely(!iov_iter_count(iter)))
return 0; return 0;
/* /*
@@ -468,30 +469,22 @@ 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 * use chacha_state after, so we can simply return those bytes to
* the user directly. * the user directly.
*/ */
if (len <= CHACHA_KEY_SIZE) { if (iov_iter_count(iter) <= CHACHA_KEY_SIZE) {
ret = len - copy_to_user(ubuf, &chacha_state[4], len); ret = copy_to_iter(&chacha_state[4], CHACHA_KEY_SIZE, iter);
goto out_zero_chacha; goto out_zero_chacha;
} }
for (;;) { for (;;) {
chacha20_block(chacha_state, output); chacha20_block(chacha_state, block);
if (unlikely(chacha_state[12] == 0)) if (unlikely(chacha_state[12] == 0))
++chacha_state[13]; ++chacha_state[13];
block_len = min_t(size_t, len, CHACHA_BLOCK_SIZE); copied = copy_to_iter(block, sizeof(block), iter);
left = copy_to_user(ubuf, output, block_len); ret += copied;
if (left) { if (!iov_iter_count(iter) || copied != sizeof(block))
ret += block_len - left;
break;
}
ubuf += block_len;
ret += block_len;
len -= block_len;
if (!len)
break; break;
BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0); BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0);
if (ret % PAGE_SIZE == 0) { if (ret % PAGE_SIZE == 0) {
if (signal_pending(current)) if (signal_pending(current))
break; break;
@@ -499,7 +492,7 @@ static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
} }
} }
memzero_explicit(output, sizeof(output)); memzero_explicit(block, sizeof(block));
out_zero_chacha: out_zero_chacha:
memzero_explicit(chacha_state, sizeof(chacha_state)); memzero_explicit(chacha_state, sizeof(chacha_state));
return ret ? ret : -EFAULT; return ret ? ret : -EFAULT;
@@ -1228,6 +1221,10 @@ static void __cold try_to_generate_entropy(void)
SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags) SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
{ {
struct iov_iter iter;
struct iovec iov;
int ret;
if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
return -EINVAL; return -EINVAL;
@@ -1238,19 +1235,18 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags
if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
return -EINVAL; return -EINVAL;
if (len > INT_MAX)
len = INT_MAX;
if (!crng_ready() && !(flags & GRND_INSECURE)) { if (!crng_ready() && !(flags & GRND_INSECURE)) {
int ret;
if (flags & GRND_NONBLOCK) if (flags & GRND_NONBLOCK)
return -EAGAIN; return -EAGAIN;
ret = wait_for_random_bytes(); ret = wait_for_random_bytes();
if (unlikely(ret)) if (unlikely(ret))
return ret; return ret;
} }
return get_random_bytes_user(ubuf, len);
ret = import_single_range(READ, ubuf, len, &iov, &iter);
if (unlikely(ret))
return ret;
return get_random_bytes_user(&iter);
} }
static __poll_t random_poll(struct file *file, poll_table *wait) static __poll_t random_poll(struct file *file, poll_table *wait)
@@ -1294,8 +1290,7 @@ static ssize_t random_write(struct file *file, const char __user *ubuf,
return (ssize_t)len; return (ssize_t)len;
} }
static ssize_t urandom_read(struct file *file, char __user *ubuf, static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
size_t len, loff_t *ppos)
{ {
static int maxwarn = 10; static int maxwarn = 10;
@@ -1304,23 +1299,22 @@ static ssize_t urandom_read(struct file *file, char __user *ubuf,
++urandom_warning.missed; ++urandom_warning.missed;
else if (ratelimit_disable || __ratelimit(&urandom_warning)) { else if (ratelimit_disable || __ratelimit(&urandom_warning)) {
--maxwarn; --maxwarn;
pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", pr_notice("%s: uninitialized urandom read (%zu bytes read)\n",
current->comm, len); current->comm, iov_iter_count(iter));
} }
} }
return get_random_bytes_user(ubuf, len); return get_random_bytes_user(iter);
} }
static ssize_t random_read(struct file *file, char __user *ubuf, static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
size_t len, loff_t *ppos)
{ {
int ret; int ret;
ret = wait_for_random_bytes(); ret = wait_for_random_bytes();
if (ret != 0) if (ret != 0)
return ret; return ret;
return get_random_bytes_user(ubuf, len); return get_random_bytes_user(iter);
} }
static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
@@ -1382,7 +1376,7 @@ static int random_fasync(int fd, struct file *filp, int on)
} }
const struct file_operations random_fops = { const struct file_operations random_fops = {
.read = random_read, .read_iter = random_read_iter,
.write = random_write, .write = random_write,
.poll = random_poll, .poll = random_poll,
.unlocked_ioctl = random_ioctl, .unlocked_ioctl = random_ioctl,
@@ -1392,7 +1386,7 @@ const struct file_operations random_fops = {
}; };
const struct file_operations urandom_fops = { const struct file_operations urandom_fops = {
.read = urandom_read, .read_iter = urandom_read_iter,
.write = random_write, .write = random_write,
.unlocked_ioctl = random_ioctl, .unlocked_ioctl = random_ioctl,
.compat_ioctl = compat_ptr_ioctl, .compat_ioctl = compat_ptr_ioctl,