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)

0 commit comments

Comments
 (0)