]> git.itanic.dy.fi Git - linux-stable/commitdiff
treewide: use get_random_{u8,u16}() when possible, part 2
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 5 Oct 2022 15:23:53 +0000 (17:23 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 11 Oct 2022 23:42:58 +0000 (17:42 -0600)
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 <gregkh@linuxfoundation.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Acked-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
arch/s390/kernel/process.c
drivers/mtd/nand/raw/nandsim.c
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pno.c
lib/test_vmalloc.c
net/rds/bind.c
net/sched/sch_sfb.c

index 5ec78555dd2e5c70e7683e76729a2b5ab8bc995f..42af4b3aa02b85d0d117f7931cd8eb28f0784004 100644 (file)
@@ -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)
index 50bcf745e81645aa03efa6f4310101228d59073f..d211939c8bdd518ad1808d6708cffb97c614485d 100644 (file)
@@ -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;
index d0a7465be586dc899bfa581163825c836fd10d50..170c61c8136cc1c0b97afe42d7adfff878e3e926 100644 (file)
@@ -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;
index a26bbbf20e62d9721f5a7051192a28494575733c..cf7780572f5b4a4bfd3b2deea5c44807e930d9ea 100644 (file)
@@ -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.
index 5b5fb4ca8d3e5523336aee389acd7ad160b28d2f..97a29172a8eec44b370ae19004f2c99610f75971 100644 (file)
@@ -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;
        }
 
index e2389fa3cff8ac5ead731335e06fa9acff8b8ef4..0366a1a029a9ea8f4d08095fdf015c7594f379e1 100644 (file)
@@ -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)) {