Skip to content

Commit f1947d7

Browse files
committed
Merge tag 'random-6.1-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random
Pull more random number generator updates from Jason Donenfeld: "This time with some large scale treewide cleanups. The intent of this pull is to clean up the way callers fetch random integers. The current rules for doing this right are: - If you want a secure or an insecure random u64, use get_random_u64() - If you want a secure or an insecure random u32, use get_random_u32() The old function prandom_u32() has been deprecated for a while now and is just a wrapper around get_random_u32(). Same for get_random_int(). - If you want a secure or an insecure random u16, use get_random_u16() - If you want a secure or an insecure random u8, use get_random_u8() - If you want secure or insecure random bytes, use get_random_bytes(). The old function prandom_bytes() has been deprecated for a while now and has long been a wrapper around get_random_bytes() - If you want a non-uniform random u32, u16, or u8 bounded by a certain open interval maximum, use prandom_u32_max() I say "non-uniform", because it doesn't do any rejection sampling or divisions. Hence, it stays within the prandom_*() namespace, not the get_random_*() namespace. I'm currently investigating a "uniform" function for 6.2. We'll see what comes of that. By applying these rules uniformly, we get several benefits: - By using prandom_u32_max() with an upper-bound that the compiler can prove at compile-time is ≤65536 or ≤256, internally get_random_u16() or get_random_u8() is used, which wastes fewer batched random bytes, and hence has higher throughput. - By using prandom_u32_max() instead of %, when the upper-bound is not a constant, division is still avoided, because prandom_u32_max() uses a faster multiplication-based trick instead. - By using get_random_u16() or get_random_u8() in cases where the return value is intended to indeed be a u16 or a u8, we waste fewer batched random bytes, and hence have higher throughput. This series was originally done by hand while I was on an airplane without Internet. Later, Kees and I worked on retroactively figuring out what could be done with Coccinelle and what had to be done manually, and then we split things up based on that. So while this touches a lot of files, the actual amount of code that's hand fiddled is comfortably small" * tag 'random-6.1-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random: prandom: remove unused functions treewide: use get_random_bytes() when possible treewide: use get_random_u32() when possible treewide: use get_random_{u8,u16}() when possible, part 2 treewide: use get_random_{u8,u16}() when possible, part 1 treewide: use prandom_u32_max() when possible, part 2 treewide: use prandom_u32_max() when possible, part 1
2 parents 8636df9 + de492c8 commit f1947d7

File tree

185 files changed

+378
-421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+378
-421
lines changed

Documentation/networking/filter.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Possible BPF extensions are shown in the following table:
305305
vlan_tci skb_vlan_tag_get(skb)
306306
vlan_avail skb_vlan_tag_present(skb)
307307
vlan_tpid skb->vlan_proto
308-
rand prandom_u32()
308+
rand get_random_u32()
309309
=================================== =================================================
310310

311311
These extensions can also be prefixed with '#'.

arch/arm/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static unsigned long sigpage_addr(const struct mm_struct *mm,
371371

372372
slots = ((last - first) >> PAGE_SHIFT) + 1;
373373

374-
offset = get_random_int() % slots;
374+
offset = prandom_u32_max(slots);
375375

376376
addr = first + (offset << PAGE_SHIFT);
377377

arch/arm/kernel/signal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ struct page *get_signal_page(void)
655655
PAGE_SIZE / sizeof(u32));
656656

657657
/* Give the signal return code some randomness */
658-
offset = 0x200 + (get_random_int() & 0x7fc);
658+
offset = 0x200 + (get_random_u16() & 0x7fc);
659659
signal_return_offset = offset;
660660

661661
/* Copy signal return handlers into the page */

arch/arm64/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ unsigned long __get_wchan(struct task_struct *p)
591591
unsigned long arch_align_stack(unsigned long sp)
592592
{
593593
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
594-
sp -= get_random_int() & ~PAGE_MASK;
594+
sp -= prandom_u32_max(PAGE_SIZE);
595595
return sp & ~0xf;
596596
}
597597

arch/arm64/kernel/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
6767
*
6868
* The resulting 5 bits of entropy is seen in SP[8:4].
6969
*/
70-
choose_random_kstack_offset(get_random_int() & 0x1FF);
70+
choose_random_kstack_offset(get_random_u16() & 0x1FF);
7171
}
7272

7373
static inline bool has_syscall_work(unsigned long flags)

arch/loongarch/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ unsigned long stack_top(void)
293293
unsigned long arch_align_stack(unsigned long sp)
294294
{
295295
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
296-
sp -= get_random_int() & ~PAGE_MASK;
296+
sp -= prandom_u32_max(PAGE_SIZE);
297297

298298
return sp & STACK_ALIGN;
299299
}

arch/loongarch/kernel/vdso.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static unsigned long vdso_base(void)
7878
unsigned long base = STACK_TOP;
7979

8080
if (current->flags & PF_RANDOMIZE) {
81-
base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
81+
base += prandom_u32_max(VDSO_RANDOMIZE_SIZE);
8282
base = PAGE_ALIGN(base);
8383
}
8484

arch/mips/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ unsigned long mips_stack_top(void)
711711
unsigned long arch_align_stack(unsigned long sp)
712712
{
713713
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
714-
sp -= get_random_int() & ~PAGE_MASK;
714+
sp -= prandom_u32_max(PAGE_SIZE);
715715

716716
return sp & ALMASK;
717717
}

arch/mips/kernel/vdso.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static unsigned long vdso_base(void)
7979
}
8080

8181
if (current->flags & PF_RANDOMIZE) {
82-
base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
82+
base += prandom_u32_max(VDSO_RANDOMIZE_SIZE);
8383
base = PAGE_ALIGN(base);
8484
}
8585

arch/parisc/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ __get_wchan(struct task_struct *p)
284284

285285
static inline unsigned long brk_rnd(void)
286286
{
287-
return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
287+
return (get_random_u32() & BRK_RND_MASK) << PAGE_SHIFT;
288288
}
289289

290290
unsigned long arch_randomize_brk(struct mm_struct *mm)

arch/parisc/kernel/sys_parisc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ static unsigned long mmap_rnd(void)
239239
unsigned long rnd = 0;
240240

241241
if (current->flags & PF_RANDOMIZE)
242-
rnd = get_random_int() & MMAP_RND_MASK;
242+
rnd = get_random_u32() & MMAP_RND_MASK;
243243

244244
return rnd << PAGE_SHIFT;
245245
}
246246

247247
unsigned long arch_mmap_rnd(void)
248248
{
249-
return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
249+
return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT;
250250
}
251251

252252
static unsigned long mmap_legacy_base(void)

arch/parisc/kernel/vdso.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
7575

7676
map_base = mm->mmap_base;
7777
if (current->flags & PF_RANDOMIZE)
78-
map_base -= (get_random_int() & 0x1f) * PAGE_SIZE;
78+
map_base -= prandom_u32_max(0x20) * PAGE_SIZE;
7979

8080
vdso_text_start = get_unmapped_area(NULL, map_base, vdso_text_len, 0, 0);
8181

arch/powerpc/crypto/crc-vpmsum_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static int __init crc_test_init(void)
8282

8383
if (len <= offset)
8484
continue;
85-
prandom_bytes(data, len);
85+
get_random_bytes(data, len);
8686
len -= offset;
8787

8888
crypto_shash_update(crct10dif_shash, data+offset, len);

arch/powerpc/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,6 @@ void notrace __ppc64_runlatch_off(void)
23032303
unsigned long arch_align_stack(unsigned long sp)
23042304
{
23052305
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2306-
sp -= get_random_int() & ~PAGE_MASK;
2306+
sp -= prandom_u32_max(PAGE_SIZE);
23072307
return sp & ~0xf;
23082308
}

arch/s390/kernel/process.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ unsigned long __get_wchan(struct task_struct *p)
224224
unsigned long arch_align_stack(unsigned long sp)
225225
{
226226
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
227-
sp -= get_random_int() & ~PAGE_MASK;
227+
sp -= prandom_u32_max(PAGE_SIZE);
228228
return sp & ~0xf;
229229
}
230230

231231
static inline unsigned long brk_rnd(void)
232232
{
233-
return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
233+
return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
234234
}
235235

236236
unsigned long arch_randomize_brk(struct mm_struct *mm)

arch/s390/kernel/vdso.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned long len)
227227
end -= len;
228228

229229
if (end > start) {
230-
offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
230+
offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
231231
addr = start + (offset << PAGE_SHIFT);
232232
} else {
233233
addr = start;

arch/s390/mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static inline int mmap_is_legacy(struct rlimit *rlim_stack)
3737

3838
unsigned long arch_mmap_rnd(void)
3939
{
40-
return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
40+
return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT;
4141
}
4242

4343
static unsigned long mmap_base_legacy(unsigned long rnd)

arch/sparc/vdso/vma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned int len)
354354
unsigned int offset;
355355

356356
/* This loses some more bits than a modulo, but is cheaper */
357-
offset = get_random_int() & (PTRS_PER_PTE - 1);
357+
offset = prandom_u32_max(PTRS_PER_PTE);
358358
return start + (offset << PAGE_SHIFT);
359359
}
360360

arch/um/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ int singlestepping(void * t)
356356
unsigned long arch_align_stack(unsigned long sp)
357357
{
358358
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
359-
sp -= get_random_int() % 8192;
359+
sp -= prandom_u32_max(8192);
360360
return sp & ~0xf;
361361
}
362362
#endif

arch/x86/entry/vdso/vma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned len)
327327
end -= len;
328328

329329
if (end > start) {
330-
offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
330+
offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
331331
addr = start + (offset << PAGE_SHIFT);
332332
} else {
333333
addr = start;

arch/x86/kernel/cpu/amd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)
503503
va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
504504

505505
/* A random value per boot for bit slice [12:upper_bit) */
506-
va_align.bits = get_random_int() & va_align.mask;
506+
va_align.bits = get_random_u32() & va_align.mask;
507507
}
508508

509509
if (cpu_has(c, X86_FEATURE_MWAITX))

arch/x86/kernel/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static unsigned long int get_module_load_offset(void)
5353
*/
5454
if (module_load_offset == 0)
5555
module_load_offset =
56-
(get_random_int() % 1024 + 1) * PAGE_SIZE;
56+
(prandom_u32_max(1024) + 1) * PAGE_SIZE;
5757
mutex_unlock(&module_kaslr_mutex);
5858
}
5959
return module_load_offset;

arch/x86/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ early_param("idle", idle_setup);
965965
unsigned long arch_align_stack(unsigned long sp)
966966
{
967967
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
968-
sp -= get_random_int() % 8192;
968+
sp -= prandom_u32_max(8192);
969969
return sp & ~0xf;
970970
}
971971

arch/x86/mm/pat/cpa-test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ static int pageattr_test(void)
136136
failed += print_split(&sa);
137137

138138
for (i = 0; i < NTEST; i++) {
139-
unsigned long pfn = prandom_u32() % max_pfn_mapped;
139+
unsigned long pfn = prandom_u32_max(max_pfn_mapped);
140140

141141
addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
142-
len[i] = prandom_u32() % NPAGES;
142+
len[i] = prandom_u32_max(NPAGES);
143143
len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
144144

145145
if (len[i] == 0)

block/blk-crypto-fallback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ static int blk_crypto_fallback_init(void)
539539
if (blk_crypto_fallback_inited)
540540
return 0;
541541

542-
prandom_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
542+
get_random_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
543543

544544
err = bioset_init(&crypto_bio_split, 64, 0, 0);
545545
if (err)

crypto/async_tx/raid6test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void makedata(int disks)
3737
int i;
3838

3939
for (i = 0; i < disks; i++) {
40-
prandom_bytes(page_address(data[i]), PAGE_SIZE);
40+
get_random_bytes(page_address(data[i]), PAGE_SIZE);
4141
dataptrs[i] = data[i];
4242
dataoffs[i] = 0;
4343
}

0 commit comments

Comments
 (0)