Skip to content

Commit

Permalink
treewide: use get_random_{u8,u16}() when possible, part 2
Browse files Browse the repository at this point in the history
Rather than truncate a 32-bit value to a 16-bit value or an 8-bit value,
simply use the get_random_{u8,u16}() functions, which are faster than
wasting the additional bytes from a 32-bit value. This was done by hand,
identifying all of the places where one of the random integer functions
was used in a non-32-bit context.

Reviewed-by: Greg Kroah-Hartman <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Reviewed-by: Yury Norov <[email protected]>
Acked-by: Jakub Kicinski <[email protected]>
Acked-by: Heiko Carstens <[email protected]> # for s390
Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed Oct 11, 2022
1 parent 7e3cf08 commit f743f16
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion arch/s390/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ unsigned long arch_align_stack(unsigned long sp)

static inline unsigned long brk_rnd(void)
{
return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
}

unsigned long arch_randomize_brk(struct mm_struct *mm)
Expand Down
2 changes: 1 addition & 1 deletion drivers/mtd/nand/raw/nandsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ static int ns_do_read_error(struct nandsim *ns, int num)

static void ns_do_bit_flips(struct nandsim *ns, int num)
{
if (bitflips && prandom_u32() < (1 << 22)) {
if (bitflips && get_random_u16() < (1 << 6)) {
int flips = 1;
if (bitflips > 1)
flips = prandom_u32_max(bitflips) + 1;
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/wireless/broadcom/brcm80211/brcmfmac/pno.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static int brcmf_pno_set_random(struct brcmf_if *ifp, struct brcmf_pno_info *pi)
memcpy(pfn_mac.mac, mac_addr, ETH_ALEN);
for (i = 0; i < ETH_ALEN; i++) {
pfn_mac.mac[i] &= mac_mask[i];
pfn_mac.mac[i] |= get_random_int() & ~(mac_mask[i]);
pfn_mac.mac[i] |= get_random_u8() & ~(mac_mask[i]);
}
/* Clear multi bit */
pfn_mac.mac[0] &= 0xFE;
Expand Down
2 changes: 1 addition & 1 deletion lib/test_vmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static int random_size_align_alloc_test(void)
int i;

for (i = 0; i < test_loop_count; i++) {
rnd = prandom_u32();
rnd = get_random_u8();

/*
* Maximum 1024 pages, if PAGE_SIZE is 4096.
Expand Down
2 changes: 1 addition & 1 deletion net/rds/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int rds_add_bound(struct rds_sock *rs, const struct in6_addr *addr,
return -EINVAL;
last = rover;
} else {
rover = max_t(u16, prandom_u32(), 2);
rover = max_t(u16, get_random_u16(), 2);
last = rover - 1;
}

Expand Down
2 changes: 1 addition & 1 deletion net/sched/sch_sfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ static int sfb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
goto enqueue;
}

r = prandom_u32() & SFB_MAX_PROB;
r = get_random_u16() & SFB_MAX_PROB;

if (unlikely(r < p_min)) {
if (unlikely(p_min > SFB_MAX_PROB / 2)) {
Expand Down

0 comments on commit f743f16

Please sign in to comment.