From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2369C433FE for ; Thu, 6 Oct 2022 04:17:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KHodUpJv6O5FqvKR21JOBhdBROv/48tBc+i5dnhtV/I=; b=qi1PIVOuzUtCzV +ROu/RHNHo7L9RQo70FXNNLI1OjJwclH7k3UYDWo8iwC36T97ZkbTQ64eA8OyviKiniZt9PanMmC+ dTgQrDpqi0Z/Fr0ZMprmxZ1hxsMRbkDSO6Y+0GUdHAtwHpjwpLKFJ+sdjSrbli3fhtCuWeLgmq5zS SI/FPDZol7B2acCcn/KdgL/cw951T3f9DCC8SmI5TxbCC/c6W+sFhwSHqCYHWAkXFMgdv4iYgwpDt fvW0gZO/gKWBaplyO1g1ORnschUup6dZTxUYg4zVW82YzT1pzmKgaR29WFra1LT15MhddXZuGt45z m2iR3c0IERyul1vjleTA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIJx-00HL3e-BF; Thu, 06 Oct 2022 04:17:17 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIJt-00HKwk-66 for linux-mtd@lists.infradead.org; Thu, 06 Oct 2022 04:17:14 +0000 Received: by mail-pl1-x629.google.com with SMTP id x1so637372plv.5 for ; Wed, 05 Oct 2022 21:16:53 -0700 (PDT) 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=vyBek+OTaUqrrqPyKsoFWRmsw776ai3i5kadxONBmtbXAm/aGhQBxP0xfBa9BKf2kS TQgGd+9v1O1P6jjcmwh6eB/xBWf6yV6l5+XkGLCbg2AJRY3qkAqymBKv3rC5DFipCCC0 T3TjQ3+nlOUOwbbXj+axrsNiGpx4P7xS8BGaoQB/rv1yjII5KkI5bCMaDYrqa0ECJSTx 1zPWQmFCKND1fWtJ3mSYmALWuoUyK7rG2T+WGoCRx6zPJIQhZY3qQyZ9kM6nAgB1M/pK B4fM5Gw0NBrAvZGUIG5ZbmKxkr52tJBNECqRS+YlB3wTJ57CQ0kGxXlVuipeEJWNw9TF 7S2Q== X-Gm-Message-State: ACrzQf2ZKfz03xdlqYz4bggnc7Ttj+t7UrTPlNZqbw64tx4AVRHlvTj5 U/myXIlnrU/NzcjO2R7W4vOSxA== X-Google-Smtp-Source: AMsMyM4cCCHH4sm8YdThV49cVWVDAgPzqhkhh6i7+17PaqDM+DNwRTUS7Um0Z/zO228Kkrjqlqz/Cg== X-Received: by 2002:a17:90a:ea95:b0:20a:f65b:143b with SMTP id h21-20020a17090aea9500b0020af65b143bmr3218311pjz.230.1665029812504; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id je12-20020a170903264c00b00176d347e9a7sm11219825plb.233.2022.10.05.21.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:16:51 -0700 (PDT) Date: Wed, 5 Oct 2022 21:16:50 -0700 From: Kees Cook To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, Ajay Singh , Akinobu Mita , Alexandre Torgue , Amitkumar Karwar , Andreas Dilger , Andreas =?iso-8859-1?Q?F=E4rber?= , Andreas Noever , Andrew Lunn , Andrew Morton , Andrii Nakryiko , Andy Gospodarek , Andy Lutomirski , Andy Shevchenko , Anil S Keshavamurthy , Anna Schumaker , Arend van Spriel , Ayush Sawal , Borislav Petkov , Chao Yu , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Christoph Hellwig , Christophe Leroy , Chuck Lever , Claudiu Beznea , Cong Wang , Dan Williams , Daniel Borkmann , "Darrick J . Wong" , Dave Hansen , David Ahern , "David S . Miller" , Dennis Dalessandro , Dick Kennedy , Dmitry Vyukov , Eric Dumazet , Florian Westphal , Franky Lin , Ganapathi Bhat , Greg Kroah-Hartman , Gregory Greenman , "H . Peter Anvin" , Hannes Reinecke , Hans Verkuil , Hante Meuleman , Hao Luo , Haoyue Xu , Heiner Kallweit , Helge Deller , Herbert Xu , Hideaki YOSHIFUJI , Hugh Dickins , Igor Mitsyanko , Ilya Dryomov , Ingo Molnar , Jack Wang , Jaegeuk Kim , Jaehoon Chung , Jakub Kicinski , Jamal Hadi Salim , "James E . J . Bottomley" , James Smart , Jan Kara , Jason Gunthorpe , Jay Vosburgh , Jean-Paul Roubelat , Jeff Layton , Jens Axboe , Jiri Olsa , Jiri Pirko , Johannes Berg , John Fastabend , John Stultz , Jon Maloy , Jonathan Corbet , Jozsef Kadlecsik , Julian Anastasov , KP Singh , Kalle Valo , Keith Busch , Lars Ellenberg , Leon Romanovsky , Manish Rangankar , Manivannan Sadhasivam , Marcelo Ricardo Leitner , Marco Elver , "Martin K . Petersen" , Martin KaFai Lau , Masami Hiramatsu , Mauro Carvalho Chehab , Maxime Coquelin , "Md . Haris Iqbal" , Michael Chan , Michael Ellerman , Michael Jamet , Michal Januszewski , Mika Westerberg , Miquel Raynal , Namjae Jeon , "Naveen N . Rao" , Neil Horman , Nicholas Piggin , Nilesh Javali , OGAWA Hirofumi , Pablo Neira Ayuso , Paolo Abeni , Peter Zijlstra , Philipp Reisner , Potnuri Bharat Teja , Pravin B Shelar , Rasmus Villemoes , Richard Weinberger , Rohit Maheshwari , Russell King , Sagi Grimberg , Santosh Shilimkar , Sergey Matyukevich , Sharvari Harisangam , Simon Horman , Song Liu , Stanislav Fomichev , Steffen Klassert , Stephen Boyd , Stephen Hemminger , Sungjong Seo , Theodore Ts'o , Thomas Gleixner , Thomas Graf , Thomas Sailer , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Trond Myklebust , Ulf Hansson , Varun Prakash , Veaceslav Falico , Vignesh Raghavendra , Vinay Kumar Yadav , Vinod Koul , Vlad Yasevich , Wenpeng Liang , Xinming Hu , Xiubo Li , Yehezkel Bernat , Ying Xue , Yishai Hadas , Yonghong Song , Yury Norov , brcm80211-dev-list.pdl@broadcom.com, cake@lists.bufferbloat.net, ceph-devel@vger.kernel.org, coreteam@netfilter.org, dccp@vger.kernel.org, dev@openvswitch.org, dmaengine@vger.kernel.org, drbd-dev@lists.linbit.com, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, linux-actions@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-fbdev@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hams@vger.kernel.org, linux-media@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nfs@vger.kernel.org, linux-nvme@lists.infradead.org, linux-raid@vger.kernel.org, linux-rdma@vger.kernel.org, linux-scsi@vger.kernel.org, linux-sctp@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linux-xfs@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, lvs-devel@vger.kernel.org, netdev@vger.kernel.org, netfilter-devel@vger.kernel.org, rds-devel@oss.oracle.com, SHA-cyfmac-dev-list@infineon.com, target-devel@vger.kernel.org, tipc-discussion@lists.sourceforge.net Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-Jason@zx2c4.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_211713_238792_6D83FD40 X-CRM114-Status: GOOD ( 25.56 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 163F6C433FE for ; Thu, 6 Oct 2022 04:42:24 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1ogIiE-00011L-VU; Thu, 06 Oct 2022 04:42:22 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1ogIiD-00011F-L3 for linux-f2fs-devel@lists.sourceforge.net; Thu, 06 Oct 2022 04:42:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=In-Reply-To:Content-Type:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=Qug9li2ezyfdBx/Qon8SuLPvbo HC+HkJrK1PM+gR2lzJ/eJjw0qpN0kerm+7wkFSO7vJmU8npIABp3x509erDC+8EYppSPQ921jTCDf BpJ/n9jdbEdFeJQ0OtkbtYsWIXQILYW/Yx1Ml4eDCPyW9QOemcMUEV2F9q/zDrVy5bxg=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To :From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=eUnbR0uNcmt75MLrcfN1YGIPbF LXwpw5zSVi6LLSBfu/OUQBKcd7PgpMRGKE/7wtuB8/jWk9wsam65TnlYmB6fdTig87i5stN3HOOm/ 6sgvX+QMttZc3tCw5KmihZsbItOmAJre4daEQn5k95lmBdX8DTj3DbeqQX6jZvyigjAU=; Received: from mail-oa1-f50.google.com ([209.85.160.50]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1ogIi6-0007Kt-UL for linux-f2fs-devel@lists.sourceforge.net; Thu, 06 Oct 2022 04:42:21 +0000 Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-1322d768ba7so986042fac.5 for ; Wed, 05 Oct 2022 21:42:14 -0700 (PDT) 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=Gm1SGg4iGa+RXMqcGxwfV+sZLVxVdmN7+PowdqCT+/930BcJeFi3U0UfbzVBqQJpQL /D9KqnftHEagjTTNX8idSFeTWFIHWZpYGiheuBefqiu4obnlRqZTalpvz5Jbz/WPSBPs ojoFpJgel/szy2IS6qHpWeFswDxS4BsAEnvafWywQW18zUxKij5r2YCLU48uWETai5u8 g0ZHukXd2GgLh+YzdkF5H/5Xgn/sPTkctQn7+Royelfpk9/2g4G6TTSimQJI4BQU4W03 b60bQsBrdocT0eAcGZOaXU9aEBFt+KQefJ2Hv0pJCAnCX1sPxOGhj/k6ULsKGAIJmCEY 6wRg== X-Gm-Message-State: ACrzQf2i6gddZ/R+d8O91BgqQtElKBF0QN3GojWQ5zU5mFA9jnge0Gf5 7TaBa1EhZXPQ1zwYrgETVP8IsTfX5F+NrMMW X-Google-Smtp-Source: AMsMyM4cCCHH4sm8YdThV49cVWVDAgPzqhkhh6i7+17PaqDM+DNwRTUS7Um0Z/zO228Kkrjqlqz/Cg== X-Received: by 2002:a17:90a:ea95:b0:20a:f65b:143b with SMTP id h21-20020a17090aea9500b0020af65b143bmr3218311pjz.230.1665029812504; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id je12-20020a170903264c00b00176d347e9a7sm11219825plb.233.2022.10.05.21.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:16:51 -0700 (PDT) Date: Wed, 5 Oct 2022 21:16:50 -0700 From: Kees Cook To: "Jason A. Donenfeld" Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-Jason@zx2c4.com> X-Headers-End: 1ogIi6-0007Kt-UL Subject: Re: [f2fs-dev] [PATCH v1 1/5] treewide: use prandom_u32_max() when possible X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel@vger.kernel.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , linux-fbdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , Andreas =?iso-8859-1?Q?F=E4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , KP Singh , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4BE2BC433FE for ; Thu, 6 Oct 2022 05:33:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 77F1A10E7DF; Thu, 6 Oct 2022 05:33:13 +0000 (UTC) Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0471B10E33E for ; Thu, 6 Oct 2022 04:16:53 +0000 (UTC) Received: by mail-pl1-x634.google.com with SMTP id n7so662063plp.1 for ; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=jsdz7smll62K63d9GLpLxaMyArCILVwhQnmJnuUqRtKJB/nB4NmOUfSGSNWjRZcq3V nMrGdr8BxuPIYDhH6kdUQhT8b54Zkek7Ijmwci9k74/ALspv8SkrE1IUoiPopoteH2Ku 6iRvMmjZIxY4QYx0J9dPZEurTRjifNkbrhCC6UKORCMfCZZmdK6Xq0lqfVJp4wZ0ax0k HDbq6VuryQ/rhq693X8LoXZOZnALOGC1WJACRDmphO7E+l6+qI6jclAx90NeuVwl8BKK knaf+fvSNuiFGSvzO/lkCeh+c0+TRu5Ln0rNnPRTDd9frwNYgaROGQPL+9Rb+v5gobCY yYZA== X-Gm-Message-State: ACrzQf1q4GLndBAgWLwCqdFQVhqESrulDsfHvJauIxBhcRItvdJlVoSN PAyWuk3/m3US9g6A+SO31o+z6Q== X-Google-Smtp-Source: AMsMyM4cCCHH4sm8YdThV49cVWVDAgPzqhkhh6i7+17PaqDM+DNwRTUS7Um0Z/zO228Kkrjqlqz/Cg== X-Received: by 2002:a17:90a:ea95:b0:20a:f65b:143b with SMTP id h21-20020a17090aea9500b0020af65b143bmr3218311pjz.230.1665029812504; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id je12-20020a170903264c00b00176d347e9a7sm11219825plb.233.2022.10.05.21.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:16:51 -0700 (PDT) Date: Wed, 5 Oct 2022 21:16:50 -0700 From: Kees Cook To: "Jason A. Donenfeld" Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-Jason@zx2c4.com> X-Mailman-Approved-At: Thu, 06 Oct 2022 05:33:08 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel@vger.kernel.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , linux-fbdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , Andreas =?iso-8859-1?Q?F=E4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , KP Singh , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jaehoon Chung , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Chao Yu , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A8BA7C433FE for ; Thu, 6 Oct 2022 06:28:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=DncYaSia/8sS8asZ6MzzYNTDwc krGGqAiOTkawGB5yCipKMLAbJjg1O/CPbDB4Nx6wImDboXGBY0uGfmCyZYoyv4MVBrCtkVcu5pzIC aJQqe5znB4OFD3rLKwOEWeNFZPxDXuTYre2Hk4kHyMngJyLnpK/SLsIE5AIsZZx4Az8sS4ynS2Whp 6MdcytjgkSUk66z8SWcRWai5wduzm5hzdV/SsptFDHjquLtOM5Mx1XyKlyf88hdiAntC1ciUyF7zq iQYA1T+Tz9TmSoEPyHMkEC27hWbJJfTBpdo9+Qm+O1gNM5o9SUV4g/JrwdMsWaXk+6eYAI+vK34Sa 7nybMacA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogKMz-0009Nw-BZ; Thu, 06 Oct 2022 06:28:33 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIJt-00HKwi-BS for linux-nvme@lists.infradead.org; Thu, 06 Oct 2022 04:17:16 +0000 Received: by mail-pj1-x1030.google.com with SMTP id l1-20020a17090a72c100b0020a6949a66aso633902pjk.1 for ; Wed, 05 Oct 2022 21:16:53 -0700 (PDT) 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=GNbTCuq/hycgiHV+6/KKT28u8DmcecS5/kZmXJmyyxqhw/wtD4Cvk++q72HRSNgvDM KhzZD31ipLp5Xw34XFJVQRu6A0RwNISSdeOok8eAiN+k69EP1cdXCWawv16Hod9iDJL1 DKiJW0n80basmKpC2yZBdaIzT9HpKu9DCUkVgRxABSlP66V70rVQGdRrIvSBbnm4nAVk mdssDHL/NZ4bQsveo+o4C10JILzxb3swydh/Q2PurMbWuFhRQ5JWrwVe0r4siB80fZtq HOLOKXkNAkWzMZW/LkkZnNsQC5QJYZT2Svdmy3PUq96mivdt3LtBotmO6sk8OtqhBmsj yn7Q== X-Gm-Message-State: ACrzQf1PZvQ1vwJ4SjUM4eYGbMCpK8UdlVfpB0QyolVwOLD5xeV8pemR ROr7RCRM5uoV88hOGTuu0D64sQ== X-Google-Smtp-Source: AMsMyM4cCCHH4sm8YdThV49cVWVDAgPzqhkhh6i7+17PaqDM+DNwRTUS7Um0Z/zO228Kkrjqlqz/Cg== X-Received: by 2002:a17:90a:ea95:b0:20a:f65b:143b with SMTP id h21-20020a17090aea9500b0020af65b143bmr3218311pjz.230.1665029812504; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id je12-20020a170903264c00b00176d347e9a7sm11219825plb.233.2022.10.05.21.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:16:51 -0700 (PDT) Date: Wed, 5 Oct 2022 21:16:50 -0700 From: Kees Cook To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, Ajay Singh , Akinobu Mita , Alexandre Torgue , Amitkumar Karwar , Andreas Dilger , Andreas =?iso-8859-1?Q?F=E4rber?= , Andreas Noever , Andrew Lunn , Andrew Morton , Andrii Nakryiko , Andy Gospodarek , Andy Lutomirski , Andy Shevchenko , Anil S Keshavamurthy , Anna Schumaker , Arend van Spriel , Ayush Sawal , Borislav Petkov , Chao Yu , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Christoph Hellwig , Christophe Leroy , Chuck Lever , Claudiu Beznea , Cong Wang , Dan Williams , Daniel Borkmann , "Darrick J . Wong" , Dave Hansen , David Ahern , "David S . Miller" , Dennis Dalessandro , Dick Kennedy , Dmitry Vyukov , Eric Dumazet , Florian Westphal , Franky Lin , Ganapathi Bhat , Greg Kroah-Hartman , Gregory Greenman , "H . Peter Anvin" , Hannes Reinecke , Hans Verkuil , Hante Meuleman , Hao Luo , Haoyue Xu , Heiner Kallweit , Helge Deller , Herbert Xu , Hideaki YOSHIFUJI , Hugh Dickins , Igor Mitsyanko , Ilya Dryomov , Ingo Molnar , Jack Wang , Jaegeuk Kim , Jaehoon Chung , Jakub Kicinski , Jamal Hadi Salim , "James E . J . Bottomley" , James Smart , Jan Kara , Jason Gunthorpe , Jay Vosburgh , Jean-Paul Roubelat , Jeff Layton , Jens Axboe , Jiri Olsa , Jiri Pirko , Johannes Berg , John Fastabend , John Stultz , Jon Maloy , Jonathan Corbet , Jozsef Kadlecsik , Julian Anastasov , KP Singh , Kalle Valo , Keith Busch , Lars Ellenberg , Leon Romanovsky , Manish Rangankar , Manivannan Sadhasivam , Marcelo Ricardo Leitner , Marco Elver , "Martin K . Petersen" , Martin KaFai Lau , Masami Hiramatsu , Mauro Carvalho Chehab , Maxime Coquelin , "Md . Haris Iqbal" , Michael Chan , Michael Ellerman , Michael Jamet , Michal Januszewski , Mika Westerberg , Miquel Raynal , Namjae Jeon , "Naveen N . Rao" , Neil Horman , Nicholas Piggin , Nilesh Javali , OGAWA Hirofumi , Pablo Neira Ayuso , Paolo Abeni , Peter Zijlstra , Philipp Reisner , Potnuri Bharat Teja , Pravin B Shelar , Rasmus Villemoes , Richard Weinberger , Rohit Maheshwari , Russell King , Sagi Grimberg , Santosh Shilimkar , Sergey Matyukevich , Sharvari Harisangam , Simon Horman , Song Liu , Stanislav Fomichev , Steffen Klassert , Stephen Boyd , Stephen Hemminger , Sungjong Seo , Theodore Ts'o , Thomas Gleixner , Thomas Graf , Thomas Sailer , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Trond Myklebust , Ulf Hansson , Varun Prakash , Veaceslav Falico , Vignesh Raghavendra , Vinay Kumar Yadav , Vinod Koul , Vlad Yasevich , Wenpeng Liang , Xinming Hu , Xiubo Li , Yehezkel Bernat , Ying Xue , Yishai Hadas , Yonghong Song , Yury Norov , brcm80211-dev-list.pdl@broadcom.com, cake@lists.bufferbloat.net, ceph-devel@vger.kernel.org, coreteam@netfilter.org, dccp@vger.kernel.org, dev@openvswitch.org, dmaengine@vger.kernel.org, drbd-dev@lists.linbit.com, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, linux-actions@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-fbdev@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hams@vger.kernel.org, linux-media@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nfs@vger.kernel.org, linux-nvme@lists.infradead.org, linux-raid@vger.kernel.org, linux-rdma@vger.kernel.org, linux-scsi@vger.kernel.org, linux-sctp@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linux-xfs@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, lvs-devel@vger.kernel.org, netdev@vger.kernel.org, netfilter-devel@vger.kernel.org, rds-devel@oss.oracle.com, SHA-cyfmac-dev-list@infineon.com, target-devel@vger.kernel.org, tipc-discussion@lists.sourceforge.net Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-Jason@zx2c4.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_211713_471955_D7004292 X-CRM114-Status: GOOD ( 26.06 ) X-Mailman-Approved-At: Wed, 05 Oct 2022 23:27:55 -0700 X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 903BCC433FE for ; Thu, 6 Oct 2022 04:52:56 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MjfFg0bV5z3c3V for ; Thu, 6 Oct 2022 15:52:55 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=WgCBVOxT; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=chromium.org (client-ip=2607:f8b0:4864:20::1033; helo=mail-pj1-x1033.google.com; envelope-from=keescook@chromium.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=WgCBVOxT; dkim-atps=neutral Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MjdSC6FLLz2xDv for ; Thu, 6 Oct 2022 15:16:57 +1100 (AEDT) Received: by mail-pj1-x1033.google.com with SMTP id i7-20020a17090a65c700b0020ad9666a86so3272100pjs.0 for ; Wed, 05 Oct 2022 21:16:57 -0700 (PDT) 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=grP1p+Jhv4E6AKY1Phnj7M/y6H1rmgFKaC+1Cq+Lob8AB+yRgY6nt+vGlkz3oPx6FU xcwf0jbapIROrI43E8FKxHJ1g+EGYwmgmz8tn2sg1FGre+/yEkQ8MO/g3KUoQ+jJixQ5 lZnFBlD2rgxmvC3Kfiw6IDwxl2EVQ0wyXQzMOKzHkMtpA47D5T4b0fScORaWzE5NU6VX /riK8suQfnaB8YUGF4XCyWfX1nYXcdK4gSq3oWsQyZ6clPYj6nG042Nq7S/Ttg56wDrd o/vedAm3iwc0h4lmPWC6NPe/KpOOuTt+1nBUIi7nZc5kQUIl2wWTSHvNwePOmoxt+atM RFOA== X-Gm-Message-State: ACrzQf0ynSq3Hboo+5uo2K5xu9U5DBVNjpNAtvK6nC2wgQhzklrBfxlB fsKOm2r5f0ev78GbIvxkpb+DLw== X-Google-Smtp-Source: AMsMyM4cCCHH4sm8YdThV49cVWVDAgPzqhkhh6i7+17PaqDM+DNwRTUS7Um0Z/zO228Kkrjqlqz/Cg== X-Received: by 2002:a17:90a:ea95:b0:20a:f65b:143b with SMTP id h21-20020a17090aea9500b0020af65b143bmr3218311pjz.230.1665029812504; Wed, 05 Oct 2022 21:16:52 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id je12-20020a170903264c00b00176d347e9a7sm11219825plb.233.2022.10.05.21.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:16:51 -0700 (PDT) Date: Wed, 5 Oct 2022 21:16:50 -0700 From: Kees Cook To: "Jason A. Donenfeld" Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-Jason@zx2c4.com> X-Mailman-Approved-At: Thu, 06 Oct 2022 15:52:03 +1100 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , ceph-devel@vger.kernel.org, Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , linux-fbdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , Andreas =?iso-8859-1?Q?F=E4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , KP Singh , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jaehoon Chung , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Chao Yu , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kees Cook Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Date: Wed, 5 Oct 2022 21:16:50 -0700 Message-ID: <202210052035.A1020E3@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-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 smtp2.osuosl.org 8A8EE409A5 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 5E49840147 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org BCACE81AA7 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 52A9881763 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=WbAjddv7dJAWgX74vxMdFtfHq8KnIvmUd6IHydPVZWw=; b=WgCBVOxTCY3r6hnjb6DkPXMWKhTpJd2tJ8grQVy4FqddmqNDeeFgajag3lajOwT2v4 a87IfTIAA5TCPftGBNiy4cY7xnOoxUEfMiQjvrPT/rt/xjKxauu3MOYhjJEXssFW35Ee /z+9ItQjm7QhlvSxza54MmNIb/ZuYs/f028rM= Content-Disposition: inline In-Reply-To: <20221005214844.2699-2-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:40PM +0200, Jason A. Donenfeld wrote: > Rather than incurring a division or requesting too many random bytes for > the given range, use the prandom_u32_max() function, which only takes > the minimum required bytes from the RNG and avoids divisions. Yes please! Since this is a treewide patch, it's helpful for (me at least) doing reviews to detail the mechanism of the transformation. e.g. I imagine this could be done with something like Coccinelle and @no_modulo@ expression E; @@ - (prandom_u32() % (E)) + prandom_u32_max(E) > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > index 118248a5d7d4..4236c799a47c 100644 > --- a/drivers/mtd/ubi/debug.h > +++ b/drivers/mtd/ubi/debug.h > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > { > if (ubi->dbg.emulate_bitflips) > - return !(prandom_u32() % 200); > + return !(prandom_u32_max(200)); > return 0; > } > Because some looks automated (why the parens?) > @@ -393,14 +387,11 @@ static struct test_driver { > > static void shuffle_array(int *arr, int n) > { > - unsigned int rnd; > int i, j; > > for (i = n - 1; i > 0; i--) { > - rnd = prandom_u32(); > - > /* Cut the range. */ > - j = rnd % i; > + j = prandom_u32_max(i); > > /* Swap indexes. */ > swap(arr[i], arr[j]); And some by hand. :) Reviewed-by: Kees Cook -- Kees Cook