Skip to content

Commit 1ce6c8d

Browse files
committed
random: check for signals after page of pool writes
get_random_bytes_user() checks for signals after producing a PAGE_SIZE worth of output, just like /dev/zero does. write_pool() is doing basically the same work (actually, slightly more expensive), and so should stop to check for signals in the same way. Let's also name it write_pool_user() to match get_random_bytes_user(), so this won't be misused in the future. Before this patch, massive writes to /dev/urandom would tie up the process for an extremely long time and make it unterminatable. After, it can be successfully interrupted. The following test program can be used to see this works as intended: #include <unistd.h> #include <fcntl.h> #include <signal.h> #include <stdio.h> static unsigned char x[~0U]; static void handle(int) { } int main(int argc, char *argv[]) { pid_t pid = getpid(), child; int fd; signal(SIGUSR1, handle); if (!(child = fork())) { for (;;) kill(pid, SIGUSR1); } fd = open("/dev/urandom", O_WRONLY); pause(); printf("interrupted after writing %zd bytes\n", write(fd, x, sizeof(x))); close(fd); kill(child, SIGTERM); return 0; } Result before: "interrupted after writing 2147479552 bytes" Result after: "interrupted after writing 4096 bytes" Cc: Dominik Brodowski <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 79025e7 commit 1ce6c8d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Diff for: drivers/char/random.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ static __poll_t random_poll(struct file *file, poll_table *wait)
12741274
return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
12751275
}
12761276

1277-
static ssize_t write_pool(struct iov_iter *iter)
1277+
static ssize_t write_pool_user(struct iov_iter *iter)
12781278
{
12791279
u8 block[BLAKE2S_BLOCK_SIZE];
12801280
ssize_t ret = 0;
@@ -1289,7 +1289,13 @@ static ssize_t write_pool(struct iov_iter *iter)
12891289
mix_pool_bytes(block, copied);
12901290
if (!iov_iter_count(iter) || copied != sizeof(block))
12911291
break;
1292-
cond_resched();
1292+
1293+
BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0);
1294+
if (ret % PAGE_SIZE == 0) {
1295+
if (signal_pending(current))
1296+
break;
1297+
cond_resched();
1298+
}
12931299
}
12941300

12951301
memzero_explicit(block, sizeof(block));
@@ -1298,7 +1304,7 @@ static ssize_t write_pool(struct iov_iter *iter)
12981304

12991305
static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter)
13001306
{
1301-
return write_pool(iter);
1307+
return write_pool_user(iter);
13021308
}
13031309

13041310
static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
@@ -1372,7 +1378,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
13721378
ret = import_single_range(WRITE, p, len, &iov, &iter);
13731379
if (unlikely(ret))
13741380
return ret;
1375-
ret = write_pool(&iter);
1381+
ret = write_pool_user(&iter);
13761382
if (unlikely(ret < 0))
13771383
return ret;
13781384
/* Since we're crediting, enforce that it was all written into the pool. */

0 commit comments

Comments
 (0)