288288#define SEC_XFER_SIZE 512
289289#define EXTRACT_SIZE 10
290290
291- #define DEBUG_RANDOM_BOOT 0
292291
293292#define LONGS (x ) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
294293
@@ -437,6 +436,7 @@ static void _extract_crng(struct crng_state *crng,
437436static void _crng_backtrack_protect (struct crng_state * crng ,
438437 __u8 tmp [CHACHA20_BLOCK_SIZE ], int used );
439438static void process_random_ready_list (void );
439+ static void _get_random_bytes (void * buf , int nbytes );
440440
441441/**********************************************************************
442442 *
@@ -777,7 +777,7 @@ static void crng_initialize(struct crng_state *crng)
777777 _extract_entropy (& input_pool , & crng -> state [4 ],
778778 sizeof (__u32 ) * 12 , 0 );
779779 else
780- get_random_bytes (& crng -> state [4 ], sizeof (__u32 ) * 12 );
780+ _get_random_bytes (& crng -> state [4 ], sizeof (__u32 ) * 12 );
781781 for (i = 4 ; i < 16 ; i ++ ) {
782782 if (!arch_get_random_seed_long (& rv ) &&
783783 !arch_get_random_long (& rv ))
@@ -851,11 +851,6 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
851851 }
852852}
853853
854- static inline void crng_wait_ready (void )
855- {
856- wait_event_interruptible (crng_init_wait , crng_ready ());
857- }
858-
859854static void _extract_crng (struct crng_state * crng ,
860855 __u8 out [CHACHA20_BLOCK_SIZE ])
861856{
@@ -1477,22 +1472,44 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
14771472 return ret ;
14781473}
14791474
1475+ #define warn_unseeded_randomness (previous ) \
1476+ _warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous))
1477+
1478+ static void _warn_unseeded_randomness (const char * func_name , void * caller ,
1479+ void * * previous )
1480+ {
1481+ #ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM
1482+ const bool print_once = false;
1483+ #else
1484+ static bool print_once __read_mostly ;
1485+ #endif
1486+
1487+ if (print_once ||
1488+ crng_ready () ||
1489+ (previous && (caller == READ_ONCE (* previous ))))
1490+ return ;
1491+ WRITE_ONCE (* previous , caller );
1492+ #ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM
1493+ print_once = true;
1494+ #endif
1495+ pr_notice ("random: %s called from %pF with crng_init=%d\n" ,
1496+ func_name , caller , crng_init );
1497+ }
1498+
14801499/*
14811500 * This function is the exported kernel interface. It returns some
14821501 * number of good random numbers, suitable for key generation, seeding
14831502 * TCP sequence numbers, etc. It does not rely on the hardware random
14841503 * number generator. For random bytes direct from the hardware RNG
1485- * (when available), use get_random_bytes_arch().
1504+ * (when available), use get_random_bytes_arch(). In order to ensure
1505+ * that the randomness provided by this function is okay, the function
1506+ * wait_for_random_bytes() should be called and return 0 at least once
1507+ * at any point prior.
14861508 */
1487- void get_random_bytes (void * buf , int nbytes )
1509+ static void _get_random_bytes (void * buf , int nbytes )
14881510{
14891511 __u8 tmp [CHACHA20_BLOCK_SIZE ];
14901512
1491- #if DEBUG_RANDOM_BOOT > 0
1492- if (!crng_ready ())
1493- printk (KERN_NOTICE "random: %pF get_random_bytes called "
1494- "with crng_init = %d\n" , (void * ) _RET_IP_ , crng_init );
1495- #endif
14961513 trace_get_random_bytes (nbytes , _RET_IP_ );
14971514
14981515 while (nbytes >= CHACHA20_BLOCK_SIZE ) {
@@ -1509,8 +1526,34 @@ void get_random_bytes(void *buf, int nbytes)
15091526 crng_backtrack_protect (tmp , CHACHA20_BLOCK_SIZE );
15101527 memzero_explicit (tmp , sizeof (tmp ));
15111528}
1529+
1530+ void get_random_bytes (void * buf , int nbytes )
1531+ {
1532+ static void * previous ;
1533+
1534+ warn_unseeded_randomness (& previous );
1535+ _get_random_bytes (buf , nbytes );
1536+ }
15121537EXPORT_SYMBOL (get_random_bytes );
15131538
1539+ /*
1540+ * Wait for the urandom pool to be seeded and thus guaranteed to supply
1541+ * cryptographically secure random numbers. This applies to: the /dev/urandom
1542+ * device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
1543+ * family of functions. Using any of these functions without first calling
1544+ * this function forfeits the guarantee of security.
1545+ *
1546+ * Returns: 0 if the urandom pool has been seeded.
1547+ * -ERESTARTSYS if the function was interrupted by a signal.
1548+ */
1549+ int wait_for_random_bytes (void )
1550+ {
1551+ if (likely (crng_ready ()))
1552+ return 0 ;
1553+ return wait_event_interruptible (crng_init_wait , crng_ready ());
1554+ }
1555+ EXPORT_SYMBOL (wait_for_random_bytes );
1556+
15141557/*
15151558 * Add a callback function that will be invoked when the nonblocking
15161559 * pool is initialised.
@@ -1865,6 +1908,8 @@ const struct file_operations urandom_fops = {
18651908SYSCALL_DEFINE3 (getrandom , char __user * , buf , size_t , count ,
18661909 unsigned int , flags )
18671910{
1911+ int ret ;
1912+
18681913 if (flags & ~(GRND_NONBLOCK |GRND_RANDOM ))
18691914 return - EINVAL ;
18701915
@@ -1877,9 +1922,9 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
18771922 if (!crng_ready ()) {
18781923 if (flags & GRND_NONBLOCK )
18791924 return - EAGAIN ;
1880- crng_wait_ready ();
1881- if (signal_pending ( current ))
1882- return - ERESTARTSYS ;
1925+ ret = wait_for_random_bytes ();
1926+ if (unlikely ( ret ))
1927+ return ret ;
18831928 }
18841929 return urandom_read (NULL , buf , count , NULL );
18851930}
@@ -2040,15 +2085,19 @@ static rwlock_t batched_entropy_reset_lock = __RW_LOCK_UNLOCKED(batched_entropy_
20402085/*
20412086 * Get a random word for internal kernel use only. The quality of the random
20422087 * number is either as good as RDRAND or as good as /dev/urandom, with the
2043- * goal of being quite fast and not depleting entropy.
2088+ * goal of being quite fast and not depleting entropy. In order to ensure
2089+ * that the randomness provided by this function is okay, the function
2090+ * wait_for_random_bytes() should be called and return 0 at least once
2091+ * at any point prior.
20442092 */
20452093static DEFINE_PER_CPU (struct batched_entropy , batched_entropy_u64 ) ;
20462094u64 get_random_u64 (void )
20472095{
20482096 u64 ret ;
2049- bool use_lock = READ_ONCE ( crng_init ) < 2 ;
2097+ bool use_lock ;
20502098 unsigned long flags = 0 ;
20512099 struct batched_entropy * batch ;
2100+ static void * previous ;
20522101
20532102#if BITS_PER_LONG == 64
20542103 if (arch_get_random_long ((unsigned long * )& ret ))
@@ -2059,6 +2108,9 @@ u64 get_random_u64(void)
20592108 return ret ;
20602109#endif
20612110
2111+ warn_unseeded_randomness (& previous );
2112+
2113+ use_lock = READ_ONCE (crng_init ) < 2 ;
20622114 batch = & get_cpu_var (batched_entropy_u64 );
20632115 if (use_lock )
20642116 read_lock_irqsave (& batched_entropy_reset_lock , flags );
@@ -2078,13 +2130,17 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32);
20782130u32 get_random_u32 (void )
20792131{
20802132 u32 ret ;
2081- bool use_lock = READ_ONCE ( crng_init ) < 2 ;
2133+ bool use_lock ;
20822134 unsigned long flags = 0 ;
20832135 struct batched_entropy * batch ;
2136+ static void * previous ;
20842137
20852138 if (arch_get_random_int (& ret ))
20862139 return ret ;
20872140
2141+ warn_unseeded_randomness (& previous );
2142+
2143+ use_lock = READ_ONCE (crng_init ) < 2 ;
20882144 batch = & get_cpu_var (batched_entropy_u32 );
20892145 if (use_lock )
20902146 read_lock_irqsave (& batched_entropy_reset_lock , flags );
0 commit comments