Skip to contentSkip to conversation

random: always fill buffer in get_random_bytes_wait

PATCH Applied Jason A. Donenfeld 2 participants 3 messages Feb 4, 2018 – Feb 28, 2018 Random number driver

lore
Share & tools
lore

Jason A. Donenfeld

author maintainer
#0 lore raw

PATCH 1/1 random: always fill buffer in get_random_bytes_wait

In the unfortunate event that a developer fails to check the return value of get_random_bytes_wait, or simply wants to make a "best effort" attempt, for whatever that's worth, it's much better to still fill the buffer with _something_ rather than catastrophically failing in the case of an interruption. This is both a defense in depth measure against inevitable programming bugs, as well as a means of making the API a bit more useful.

---
 include/linux/random.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/random.h +1 -3
index 4024f7d9c77d..2ddf13b4281e 100644
@@ -85,10 +85,8 @@ static inline unsigned long get_random_canary(void)
 static inline int get_random_bytes_wait(void *buf, int nbytes)
 {
 	int ret = wait_for_random_bytes();
-	if (unlikely(ret))
-		return ret;
 	get_random_bytes(buf, nbytes);
-	return 0;
+	return ret;
 }
 
 #define declare_get_random_var_wait(var) \
Signature · 1 line
2.16.1
maintainer Applied
#2 lore raw
In reply to Jason A. Donenfeld
Quoted 9 lines · Jason A. Donenfeld

In the unfortunate event that a developer fails to check the return value of get_random_bytes_wait, or simply wants to make a "best effort" attempt, for whatever that's worth, it's much better to still fill the buffer with _something_ rather than catastrophically failing in the case of an interruption. This is both a defense in depth measure against inevitable programming bugs, as well as a means of making the API a bit more useful.

Thanks, applied.

- Ted