random: allow partial reads if later user copies fail
commit 5209aed5137880fa229746cb521f715e55596460 upstream. Rather than failing entirely if a copy_to_user() fails at some point, instead we should return a partial read for the amount that succeeded prior, unless none succeeded at all, in which case we return -EFAULT as before. This makes it consistent with other reader interfaces. For example, the following snippet for /dev/zero outputs "4" followed by "1": int fd; void *x = mmap(NULL, 4096, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); assert(x != MAP_FAILED); fd = open("/dev/zero", O_RDONLY); assert(fd >= 0); printf("%zd\n", read(fd, x, 4)); printf("%zd\n", read(fd, x + 4095, 4)); close(fd); This brings that same standard behavior to the various RNG reader interfaces. While we're at it, we can streamline the loop logic a little bit. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Cc: Jann Horn <jannh@google.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:

committed by
Greg Kroah-Hartman

parent
1805d20dfb
commit
72a9ec8d75
@@ -525,8 +525,7 @@ EXPORT_SYMBOL(get_random_bytes);
|
|||||||
|
|
||||||
static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
|
static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
|
||||||
{
|
{
|
||||||
ssize_t ret = 0;
|
size_t len, left, ret = 0;
|
||||||
size_t len;
|
|
||||||
u32 chacha_state[CHACHA_STATE_WORDS];
|
u32 chacha_state[CHACHA_STATE_WORDS];
|
||||||
u8 output[CHACHA_BLOCK_SIZE];
|
u8 output[CHACHA_BLOCK_SIZE];
|
||||||
|
|
||||||
@@ -545,37 +544,40 @@ static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
|
|||||||
* the user directly.
|
* the user directly.
|
||||||
*/
|
*/
|
||||||
if (nbytes <= CHACHA_KEY_SIZE) {
|
if (nbytes <= CHACHA_KEY_SIZE) {
|
||||||
ret = copy_to_user(buf, &chacha_state[4], nbytes) ? -EFAULT : nbytes;
|
ret = nbytes - copy_to_user(buf, &chacha_state[4], nbytes);
|
||||||
goto out_zero_chacha;
|
goto out_zero_chacha;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
for (;;) {
|
||||||
chacha20_block(chacha_state, output);
|
chacha20_block(chacha_state, output);
|
||||||
if (unlikely(chacha_state[12] == 0))
|
if (unlikely(chacha_state[12] == 0))
|
||||||
++chacha_state[13];
|
++chacha_state[13];
|
||||||
|
|
||||||
len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE);
|
len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE);
|
||||||
if (copy_to_user(buf, output, len)) {
|
left = copy_to_user(buf, output, len);
|
||||||
ret = -EFAULT;
|
if (left) {
|
||||||
|
ret += len - left;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
nbytes -= len;
|
|
||||||
buf += len;
|
buf += len;
|
||||||
ret += len;
|
ret += len;
|
||||||
|
nbytes -= len;
|
||||||
|
if (!nbytes)
|
||||||
|
break;
|
||||||
|
|
||||||
BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);
|
BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);
|
||||||
if (!(ret % PAGE_SIZE) && nbytes) {
|
if (ret % PAGE_SIZE == 0) {
|
||||||
if (signal_pending(current))
|
if (signal_pending(current))
|
||||||
break;
|
break;
|
||||||
cond_resched();
|
cond_resched();
|
||||||
}
|
}
|
||||||
} while (nbytes);
|
}
|
||||||
|
|
||||||
memzero_explicit(output, sizeof(output));
|
memzero_explicit(output, sizeof(output));
|
||||||
out_zero_chacha:
|
out_zero_chacha:
|
||||||
memzero_explicit(chacha_state, sizeof(chacha_state));
|
memzero_explicit(chacha_state, sizeof(chacha_state));
|
||||||
return ret;
|
return ret ? ret : -EFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user