Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2645212rwb; Thu, 17 Nov 2022 13:59:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf56i8XDH0Bjl/NHJ3S6Yi3xeCsYnx4g+uVKzejYbdBigU9czRToa9RZzbFZSnpIUmZmzy/8 X-Received: by 2002:a63:4285:0:b0:477:15c8:cd8a with SMTP id p127-20020a634285000000b0047715c8cd8amr2427571pga.595.1668722351576; Thu, 17 Nov 2022 13:59:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668722351; cv=none; d=google.com; s=arc-20160816; b=aQWkA9FKLanZanAs0p87gAjsjWQvYj6PzL9QZLCiy714Tf7M2rBfnyiiyggeZlHhDW L+XJ//Rfc3aSqlPzVB5vhkVrC1W2xIELz7GPu6FUYnmfzm+l4SEE0Y9efm8M0YX1oMno C9QsmgswFWbFUXvwWGEj4KBQT7C4i7rwTpXT7kDqR9K6FjtH1Fxfo1XJRWz72FfNl+4W lcJwHCApe/9/I6kGuomcVuUVdHNeJi5VhVUR2wAPxglv7Lu5XzghYzOHShWYo5yiDkiA JybrVOJRbAgc+3YIXNOd2WSrDNvDekX3umXs/PxCn1oZ0pSvIQS7VzFWX8Di8mZSbgDz AgHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=IIh+nGIerLfk4ZB9SpoRZWMODmjSUqXiyhjydKb0Cds=; b=Gy/nzTXteiaBcxnyB7lPK7NJW1YENhg8WK7yp4u8CmwLSqKHiI08x3cq7hLZDQcqHL BbKWoCbutmwv5VSyWWQdGMDC8TL1LOUWAId8csDD0W8+wSKzSl0kAGzCh8631+7PCBou aTHdQIQFVtNptXYX0qbZ+PiraWnYG7z+DNqrtGdEj4GNM7H9Plu8NEGI0h3I9LQp2fcu /28hgQKHOv4X+95K26d3/luQbwUrLPfjxu9FtE36MXFxd+CvDmrweqqPzeibEKfPrvVu u/6v8lYAQRddVKHEcB0pkSU5C8X39D89tIMrNsL/i/U7Q8TpS35dy5/YIsbZIc3r+hNB Xy5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PJZ6jbnQ; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k8-20020a170902c40800b00186db024863si2188384plk.612.2022.11.17.13.58.52; Thu, 17 Nov 2022 13:59:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PJZ6jbnQ; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241142AbiKQV6W (ORCPT + 99 others); Thu, 17 Nov 2022 16:58:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241094AbiKQV5u (ORCPT ); Thu, 17 Nov 2022 16:57:50 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBF227DED9 for ; Thu, 17 Nov 2022 13:57:15 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id b21so2868418plc.9 for ; Thu, 17 Nov 2022 13:57:15 -0800 (PST) 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:message-id:reply-to; bh=IIh+nGIerLfk4ZB9SpoRZWMODmjSUqXiyhjydKb0Cds=; b=PJZ6jbnQhQq9tru88c3QcgjfZaJDnoJRCzzf1bX0/gWQf6BO1kYnGuZiAkcQbfZxF1 wVZdaktXE7y7G0iGR9w0mDib97tK+9Z1KEN4DGKZzUJEwZpO/1SRERjEZW4dZS1lSmQm 6pLSDQke2I1tY0Rbt7braOr4eQDXUxJpKb4Zg= 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 :message-id:reply-to; bh=IIh+nGIerLfk4ZB9SpoRZWMODmjSUqXiyhjydKb0Cds=; b=X+AJOZRltn+uADrFYbz6agOQtxon80HNACGVaig/C+OKC3MvzRnujmGPZQ5qvIFC4i SMLBF6Wy1f35FQOYwSnvkY+c+wgtPHyjdI5IUjWC2CLrPCNc5ijHfzqNrApUVgmGNNvo UnO0kvNFlQzrx8HNq8lkaW+K7qks3hJAA+duireH6HnGzyR1FJSFPyoikDyc+40s2MXl mEGA0FLUFCq7kCyNoSYXODQ/V+XEo/ET7uzpNPrnVDjfDRYSh8QfhYT2hXEFMRaqtkJ6 oNMYnLlRrXAzPJge6slVxpk0k/hpkim7TAzTK9/vJYruHbGs5STpqyydb/CSqrssAgFR WFoQ== X-Gm-Message-State: ANoB5pkTCavJLZdeT2IAe8JxY7EwatqbFvAA3LmyppcWdxIEJjwSAFeb 4kEU5ww7WCvjzylhLkZ/dxO34A== X-Received: by 2002:a17:90a:dd82:b0:212:fdb1:720b with SMTP id l2-20020a17090add8200b00212fdb1720bmr10785160pjv.66.1668722234748; Thu, 17 Nov 2022 13:57:14 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id a28-20020aa795bc000000b0056beae3dee2sm1674860pfk.145.2022.11.17.13.57.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 13:57:14 -0800 (PST) Date: Thu, 17 Nov 2022 13:57:13 -0800 From: Kees Cook To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, patches@lists.linux.dev, Greg Kroah-Hartman , Jakub Kicinski , Russell King , Catalin Marinas , Thomas Bogendoerfer , Heiko Carstens , Herbert Xu , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Jani Nikula , Jason Gunthorpe , Sakari Ailus , "Martin K . Petersen" , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Richard Weinberger , "Darrick J . Wong" , SeongJae Park , Thomas Gleixner , Andrew Morton , Michael Ellerman , Helge Deller , netdev@vger.kernel.org, linux-crypto@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mmc@vger.kernel.org, linux-parisc@vger.kernel.org Subject: Re: [PATCH v3 3/3] treewide: use get_random_u32_inclusive() when possible Message-ID: <202211171349.F42BA5B0@keescook> References: <20221114164558.1180362-1-Jason@zx2c4.com> <20221117202906.2312482-1-Jason@zx2c4.com> <20221117202906.2312482-4-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221117202906.2312482-4-Jason@zx2c4.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Thu, Nov 17, 2022 at 09:29:06PM +0100, Jason A. Donenfeld wrote: > These cases were done with this Coccinelle: > > @@ > expression H; > expression L; > @@ > - (get_random_u32_below(H) + L) > + get_random_u32_inclusive(L, H + L - 1) > > @@ > expression H; > expression L; > expression E; > @@ > get_random_u32_inclusive(L, > H > - + E > - - E > ) > > @@ > expression H; > expression L; > expression E; > @@ > get_random_u32_inclusive(L, > H > - - E > - + E > ) > > @@ > expression H; > expression L; > expression E; > expression F; > @@ > get_random_u32_inclusive(L, > H > - - E > + F > - + E > ) > > @@ > expression H; > expression L; > expression E; > expression F; > @@ > get_random_u32_inclusive(L, > H > - + E > + F > - - E > ) > > And then subsequently cleaned up by hand, with several automatic cases > rejected if it didn't make sense contextually. > > Reviewed-by: Greg Kroah-Hartman > Reviewed-by: Jason Gunthorpe # for infiniband > Signed-off-by: Jason A. Donenfeld > --- > arch/x86/kernel/module.c | 2 +- > crypto/rsa-pkcs1pad.c | 2 +- > crypto/testmgr.c | 10 ++++---- > drivers/bus/mhi/host/internal.h | 2 +- > drivers/dma-buf/st-dma-fence-chain.c | 2 +- > drivers/infiniband/core/cma.c | 2 +- > drivers/infiniband/hw/hns/hns_roce_ah.c | 5 ++-- > drivers/mtd/nand/raw/nandsim.c | 2 +- > drivers/net/wireguard/selftest/allowedips.c | 8 +++--- > .../broadcom/brcm80211/brcmfmac/p2p.c | 2 +- > .../net/wireless/intel/iwlwifi/mvm/mac-ctxt.c | 2 +- > fs/f2fs/segment.c | 6 ++--- > kernel/kcsan/selftest.c | 2 +- > lib/test_hexdump.c | 10 ++++---- > lib/test_printf.c | 2 +- > lib/test_vmalloc.c | 6 ++--- > mm/kasan/kasan_test.c | 6 ++--- > mm/kfence/kfence_test.c | 2 +- > mm/swapfile.c | 5 ++-- > net/bluetooth/mgmt.c | 5 ++-- > net/core/pktgen.c | 25 ++++++++----------- > net/ipv4/tcp_input.c | 2 +- > net/ipv6/addrconf.c | 6 ++--- > net/netfilter/nf_nat_helper.c | 2 +- > net/xfrm/xfrm_state.c | 2 +- > 25 files changed, 56 insertions(+), 64 deletions(-) Even the diffstat agrees this is a nice clean-up. :) Reviewed-by: Kees Cook The only comment I have is that maybe these cases can just be left as-is with _below()? > - size_t len = get_random_u32_below(rs) + gs; > + size_t len = get_random_u32_inclusive(gs, rs + gs - 1); It seems like writing it in the form of base plus [0, limit) is clearer? size_t len = gs + get_random_u32_below(rs); But there is only a handful, so *shrug* All the others are much cleaner rewritten as _inclusive(). -- Kees Cook