Skip to content

Commit 753d433

Browse files
tchardingtytso
authored andcommitted
random: Return nbytes filled from hw RNG
Currently the function get_random_bytes_arch() has return value 'void'. If the hw RNG fails we currently fall back to using get_random_bytes(). This defeats the purpose of requesting random material from the hw RNG in the first place. There are currently no intree users of get_random_bytes_arch(). Only get random bytes from the hw RNG, make function return the number of bytes retrieved from the hw RNG. Acked-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Tobin C. Harding <me@tobin.cc> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 8ddd6ef commit 753d433

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

drivers/char/random.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,26 +1725,28 @@ EXPORT_SYMBOL(del_random_ready_callback);
17251725
* key known by the NSA). So it's useful if we need the speed, but
17261726
* only if we're willing to trust the hardware manufacturer not to
17271727
* have put in a back door.
1728+
*
1729+
* Return number of bytes filled in.
17281730
*/
1729-
void get_random_bytes_arch(void *buf, int nbytes)
1731+
int __must_check get_random_bytes_arch(void *buf, int nbytes)
17301732
{
1733+
int left = nbytes;
17311734
char *p = buf;
17321735

1733-
trace_get_random_bytes_arch(nbytes, _RET_IP_);
1734-
while (nbytes) {
1736+
trace_get_random_bytes_arch(left, _RET_IP_);
1737+
while (left) {
17351738
unsigned long v;
1736-
int chunk = min(nbytes, (int)sizeof(unsigned long));
1739+
int chunk = min_t(int, left, sizeof(unsigned long));
17371740

17381741
if (!arch_get_random_long(&v))
17391742
break;
17401743

17411744
memcpy(p, &v, chunk);
17421745
p += chunk;
1743-
nbytes -= chunk;
1746+
left -= chunk;
17441747
}
17451748

1746-
if (nbytes)
1747-
get_random_bytes(p, nbytes);
1749+
return nbytes - left;
17481750
}
17491751
EXPORT_SYMBOL(get_random_bytes_arch);
17501752

include/linux/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern void get_random_bytes(void *buf, int nbytes);
3838
extern int wait_for_random_bytes(void);
3939
extern int add_random_ready_callback(struct random_ready_callback *rdy);
4040
extern void del_random_ready_callback(struct random_ready_callback *rdy);
41-
extern void get_random_bytes_arch(void *buf, int nbytes);
41+
extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
4242

4343
#ifndef MODULE
4444
extern const struct file_operations random_fops, urandom_fops;

0 commit comments

Comments
 (0)