Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2393156pxb; Mon, 20 Sep 2021 21:12:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlDMl9mmmMmY7FuKLtWrqMN16samsooWU4ge5vd4BgANILAkTpey7YDhNwzkEouodyRjW7 X-Received: by 2002:a17:906:60c2:: with SMTP id f2mr31566729ejk.531.1632197544061; Mon, 20 Sep 2021 21:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632197544; cv=none; d=google.com; s=arc-20160816; b=re5Ds6BKJAXANE2kO75Bu3Xlgf2Ot9XJIE1sqFTzHQPiZQiVLLm5PL2vGnhHw3QDD5 eA9Hep31iYjZYaKlYuYTWFSbK/aFoMCONgRcM23EASCW3OF0JNZiXA6eDWjYpyr3KCPr WJrVlrTq8dWR1HsdzORPBOwSspEUhAehzaQAEtPrdZugpcVX435WRi0LiviAwjv3/yWO EPAColC/rRWDKc6I0CA9X5VCyAPFgmVfIsQhv23UnOEDe/VVULZ4pB6Okbxm380BWfkZ P3bxrk7Epw79B5sLfLNZW9D29BbVupckOsLAwhv7/whJk0Zw3lHOd4OYVG+rZyb342oD 7/1A== 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=zn1EkZZtYwu+/CqIaImEzdF2wzQ3/Q4ICvhMSwY9pc4=; b=PX8kGjkFJL/TClqu0HSb3bUaJJuKq1WGKVH8FhQeklwAJc4ewmGHOZozkCCijABUeh cIc8NuvNmwVjSf2trQZFO+fgPcTA8TpzsPoB9bZPYQtpCQyPytiGsh9Zj/fVofXduMb8 40iEflooWr5loAZFezM88VdlMzjyqy7Vk3wYAnsN/KYsGtU+jDRosau/pF95Eu6idcFg H5dpHC7dYeThODNcwe/LepejXgd0gZrdngeB1l7DPZiuue1YLmbWVPb7zZ6rljW+PFOc b4eZQKpJvpTayFqlQUSYCtrGuSHTeFEKh/zXZ3aZj0kiTKC8I1bSc/IWdsQ+uHNh7lnV blgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Mplo5+lx; 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 h17si16619966edb.543.2021.09.20.21.12.00; Mon, 20 Sep 2021 21:12:24 -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=Mplo5+lx; 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 S231356AbhIUEIN (ORCPT + 99 others); Tue, 21 Sep 2021 00:08:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230357AbhIUEIF (ORCPT ); Tue, 21 Sep 2021 00:08:05 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DF66C061575 for ; Mon, 20 Sep 2021 21:06:37 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id v2so12470397plp.8 for ; Mon, 20 Sep 2021 21:06:37 -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=zn1EkZZtYwu+/CqIaImEzdF2wzQ3/Q4ICvhMSwY9pc4=; b=Mplo5+lxH3SP68tQyWmFu/JWZyX9HRHbKiM7MH93oAMFkjf89geoL5h+W5avJnSX0u qupSiaeZZ7SpByvqSFmys6LAoCfR91AB5YSr4tEfj2gLJ1B9VV3Ol4hqfOgCbnoqgWBl N2eHgmNlgp50nHZcJKq7lnH7V8nTo4FmRu3Bo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=zn1EkZZtYwu+/CqIaImEzdF2wzQ3/Q4ICvhMSwY9pc4=; b=ZYIOa3T47tGoU4Tt4RlqH5Cxiz7sqV1fqHNNgkgeg8lCVFlrHv3FIplV1tkiZa6j/h DDvrMPzlUejFYUYW3GYQqGpQKPxaFpHbxRu1PQM2hf232d1HApXwma5gRbMkPEoZVNmY 9BANryUgOi1SWOLefrdteUcbvj4Len4I4G4PgZLsR1MxVWfE4KBiSvb/ZDfq5XW8ck9G h6lsE2cd5Sv+uG6IsMaAANeEgMLGnMu7HS1C3eMweDt7rVW+gKau8SgklY701qoeDZ2N osR/OD5KkLefSwIrIcGEIPfi1hMPVZuvmz1qdo5BGeOUYwQwesRQj3SohsYga14FJFXu Xq0Q== X-Gm-Message-State: AOAM532jl5lvgXAg+LbEqhLacYOHdLMVgFLxU06ttmvWKdVdYLueInhZ 2BRick0xNBGsYq6oW0xE3lX3XA== X-Received: by 2002:a17:90b:4a82:: with SMTP id lp2mr2861629pjb.57.1632197196917; Mon, 20 Sep 2021 21:06:36 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id v3sm15675641pfc.193.2021.09.20.21.06.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 21:06:36 -0700 (PDT) Date: Mon, 20 Sep 2021 21:06:35 -0700 From: Kees Cook To: Len Baker Cc: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Jakub Kicinski , Yevgeny Kliteynik , Alex Vesker , Erez Shitrit , Jianbo Liu , netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] net/mlx5: DR, Prefer kcalloc over open coded arithmetic Message-ID: <202109202105.9E901893@keescook> References: <20210905074936.15723-1-len.baker@gmx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210905074936.15723-1-len.baker@gmx.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 05, 2021 at 09:49:36AM +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, refactor the code a bit to use the purpose specific kcalloc() > function instead of the argument size * count 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 > --- > .../net/ethernet/mellanox/mlx5/core/steering/dr_action.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c > index 6475ba35cf6b..e8957dad3bb1 100644 > --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c > +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c > @@ -716,6 +716,7 @@ mlx5dr_action_create_mult_dest_tbl(struct mlx5dr_domain *dmn, > struct mlx5dr_action *action; > bool reformat_req = false; > u32 num_of_ref = 0; > + u32 ref_act_cnt; > int ret; > int i; > > @@ -724,11 +725,14 @@ mlx5dr_action_create_mult_dest_tbl(struct mlx5dr_domain *dmn, > return NULL; > } > > - hw_dests = kzalloc(sizeof(*hw_dests) * num_of_dests, GFP_KERNEL); > + hw_dests = kcalloc(num_of_dests, sizeof(*hw_dests), GFP_KERNEL); > if (!hw_dests) > return NULL; > > - ref_actions = kzalloc(sizeof(*ref_actions) * num_of_dests * 2, GFP_KERNEL); > + if (unlikely(check_mul_overflow(num_of_dests, 2u, &ref_act_cnt))) > + goto free_hw_dests; > + > + ref_actions = kcalloc(ref_act_cnt, sizeof(*ref_actions), GFP_KERNEL); In the future, consider array3_size(), but this is fine too. :) -Kees > if (!ref_actions) > goto free_hw_dests; > > -- > 2.25.1 > -- Kees Cook