aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-07-15 15:44:02 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-07-15 15:44:02 -0400
commit52f6c588c77b76d548201470c2a28263a41b462b (patch)
treecbb4207714e82f10932a546469bfb3db84051c33
parent78dcf73421a879d22319d3889119945b85954a68 (diff)
parent72e5c740f6335e27253b8ff64d23d00337091535 (diff)
Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random
Pull random updates from Ted Ts'o: "Add wait_for_random_bytes() and get_random_*_wait() functions so that callers can more safely get random bytes if they can block until the CRNG is initialized. Also print a warning if get_random_*() is called before the CRNG is initialized. By default, only one single-line warning will be printed per boot. If CONFIG_WARN_ALL_UNSEEDED_RANDOM is defined, then a warning will be printed for each function which tries to get random bytes before the CRNG is initialized. This can get spammy for certain architecture types, so it is not enabled by default" * tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random: random: reorder READ_ONCE() in get_random_uXX random: suppress spammy warnings about unseeded randomness random: warn when kernel uses unseeded randomness net/route: use get_random_int for random counter net/neighbor: use get_random_u32 for 32-bit hash random rhashtable: use get_random_u32 for hash_rnd ceph: ensure RNG is seeded before using iscsi: ensure RNG is seeded before use cifs: use get_random_u32 for 32-bit lock random random: add get_random_{bytes,u32,u64,int,long,once}_wait family random: add wait_for_random_bytes() API
-rw-r--r--drivers/char/random.c96
-rw-r--r--drivers/target/iscsi/iscsi_target_auth.c14
-rw-r--r--drivers/target/iscsi/iscsi_target_login.c22
-rw-r--r--fs/cifs/cifsfs.c2
-rw-r--r--include/linux/net.h2
-rw-r--r--include/linux/once.h2
-rw-r--r--include/linux/random.h26
-rw-r--r--lib/Kconfig.debug28
-rw-r--r--lib/rhashtable.c2
-rw-r--r--net/ceph/ceph_common.c6
-rw-r--r--net/core/neighbour.c3
-rw-r--r--net/ipv4/route.c3
12 files changed, 168 insertions, 38 deletions
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 23cab7a8c1c1..afa3ce7d3e72 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -288,7 +288,6 @@
288#define SEC_XFER_SIZE 512 288#define SEC_XFER_SIZE 512
289#define EXTRACT_SIZE 10 289#define EXTRACT_SIZE 10
290 290
291#define DEBUG_RANDOM_BOOT 0
292 291
293#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long)) 292#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
294 293
@@ -437,6 +436,7 @@ static void _extract_crng(struct crng_state *crng,
437static void _crng_backtrack_protect(struct crng_state *crng, 436static void _crng_backtrack_protect(struct crng_state *crng,
438 __u8 tmp[CHACHA20_BLOCK_SIZE], int used); 437 __u8 tmp[CHACHA20_BLOCK_SIZE], int used);
439static void process_random_ready_list(void); 438static void process_random_ready_list(void);
439static void _get_random_bytes(void *buf, int nbytes);
440 440
441/********************************************************************** 441/**********************************************************************
442 * 442 *
@@ -777,7 +777,7 @@ static void crng_initialize(struct crng_state *crng)
777 _extract_entropy(&input_pool, &crng->state[4], 777 _extract_entropy(&input_pool, &crng->state[4],
778 sizeof(__u32) * 12, 0); 778 sizeof(__u32) * 12, 0);
779 else 779 else
780 get_random_bytes(&crng->state[4], sizeof(__u32) * 12); 780 _get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
781 for (i = 4; i < 16; i++) { 781 for (i = 4; i < 16; i++) {
782 if (!arch_get_random_seed_long(&rv) && 782 if (!arch_get_random_seed_long(&rv) &&
783 !arch_get_random_long(&rv)) 783 !arch_get_random_long(&rv))
@@ -851,11 +851,6 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
851 } 851 }
852} 852}
853 853
854static inline void crng_wait_ready(void)
855{
856 wait_event_interruptible(crng_init_wait, crng_ready());
857}
858
859static void _extract_crng(struct crng_state *crng, 854static void _extract_crng(struct crng_state *crng,
860 __u8 out[CHACHA20_BLOCK_SIZE]) 855 __u8 out[CHACHA20_BLOCK_SIZE])
861{ 856{
@@ -1477,22 +1472,44 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
1477 return ret; 1472 return ret;
1478} 1473}
1479 1474
1475#define warn_unseeded_randomness(previous) \
1476 _warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous))
1477
1478static 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
1480/* 1499/*
1481 * This function is the exported kernel interface. It returns some 1500 * This function is the exported kernel interface. It returns some
1482 * number of good random numbers, suitable for key generation, seeding 1501 * number of good random numbers, suitable for key generation, seeding
1483 * TCP sequence numbers, etc. It does not rely on the hardware random 1502 * TCP sequence numbers, etc. It does not rely on the hardware random
1484 * number generator. For random bytes direct from the hardware RNG 1503 * 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.
1486 */ 1508 */
1487void get_random_bytes(void *buf, int nbytes) 1509static void _get_random_bytes(void *buf, int nbytes)
1488{ 1510{
1489 __u8 tmp[CHACHA20_BLOCK_SIZE]; 1511 __u8 tmp[CHACHA20_BLOCK_SIZE];
1490 1512
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
1496 trace_get_random_bytes(nbytes, _RET_IP_); 1513 trace_get_random_bytes(nbytes, _RET_IP_);
1497 1514
1498 while (nbytes >= CHACHA20_BLOCK_SIZE) { 1515 while (nbytes >= CHACHA20_BLOCK_SIZE) {
@@ -1509,9 +1526,35 @@ void get_random_bytes(void *buf, int nbytes)
1509 crng_backtrack_protect(tmp, CHACHA20_BLOCK_SIZE); 1526 crng_backtrack_protect(tmp, CHACHA20_BLOCK_SIZE);
1510 memzero_explicit(tmp, sizeof(tmp)); 1527 memzero_explicit(tmp, sizeof(tmp));
1511} 1528}
1529
1530void 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}
1512EXPORT_SYMBOL(get_random_bytes); 1537EXPORT_SYMBOL(get_random_bytes);
1513 1538
1514/* 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 */
1549int 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}
1555EXPORT_SYMBOL(wait_for_random_bytes);
1556
1557/*
1515 * Add a callback function that will be invoked when the nonblocking 1558 * Add a callback function that will be invoked when the nonblocking
1516 * pool is initialised. 1559 * pool is initialised.
1517 * 1560 *
@@ -1865,6 +1908,8 @@ const struct file_operations urandom_fops = {
1865SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, 1908SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
1866 unsigned int, flags) 1909 unsigned int, flags)
1867{ 1910{
1911 int ret;
1912
1868 if (flags & ~(GRND_NONBLOCK|GRND_RANDOM)) 1913 if (flags & ~(GRND_NONBLOCK|GRND_RANDOM))
1869 return -EINVAL; 1914 return -EINVAL;
1870 1915
@@ -1877,9 +1922,9 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
1877 if (!crng_ready()) { 1922 if (!crng_ready()) {
1878 if (flags & GRND_NONBLOCK) 1923 if (flags & GRND_NONBLOCK)
1879 return -EAGAIN; 1924 return -EAGAIN;
1880 crng_wait_ready(); 1925 ret = wait_for_random_bytes();
1881 if (signal_pending(current)) 1926 if (unlikely(ret))
1882 return -ERESTARTSYS; 1927 return ret;
1883 } 1928 }
1884 return urandom_read(NULL, buf, count, NULL); 1929 return urandom_read(NULL, buf, count, NULL);
1885} 1930}
@@ -2040,15 +2085,19 @@ static rwlock_t batched_entropy_reset_lock = __RW_LOCK_UNLOCKED(batched_entropy_
2040/* 2085/*
2041 * Get a random word for internal kernel use only. The quality of the random 2086 * Get a random word for internal kernel use only. The quality of the random
2042 * number is either as good as RDRAND or as good as /dev/urandom, with the 2087 * 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.
2044 */ 2092 */
2045static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64); 2093static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64);
2046u64 get_random_u64(void) 2094u64 get_random_u64(void)
2047{ 2095{
2048 u64 ret; 2096 u64 ret;
2049 bool use_lock = READ_ONCE(crng_init) < 2; 2097 bool use_lock;
2050 unsigned long flags = 0; 2098 unsigned long flags = 0;
2051 struct batched_entropy *batch; 2099 struct batched_entropy *batch;
2100 static void *previous;
2052 2101
2053#if BITS_PER_LONG == 64 2102#if BITS_PER_LONG == 64
2054 if (arch_get_random_long((unsigned long *)&ret)) 2103 if (arch_get_random_long((unsigned long *)&ret))
@@ -2059,6 +2108,9 @@ u64 get_random_u64(void)
2059 return ret; 2108 return ret;
2060#endif 2109#endif
2061 2110
2111 warn_unseeded_randomness(&previous);
2112
2113 use_lock = READ_ONCE(crng_init) < 2;
2062 batch = &get_cpu_var(batched_entropy_u64); 2114 batch = &get_cpu_var(batched_entropy_u64);
2063 if (use_lock) 2115 if (use_lock)
2064 read_lock_irqsave(&batched_entropy_reset_lock, flags); 2116 read_lock_irqsave(&batched_entropy_reset_lock, flags);
@@ -2078,13 +2130,17 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32);
2078u32 get_random_u32(void) 2130u32 get_random_u32(void)
2079{ 2131{
2080 u32 ret; 2132 u32 ret;
2081 bool use_lock = READ_ONCE(crng_init) < 2; 2133 bool use_lock;
2082 unsigned long flags = 0; 2134 unsigned long flags = 0;
2083 struct batched_entropy *batch; 2135 struct batched_entropy *batch;
2136 static void *previous;
2084 2137
2085 if (arch_get_random_int(&ret)) 2138 if (arch_get_random_int(&ret))
2086 return ret; 2139 return ret;
2087 2140
2141 warn_unseeded_randomness(&previous);
2142
2143 use_lock = READ_ONCE(crng_init) < 2;
2088 batch = &get_cpu_var(batched_entropy_u32); 2144 batch = &get_cpu_var(batched_entropy_u32);
2089 if (use_lock) 2145 if (use_lock)
2090 read_lock_irqsave(&batched_entropy_reset_lock, flags); 2146 read_lock_irqsave(&batched_entropy_reset_lock, flags);
diff --git a/drivers/target/iscsi/iscsi_target_auth.c b/drivers/target/iscsi/iscsi_target_auth.c
index 903b667f8e01..f9bc8ec6fb6b 100644
--- a/drivers/target/iscsi/iscsi_target_auth.c
+++ b/drivers/target/iscsi/iscsi_target_auth.c
@@ -47,18 +47,21 @@ static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
47 } 47 }
48} 48}
49 49
50static void chap_gen_challenge( 50static int chap_gen_challenge(
51 struct iscsi_conn *conn, 51 struct iscsi_conn *conn,
52 int caller, 52 int caller,
53 char *c_str, 53 char *c_str,
54 unsigned int *c_len) 54 unsigned int *c_len)
55{ 55{
56 int ret;
56 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1]; 57 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
57 struct iscsi_chap *chap = conn->auth_protocol; 58 struct iscsi_chap *chap = conn->auth_protocol;
58 59
59 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1); 60 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
60 61
61 get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH); 62 ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH);
63 if (unlikely(ret))
64 return ret;
62 chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge, 65 chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
63 CHAP_CHALLENGE_LENGTH); 66 CHAP_CHALLENGE_LENGTH);
64 /* 67 /*
@@ -69,6 +72,7 @@ static void chap_gen_challenge(
69 72
70 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client", 73 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
71 challenge_asciihex); 74 challenge_asciihex);
75 return 0;
72} 76}
73 77
74static int chap_check_algorithm(const char *a_str) 78static int chap_check_algorithm(const char *a_str)
@@ -143,6 +147,7 @@ static struct iscsi_chap *chap_server_open(
143 case CHAP_DIGEST_UNKNOWN: 147 case CHAP_DIGEST_UNKNOWN:
144 default: 148 default:
145 pr_err("Unsupported CHAP_A value\n"); 149 pr_err("Unsupported CHAP_A value\n");
150 kfree(conn->auth_protocol);
146 return NULL; 151 return NULL;
147 } 152 }
148 153
@@ -156,7 +161,10 @@ static struct iscsi_chap *chap_server_open(
156 /* 161 /*
157 * Generate Challenge. 162 * Generate Challenge.
158 */ 163 */
159 chap_gen_challenge(conn, 1, aic_str, aic_len); 164 if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
165 kfree(conn->auth_protocol);
166 return NULL;
167 }
160 168
161 return chap; 169 return chap;
162} 170}
diff --git a/drivers/target/iscsi/iscsi_target_login.c b/drivers/target/iscsi/iscsi_target_login.c
index 92b96b51d506..e9bdc8b86e7d 100644
--- a/drivers/target/iscsi/iscsi_target_login.c
+++ b/drivers/target/iscsi/iscsi_target_login.c
@@ -245,22 +245,26 @@ int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
245 return 0; 245 return 0;
246} 246}
247 247
248static void iscsi_login_set_conn_values( 248static int iscsi_login_set_conn_values(
249 struct iscsi_session *sess, 249 struct iscsi_session *sess,
250 struct iscsi_conn *conn, 250 struct iscsi_conn *conn,
251 __be16 cid) 251 __be16 cid)
252{ 252{
253 int ret;
253 conn->sess = sess; 254 conn->sess = sess;
254 conn->cid = be16_to_cpu(cid); 255 conn->cid = be16_to_cpu(cid);
255 /* 256 /*
256 * Generate a random Status sequence number (statsn) for the new 257 * Generate a random Status sequence number (statsn) for the new
257 * iSCSI connection. 258 * iSCSI connection.
258 */ 259 */
259 get_random_bytes(&conn->stat_sn, sizeof(u32)); 260 ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
261 if (unlikely(ret))
262 return ret;
260 263
261 mutex_lock(&auth_id_lock); 264 mutex_lock(&auth_id_lock);
262 conn->auth_id = iscsit_global->auth_id++; 265 conn->auth_id = iscsit_global->auth_id++;
263 mutex_unlock(&auth_id_lock); 266 mutex_unlock(&auth_id_lock);
267 return 0;
264} 268}
265 269
266__printf(2, 3) int iscsi_change_param_sprintf( 270__printf(2, 3) int iscsi_change_param_sprintf(
@@ -306,7 +310,11 @@ static int iscsi_login_zero_tsih_s1(
306 return -ENOMEM; 310 return -ENOMEM;
307 } 311 }
308 312
309 iscsi_login_set_conn_values(sess, conn, pdu->cid); 313 ret = iscsi_login_set_conn_values(sess, conn, pdu->cid);
314 if (unlikely(ret)) {
315 kfree(sess);
316 return ret;
317 }
310 sess->init_task_tag = pdu->itt; 318 sess->init_task_tag = pdu->itt;
311 memcpy(&sess->isid, pdu->isid, 6); 319 memcpy(&sess->isid, pdu->isid, 6);
312 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn); 320 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
@@ -497,8 +505,7 @@ static int iscsi_login_non_zero_tsih_s1(
497{ 505{
498 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; 506 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
499 507
500 iscsi_login_set_conn_values(NULL, conn, pdu->cid); 508 return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
501 return 0;
502} 509}
503 510
504/* 511/*
@@ -554,9 +561,8 @@ static int iscsi_login_non_zero_tsih_s2(
554 atomic_set(&sess->session_continuation, 1); 561 atomic_set(&sess->session_continuation, 1);
555 spin_unlock_bh(&sess->conn_lock); 562 spin_unlock_bh(&sess->conn_lock);
556 563
557 iscsi_login_set_conn_values(sess, conn, pdu->cid); 564 if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
558 565 iscsi_copy_param_list(&conn->param_list,
559 if (iscsi_copy_param_list(&conn->param_list,
560 conn->tpg->param_list, 0) < 0) { 566 conn->tpg->param_list, 0) < 0) {
561 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 567 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
562 ISCSI_LOGIN_STATUS_NO_RESOURCES); 568 ISCSI_LOGIN_STATUS_NO_RESOURCES);
diff --git a/fs/cifs/cifsfs.c b/fs/cifs/cifsfs.c
index 556f480c6936..180b3356ff86 100644
--- a/fs/cifs/cifsfs.c
+++ b/fs/cifs/cifsfs.c
@@ -1354,7 +1354,7 @@ init_cifs(void)
1354 spin_lock_init(&cifs_tcp_ses_lock); 1354 spin_lock_init(&cifs_tcp_ses_lock);
1355 spin_lock_init(&GlobalMid_Lock); 1355 spin_lock_init(&GlobalMid_Lock);
1356 1356
1357 get_random_bytes(&cifs_lock_secret, sizeof(cifs_lock_secret)); 1357 cifs_lock_secret = get_random_u32();
1358 1358
1359 if (cifs_max_pending < 2) { 1359 if (cifs_max_pending < 2) {
1360 cifs_max_pending = 2; 1360 cifs_max_pending = 2;
diff --git a/include/linux/net.h b/include/linux/net.h
index abcfa46a2bd9..dda2cc939a53 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -274,6 +274,8 @@ do { \
274 274
275#define net_get_random_once(buf, nbytes) \ 275#define net_get_random_once(buf, nbytes) \
276 get_random_once((buf), (nbytes)) 276 get_random_once((buf), (nbytes))
277#define net_get_random_once_wait(buf, nbytes) \
278 get_random_once_wait((buf), (nbytes))
277 279
278int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, 280int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
279 size_t num, size_t len); 281 size_t num, size_t len);
diff --git a/include/linux/once.h b/include/linux/once.h
index 285f12cb40e6..9c98aaa87cbc 100644
--- a/include/linux/once.h
+++ b/include/linux/once.h
@@ -53,5 +53,7 @@ void __do_once_done(bool *done, struct static_key *once_key,
53 53
54#define get_random_once(buf, nbytes) \ 54#define get_random_once(buf, nbytes) \
55 DO_ONCE(get_random_bytes, (buf), (nbytes)) 55 DO_ONCE(get_random_bytes, (buf), (nbytes))
56#define get_random_once_wait(buf, nbytes) \
57 DO_ONCE(get_random_bytes_wait, (buf), (nbytes)) \
56 58
57#endif /* _LINUX_ONCE_H */ 59#endif /* _LINUX_ONCE_H */
diff --git a/include/linux/random.h b/include/linux/random.h
index 1fa0dc880bd7..eafea6a09361 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -34,6 +34,7 @@ extern void add_input_randomness(unsigned int type, unsigned int code,
34extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy; 34extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
35 35
36extern void get_random_bytes(void *buf, int nbytes); 36extern void get_random_bytes(void *buf, int nbytes);
37extern int wait_for_random_bytes(void);
37extern int add_random_ready_callback(struct random_ready_callback *rdy); 38extern int add_random_ready_callback(struct random_ready_callback *rdy);
38extern void del_random_ready_callback(struct random_ready_callback *rdy); 39extern void del_random_ready_callback(struct random_ready_callback *rdy);
39extern void get_random_bytes_arch(void *buf, int nbytes); 40extern void get_random_bytes_arch(void *buf, int nbytes);
@@ -78,6 +79,31 @@ static inline unsigned long get_random_canary(void)
78 return val & CANARY_MASK; 79 return val & CANARY_MASK;
79} 80}
80 81
82/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
83 * Returns the result of the call to wait_for_random_bytes. */
84static inline int get_random_bytes_wait(void *buf, int nbytes)
85{
86 int ret = wait_for_random_bytes();
87 if (unlikely(ret))
88 return ret;
89 get_random_bytes(buf, nbytes);
90 return 0;
91}
92
93#define declare_get_random_var_wait(var) \
94 static inline int get_random_ ## var ## _wait(var *out) { \
95 int ret = wait_for_random_bytes(); \
96 if (unlikely(ret)) \
97 return ret; \
98 *out = get_random_ ## var(); \
99 return 0; \
100 }
101declare_get_random_var_wait(u32)
102declare_get_random_var_wait(u64)
103declare_get_random_var_wait(int)
104declare_get_random_var_wait(long)
105#undef declare_get_random_var
106
81unsigned long randomize_page(unsigned long start, unsigned long range); 107unsigned long randomize_page(unsigned long start, unsigned long range);
82 108
83u32 prandom_u32(void); 109u32 prandom_u32(void);
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 789c6e9e5e01..98fe715522e8 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1223,6 +1223,34 @@ config STACKTRACE
1223 It is also used by various kernel debugging features that require 1223 It is also used by various kernel debugging features that require
1224 stack trace generation. 1224 stack trace generation.
1225 1225
1226config WARN_ALL_UNSEEDED_RANDOM
1227 bool "Warn for all uses of unseeded randomness"
1228 default n
1229 help
1230 Some parts of the kernel contain bugs relating to their use of
1231 cryptographically secure random numbers before it's actually possible
1232 to generate those numbers securely. This setting ensures that these
1233 flaws don't go unnoticed, by enabling a message, should this ever
1234 occur. This will allow people with obscure setups to know when things
1235 are going wrong, so that they might contact developers about fixing
1236 it.
1237
1238 Unfortunately, on some models of some architectures getting
1239 a fully seeded CRNG is extremely difficult, and so this can
1240 result in dmesg getting spammed for a surprisingly long
1241 time. This is really bad from a security perspective, and
1242 so architecture maintainers really need to do what they can
1243 to get the CRNG seeded sooner after the system is booted.
1244 However, since users can not do anything actionble to
1245 address this, by default the kernel will issue only a single
1246 warning for the first use of unseeded randomness.
1247
1248 Say Y here if you want to receive warnings for all uses of
1249 unseeded randomness. This will be of use primarily for
1250 those developers interersted in improving the security of
1251 Linux kernels running on their architecture (or
1252 subarchitecture).
1253
1226config DEBUG_KOBJECT 1254config DEBUG_KOBJECT
1227 bool "kobject debugging" 1255 bool "kobject debugging"
1228 depends on DEBUG_KERNEL 1256 depends on DEBUG_KERNEL
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 42466c167257..707ca5d677c6 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -234,7 +234,7 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
234 234
235 INIT_LIST_HEAD(&tbl->walkers); 235 INIT_LIST_HEAD(&tbl->walkers);
236 236
237 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 237 tbl->hash_rnd = get_random_u32();
238 238
239 for (i = 0; i < nbuckets; i++) 239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); 240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
diff --git a/net/ceph/ceph_common.c b/net/ceph/ceph_common.c
index 3d265c5cb6d0..5c036d2f401e 100644
--- a/net/ceph/ceph_common.c
+++ b/net/ceph/ceph_common.c
@@ -599,7 +599,11 @@ struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
599{ 599{
600 struct ceph_client *client; 600 struct ceph_client *client;
601 struct ceph_entity_addr *myaddr = NULL; 601 struct ceph_entity_addr *myaddr = NULL;
602 int err = -ENOMEM; 602 int err;
603
604 err = wait_for_random_bytes();
605 if (err < 0)
606 return ERR_PTR(err);
603 607
604 client = kzalloc(sizeof(*client), GFP_KERNEL); 608 client = kzalloc(sizeof(*client), GFP_KERNEL);
605 if (client == NULL) 609 if (client == NULL)
diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index e31fc11a8000..d0713627deb6 100644
--- a/net/core/neighbour.c
+++ b/net/core/neighbour.c
@@ -347,8 +347,7 @@ out_entries:
347 347
348static void neigh_get_hash_rnd(u32 *x) 348static void neigh_get_hash_rnd(u32 *x)
349{ 349{
350 get_random_bytes(x, sizeof(*x)); 350 *x = get_random_u32() | 1;
351 *x |= 1;
352} 351}
353 352
354static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift) 353static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index c816cd53f7fc..0383e66f59bc 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -2979,8 +2979,7 @@ static __net_init int rt_genid_init(struct net *net)
2979{ 2979{
2980 atomic_set(&net->ipv4.rt_genid, 0); 2980 atomic_set(&net->ipv4.rt_genid, 0);
2981 atomic_set(&net->fnhe_genid, 0); 2981 atomic_set(&net->fnhe_genid, 0);
2982 get_random_bytes(&net->ipv4.dev_addr_genid, 2982 atomic_set(&net->ipv4.dev_addr_genid, get_random_int());
2983 sizeof(net->ipv4.dev_addr_genid));
2984 return 0; 2983 return 0;
2985} 2984}
2986 2985