All of lore.kernel.org
 help / color / mirror / Atom feed
From: Saeed Mahameed <saeed@kernel.org>
To: "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org, Len Baker <len.baker@gmx.com>,
	Saeed Mahameed <saeedm@nvidia.com>
Subject: [net-next 09/13] net/mlx5: DR, Prefer kcalloc over open coded arithmetic
Date: Fri, 15 Oct 2021 17:38:58 -0700	[thread overview]
Message-ID: <20211016003902.57116-10-saeed@kernel.org> (raw)
In-Reply-To: <20211016003902.57116-1-saeed@kernel.org>

From: Len Baker <len.baker@gmx.com>

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 <len.baker@gmx.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
 .../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 50630112c8ff..07936841ce99 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_action.c
@@ -854,6 +854,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;
 
@@ -862,11 +863,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);
 	if (!ref_actions)
 		goto free_hw_dests;
 
-- 
2.31.1


  parent reply	other threads:[~2021-10-16  0:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-16  0:38 [pull request][net-next 00/13] mlx5 updates 2021-10-15 Saeed Mahameed
2021-10-16  0:38 ` [net-next 01/13] net/mlx5: Add layout to support default timeouts register Saeed Mahameed
2021-10-16  8:00   ` patchwork-bot+netdevbpf
2021-10-16  0:38 ` [net-next 02/13] net/mlx5: Read timeout values from init segment Saeed Mahameed
2021-10-16  0:38 ` [net-next 03/13] net/mlx5: Read timeout values from DTOR Saeed Mahameed
2021-10-16  0:38 ` [net-next 04/13] net/mlx5: Bridge, provide flow source hints Saeed Mahameed
2021-10-16  0:38 ` [net-next 05/13] net/mlx5i: Enable Rx steering for IPoIB via ethtool Saeed Mahameed
2021-10-16  0:38 ` [net-next 06/13] net/mlx5: Disable roce at HCA level Saeed Mahameed
2021-10-16  0:38 ` [net-next 07/13] net/mlx5: CT: Fix missing cleanup of ct nat table on init failure Saeed Mahameed
2021-10-16  0:38 ` [net-next 08/13] net/mlx5e: Add extack msgs related to TC for better debug Saeed Mahameed
2021-10-16  0:38 ` Saeed Mahameed [this message]
2021-10-16  0:38 ` [net-next 10/13] net/mlx5: Check return status first when querying system_image_guid Saeed Mahameed
2021-10-16  0:39 ` [net-next 11/13] net/mlx5: Introduce new device index wrapper Saeed Mahameed
2021-10-16  0:39 ` [net-next 12/13] net/mlx5: Use native_port_num as 1st option of device index Saeed Mahameed
2021-10-16  0:39 ` [net-next 13/13] net/mlx5: Use system_image_guid to determine bonding Saeed Mahameed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211016003902.57116-10-saeed@kernel.org \
    --to=saeed@kernel.org \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=len.baker@gmx.com \
    --cc=netdev@vger.kernel.org \
    --cc=saeedm@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.