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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74941C4332F for ; Thu, 17 Nov 2022 21:58:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241134AbiKQV6W (ORCPT ); Thu, 17 Nov 2022 16:58:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241093AbiKQV5u (ORCPT ); Thu, 17 Nov 2022 16:57:50 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBE9C7DED8 for ; Thu, 17 Nov 2022 13:57:15 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id d13-20020a17090a3b0d00b00213519dfe4aso3283528pjc.2 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=ThIhrPmDz8ak2ghAoGPYaN4h2CFlJrak76qiKA3OUfQLumc2YWAqE1GS1bkFk1ffUT 8rRS8nZwDCvxOVdlkaz8Y+zgHOGmd0MX6paPivY4GdvM52kZcrF1yGoyAlbMWZVjTZIo rtDUjXoAvRVyGzckTzt21YFse3V04msERvda9O0Z5895ZKtlb4YHwef3xVnBd191jend BusRjjeOSs8IbS7qg4ARGRH8+p5vb26FpyuA6U32ZmWu9BLWfhfpszNlsNaGldlT4Rg+ 1q+1OXHDtPLhV1N2VIQynWsk1cOkoJNUKoWk0BwZNVCLh2xOoILBuuFoM8t992SDBg7L cvrA== X-Gm-Message-State: ANoB5pmYyssnkMaL+YtRFWOO/uAOV4lWePS/65KSfSzTDVXP3VbKUyGf 8eBO1n8SVqSINZQUWpmDLKufVA== X-Google-Smtp-Source: AA0mqf5F2mDePv/SsUAusLJAu22vdbKLGH3peZOY+MZBhXcyEG7NIlWlaGpyKLA4kx4t011dWowR5A== 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> Precedence: bulk List-ID: X-Mailing-List: linux-block@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 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 2140DC433FE for ; Thu, 17 Nov 2022 21:58:16 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4NCv1L2KfYz3cF6 for ; Fri, 18 Nov 2022 08:58:14 +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=PJZ6jbnQ; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=chromium.org (client-ip=2607:f8b0:4864:20::1030; helo=mail-pj1-x1030.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=PJZ6jbnQ; dkim-atps=neutral Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) (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 4NCv0K3Kvsz3bhn for ; Fri, 18 Nov 2022 08:57:18 +1100 (AEDT) Received: by mail-pj1-x1030.google.com with SMTP id d13-20020a17090a3b0d00b00213519dfe4aso3283529pjc.2 for ; Thu, 17 Nov 2022 13:57:18 -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=wB3e2q37b+knDe+5dvesPDFo0o2ywdBwm59l5ZQORmlsD9L/RM8qcoNi2d1l+FNsoi QqDxpqfllq5YvGNTph+atoAYK0nilI3aT0aixRXK8Li09DbqArrLoSG3zN4lDYDglUtj fSCbB/elcsse+r6NUiovOG+IB4Ss7xYfjf4rPw9ZCClpIIwHS066dx81h9A1Usi9+5cv QfNrmEJ2prcuwPkUmrT6uRSyxIponsVNiOfAEPv+xEhnvRpVt5SCIdx28h1DRXCvGBGS sNBu40UjYmZSeO8USY0HOOhXU/W7fekY7FRCkPzLtf5nWsPITpLJawVjq8cz3uMg3oLU lDSg== X-Gm-Message-State: ANoB5plRmiBW/DSBC4hIE7aK0T6uY6K0tcjAsktnHzTnqmBU9YJEt/vr oljf10f9aW+tB6eT/7TIHV94Mg== X-Google-Smtp-Source: AA0mqf5F2mDePv/SsUAusLJAu22vdbKLGH3peZOY+MZBhXcyEG7NIlWlaGpyKLA4kx4t011dWowR5A== 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" 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-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: "Darrick J . Wong" , patches@lists.linux.dev, netdev@vger.kernel.org, Andreas Dilger , Herbert Xu , Richard Weinberger , Helge Deller , Russell King , Jason Gunthorpe , Catalin Marinas , Jakub Kicinski , linux-mips@vger.kernel.org, linux-media@vger.kernel.org, Heiko Carstens , Jani Nikula , linux-block@vger.kernel.org, SeongJae Park , loongarch@lists.linux.dev, Jaegeuk Kim , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, Thomas Bogendoerfer , Theodore Ts'o , linux-parisc@vger.kernel.org, "Martin K . Petersen" , Greg Kroah-Hartman , linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org, Chr istoph =?iso-8859-1?Q?B=F6hmwalder?= , linux-crypto@vger.kernel.org, Sakari Ailus , linux-fsdevel@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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 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 F0E5EC4332F for ; Thu, 17 Nov 2022 21:58:16 +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=Dzb8VY2eRQmPfaG8gAWdUZfCNxkmBOxMLM7r2qW7aSE=; b=Nqjdn08yWFcEvr fzI6uuxZKYABzCXCWlAdlfGhBUruOoFA+5WQEyudB1rCbtVVIr0dydhZh/b+Its74DoqheP892ZUO Uw4X8mJB047u1Jkp/zF4RxwbQU9WhheeH/96Q1PCklkfWLIraCNi34FzVvknHdx1pHPuntQOnmHny +KcFUHKOKVZEVCRIkaPKTBUFA0EFdBc5IwzK2OuWLyp3nK5HxF353SokXd3jS70BOncEksJwPbp7x iH/iP0BeLMoM3vRgFlbvSGHObz334cTu9EwoAjaFputIZ9K55QKeWwKfB++zZ2dyYeOTA0Qab1AO9 4LYDsNe42Vc/ejab87Nw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovmsq-0009GA-3o; Thu, 17 Nov 2022 21:57:20 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovmsn-0009FK-Au for linux-arm-kernel@lists.infradead.org; Thu, 17 Nov 2022 21:57:19 +0000 Received: by mail-pj1-x102d.google.com with SMTP id b1-20020a17090a7ac100b00213fde52d49so3270298pjl.3 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=21cqRhP1mP3+TaSNrRfoNbekpJBlxVBaJUTZrQlb1fn6nYq24Sd0KILk1B+pK8CasY S6XbRwP5XFzbMtUU5wFX9HFw1wVUsp5g78wpMqTJWP5cnKPu9Zz6+cQwDRq38E7sWotP F2f6Z/IyrJtzFTDrm9e5/3UOMGhre02xp/csi/ANzHN6uyC0gLvltcBgsKTaeCKc9oLy YXBVpRQReRbVubRfVVCPg88voOZuVYN0kdtbxjssgyPLk7aZuPR/yUiY5wOZy5yOAgn5 G1YAPaBRyDXKNfKHazAO2vRyqKX1F6wwFm7VRIEpHBLZY+pxqX5JBmY5CxKGBpYRy/sU ScXg== X-Gm-Message-State: ANoB5pnamOFTKrchNoqLvjngCamUndEXUuFrv03aNai/DdpS40Kd27KI EsUA+fUjVzJVa+QypdA4DUr93A== X-Google-Smtp-Source: AA0mqf5F2mDePv/SsUAusLJAu22vdbKLGH3peZOY+MZBhXcyEG7NIlWlaGpyKLA4kx4t011dWowR5A== 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-Disposition: inline In-Reply-To: <20221117202906.2312482-4-Jason@zx2c4.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221117_135717_418631_5A8D27F0 X-CRM114-Status: GOOD ( 17.93 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel