History

random: make consistent use of buf and len

The current code was a mix of "nbytes", "count", "size", "buffer", "in", and so forth. Instead, let's clean this up by naming input parameters "buf" (or "ubuf") and "len", so that you always understand that you're reading this variety of function argument.

Jason A. Donenfeld committed 4 years ago

Commit a1940261 parent 7c3a8a12 files · +103 −108Browse files

Unified · Split

2 files
  1. drivers/char/random.c +97 −102
  2. include/linux/random.h +6 −6
drivers/char/random.c+97−102
@@ -209,7 +209,7 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller) · 208 unmodified lines
209 *209 *
210 * There are a few exported interfaces for use by other drivers:210 * There are a few exported interfaces for use by other drivers:
211 *211 *
212 * void get_random_bytes(void *buf, size_t nbytes)212 * void get_random_bytes(void *buf, size_t len)
213 * u32 get_random_u32()213 * u32 get_random_u32()
214 * u64 get_random_u64()214 * u64 get_random_u64()
215 * unsigned int get_random_int()215 * unsigned int get_random_int()
@@ -250,7 +250,7 @@ static DEFINE_PER_CPU(struct crng, crngs) = { · 34 unmodified lines
250};250};
251 251
252/* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */252/* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */
253static void extract_entropy(void *buf, size_t nbytes);253static void extract_entropy(void *buf, size_t len);
254 254
255/* This extracts a new crng key from the input pool. */255/* This extracts a new crng key from the input pool. */
256static void crng_reseed(void)256static void crng_reseed(void)
@@ -404,24 +404,24 @@ static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS], · 147 unmodified lines
404 local_unlock_irqrestore(&crngs.lock, flags);404 local_unlock_irqrestore(&crngs.lock, flags);
405}405}
406 406
407static void _get_random_bytes(void *buf, size_t nbytes)407static void _get_random_bytes(void *buf, size_t len)
408{408{
409 u32 chacha_state[CHACHA_STATE_WORDS];409 u32 chacha_state[CHACHA_STATE_WORDS];
410 u8 tmp[CHACHA_BLOCK_SIZE];410 u8 tmp[CHACHA_BLOCK_SIZE];
411 size_t len;411 size_t first_block_len;
412 412
413 if (!nbytes)413 if (!len)
414 return;414 return;
415 415
416 len = min_t(size_t, 32, nbytes);416 first_block_len = min_t(size_t, 32, len);
417 crng_make_state(chacha_state, buf, len);417 crng_make_state(chacha_state, buf, first_block_len);
418 nbytes -= len;418 len -= first_block_len;
419 buf += len;419 buf += first_block_len;
420 420
421 while (nbytes) {421 while (len) {
422 if (nbytes < CHACHA_BLOCK_SIZE) {422 if (len < CHACHA_BLOCK_SIZE) {
423 chacha20_block(chacha_state, tmp);423 chacha20_block(chacha_state, tmp);
424 memcpy(buf, tmp, nbytes);424 memcpy(buf, tmp, len);
425 memzero_explicit(tmp, sizeof(tmp));425 memzero_explicit(tmp, sizeof(tmp));
426 break;426 break;
427 }427 }
@@ -429,7 +429,7 @@ static void _get_random_bytes(void *buf, size_t nbytes) · 1 unmodified line
429 chacha20_block(chacha_state, buf);429 chacha20_block(chacha_state, buf);
430 if (unlikely(chacha_state[12] == 0))430 if (unlikely(chacha_state[12] == 0))
431 ++chacha_state[13];431 ++chacha_state[13];
432 nbytes -= CHACHA_BLOCK_SIZE;432 len -= CHACHA_BLOCK_SIZE;
433 buf += CHACHA_BLOCK_SIZE;433 buf += CHACHA_BLOCK_SIZE;
434 }434 }
435 435
@@ -446,20 +446,20 @@ static void _get_random_bytes(void *buf, size_t nbytes) · 10 unmodified lines
446 * wait_for_random_bytes() should be called and return 0 at least once446 * wait_for_random_bytes() should be called and return 0 at least once
447 * at any point prior.447 * at any point prior.
448 */448 */
449void get_random_bytes(void *buf, size_t nbytes)449void get_random_bytes(void *buf, size_t len)
450{450{
451 warn_unseeded_randomness();451 warn_unseeded_randomness();
452 _get_random_bytes(buf, nbytes);452 _get_random_bytes(buf, len);
453}453}
454EXPORT_SYMBOL(get_random_bytes);454EXPORT_SYMBOL(get_random_bytes);
455 455
456static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)456static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
457{457{
458 size_t len, left, ret = 0;458 size_t block_len, left, ret = 0;
459 u32 chacha_state[CHACHA_STATE_WORDS];459 u32 chacha_state[CHACHA_STATE_WORDS];
460 u8 output[CHACHA_BLOCK_SIZE];460 u8 output[CHACHA_BLOCK_SIZE];
461 461
462 if (!nbytes)462 if (!len)
463 return 0;463 return 0;
464 464
465 /*465 /*
@@ -473,8 +473,8 @@ static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes) · 7 unmodified lines
473 * use chacha_state after, so we can simply return those bytes to473 * use chacha_state after, so we can simply return those bytes to
474 * the user directly.474 * the user directly.
475 */475 */
476 if (nbytes <= CHACHA_KEY_SIZE) {476 if (len <= CHACHA_KEY_SIZE) {
477 ret = nbytes - copy_to_user(buf, &chacha_state[4], nbytes);477 ret = len - copy_to_user(ubuf, &chacha_state[4], len);
478 goto out_zero_chacha;478 goto out_zero_chacha;
479 }479 }
480 480
@@ -483,17 +483,17 @@ static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes) · 2 unmodified lines
483 if (unlikely(chacha_state[12] == 0))483 if (unlikely(chacha_state[12] == 0))
484 ++chacha_state[13];484 ++chacha_state[13];
485 485
486 len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE);486 block_len = min_t(size_t, len, CHACHA_BLOCK_SIZE);
487 left = copy_to_user(buf, output, len);487 left = copy_to_user(ubuf, output, block_len);
488 if (left) {488 if (left) {
489 ret += len - left;489 ret += block_len - left;
490 break;490 break;
491 }491 }
492 492
493 buf += len;493 ubuf += block_len;
494 ret += len;494 ret += block_len;
495 nbytes -= len;495 len -= block_len;
496 if (!nbytes)496 if (!len)
497 break;497 break;
498 498
499 BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);499 BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);
@@ -667,24 +667,24 @@ unsigned long randomize_page(unsigned long start, unsigned long range) · 167 unmodified lines
667 * use. Use get_random_bytes() instead. It returns the number of667 * use. Use get_random_bytes() instead. It returns the number of
668 * bytes filled in.668 * bytes filled in.
669 */669 */
670size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes)670size_t __must_check get_random_bytes_arch(void *buf, size_t len)
671{671{
672 size_t left = nbytes;672 size_t left = len;
673 u8 *p = buf;673 u8 *p = buf;
674 674
675 while (left) {675 while (left) {
676 unsigned long v;676 unsigned long v;
677 size_t chunk = min_t(size_t, left, sizeof(unsigned long));677 size_t block_len = min_t(size_t, left, sizeof(unsigned long));
678 678
679 if (!arch_get_random_long(&v))679 if (!arch_get_random_long(&v))
680 break;680 break;
681 681
682 memcpy(p, &v, chunk);682 memcpy(p, &v, block_len);
683 p += chunk;683 p += block_len;
684 left -= chunk;684 left -= block_len;
685 }685 }
686 686
687 return nbytes - left;687 return len - left;
688}688}
689EXPORT_SYMBOL(get_random_bytes_arch);689EXPORT_SYMBOL(get_random_bytes_arch);
690 690
@@ -695,15 +695,15 @@ EXPORT_SYMBOL(get_random_bytes_arch); · 4 unmodified lines
695 *695 *
696 * Callers may add entropy via:696 * Callers may add entropy via:
697 *697 *
698 * static void mix_pool_bytes(const void *in, size_t nbytes)698 * static void mix_pool_bytes(const void *buf, size_t len)
699 *699 *
700 * After which, if added entropy should be credited:700 * After which, if added entropy should be credited:
701 *701 *
702 * static void credit_init_bits(size_t nbits)702 * static void credit_init_bits(size_t bits)
703 *703 *
704 * Finally, extract entropy via:704 * Finally, extract entropy via:
705 *705 *
706 * static void extract_entropy(void *buf, size_t nbytes)706 * static void extract_entropy(void *buf, size_t len)
707 *707 *
708 **********************************************************************/708 **********************************************************************/
709 709
@@ -725,9 +725,9 @@ static struct { · 15 unmodified lines
725 .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),725 .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
726};726};
727 727
728static void _mix_pool_bytes(const void *in, size_t nbytes)728static void _mix_pool_bytes(const void *buf, size_t len)
729{729{
730 blake2s_update(&input_pool.hash, in, nbytes);730 blake2s_update(&input_pool.hash, buf, len);
731}731}
732 732
733/*733/*
@@ -735,12 +735,12 @@ static void _mix_pool_bytes(const void *in, size_t nbytes) · 1 unmodified line
735 * update the initialization bit counter; the caller should call735 * update the initialization bit counter; the caller should call
736 * credit_init_bits if this is appropriate.736 * credit_init_bits if this is appropriate.
737 */737 */
738static void mix_pool_bytes(const void *in, size_t nbytes)738static void mix_pool_bytes(const void *buf, size_t len)
739{739{
740 unsigned long flags;740 unsigned long flags;
741 741
742 spin_lock_irqsave(&input_pool.lock, flags);742 spin_lock_irqsave(&input_pool.lock, flags);
743 _mix_pool_bytes(in, nbytes);743 _mix_pool_bytes(buf, len);
744 spin_unlock_irqrestore(&input_pool.lock, flags);744 spin_unlock_irqrestore(&input_pool.lock, flags);
745}745}
746 746
@@ -748,7 +748,7 @@ static void mix_pool_bytes(const void *in, size_t nbytes) · 1 unmodified line
748 * This is an HKDF-like construction for using the hashed collected entropy748 * This is an HKDF-like construction for using the hashed collected entropy
749 * as a PRF key, that's then expanded block-by-block.749 * as a PRF key, that's then expanded block-by-block.
750 */750 */
751static void extract_entropy(void *buf, size_t nbytes)751static void extract_entropy(void *buf, size_t len)
752{752{
753 unsigned long flags;753 unsigned long flags;
754 u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE];754 u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE];
@@ -777,12 +777,12 @@ static void extract_entropy(void *buf, size_t nbytes) · 22 unmodified lines
777 spin_unlock_irqrestore(&input_pool.lock, flags);777 spin_unlock_irqrestore(&input_pool.lock, flags);
778 memzero_explicit(next_key, sizeof(next_key));778 memzero_explicit(next_key, sizeof(next_key));
779 779
780 while (nbytes) {780 while (len) {
781 i = min_t(size_t, nbytes, BLAKE2S_HASH_SIZE);781 i = min_t(size_t, len, BLAKE2S_HASH_SIZE);
782 /* output = HASHPRF(seed, RDSEED || ++counter) */782 /* output = HASHPRF(seed, RDSEED || ++counter) */
783 ++block.counter;783 ++block.counter;
784 blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed));784 blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed));
785 nbytes -= i;785 len -= i;
786 buf += i;786 buf += i;
787 }787 }
788 788
@@ -790,16 +790,16 @@ static void extract_entropy(void *buf, size_t nbytes) · 1 unmodified line
790 memzero_explicit(&block, sizeof(block));790 memzero_explicit(&block, sizeof(block));
791}791}
792 792
793static void credit_init_bits(size_t nbits)793static void credit_init_bits(size_t bits)
794{794{
795 static struct execute_work set_ready;795 static struct execute_work set_ready;
796 unsigned int new, orig, add;796 unsigned int new, orig, add;
797 unsigned long flags;797 unsigned long flags;
798 798
799 if (crng_ready() || !nbits)799 if (crng_ready() || !bits)
800 return;800 return;
801 801
802 add = min_t(size_t, nbits, POOL_BITS);802 add = min_t(size_t, bits, POOL_BITS);
803 803
804 do {804 do {
805 orig = READ_ONCE(input_pool.init_bits);805 orig = READ_ONCE(input_pool.init_bits);
@@ -835,14 +835,12 @@ static void credit_init_bits(size_t nbits) · 29 unmodified lines
835 * The following exported functions are used for pushing entropy into835 * The following exported functions are used for pushing entropy into
836 * the above entropy accumulation routines:836 * the above entropy accumulation routines:
837 *837 *
838 * void add_device_randomness(const void *buf, size_t size);838 * void add_device_randomness(const void *buf, size_t len);
839 * void add_hwgenerator_randomness(const void *buffer, size_t count,839 * void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
840 * size_t entropy);840 * void add_bootloader_randomness(const void *buf, size_t len);
841 * void add_bootloader_randomness(const void *buf, size_t size);841 * void add_vmfork_randomness(const void *unique_vm_id, size_t len);
842 * void add_vmfork_randomness(const void *unique_vm_id, size_t size);
843 * void add_interrupt_randomness(int irq);842 * void add_interrupt_randomness(int irq);
844 * void add_input_randomness(unsigned int type, unsigned int code,843 * void add_input_randomness(unsigned int type, unsigned int code, unsigned int value);
845 * unsigned int value);
846 * void add_disk_randomness(struct gendisk *disk);844 * void add_disk_randomness(struct gendisk *disk);
847 *845 *
848 * add_device_randomness() adds data to the input pool that846 * add_device_randomness() adds data to the input pool that
@@ -937,7 +935,7 @@ int __init random_init(const char *command_line) · 88 unmodified lines
937{935{
938 ktime_t now = ktime_get_real();936 ktime_t now = ktime_get_real();
939 unsigned int i, arch_bytes;937 unsigned int i, arch_bytes;
940 unsigned long rv;938 unsigned long entropy;
941 939
942#if defined(LATENT_ENTROPY_PLUGIN)940#if defined(LATENT_ENTROPY_PLUGIN)
943 static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;941 static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;
@@ -945,13 +943,13 @@ int __init random_init(const char *command_line) · 1 unmodified line
945#endif943#endif
946 944
947 for (i = 0, arch_bytes = BLAKE2S_BLOCK_SIZE;945 for (i = 0, arch_bytes = BLAKE2S_BLOCK_SIZE;
948 i < BLAKE2S_BLOCK_SIZE; i += sizeof(rv)) {946 i < BLAKE2S_BLOCK_SIZE; i += sizeof(entropy)) {
949 if (!arch_get_random_seed_long_early(&rv) &&947 if (!arch_get_random_seed_long_early(&entropy) &&
950 !arch_get_random_long_early(&rv)) {948 !arch_get_random_long_early(&entropy)) {
951 rv = random_get_entropy();949 entropy = random_get_entropy();
952 arch_bytes -= sizeof(rv);950 arch_bytes -= sizeof(entropy);
953 }951 }
954 _mix_pool_bytes(&rv, sizeof(rv));952 _mix_pool_bytes(&entropy, sizeof(entropy));
955 }953 }
956 _mix_pool_bytes(&now, sizeof(now));954 _mix_pool_bytes(&now, sizeof(now));
957 _mix_pool_bytes(utsname(), sizeof(*(utsname())));955 _mix_pool_bytes(utsname(), sizeof(*(utsname())));
@@ -978,14 +976,14 @@ int __init random_init(const char *command_line) · 20 unmodified lines
978 * the entropy pool having similar initial state across largely976 * the entropy pool having similar initial state across largely
979 * identical devices.977 * identical devices.
980 */978 */
981void add_device_randomness(const void *buf, size_t size)979void add_device_randomness(const void *buf, size_t len)
982{980{
983 unsigned long entropy = random_get_entropy();981 unsigned long entropy = random_get_entropy();
984 unsigned long flags;982 unsigned long flags;
985 983
986 spin_lock_irqsave(&input_pool.lock, flags);984 spin_lock_irqsave(&input_pool.lock, flags);
987 _mix_pool_bytes(&entropy, sizeof(entropy));985 _mix_pool_bytes(&entropy, sizeof(entropy));
988 _mix_pool_bytes(buf, size);986 _mix_pool_bytes(buf, len);
989 spin_unlock_irqrestore(&input_pool.lock, flags);987 spin_unlock_irqrestore(&input_pool.lock, flags);
990}988}
991EXPORT_SYMBOL(add_device_randomness);989EXPORT_SYMBOL(add_device_randomness);
@@ -995,10 +993,9 @@ EXPORT_SYMBOL(add_device_randomness); · 3 unmodified lines
995 * Those devices may produce endless random bits and will be throttled993 * Those devices may produce endless random bits and will be throttled
996 * when our pool is full.994 * when our pool is full.
997 */995 */
998void add_hwgenerator_randomness(const void *buffer, size_t count,996void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy)
999 size_t entropy)
1000{997{
1001 mix_pool_bytes(buffer, count);998 mix_pool_bytes(buf, len);
1002 credit_init_bits(entropy);999 credit_init_bits(entropy);
1003 1000
1004 /*1001 /*
@@ -1014,11 +1011,11 @@ EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); · 9 unmodified lines
1014 * Handle random seed passed by bootloader, and credit it if1011 * Handle random seed passed by bootloader, and credit it if
1015 * CONFIG_RANDOM_TRUST_BOOTLOADER is set.1012 * CONFIG_RANDOM_TRUST_BOOTLOADER is set.
1016 */1013 */
1017void add_bootloader_randomness(const void *buf, size_t size)1014void add_bootloader_randomness(const void *buf, size_t len)
1018{1015{
1019 mix_pool_bytes(buf, size);1016 mix_pool_bytes(buf, len);
1020 if (trust_bootloader)1017 if (trust_bootloader)
1021 credit_init_bits(size * 8);1018 credit_init_bits(len * 8);
1022}1019}
1023EXPORT_SYMBOL_GPL(add_bootloader_randomness);1020EXPORT_SYMBOL_GPL(add_bootloader_randomness);
1024 1021
@@ -1030,9 +1027,9 @@ static BLOCKING_NOTIFIER_HEAD(vmfork_chain); · 5 unmodified lines
1030 * don't credit it, but we do immediately force a reseed after so1027 * don't credit it, but we do immediately force a reseed after so
1031 * that it's used by the crng posthaste.1028 * that it's used by the crng posthaste.
1032 */1029 */
1033void add_vmfork_randomness(const void *unique_vm_id, size_t size)1030void add_vmfork_randomness(const void *unique_vm_id, size_t len)
1034{1031{
1035 add_device_randomness(unique_vm_id, size);1032 add_device_randomness(unique_vm_id, len);
1036 if (crng_ready()) {1033 if (crng_ready()) {
1037 crng_reseed();1034 crng_reseed();
1038 pr_notice("crng reseeded due to virtual machine fork\n");1035 pr_notice("crng reseeded due to virtual machine fork\n");
@@ -1252,8 +1249,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned int nu · 213 unmodified lines
1252 credit_init_bits(bits);1249 credit_init_bits(bits);
1253}1250}
1254 1251
1255void add_input_randomness(unsigned int type, unsigned int code,1252void add_input_randomness(unsigned int type, unsigned int code, unsigned int value)
1256 unsigned int value)
1257{1253{
1258 static unsigned char last_value;1254 static unsigned char last_value;
1259 static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES };1255 static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES };
@@ -1388,8 +1384,7 @@ static void try_to_generate_entropy(void) · 128 unmodified lines
1388 *1384 *
1389 **********************************************************************/1385 **********************************************************************/
1390 1386
1391SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int,1387SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
1392 flags)
1393{1388{
1394 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))1389 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
1395 return -EINVAL;1390 return -EINVAL;
@@ -1401,8 +1396,8 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, · 5 unmodified lines
1401 if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))1396 if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
1402 return -EINVAL;1397 return -EINVAL;
1403 1398
1404 if (count > INT_MAX)1399 if (len > INT_MAX)
1405 count = INT_MAX;1400 len = INT_MAX;
1406 1401
1407 if (!crng_ready() && !(flags & GRND_INSECURE)) {1402 if (!crng_ready() && !(flags & GRND_INSECURE)) {
1408 int ret;1403 int ret;
@@ -1413,7 +1408,7 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, · 4 unmodified lines
1413 if (unlikely(ret))1408 if (unlikely(ret))
1414 return ret;1409 return ret;
1415 }1410 }
1416 return get_random_bytes_user(buf, count);1411 return get_random_bytes_user(ubuf, len);
1417}1412}
1418 1413
1419static __poll_t random_poll(struct file *file, poll_table *wait)1414static __poll_t random_poll(struct file *file, poll_table *wait)
@@ -1422,21 +1417,21 @@ static __poll_t random_poll(struct file *file, poll_table *wait) · 2 unmodified lines
1422 return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;1417 return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
1423}1418}
1424 1419
1425static int write_pool(const char __user *ubuf, size_t count)1420static int write_pool(const char __user *ubuf, size_t len)
1426{1421{
1427 size_t len;1422 size_t block_len;
1428 int ret = 0;1423 int ret = 0;
1429 u8 block[BLAKE2S_BLOCK_SIZE];1424 u8 block[BLAKE2S_BLOCK_SIZE];
1430 1425
1431 while (count) {1426 while (len) {
1432 len = min(count, sizeof(block));1427 block_len = min(len, sizeof(block));
1433 if (copy_from_user(block, ubuf, len)) {1428 if (copy_from_user(block, ubuf, block_len)) {
1434 ret = -EFAULT;1429 ret = -EFAULT;
1435 goto out;1430 goto out;
1436 }1431 }
1437 count -= len;1432 len -= block_len;
1438 ubuf += len;1433 ubuf += block_len;
1439 mix_pool_bytes(block, len);1434 mix_pool_bytes(block, block_len);
1440 cond_resched();1435 cond_resched();
1441 }1436 }
1442 1437
@@ -1445,20 +1440,20 @@ out: · 2 unmodified lines
1445 return ret;1440 return ret;
1446}1441}
1447 1442
1448static ssize_t random_write(struct file *file, const char __user *buffer,1443static ssize_t random_write(struct file *file, const char __user *ubuf,
1449 size_t count, loff_t *ppos)1444 size_t len, loff_t *ppos)
1450{1445{
1451 int ret;1446 int ret;
1452 1447
1453 ret = write_pool(buffer, count);1448 ret = write_pool(ubuf, len);
1454 if (ret)1449 if (ret)
1455 return ret;1450 return ret;
1456 1451
1457 return (ssize_t)count;1452 return (ssize_t)len;
1458}1453}
1459 1454
1460static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,1455static ssize_t urandom_read(struct file *file, char __user *ubuf,
1461 loff_t *ppos)1456 size_t len, loff_t *ppos)
1462{1457{
1463 static int maxwarn = 10;1458 static int maxwarn = 10;
1464 1459
@@ -1475,22 +1470,22 @@ static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, · 10 unmodified lines
1475 else if (ratelimit_disable || __ratelimit(&urandom_warning)) {1470 else if (ratelimit_disable || __ratelimit(&urandom_warning)) {
1476 --maxwarn;1471 --maxwarn;
1477 pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",1472 pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
1478 current->comm, nbytes);1473 current->comm, len);
1479 }1474 }
1480 }1475 }
1481 1476
1482 return get_random_bytes_user(buf, nbytes);1477 return get_random_bytes_user(ubuf, len);
1483}1478}
1484 1479
1485static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,1480static ssize_t random_read(struct file *file, char __user *ubuf,
1486 loff_t *ppos)1481 size_t len, loff_t *ppos)
1487{1482{
1488 int ret;1483 int ret;
1489 1484
1490 ret = wait_for_random_bytes();1485 ret = wait_for_random_bytes();
1491 if (ret != 0)1486 if (ret != 0)
1492 return ret;1487 return ret;
1493 return get_random_bytes_user(buf, nbytes);1488 return get_random_bytes_user(ubuf, len);
1494}1489}
1495 1490
1496static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)1491static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
@@ -1615,7 +1610,7 @@ static u8 sysctl_bootid[UUID_SIZE]; · 118 unmodified lines
1615 * UUID. The difference is in whether table->data is NULL; if it is,1610 * UUID. The difference is in whether table->data is NULL; if it is,
1616 * then a new UUID is generated and returned to the user.1611 * then a new UUID is generated and returned to the user.
1617 */1612 */
1618static int proc_do_uuid(struct ctl_table *table, int write, void *buffer,1613static int proc_do_uuid(struct ctl_table *table, int write, void *buf,
1619 size_t *lenp, loff_t *ppos)1614 size_t *lenp, loff_t *ppos)
1620{1615{
1621 u8 tmp_uuid[UUID_SIZE], *uuid;1616 u8 tmp_uuid[UUID_SIZE], *uuid;
@@ -1642,14 +1637,14 @@ static int proc_do_uuid(struct ctl_table *table, int write, void *buffer, · 20 unmodified lines
1642 }1637 }
1643 1638
1644 snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid);1639 snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid);
1645 return proc_dostring(&fake_table, 0, buffer, lenp, ppos);1640 return proc_dostring(&fake_table, 0, buf, lenp, ppos);
1646}1641}
1647 1642
1648/* The same as proc_dointvec, but writes don't change anything. */1643/* The same as proc_dointvec, but writes don't change anything. */
1649static int proc_do_rointvec(struct ctl_table *table, int write, void *buffer,1644static int proc_do_rointvec(struct ctl_table *table, int write, void *buf,
1650 size_t *lenp, loff_t *ppos)1645 size_t *lenp, loff_t *ppos)
1651{1646{
1652 return write ? 0 : proc_dointvec(table, 0, buffer, lenp, ppos);1647 return write ? 0 : proc_dointvec(table, 0, buf, lenp, ppos);
1653}1648}
1654 1649
1655static struct ctl_table random_table[] = {1650static struct ctl_table random_table[] = {
include/linux/random.h+6−6
@@ -12,12 +12,12 @@ · 11 unmodified lines
12 12
13struct notifier_block;13struct notifier_block;
14 14
15void add_device_randomness(const void *, size_t);15void add_device_randomness(const void *buf, size_t len);
16void add_bootloader_randomness(const void *, size_t);16void add_bootloader_randomness(const void *buf, size_t len);
17void add_input_randomness(unsigned int type, unsigned int code,17void add_input_randomness(unsigned int type, unsigned int code,
18 unsigned int value) __latent_entropy;18 unsigned int value) __latent_entropy;
19void add_interrupt_randomness(int irq) __latent_entropy;19void add_interrupt_randomness(int irq) __latent_entropy;
20void add_hwgenerator_randomness(const void *buffer, size_t count, size_t entropy);20void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
21 21
22#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)22#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
23static inline void add_latent_entropy(void)23static inline void add_latent_entropy(void)
@@ -29,7 +29,7 @@ static inline void add_latent_entropy(void) { } · 5 unmodified lines
29#endif29#endif
30 30
31#if IS_ENABLED(CONFIG_VMGENID)31#if IS_ENABLED(CONFIG_VMGENID)
32void add_vmfork_randomness(const void *unique_vm_id, size_t size);32void add_vmfork_randomness(const void *unique_vm_id, size_t len);
33int register_random_vmfork_notifier(struct notifier_block *nb);33int register_random_vmfork_notifier(struct notifier_block *nb);
34int unregister_random_vmfork_notifier(struct notifier_block *nb);34int unregister_random_vmfork_notifier(struct notifier_block *nb);
35#else35#else
@@ -37,8 +37,8 @@ static inline int register_random_vmfork_notifier(struct notifier_block *nb) { r · 1 unmodified line
37static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }37static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
38#endif38#endif
39 39
40void get_random_bytes(void *buf, size_t nbytes);40void get_random_bytes(void *buf, size_t len);
41size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes);41size_t __must_check get_random_bytes_arch(void *buf, size_t len);
42u32 get_random_u32(void);42u32 get_random_u32(void);
43u64 get_random_u64(void);43u64 get_random_u64(void);
44static inline unsigned int get_random_int(void)44static inline unsigned int get_random_int(void)