-
- Downloads
treewide: use get_random_{u8,u16}() when possible, part 1
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 mechanically with this coccinelle script: @@ expression E; identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16; typedef __be16; typedef __le16; typedef u8; @@ ( - (get_random_u32() & 0xffff) + get_random_u16() | - (get_random_u32() & 0xff) + get_random_u8() | - (get_random_u32() % 65536) + get_random_u16() | - (get_random_u32() % 256) + get_random_u8() | - (get_random_u32() >> 16) + get_random_u16() | - (get_random_u32() >> 24) + get_random_u8() | - (u16)get_random_u32() + get_random_u16() | - (u8)get_random_u32() + get_random_u8() | - (__be16)get_random_u32() + (__be16)get_random_u16() | - (__le16)get_random_u32() + (__le16)get_random_u16() | - prandom_u32_max(65536) + get_random_u16() | - prandom_u32_max(256) + get_random_u8() | - E->inet_id = get_random_u32() + E->inet_id = get_random_u16() ) @@ identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16; identifier v; @@ - u16 v = get_random_u32(); + u16 v = get_random_u16(); @@ identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u8; identifier v; @@ - u8 v = get_random_u32(); + u8 v = get_random_u8(); @@ identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16; u16 v; @@ - v = get_random_u32(); + v = get_random_u16(); @@ identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u8; u8 v; @@ - v = get_random_u32(); + v = get_random_u8(); // 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)) // Examine limits @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 < 256: coccinelle.RESULT = cocci.make_ident("get_random_u8") elif value < 65536: coccinelle.RESULT = cocci.make_ident("get_random_u16") else: print("Skipping large mask of %s" % (literal)) cocci.include_match(False) // Replace the literal mask with the calculated result. @plus_one@ expression literal_mask.LITERAL; position literal_mask.p; identifier add_one.RESULT; identifier FUNC; @@ - (FUNC()@p & (LITERAL)) + (RESULT() & LITERAL) 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: Toke Høiland-Jørgensen <toke@toke.dk> # for sch_cake Signed-off-by:
Jason A. Donenfeld <Jason@zx2c4.com>
Showing
- arch/arm/kernel/signal.c 1 addition, 1 deletionarch/arm/kernel/signal.c
- arch/arm64/kernel/syscall.c 1 addition, 1 deletionarch/arm64/kernel/syscall.c
- crypto/testmgr.c 4 additions, 4 deletionscrypto/testmgr.c
- drivers/media/common/v4l2-tpg/v4l2-tpg-core.c 1 addition, 1 deletiondrivers/media/common/v4l2-tpg/v4l2-tpg-core.c
- drivers/media/test-drivers/vivid/vivid-radio-rx.c 2 additions, 2 deletionsdrivers/media/test-drivers/vivid/vivid-radio-rx.c
- drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c 1 addition, 1 deletiondrivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c
- drivers/net/hamradio/baycom_epp.c 1 addition, 1 deletiondrivers/net/hamradio/baycom_epp.c
- drivers/net/hamradio/hdlcdrv.c 1 addition, 1 deletiondrivers/net/hamradio/hdlcdrv.c
- drivers/net/hamradio/yam.c 1 addition, 1 deletiondrivers/net/hamradio/yam.c
- drivers/net/wireguard/selftest/allowedips.c 2 additions, 2 deletionsdrivers/net/wireguard/selftest/allowedips.c
- drivers/net/wireless/st/cw1200/wsm.c 1 addition, 1 deletiondrivers/net/wireless/st/cw1200/wsm.c
- drivers/scsi/lpfc/lpfc_hbadisc.c 3 additions, 3 deletionsdrivers/scsi/lpfc/lpfc_hbadisc.c
- lib/cmdline_kunit.c 2 additions, 2 deletionslib/cmdline_kunit.c
- net/dccp/ipv4.c 2 additions, 2 deletionsnet/dccp/ipv4.c
- net/ipv4/datagram.c 1 addition, 1 deletionnet/ipv4/datagram.c
- net/ipv4/ip_output.c 1 addition, 1 deletionnet/ipv4/ip_output.c
- net/ipv4/tcp_ipv4.c 2 additions, 2 deletionsnet/ipv4/tcp_ipv4.c
- net/mac80211/scan.c 1 addition, 1 deletionnet/mac80211/scan.c
- net/netfilter/nf_nat_core.c 2 additions, 2 deletionsnet/netfilter/nf_nat_core.c
- net/sched/sch_cake.c 3 additions, 3 deletionsnet/sched/sch_cake.c
Loading
Please register or sign in to comment