@@ -89,14 +89,17 @@ static RAW_NOTIFIER_HEAD(random_ready_chain);
89
89
/* Control how we warn userspace. */
90
90
static struct ratelimit_state unseeded_warning =
91
91
RATELIMIT_STATE_INIT ("warn_unseeded_randomness" , HZ , 3 );
92
+ static struct ratelimit_state urandom_warning =
93
+ RATELIMIT_STATE_INIT ("warn_urandom_randomness" , HZ , 3 );
92
94
static int ratelimit_disable __read_mostly ;
93
95
module_param_named (ratelimit_disable , ratelimit_disable , int , 0644 );
94
96
MODULE_PARM_DESC (ratelimit_disable , "Disable random ratelimit suppression" );
95
97
96
98
/*
97
99
* 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.
100
103
*
101
104
* Returns: true if the input pool has been seeded.
102
105
* false if the input pool has not been seeded.
@@ -112,10 +115,10 @@ static void try_to_generate_entropy(void);
112
115
113
116
/*
114
117
* 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 .
119
122
*
120
123
* Returns: 0 if the input pool has been seeded.
121
124
* -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
220
223
* unsigned long get_random_long()
221
224
*
222
225
* 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.
227
230
*
228
231
*********************************************************************/
229
232
@@ -300,6 +303,11 @@ static void crng_reseed(bool force)
300
303
unseeded_warning .missed );
301
304
unseeded_warning .missed = 0 ;
302
305
}
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
+ }
303
311
}
304
312
}
305
313
@@ -979,8 +987,10 @@ int __init rand_initialize(void)
979
987
pr_notice ("crng init done (trusting CPU's manufacturer)\n" );
980
988
}
981
989
982
- if (ratelimit_disable )
990
+ if (ratelimit_disable ) {
991
+ urandom_warning .interval = 0 ;
983
992
unseeded_warning .interval = 0 ;
993
+ }
984
994
return 0 ;
985
995
}
986
996
@@ -1420,16 +1430,20 @@ static void try_to_generate_entropy(void)
1420
1430
* getrandom(2) is the primary modern interface into the RNG and should
1421
1431
* be used in preference to anything else.
1422
1432
*
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.
1426
1441
*
1427
1442
* Writing to either /dev/random or /dev/urandom adds entropy to
1428
1443
* the input pool but does not credit it.
1429
1444
*
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.
1433
1447
*
1434
1448
* Both /dev/random and /dev/urandom have the same set of ioctls for
1435
1449
* 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,
1514
1528
return (ssize_t )count ;
1515
1529
}
1516
1530
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
+
1517
1546
static ssize_t random_read (struct file * file , char __user * buf , size_t nbytes ,
1518
1547
loff_t * ppos )
1519
1548
{
@@ -1600,6 +1629,15 @@ const struct file_operations random_fops = {
1600
1629
.llseek = noop_llseek ,
1601
1630
};
1602
1631
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
+
1603
1641
1604
1642
/********************************************************************
1605
1643
*
0 commit comments