Skip to content

Commit 0313bc2

Browse files
committed
Revert "random: block in /dev/urandom"
This reverts commit 6f98a4b. It turns out we still can't do this. Way too many platforms that don't have any real source of randomness at boot and no jitter entropy because they don't even have a cycle counter. As reported by Guenter Roeck: "This causes a large number of qemu boot test failures for various architectures (arm, m68k, microblaze, sparc32, xtensa are the ones I observed). Common denominator is that boot hangs at 'Saving random seed:'" This isn't hugely unexpected - we tried it, it failed, so now we'll revert it. Link: https://lore.kernel.org/all/[email protected]/ Reported-and-bisected-by: Guenter Roeck <[email protected]> Cc: Jason Donenfeld <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b47d5a4 commit 0313bc2

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

drivers/char/mem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static const struct memdev {
707707
[5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT },
708708
[7] = { "full", 0666, &full_fops, 0 },
709709
[8] = { "random", 0666, &random_fops, 0 },
710-
[9] = { "urandom", 0666, &random_fops, 0 },
710+
[9] = { "urandom", 0666, &urandom_fops, 0 },
711711
#ifdef CONFIG_PRINTK
712712
[11] = { "kmsg", 0644, &kmsg_fops, 0 },
713713
#endif

drivers/char/random.c

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,17 @@ static RAW_NOTIFIER_HEAD(random_ready_chain);
8989
/* Control how we warn userspace. */
9090
static struct ratelimit_state unseeded_warning =
9191
RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3);
92+
static struct ratelimit_state urandom_warning =
93+
RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3);
9294
static int ratelimit_disable __read_mostly;
9395
module_param_named(ratelimit_disable, ratelimit_disable, int, 0644);
9496
MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
9597

9698
/*
9799
* Returns whether or not the input pool has been seeded and thus guaranteed
98-
* to supply cryptographically secure random numbers. This applies to
99-
* get_random_bytes() and get_random_{u32,u64,int,long}().
100+
* to supply cryptographically secure random numbers. This applies to: the
101+
* /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
102+
* ,u64,int,long} family of functions.
100103
*
101104
* Returns: true if the input pool has been seeded.
102105
* false if the input pool has not been seeded.
@@ -112,10 +115,10 @@ static void try_to_generate_entropy(void);
112115

113116
/*
114117
* Wait for the input pool to be seeded and thus guaranteed to supply
115-
* cryptographically secure random numbers. This applies to
116-
* get_random_bytes() and get_random_{u32,u64,int,long}(). Using any
117-
* of these functions without first calling this function means that
118-
* the returned numbers might not be cryptographically secure.
118+
* cryptographically secure random numbers. This applies to: the /dev/urandom
119+
* device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
120+
* family of functions. Using any of these functions without first calling
121+
* this function forfeits the guarantee of security.
119122
*
120123
* Returns: 0 if the input pool has been seeded.
121124
* -ERESTARTSYS if the function was interrupted by a signal.
@@ -220,10 +223,10 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller, void
220223
* unsigned long get_random_long()
221224
*
222225
* These interfaces will return the requested number of random bytes
223-
* into the given buffer or as a return value. The returned numbers are
224-
* the same as those of getrandom(0). The integer family of functions may
225-
* be higher performance for one-off random integers, because they do a
226-
* bit of buffering and do not invoke reseeding.
226+
* into the given buffer or as a return value. This is equivalent to
227+
* a read from /dev/urandom. The integer family of functions may be
228+
* higher performance for one-off random integers, because they do a
229+
* bit of buffering.
227230
*
228231
*********************************************************************/
229232

@@ -300,6 +303,11 @@ static void crng_reseed(bool force)
300303
unseeded_warning.missed);
301304
unseeded_warning.missed = 0;
302305
}
306+
if (urandom_warning.missed) {
307+
pr_notice("%d urandom warning(s) missed due to ratelimiting\n",
308+
urandom_warning.missed);
309+
urandom_warning.missed = 0;
310+
}
303311
}
304312
}
305313

@@ -979,8 +987,10 @@ int __init rand_initialize(void)
979987
pr_notice("crng init done (trusting CPU's manufacturer)\n");
980988
}
981989

982-
if (ratelimit_disable)
990+
if (ratelimit_disable) {
991+
urandom_warning.interval = 0;
983992
unseeded_warning.interval = 0;
993+
}
984994
return 0;
985995
}
986996

@@ -1420,16 +1430,20 @@ static void try_to_generate_entropy(void)
14201430
* getrandom(2) is the primary modern interface into the RNG and should
14211431
* be used in preference to anything else.
14221432
*
1423-
* Reading from /dev/random and /dev/urandom both have the same effect
1424-
* as calling getrandom(2) with flags=0. (In earlier versions, however,
1425-
* they each had different semantics.)
1433+
* Reading from /dev/random has the same functionality as calling
1434+
* getrandom(2) with flags=0. In earlier versions, however, it had
1435+
* vastly different semantics and should therefore be avoided, to
1436+
* prevent backwards compatibility issues.
1437+
*
1438+
* Reading from /dev/urandom has the same functionality as calling
1439+
* getrandom(2) with flags=GRND_INSECURE. Because it does not block
1440+
* waiting for the RNG to be ready, it should not be used.
14261441
*
14271442
* Writing to either /dev/random or /dev/urandom adds entropy to
14281443
* the input pool but does not credit it.
14291444
*
1430-
* Polling on /dev/random or /dev/urandom indicates when the RNG
1431-
* is initialized, on the read side, and when it wants new entropy,
1432-
* on the write side.
1445+
* Polling on /dev/random indicates when the RNG is initialized, on
1446+
* the read side, and when it wants new entropy, on the write side.
14331447
*
14341448
* Both /dev/random and /dev/urandom have the same set of ioctls for
14351449
* adding entropy, getting the entropy count, zeroing the count, and
@@ -1514,6 +1528,21 @@ static ssize_t random_write(struct file *file, const char __user *buffer,
15141528
return (ssize_t)count;
15151529
}
15161530

1531+
static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,
1532+
loff_t *ppos)
1533+
{
1534+
static int maxwarn = 10;
1535+
1536+
if (!crng_ready() && maxwarn > 0) {
1537+
maxwarn--;
1538+
if (__ratelimit(&urandom_warning))
1539+
pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
1540+
current->comm, nbytes);
1541+
}
1542+
1543+
return get_random_bytes_user(buf, nbytes);
1544+
}
1545+
15171546
static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,
15181547
loff_t *ppos)
15191548
{
@@ -1600,6 +1629,15 @@ const struct file_operations random_fops = {
16001629
.llseek = noop_llseek,
16011630
};
16021631

1632+
const struct file_operations urandom_fops = {
1633+
.read = urandom_read,
1634+
.write = random_write,
1635+
.unlocked_ioctl = random_ioctl,
1636+
.compat_ioctl = compat_ptr_ioctl,
1637+
.fasync = random_fasync,
1638+
.llseek = noop_llseek,
1639+
};
1640+
16031641

16041642
/********************************************************************
16051643
*

include/linux/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern int unregister_random_ready_notifier(struct notifier_block *nb);
4848
extern size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes);
4949

5050
#ifndef MODULE
51-
extern const struct file_operations random_fops;
51+
extern const struct file_operations random_fops, urandom_fops;
5252
#endif
5353

5454
u32 get_random_u32(void);

0 commit comments

Comments
 (0)