Skip to content

Commit aba120c

Browse files
committed
random: do not allow user to keep crng key around on stack
The fast key erasure RNG design relies on the key that's used to be used and then discarded. We do this, making judicious use of memzero_explicit(). However, reads to /dev/urandom and calls to getrandom() involve a copy_to_user(), and userspace can use FUSE or userfaultfd, or make a massive call, dynamically remap memory addresses as it goes, and set the process priority to idle, in order to keep a kernel stack alive indefinitely. By probing /proc/sys/kernel/random/entropy_avail to learn when the crng key is refreshed, a malicious userspace could mount this attack every 5 minutes thereafter, breaking the crng's forward secrecy. In order to fix this, we just overwrite the stack's key with the first 32 bytes of the "free" fast key erasure output. If we're returning <= 32 bytes to the user, then we can still return those bytes directly, so that short reads don't become slower. And for long reads, the difference is hopefully lost in the amortization, so it doesn't change much, with that amortization helping variously for medium reads. We don't need to do this for get_random_bytes() and the various kernel-space callers, and later, if we ever switch to always batching, this won't be necessary either, so there's no need to change the API of these functions. Cc: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jann Horn <jannh@google.com> Fixes: c92e040 ("random: add backtracking protection to the CRNG") Fixes: 186873c ("random: use simpler fast key erasure flow on per-cpu keys") Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 48bff10 commit aba120c

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

drivers/char/random.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,19 +532,29 @@ static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
532532
if (!nbytes)
533533
return 0;
534534

535-
len = min_t(size_t, 32, nbytes);
536-
crng_make_state(chacha_state, output, len);
537-
538-
if (copy_to_user(buf, output, len))
539-
return -EFAULT;
540-
nbytes -= len;
541-
buf += len;
542-
ret += len;
535+
/*
536+
* Immediately overwrite the ChaCha key at index 4 with random
537+
* bytes, in case userspace causes copy_to_user() below to sleep
538+
* forever, so that we still retain forward secrecy in that case.
539+
*/
540+
crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA_KEY_SIZE);
541+
/*
542+
* However, if we're doing a read of len <= 32, we don't need to
543+
* use chacha_state after, so we can simply return those bytes to
544+
* the user directly.
545+
*/
546+
if (nbytes <= CHACHA_KEY_SIZE) {
547+
ret = copy_to_user(buf, &chacha_state[4], nbytes) ? -EFAULT : nbytes;
548+
goto out_zero_chacha;
549+
}
543550

544-
while (nbytes) {
551+
do {
545552
if (large_request && need_resched()) {
546-
if (signal_pending(current))
553+
if (signal_pending(current)) {
554+
if (!ret)
555+
ret = -ERESTARTSYS;
547556
break;
557+
}
548558
schedule();
549559
}
550560

@@ -561,10 +571,11 @@ static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
561571
nbytes -= len;
562572
buf += len;
563573
ret += len;
564-
}
574+
} while (nbytes);
565575

566-
memzero_explicit(chacha_state, sizeof(chacha_state));
567576
memzero_explicit(output, sizeof(output));
577+
out_zero_chacha:
578+
memzero_explicit(chacha_state, sizeof(chacha_state));
568579
return ret;
569580
}
570581

0 commit comments

Comments
 (0)