@@ -89,14 +89,17 @@ static RAW_NOTIFIER_HEAD(random_ready_chain);
8989/* Control how we warn userspace. */
9090static 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 );
9294static int ratelimit_disable __read_mostly ;
9395module_param_named (ratelimit_disable , ratelimit_disable , int , 0644 );
9496MODULE_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+
15171546static 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 *
0 commit comments