Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1237313pxb; Sun, 12 Sep 2021 12:10:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwF2Bw+ujjbwFzjQ6FywS4KSb6/VK1X5cuqHe00bT9k5wLtmnivh8mTqPeovn9mX2QDuxpk X-Received: by 2002:a50:c90b:: with SMTP id o11mr9297143edh.216.1631473810480; Sun, 12 Sep 2021 12:10:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631473810; cv=none; d=google.com; s=arc-20160816; b=B1M7bMv6RERjsvIJ3E8DU18ToJ29UmIZN1BnAusmJeOBiUhYct94VS5Aw73qY8gi7l C9VOo6Pj3tEACP260hkj2Kin+uIEs9KOp2nTCcew7FYxMWoCuKMWkYIzjJc/TM7BGe1l KtG/ALecbuEwps5BSqJ3uXNaiGVhsU9MfEthogbsDsoZx0bexVVMp6nR1lybvTrH07ga 6VpW6s+BGKaeMZqTvXcVKcbMYuwpZ3EMB2Ez+vs+TeKVLOGE807mf/tjcA6juCK/50hj DfLXAvse9FRqyPh0F8tXHGuci8KgI1UuJij/n57uiurEmq0VdLFHVJQ2FZMx9Jaqcclj Ilyg== 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=jSYK5XW1c4kx3ti1DlIIP2nf9sUDrPPZxqTtKs0Ocag=; b=u8WG1R3LMBSO8cltPMXoloCrwP6H8ryEDKoaqctBxWxcOvfCTsP9janE5wqxUJrEAQ THQ2TLdxy6vlpYX3p1680oziYom6felJXGcJrwGbkQgyFrOSjBjU8ebS5jtklYiyQxYc jAs64LvAhie4SfEc75KFiqAKiyUVQ+IpaHOZw2XLD2/TSgQorbFHVLv7eKxRSkwvGsnY VkmruDtiOQ3KIOenry4w2FOauUoDurfCWo90oZMzX48+T+dvnk3LtzA2FonAlTAP41vA c1Ui4m1MeXAgwO6Qqf7lrpOoaHcSNbT+SZW2A5IGzcCxE4N94n4bmEZY/nSOOSeGV5Bd lFGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=na6oNA+5; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bd2si4951299edb.304.2021.09.12.12.09.46; Sun, 12 Sep 2021 12:10:10 -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=@kernel.org header.s=k20201202 header.b=na6oNA+5; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235785AbhILTFh (ORCPT + 99 others); Sun, 12 Sep 2021 15:05:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:46920 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229732AbhILTFc (ORCPT ); Sun, 12 Sep 2021 15:05:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0143B6103D; Sun, 12 Sep 2021 19:04:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631473457; bh=1OUTp3LU5sr6YMJWSvBJzrKkQbU9FLlZnEdGkm0fnrs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=na6oNA+5KeCeP56MibSWsm5dbcv4wSrasnDCdAeKV2Cu1nTb62o9ajyfZvsAU/0DG Q5gihgijxbOiX1Brc2ELNNVvk8wEJ0QZc7RXI2wSweElVBi80FcTOql21JR1+bndMx nYPREKJI5ESgBKTDWz8RRD+a2bo9QweOaTyMne/hAzWgeqKHzjU/de4V3lBMQjRUDx WomjZ4B2cJ+2ovMwTw6uT/9gKHfxijxDq/ERYrva93bsh+umGXw9pnxGCbTrBhzsZo szMtPLoIRZdMz3QQ2eZ9snpSuuYEf6j2T3azvzY7/0+9qs0NawdVLvFubt+9Y3n2KG LHYmBhwUic14g== Date: Sun, 12 Sep 2021 14:07:58 -0500 From: "Gustavo A. R. Silva" To: Len Baker Cc: Simon Horman , Jakub Kicinski , "David S. Miller" , Kees Cook , oss-drivers@corigine.com, netdev@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] nfp: Prefer struct_size over open coded arithmetic Message-ID: <20210912190758.GA146608@embeddedor> References: <20210912131057.2285-1-len.baker@gmx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210912131057.2285-1-len.baker@gmx.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 12, 2021 at 03:10:57PM +0200, Len Baker wrote: > As noted in the "Deprecated Interfaces, Language Features, Attributes, > and Conventions" documentation [1], 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 struct_size() helper to do the arithmetic instead of the > argument "size + count * size" in the kzalloc() function. > > [1] https://www.kernel.org/doc/html/v5.14/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments > > Signed-off-by: Len Baker Reviewed-by: Gustavo A. R. Silva I'll take this in my -next tree. :) Thanks, Len. -- Gustavo > --- > drivers/net/ethernet/netronome/nfp/nfp_net_repr.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > > diff --git a/drivers/net/ethernet/netronome/nfp/nfp_net_repr.c b/drivers/net/ethernet/netronome/nfp/nfp_net_repr.c > index 3b8e675087de..369f6ae700c7 100644 > --- a/drivers/net/ethernet/netronome/nfp/nfp_net_repr.c > +++ b/drivers/net/ethernet/netronome/nfp/nfp_net_repr.c > @@ -499,8 +499,7 @@ struct nfp_reprs *nfp_reprs_alloc(unsigned int num_reprs) > { > struct nfp_reprs *reprs; > > - reprs = kzalloc(sizeof(*reprs) + > - num_reprs * sizeof(struct net_device *), GFP_KERNEL); > + reprs = kzalloc(struct_size(reprs, reprs, num_reprs), GFP_KERNEL); > if (!reprs) > return NULL; > reprs->num_reprs = num_reprs; > -- > 2.25.1 >