diff --git a/drivers/char/random.c b/drivers/char/random.c index 1ef2640..618ca9b 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -320,6 +320,13 @@ static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS; static int random_min_urandom_seed = 60; /* + * If /dev/random can't fulfil a request, block unless we can return + * this many bytes. If O_NONBLOCK is set, we always return, + * unconditionally. + */ +static int random_min_return = 1; + +/* * Originally, we used a primitive polynomial of degree .poolwords * over GF(2). The taps for various sizes are defined below. They * were chosen to be evenly spaced except for the last tap, which is 1 @@ -1702,24 +1709,26 @@ static ssize_t _random_read(int nonblock, char __user *buf, size_t nbytes) { ssize_t n; + size_t done = 0; if (nbytes == 0) return 0; nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE); - while (1) { - n = extract_entropy_user(&blocking_pool, buf, nbytes); + while (done < nbytes) { + n = extract_entropy_user(&blocking_pool, buf, nbytes-done); if (n < 0) - return n; + return done ? done : n; trace_random_read(n*8, (nbytes-n)*8, ENTROPY_BITS(&blocking_pool), ENTROPY_BITS(&input_pool)); - if (n > 0) - return n; + done += n; + if (done >= min_t(size_t, nbytes, random_min_read)) + break; /* Pool is (near) empty. Maybe wait and retry. */ if (nonblock) - return -EAGAIN; + return done ? done : -EAGAIN; wait_event_interruptible(random_read_wait, ENTROPY_BITS(&input_pool) >= @@ -1727,6 +1736,7 @@ _random_read(int nonblock, char __user *buf, size_t nbytes) if (signal_pending(current)) return -ERESTARTSYS; } + return done; } static ssize_t @@ -1909,6 +1919,8 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, #include +static int min_random_min_read = 1; +static int max_random_min_read = SEC_XFER_SIZE; static int min_read_thresh = 8, min_write_thresh; static int max_read_thresh = OUTPUT_POOL_WORDS * 32; static int max_write_thresh = INPUT_POOL_WORDS * 32; @@ -2022,6 +2034,15 @@ struct ctl_table random_table[] = { .mode = 0444, .proc_handler = proc_do_uuid, }, + { + .procname = "random_min_return", + .data = &random_min_return, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &min_random_min_read, + .extra2 = &max_random_min_read, + }, #ifdef ADD_INTERRUPT_BENCH { .procname = "add_interrupt_avg_cycles",