Skip to content

Commit 7e3cf08

Browse files
committed
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>
1 parent 8b3ccbc commit 7e3cf08

File tree

21 files changed

+34
-34
lines changed

21 files changed

+34
-34
lines changed

arch/arm/kernel/signal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ struct page *get_signal_page(void)
655655
PAGE_SIZE / sizeof(u32));
656656

657657
/* Give the signal return code some randomness */
658-
offset = 0x200 + (get_random_int() & 0x7fc);
658+
offset = 0x200 + (get_random_u16() & 0x7fc);
659659
signal_return_offset = offset;
660660

661661
/* Copy signal return handlers into the page */

arch/arm64/kernel/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
6767
*
6868
* The resulting 5 bits of entropy is seen in SP[8:4].
6969
*/
70-
choose_random_kstack_offset(get_random_int() & 0x1FF);
70+
choose_random_kstack_offset(get_random_u16() & 0x1FF);
7171
}
7272

7373
static inline bool has_syscall_work(unsigned long flags)

crypto/testmgr.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,24 +927,24 @@ static void generate_random_bytes(u8 *buf, size_t count)
927927
b = 0xff;
928928
break;
929929
default:
930-
b = (u8)prandom_u32();
930+
b = get_random_u8();
931931
break;
932932
}
933933
memset(buf, b, count);
934934
mutate_buffer(buf, count);
935935
break;
936936
case 2:
937937
/* Ascending or descending bytes, plus optional mutations */
938-
increment = (u8)prandom_u32();
939-
b = (u8)prandom_u32();
938+
increment = get_random_u8();
939+
b = get_random_u8();
940940
for (i = 0; i < count; i++, b += increment)
941941
buf[i] = b;
942942
mutate_buffer(buf, count);
943943
break;
944944
default:
945945
/* Fully random bytes */
946946
for (i = 0; i < count; i++)
947-
buf[i] = (u8)prandom_u32();
947+
buf[i] = get_random_u8();
948948
}
949949
}
950950

drivers/media/common/v4l2-tpg/v4l2-tpg-core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ static void precalculate_color(struct tpg_data *tpg, int k)
870870
g = tpg_colors[col].g;
871871
b = tpg_colors[col].b;
872872
} else if (tpg->pattern == TPG_PAT_NOISE) {
873-
r = g = b = prandom_u32_max(256);
873+
r = g = b = get_random_u8();
874874
} else if (k == TPG_COLOR_RANDOM) {
875875
r = g = b = tpg->qual_offset + prandom_u32_max(196);
876876
} else if (k >= TPG_COLOR_RAMP) {

drivers/media/test-drivers/vivid/vivid-radio-rx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ ssize_t vivid_radio_rx_read(struct file *file, char __user *buf,
104104
break;
105105
case 2:
106106
rds.block |= V4L2_RDS_BLOCK_ERROR;
107-
rds.lsb = prandom_u32_max(256);
108-
rds.msb = prandom_u32_max(256);
107+
rds.lsb = get_random_u8();
108+
rds.msb = get_random_u8();
109109
break;
110110
case 3: /* Skip block altogether */
111111
if (i)

drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ static void make_established(struct sock *sk, u32 snd_isn, unsigned int opt)
14661466
tp->write_seq = snd_isn;
14671467
tp->snd_nxt = snd_isn;
14681468
tp->snd_una = snd_isn;
1469-
inet_sk(sk)->inet_id = prandom_u32();
1469+
inet_sk(sk)->inet_id = get_random_u16();
14701470
assign_rxopt(sk, opt);
14711471

14721472
if (tp->rcv_wnd > (RCV_BUFSIZ_M << 10))

drivers/net/hamradio/baycom_epp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
438438
if ((--bc->hdlctx.slotcnt) > 0)
439439
return 0;
440440
bc->hdlctx.slotcnt = bc->ch_params.slottime;
441-
if (prandom_u32_max(256) > bc->ch_params.ppersist)
441+
if (get_random_u8() > bc->ch_params.ppersist)
442442
return 0;
443443
}
444444
}

drivers/net/hamradio/hdlcdrv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void hdlcdrv_arbitrate(struct net_device *dev, struct hdlcdrv_state *s)
377377
if ((--s->hdlctx.slotcnt) > 0)
378378
return;
379379
s->hdlctx.slotcnt = s->ch_params.slottime;
380-
if (prandom_u32_max(256) > s->ch_params.ppersist)
380+
if (get_random_u8() > s->ch_params.ppersist)
381381
return;
382382
start_tx(dev, s);
383383
}

drivers/net/hamradio/yam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static void yam_arbitrate(struct net_device *dev)
626626
yp->slotcnt = yp->slot / 10;
627627

628628
/* is random > persist ? */
629-
if (prandom_u32_max(256) > yp->pers)
629+
if (get_random_u8() > yp->pers)
630630
return;
631631

632632
yam_start_tx(dev, yp);

drivers/net/wireguard/selftest/allowedips.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static __init bool randomized_test(void)
310310
for (k = 0; k < 4; ++k)
311311
mutated[k] = (mutated[k] & mutate_mask[k]) |
312312
(~mutate_mask[k] &
313-
prandom_u32_max(256));
313+
get_random_u8());
314314
cidr = prandom_u32_max(32) + 1;
315315
peer = peers[prandom_u32_max(NUM_PEERS)];
316316
if (wg_allowedips_insert_v4(&t,
@@ -354,7 +354,7 @@ static __init bool randomized_test(void)
354354
for (k = 0; k < 4; ++k)
355355
mutated[k] = (mutated[k] & mutate_mask[k]) |
356356
(~mutate_mask[k] &
357-
prandom_u32_max(256));
357+
get_random_u8());
358358
cidr = prandom_u32_max(128) + 1;
359359
peer = peers[prandom_u32_max(NUM_PEERS)];
360360
if (wg_allowedips_insert_v6(&t,

0 commit comments

Comments
 (0)