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 X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB6D6C433F5 for ; Tue, 21 Sep 2021 04:06:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D0C9660F70 for ; Tue, 21 Sep 2021 04:06:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230318AbhIUEIM (ORCPT ); Tue, 21 Sep 2021 00:08:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbhIUEIF (ORCPT ); Tue, 21 Sep 2021 00:08:05 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BDD8C061574 for ; Mon, 20 Sep 2021 21:06:37 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id c4so12478004pls.6 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=Lx4R3fMTc3WL4EoZjj4eIzG8tg3Hu6WzFokEMx6BNY9p71Lhz7IhErakMxXsOqfww1 DeNxfjF+wWm/A6KxUwPpTaQk0fTVW1faDoNwuY8gmDwhWxYRxWwcG70fHbyN7OT77CY/ 2UYq6YqTdeLfeOKfZByU+De5Nn3iVHWL0682mlf8jQQ08V7NsjdEKxYfwGQu2ItXtebj kTzoxr58magAvMaUD1SuaLSwMCSaq20MoZ5rSEUL+5xzxqTZ5G+WWhFD8CZ2nUvxA0Ws HUkWPAFRpp5chHL6SIAe5M7yzADjYAE+hh1M1PX/rA8+D8NUU2mFJKOCy58J+mRp2s7s suyg== X-Gm-Message-State: AOAM5338ANdLyGqGmOazyU0HWTjRkTwyLZmyGGnxk8cBga8i/XSybaC+ 1mWfr/VlZM+Lbvq7S0fSZ9zhQg== X-Google-Smtp-Source: ABdhPJyx5YX9MminKBo66Os9ZmhaP/fJEgBHFu0WDHKC3YgKNy061CfxIN6SG+aQUmV8dm/D3dlZQg== 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-rdma@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