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 <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Yury Norov <[email protected]> Reviewed-by: KP Singh <[email protected]> Reviewed-by: Jan Kara <[email protected]> # for ext4 and sbitmap Reviewed-by: Christoph Böhmwalder <[email protected]> # for drbd Acked-by: Jakub Kicinski <[email protected]> Acked-by: Heiko Carstens <[email protected]> # for s390 Acked-by: Ulf Hansson <[email protected]> # for mmc Acked-by: Darrick J. Wong <[email protected]> # for xfs Signed-off-by: Jason A. Donenfeld <[email protected]>
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;

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/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)

0 commit comments

Comments
 (0)