From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kees Cook Subject: Re: [PATCH v1 2/5] treewide: use get_random_{u8, u16}() when possible Date: Wed, 5 Oct 2022 21:38:02 -0700 Message-ID: <202210052126.B34A2C62@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-3-Jason@zx2c4.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 84AB161008 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 836AE60FFA DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 0E34B83385 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org B674681DBF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=+S5GQGZ6/D/Mg+joUObE0SvwDFyqkvRT2sP5/5vRQvk=; b=FnhfzhzYXlNcl6IxqwADyyzc747mTy8MU4rkgJN+LmLi1BBuM+8meOEr2rKBNA5cdb UV5cg1R4d2Z6xBRg09lXiXAPPCtiRk0FnLoDXMxi1ZwCUSCYDye4OaZgSAusHDiISi0X vF5wP8hNjXz8Z2pcn3GjqjTxVus9QcfOT5k48= Content-Disposition: inline In-Reply-To: <20221005214844.2699-3-Jason-OnJsPKxuuEcAvxtiuMwx3w@public.gmane.org> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces-yBygre7rU0TnMu66kgdUjQ@public.gmane.org Sender: "dev" To: "Jason A. Donenfeld" Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org, Andrii Nakryiko , Hans Verkuil , linux-sctp-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vg On Wed, Oct 05, 2022 at 11:48:41PM +0200, Jason A. Donenfeld wrote: > 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. > > Signed-off-by: Jason A. Donenfeld Same question about "mechanism of transformation". > diff --git a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c > index ddfe9208529a..ac452a0111a9 100644 > --- a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c > +++ b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c > @@ -1467,7 +1467,7 @@ static void make_established(struct sock *sk, u32 snd_isn, unsigned int opt) > tp->write_seq = snd_isn; > tp->snd_nxt = snd_isn; > tp->snd_una = snd_isn; > - inet_sk(sk)->inet_id = prandom_u32(); > + inet_sk(sk)->inet_id = get_random_u16(); > assign_rxopt(sk, opt); > > if (tp->rcv_wnd > (RCV_BUFSIZ_M << 10)) This one I had to go look at -- inet_id is u16, so yeah. :) > diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c > index 56ffaa8dd3f6..0131ed2cd1bd 100644 > --- a/lib/test_vmalloc.c > +++ b/lib/test_vmalloc.c > @@ -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. This wasn't obvious either, but it looks like it's because it never consumes more than u8? > diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c > index 7981be526f26..57c7686ac485 100644 > --- a/net/netfilter/nf_nat_core.c > +++ b/net/netfilter/nf_nat_core.c > @@ -468,7 +468,7 @@ static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple, > if (range->flags & NF_NAT_RANGE_PROTO_OFFSET) > off = (ntohs(*keyptr) - ntohs(range->base_proto.all)); > else > - off = prandom_u32(); > + off = get_random_u16(); > > attempts = range_size; Yup, u16 off; > diff --git a/net/sched/sch_sfb.c b/net/sched/sch_sfb.c > index 2829455211f8..7eb70acb4d58 100644 > --- a/net/sched/sch_sfb.c > +++ b/net/sched/sch_sfb.c > @@ -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)) { include/uapi/linux/pkt_sched.h:#define SFB_MAX_PROB 0xFFFF Reviewed-by: Kees Cook -- Kees Cook