Skip to content

Commit 81895a6

Browse files
committed
treewide: use prandom_u32_max() when possible, part 1
Rather than incurring a division or requesting too many random bytes for the given range, use the prandom_u32_max() function, which only takes the minimum required bytes from the RNG and avoids divisions. This was done mechanically with this coccinelle script: @basic@ expression E; type T; identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u64; @@ ( - ((T)get_random_u32() % (E)) + prandom_u32_max(E) | - ((T)get_random_u32() & ((E) - 1)) + prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2) | - ((u64)(E) * get_random_u32() >> 32) + prandom_u32_max(E) | - ((T)get_random_u32() & ~PAGE_MASK) + prandom_u32_max(PAGE_SIZE) ) @multi_line@ identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; identifier RAND; expression E; @@ - RAND = get_random_u32(); ... when != RAND - RAND %= (E); + RAND = prandom_u32_max(E); // Find a potential literal @literal_mask@ expression LITERAL; type T; identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; position p; @@ ((T)get_random_u32()@p & (LITERAL)) // Add one to the literal. @script:python add_one@ literal << literal_mask.LITERAL; RESULT; @@ value = None if literal.startswith('0x'): value = int(literal, 16) elif literal[0] in '123456789': value = int(literal, 10) if value is None: print("I don't know how to handle %s" % (literal)) cocci.include_match(False) elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 2**16 - 1 or value == 2**8 - 1: print("Skipping 0x%x for cleanup elsewhere" % (value)) cocci.include_match(False) elif value & (value + 1) != 0: print("Skipping 0x%x because it's not a power of two minus one" % (value)) cocci.include_match(False) elif literal.startswith('0x'): coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1)) else: coccinelle.RESULT = cocci.make_expr("%d" % (value + 1)) // Replace the literal mask with the calculated result. @plus_one@ expression literal_mask.LITERAL; position literal_mask.p; expression add_one.RESULT; identifier FUNC; @@ - (FUNC()@p & (LITERAL)) + prandom_u32_max(RESULT) @collapse_ret@ type T; identifier VAR; expression E; @@ { - T VAR; - VAR = (E); - return VAR; + return E; } @drop_var@ type T; identifier VAR; @@ { - T VAR; ... when != VAR } Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Kees Cook <keescook@chromium.org> Reviewed-by: Yury Norov <yury.norov@gmail.com> Reviewed-by: KP Singh <kpsingh@kernel.org> Reviewed-by: Jan Kara <jack@suse.cz> # for ext4 and sbitmap Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> # for drbd Acked-by: Jakub Kicinski <kuba@kernel.org> Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390 Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # for mmc Acked-by: Darrick J. Wong <djwong@kernel.org> # for xfs Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent d465bff commit 81895a6

File tree

89 files changed

+204
-218
lines changed

Some content is hidden

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

89 files changed

+204
-218
lines changed

arch/arm/kernel/process.c

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

376376
slots = ((last - first) >> PAGE_SHIFT) + 1;
377377

378-
offset = get_random_int() % slots;
378+
offset = prandom_u32_max(slots);
379379

380380
addr = first + (offset << PAGE_SHIFT);
381381

arch/arm64/kernel/process.c

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

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/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/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,6 @@ void notrace __ppc64_runlatch_off(void)
23082308
unsigned long arch_align_stack(unsigned long sp)
23092309
{
23102310
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2311-
sp -= get_random_int() & ~PAGE_MASK;
2311+
sp -= prandom_u32_max(PAGE_SIZE);
23122312
return sp & ~0xf;
23132313
}

arch/s390/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ 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

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;

0 commit comments

Comments
 (0)