Re: [PATCH 1/2] random: convert to using fops->read_iter()

From: Al Viro
Date: Thu May 19 2022 - 23:11:43 EST


On Thu, May 19, 2022 at 05:31:36PM -0600, Jens Axboe wrote:

> @@ -418,25 +419,23 @@ static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
> * the user directly.
> */
> if (len <= CHACHA_KEY_SIZE) {
> - ret = len - copy_to_user(ubuf, &chacha_state[4], len);
> + ret = copy_to_iter(&chacha_state[4], len, to);
> goto out_zero_chacha;
> }
>
> for (;;) {
> + size_t copied;
> +
> chacha20_block(chacha_state, output);
> if (unlikely(chacha_state[12] == 0))
> ++chacha_state[13];
>
> block_len = min_t(size_t, len, CHACHA_BLOCK_SIZE);
> - left = copy_to_user(ubuf, output, block_len);
> - if (left) {
> - ret += block_len - left;
> + copied = copy_to_iter(output, block_len, to);
> + ret += copied;
> + if (copied != block_len)
> break;
> - }

copied = copy_to_iter(output, CHACHA_BLOCK_SIZE, to);
ret += copied;
if (copied != CHACHA_BLOCK_SIZE) {
if (!ret)
ret = -EFAULT;
break;
}
}

> SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
> {
> + struct iovec iov = { .iov_base = ubuf };
> + struct iov_iter iter;

import_single_range(READ, ubuf, len, &iov, &iter)

(note, BTW, that this'll cap len)