Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1619060lqe; Mon, 8 Apr 2024 14:45:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUAugEBjXrCUwhtdHLJXQKxrMcQmZ4c67VUuRmqZe1I+FZn1ecmg1Bz2MlrAyFnhb4vwjznIe9NDtOTsZ+/V6XkaXazfNfDy6ZkDyECkA== X-Google-Smtp-Source: AGHT+IF1oyOOYhVOQpFgMFHFFUD2uJAkTgXfkyyekTcA1qST5HKsCzDeD9GvGmOpc4zHKqqs+yon X-Received: by 2002:a05:620a:5647:b0:78d:695b:8b7c with SMTP id vw7-20020a05620a564700b0078d695b8b7cmr2467460qkn.51.1712612720616; Mon, 08 Apr 2024 14:45:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712612720; cv=pass; d=google.com; s=arc-20160816; b=XaPORqFIV3IbzIARd7kC0LKLcAdzOwsOqytRz65deXMTdmcD5+QiFfPKyJ91o7C/iC 35N/LawAC+tbrit6NWBuP70Ybb5AWWfXURIqajH6TXaY5GgbEfgEgKJ6mjsNb58n7sH8 yUvnqm+rvpOsl/HwOPFE4/J+s5WDepRcnTLrnsCwLOCV2HUt5n5A3cWj/BDQ857LP3i1 XYmZ9Va8E77S28lATHce/4aB38VW27rAuoO9vgId954VCYT6DpR9CGKLDM77dmE6bxZ3 DryZ7sAy41Pnu/1wXyqu0GCRotSdEgpdgsjiEOMsJh51Wl/EoOpBNxrwURCgjgDSoVdv u4KA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=fWZpTS9XdHFgXpHr6/MbkrB3un8w3RjB6kb0cWMEi/E=; fh=/lWyO5yQ6tBVaqV0ZfDA9efgtDxlVgMANiCjv0Joghk=; b=uJFi3ASxGWIFsMmWvgX4WqKOUPQXzhh4PXkQ65RKTUv489Yt7XjaSN/cUL2vLxFaAG zI3/MV0hQw/Ebi6xqrz0bKkQAs02Uetg7j6yaDxfPkIeKJNB1EKDUTNtTjZYu3VcPMZN mWDjsOc38G1DYVPs0XLz4s8S7KNDwJjfG7WZsHa1zgUmIe+LoByTPB2kQAFPmQo8ZoHX I7lGIF9G0nzfuAkbISeP5/c5X4uAtnmGmY7JSkdUFYe13/tLTi5zvh4rVRElhFaUWCAy sg79zL27VY/pK+/SRo9eEk9GeEa+/51x5giws3Iqu2oTbx53Ntjgjvfd4xDPCO8NFgNi MHpw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oCKAg4T5; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-135952-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135952-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id pe7-20020a05620a850700b0078be3bbc87fsi9149341qkn.422.2024.04.08.14.45.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 14:45:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135952-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oCKAg4T5; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-135952-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135952-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0B2091C21DF0 for ; Mon, 8 Apr 2024 21:45:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58CFA148FE3; Mon, 8 Apr 2024 21:45:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oCKAg4T5" Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4AFD1442F3 for ; Mon, 8 Apr 2024 21:45:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612711; cv=none; b=GwjXkR3gjE0TDbyz7OgNAHjsAsU9Ze8HAiQvkrq0wE/X/VeNjTH/4rC8J8VDWObvQY3aX839mee0rOfl7yGRBxaMOYpMnqVx8ksx6o3KcsotbQuvY24lj4mmwmMGGPtNiexU7nwq/HsnwXBVNm4GQLywgvX4AQ26kK2F3dgTgoE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612711; c=relaxed/simple; bh=X7881QJirmg5kgKlNpkQ8RqV6ValjW0F1QdmK3UujVM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fKXW95u6T7jzdVBx2GAC9Zvz15pgNfs+gH8VJltbD7RcGI6yzMJwGpAwBGB11HxdpKrmcavogZ7107zDViyzFoAd4ZMQ+HnGdWntcqlzUoZ+joornbLmHTCrVm830g0upMBxGBJC/AKJwHzbVX3PqZygRQj3O2J4AgREzfRn/v8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oCKAg4T5; arc=none smtp.client-ip=209.85.166.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-io1-f53.google.com with SMTP id ca18e2360f4ac-7c8d2e0e9cfso131890639f.1 for ; Mon, 08 Apr 2024 14:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712612709; x=1713217509; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=fWZpTS9XdHFgXpHr6/MbkrB3un8w3RjB6kb0cWMEi/E=; b=oCKAg4T5S6hImvAITr6uNGMtSyMLcpuU0HEv8kCQgByNXXAIKLV/86jkYvllx6VqM9 zo9+/144w1GgnwrURX+hm+jz8xcxfgdwH4lHJcvq7cAO2w9LCAqlKjiT6jVdq6jRn0Lj F+u0sO+gKOUw0vOnhGnm4RZksUsTb0X5lDenEIYpZEZSiIUsa3Fnstu2kr+4AwjhtMfE e9A6MTkGb45qQ3Ok7z1Tqp1su1ES6U/us9vj0G+/zc55+UJq1wDg2w4CRVA8088KElUq PwAvSxKGnyO5XyXGj4+K5IBGfX4ImKxsPUomMp+NSEtlRNO6n5fz/vRmsVBEHcwUoOIo dLMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712612709; x=1713217509; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fWZpTS9XdHFgXpHr6/MbkrB3un8w3RjB6kb0cWMEi/E=; b=p0j9P3nJD75M0jyuBy032OPyCwjEyXVOKjxxJBxmgeWPFBBMToTKwBcQrhbEuVIR7c tIhsghWrohxlBaRGY2YYrA0vOc37tKTuwJN3iA4pkfTTcawYbsriPdJjGPf3QhuDUFOj nPJp7nHz7d1+/I0HWdOigTDBTZmLtWH9xiPQYBSZEvPAmTiKGS53LG1tyw4lDrJ3Lnfg aSqcFIGsQ5ZZKOjZg11V1STLkoqmZ/97C3Z9feiMqatUdTAzDuwvk3kifrOFfOhY+8Hc V5ijvkLtsprGfTEwwkXw15PPqHQR+UFhU5mxnfOsQVqfwBfVyZbNmKXySjxt6CnlfAMH N6NA== X-Forwarded-Encrypted: i=1; AJvYcCWEkZEDDCGEO775dBmhoXRdRmTdGn33A1P4ry+/gyD+PL63KU70rNgvtCZHYbjCi1Me/REvYXNsd0D7/d3lnvc0bUqVXmpWbylxnfrM X-Gm-Message-State: AOJu0YzXUmxULwMVd0MfjpYRrtAhVLjo+rrlrkbUCo0DbPhajWz72sRS gTqAa7+WTSDRd18rjSJZ7ekpxjnCkcPmiKpAzzBps9IJ4dijfbktCC0yx11Bjw== X-Received: by 2002:a05:6602:47cc:b0:7d5:ea22:5a0 with SMTP id ea12-20020a05660247cc00b007d5ea2205a0mr4333622iob.9.1712612708751; Mon, 08 Apr 2024 14:45:08 -0700 (PDT) Received: from google.com (30.64.135.34.bc.googleusercontent.com. [34.135.64.30]) by smtp.gmail.com with ESMTPSA id s6-20020a056602168600b007d5e2b5803fsm1239469iow.17.2024.04.08.14.45.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 14:45:08 -0700 (PDT) Date: Mon, 8 Apr 2024 21:45:05 +0000 From: Justin Stitt To: Erick Archer Cc: Long Li , Ajay Sharma , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Jason Gunthorpe , Leon Romanovsky , Shradha Gupta , Konstantin Taranov , linux-rdma@vger.kernel.org, linux-hyperv@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH v3 3/3] net: mana: Avoid open coded arithmetic Message-ID: References: <20240406142337.16241-1-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Hi, On Sat, Apr 06, 2024 at 04:23:37PM +0200, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "req" variable is a pointer to "struct mana_cfg_rx_steer_req_v2" > and this structure ends in a flexible array: > > struct mana_cfg_rx_steer_req_v2 { > [...] > mana_handle_t indir_tab[] __counted_by(num_indir_entries); > }; > > the preferred way in the kernel is to use the struct_size() helper to > do the arithmetic instead of the calculation "size + size * count" in > the kzalloc() function. > > Moreover, use the "offsetof" helper to get the indirect table offset > instead of the "sizeof" operator and avoid the open-coded arithmetic in > pointers using the new flex member. This new structure member also allow > us to remove the "req_indir_tab" variable since it is no longer needed. > > Now, it is also possible to use the "flex_array_size" helper to compute > the size of these trailing elements in the "memcpy" function. > > This way, the code is more readable and safer. > > This code was detected with the help of Coccinelle, and audited and > modified manually. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/160 [2] > Signed-off-by: Erick Archer Reviewed-by: Justin Stitt > --- > drivers/net/ethernet/microsoft/mana/mana_en.c | 14 ++++++-------- > 1 file changed, 6 insertions(+), 8 deletions(-) > > diff --git a/drivers/net/ethernet/microsoft/mana/mana_en.c b/drivers/net/ethernet/microsoft/mana/mana_en.c > index d8af5e7e15b4..f2fae659bf3b 100644 > --- a/drivers/net/ethernet/microsoft/mana/mana_en.c > +++ b/drivers/net/ethernet/microsoft/mana/mana_en.c > @@ -1058,11 +1058,10 @@ static int mana_cfg_vport_steering(struct mana_port_context *apc, > struct mana_cfg_rx_steer_req_v2 *req; > struct mana_cfg_rx_steer_resp resp = {}; > struct net_device *ndev = apc->ndev; > - mana_handle_t *req_indir_tab; > u32 req_buf_size; > int err; > > - req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries; > + req_buf_size = struct_size(req, indir_tab, num_entries); > req = kzalloc(req_buf_size, GFP_KERNEL); > if (!req) > return -ENOMEM; > @@ -1074,7 +1073,8 @@ static int mana_cfg_vport_steering(struct mana_port_context *apc, > > req->vport = apc->port_handle; > req->num_indir_entries = num_entries; > - req->indir_tab_offset = sizeof(*req); > + req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2, > + indir_tab); > req->rx_enable = rx; > req->rss_enable = apc->rss_state; > req->update_default_rxobj = update_default_rxobj; > @@ -1086,11 +1086,9 @@ static int mana_cfg_vport_steering(struct mana_port_context *apc, > if (update_key) > memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE); > > - if (update_tab) { > - req_indir_tab = (mana_handle_t *)(req + 1); > - memcpy(req_indir_tab, apc->rxobj_table, > - req->num_indir_entries * sizeof(mana_handle_t)); > - } > + if (update_tab) > + memcpy(req->indir_tab, apc->rxobj_table, > + flex_array_size(req, indir_tab, req->num_indir_entries)); > > err = mana_send_request(apc->ac, req, req_buf_size, &resp, > sizeof(resp)); > -- > 2.25.1 > Thanks Justin