Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1169319pxb; Sun, 22 Aug 2021 08:02:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGfuCVa0/h/PEdxNRpomSvToFxlqdeMSEbEGOQTP2W4CKb3VQnxBWttG4GHwReaHiOwraJ X-Received: by 2002:a17:907:76b0:: with SMTP id jw16mr8732209ejc.193.1629644526808; Sun, 22 Aug 2021 08:02:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629644526; cv=none; d=google.com; s=arc-20160816; b=gxCD2jzL2s/HwoaGi3xs4pgQxDY5gXBGfUmPNlySRuGkrM792Lw4XN/VTX6ujTsPUw hMnhblESyT+NIx1vq2BoTaDQnLDJV2oGn7B4ltEEZm/PFSqtTCJrH+LwSwjHghsxkYZx oW7nGC5w+oh2ZlaG+gWkKM4QBDSvwAsEYN2nAIpGsy0Tgd7XNzUKjASp/qSXjQ0pX41U hw8lkVZNS2OhxtMP1XwR1z9moOeCuGCGvm27mRsbPyuZUDTxnHLCRJ5Zn6Ehlsi5Rmrx YPm02IomberjHmMrytfWncpxBs4tIBMgAxuKZhDGKZpDZffb0WB3I5aPPh4LGpsaRG6v 3anQ== 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=KtofH/4/jYATs6D3O66/kVjkwIShCI9KUHIKJ5lCCCQ=; b=KDo43y/yb30yRpWHZcXGwjcGoIs2IGWlT3tiYGsP2NhgYo1v2MY5kd8DxGFxF4++/x tRTRNcpv1myA8syFplkce7Xi4idB5s0YdCmrHL4egmg8I5b1cJ/wBPX1Tud4hADjK44G P9eZxyhijcrSMB2pu8w+b+OHIwtxGI4vmU32D7qWTClmuaFQt/M6Yz5SRRBg/QxrLJY+ /Js2kNcmunJbvVjLLaf7GCSVqD1BTgMeT8ybakfWWS7xHacnUM/3I1nKu0J6Kbr6NQ1l L+4kYvG3z7K8eUYH4ElPO9ggF5IKPUv/CwfjGgvC89a/7nWybXDYBHexgvgsRMt1OKLX aXjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FppxUShm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r2si3577593edy.116.2021.08.22.08.01.42; Sun, 22 Aug 2021 08:02:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FppxUShm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S233009AbhHVPAf (ORCPT + 99 others); Sun, 22 Aug 2021 11:00:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233549AbhHVPAe (ORCPT ); Sun, 22 Aug 2021 11:00:34 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AED97C061756 for ; Sun, 22 Aug 2021 07:59:53 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id t42so10532332pfg.12 for ; Sun, 22 Aug 2021 07:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=KtofH/4/jYATs6D3O66/kVjkwIShCI9KUHIKJ5lCCCQ=; b=FppxUShmrv1HQlTx8QZBDRPqLPtReo25tMpskXxjbrkbA4X1jxuCaO4f6Jneu1lUsJ IdYXunWAD0daSap4XsBjkgT8GoEdcyeMjXonPXUCMkqLZSMPeXhVWatiGgJuHPEPX8P9 7uxra4LgX1SoqkAwok8hnkAnjgVqNbQocISMg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=KtofH/4/jYATs6D3O66/kVjkwIShCI9KUHIKJ5lCCCQ=; b=XjDD94VbRRuJZ7ZwElqemWNJRyKNqfx+CfQG70c/Ht9rkVHfUx/w7wdjVg8AnWij9t yqNZbt+RbNo+wkAb7sV7+i0qa8y+YZCuYWZJRxlu1qvoZJdTy36LRDnIL9be/sAZ/bMz By+5lXfObyE5RniTEOdkvs7XE2gZ1T4bcM3oT3SvKKgIb8cCTtaPr6qSaHT+jKc+m0Fz ShTLIKT5gnlL/ezxmQvPnzJseLtbKDIXx5oaVil9eScQw1yx7pzcM7kO7hVamlxWLk6Q NoihoHcfB2dUhCkpQHfa06uR9Tt2FmF37O+7Qfz/PLnU/RZhKao/7xuU6kev1jZOg5HC QUJg== X-Gm-Message-State: AOAM532uUQw2Wu/2JQkUXsuu+0wx9VWETDsSDvto26DjnmjR9TUw/n6p cPWnOs0bW/tHfQH4/Ltp8mgJ7A== X-Received: by 2002:a63:1247:: with SMTP id 7mr27588364pgs.366.1629644393320; Sun, 22 Aug 2021 07:59:53 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id t18sm12779320pfg.111.2021.08.22.07.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Aug 2021 07:59:52 -0700 (PDT) Date: Sun, 22 Aug 2021 07:59:51 -0700 From: Kees Cook To: Len Baker Cc: Greg Kroah-Hartman , Michael Straube , Lee Jones , linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 2/2] staging/rtl8192u: Prefer kcalloc over open coded arithmetic Message-ID: <202108220751.4E6ADBA@keescook> References: <20210822142820.5109-1-len.baker@gmx.com> <20210822142820.5109-3-len.baker@gmx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210822142820.5109-3-len.baker@gmx.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Aug 22, 2021 at 04:28:20PM +0200, Len Baker wrote: > Dynamic size calculations (especially multiplication) should not be > performed in memory allocator (or similar) function arguments due to the > risk of them overflowing. This could lead to values wrapping around and > a smaller allocation being made than the caller was expecting. Using > those allocations could lead to linear overflows of heap memory and > other misbehaviors. > > So, use the purpose specific kcalloc() function instead of the argument > size * count in the kzalloc() function. It might be useful to reference the documentation on why this change is desired: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments Here and in the docs, though, it's probably worth noting that these aren't actually dynamic sizes: both sides of the multiplication are constant values. I still think it's best to refactor these anyway, just to keep the open-coded math idiom out of code, though. Also, have you looked at Coccinelle at all? I have a hideous pile of rules that try to find these instances, but it really needs improvement: https://github.com/kees/coccinelle-linux-allocator-overflow/tree/trunk/array_size Reviewed-by: Kees Cook > > Signed-off-by: Len Baker > --- > drivers/staging/rtl8192u/r819xU_phy.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/staging/rtl8192u/r819xU_phy.c b/drivers/staging/rtl8192u/r819xU_phy.c > index ff6fe2ee3349..97f4d89500ae 100644 > --- a/drivers/staging/rtl8192u/r819xU_phy.c > +++ b/drivers/staging/rtl8192u/r819xU_phy.c > @@ -1195,17 +1195,17 @@ static u8 rtl8192_phy_SwChnlStepByStep(struct net_device *dev, u8 channel, > u8 e_rfpath; > bool ret; > > - pre_cmd = kzalloc(sizeof(*pre_cmd) * MAX_PRECMD_CNT, GFP_KERNEL); > + pre_cmd = kcalloc(MAX_PRECMD_CNT, sizeof(*pre_cmd), GFP_KERNEL); > if (!pre_cmd) > return false; > > - post_cmd = kzalloc(sizeof(*post_cmd) * MAX_POSTCMD_CNT, GFP_KERNEL); > + post_cmd = kcalloc(MAX_POSTCMD_CNT, sizeof(*post_cmd), GFP_KERNEL); > if (!post_cmd) { > kfree(pre_cmd); > return false; > } > > - rf_cmd = kzalloc(sizeof(*rf_cmd) * MAX_RFDEPENDCMD_CNT, GFP_KERNEL); > + rf_cmd = kcalloc(MAX_RFDEPENDCMD_CNT, sizeof(*rf_cmd), GFP_KERNEL); > if (!rf_cmd) { > kfree(pre_cmd); > kfree(post_cmd); > -- > 2.25.1 > -- Kees Cook