From patchwork Sat Apr 6 14:23:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13619851 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03olkn2036.outbound.protection.outlook.com [40.92.58.36]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 958B53A26E; Sat, 6 Apr 2024 15:04:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.58.36 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712415852; cv=fail; b=MsWZzGa+733lplkntNpEregfKXlxhim4s29SmEAmmcz+Vt4WjHYjBzOBlhJ6O4pMD3ynuskMUmKr4eWbGfwTWU2oTwyY1b/8XRUUdFcxH8ByHClHqJR0P8+6VF+WpCLCoAMrIPeTqx+0hu5TRJmedhG6DH9zdr6W4pEGi3S5OoY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712415852; c=relaxed/simple; bh=udZQWB1iHSON6Jy81Rcxx1SaFbiMLHb7BiHTM1xVLi4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=B6OVISqp9NpTIEgP11ul7i4FAY//npie8EK/PZfwUGTccR8+m1tp16z91kvhagSYi3HkhAjXKB53rS1HLqZGkOKzUIe+n1vuGfofrmvizDZtP1ZrqQGrwOeBzs98CG4E/ExpQMfzrMJjayL55rvbHkzYYxEChPOR/B2LAM7dLJI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=E2K1vBBB; arc=fail smtp.client-ip=40.92.58.36 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="E2K1vBBB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aHGH1Br6HzWSxEdHWI92YrBuQVyfY/Je698qMyH480P+O7i51z/tIWtJYGHtTQOzj9T6+iBVuZBSd+dQC0y2eGXNc0ceRARHU9V/aeHCFixkzTQ4Zl9LTKTn4VnK8joHZCY0lXgiBpNnFLYDZYbxy9ICC2Eu23+25jwZIDZTJw/JpSS88g4yw7v02BviUKFLfEonHJqJXUahYamznz5qlAo8/gDYQ/wxyQSQoqBxvYRU8WdJIIDEaGyCuDSkTb4dizrwVq7rUAVCQChws4gJszvlYW2+j9/OpTVPv/uDio3+AfftH9Ji00tfFAURb9L/yj8CSqO6ETuNxUyUXrIj9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZcUvGjwt8FmHE1+S5Uzrvv3iKz9VISom3sZL+jmw/TE=; b=X8J6md71OA8+7KJiocvEMszohe1C+NCOYxX9vsilNFvCNb52emXBinTwQucpVUrHwo8wDp+rZqEqhdvSBrOa7KeRfso24oxbZpO8vPZNWi7VoN6vMA97kcCgDP0O6B1u3nkCs+b2PNxbRw79BxEw/IxOo5QX4YCjupCc2LHF90kJOqg42yfxVfMbi3VqFXibwfbekUFl1OH6uwxotObxbgJJKFT+iOBvbbtR4vyDrVXRSppPmo9J038kUmvR2O7ctGiWuRf2ru5zS5JTK7jH1blc7uwYwns3bKEFbKGmwo9/OHxS0yPXCkNbSjkMU0fKLNb+pHM7IZdqfE/S5XM1EQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZcUvGjwt8FmHE1+S5Uzrvv3iKz9VISom3sZL+jmw/TE=; b=E2K1vBBBPPVJluTL6dqq/enfmyjAvYUQ1Vz8X7fuCSioo65EJ1P0lIjb29JobjLUHw7gQKsXmfl/1Xbw6LBbroIp9fk1FxQbBFEAFr42UdG3AHMy/owEMExDttLBFRDz7Oj+3yg3sZyPANVNOt0BVgXixxDMBCufEH13yBZCzRGtA7SZrB5IgNpYDpYa582J+HBVdoYUMHSQXRnVNw2QKZoYiH+k/yyCX5/b59Cxa5YOF3jsR4dIZEIw/ZDkmwD5WPVJ2dd2Icvy7x9kZH3fqMW5iV0VAPUSjiRmcXz7Fp8MqBFvVRM4gse9HHpb10YlMwAAgDrlF26gjBQsxIJblQ== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by AM9PR02MB7266.eurprd02.prod.outlook.com (2603:10a6:20b:3e3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Sat, 6 Apr 2024 15:04:07 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486%4]) with mapi id 15.20.7409.049; Sat, 6 Apr 2024 15:04:07 +0000 From: Erick Archer To: 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 , Justin Stitt , Jason Gunthorpe , Leon Romanovsky , Shradha Gupta , Konstantin Taranov Cc: Erick Archer , 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: [PATCH v3 1/3] net: mana: Add flex array to struct mana_cfg_rx_steer_req_v2 Date: Sat, 6 Apr 2024 16:23:35 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240406142337.16241-1-erick.archer@outlook.com> References: <20240406142337.16241-1-erick.archer@outlook.com> X-TMN: [cTg0LrlH6ACousoX0jF5CgRxh1dEokR/] X-ClientProxiedBy: MA3P292CA0012.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250:2c::16) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240406142337.16241-2-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|AM9PR02MB7266:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f12e0ca-08f7-4b5f-25c6-08dc564acd3e X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: j/4aTcYp7k7nikUlGYBpBZx1qLMZpfQhqIRP1EC78ndgUZuRwUt4e4U5oMly93ETM2h7N+E+ae3tXEFt3qCsB/+ChSP9XfNGxjCah6/6W0q4lHL/J/9oyeK/xKwajwhaocqm3ApC+A8Dq9f9ZyE+1GPbGSKVKRWythvKO9lkjDq6wb7JMK4zmNo2H2vDFTOYiOKqXHw/ukbvAfqWhXxn3gsT7v23t4+s+5XXLWhHuroWW0M0P1REtVWyDjidIm+cl1wDFhuURMq3RJeO7KYE5sfbv4SLAkPblvWszNxRPlvWfMZG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W7fwSHzrS7fn/aveszDxUi508zz9NpOhHqfmPjEc1Q/z9+yji9MQcUuuPT9sazshCiqQgK7c21KcWO8/0xsHJOvXHIVfCp+OjagDKPqk84K9VBatSUv2E7aCV2RCMGl7h1lIcqacEpopoX0hhpSoSDGChVhlUaL2pxcbODJY1YkzAv9NxlqaBt3gk42/hP0FoygAPiLNMIycEqy4Raj1G2n9k9JI+Zm7u59G0j74ExKWFpBo2+dWrqgaxfXRzPE6TTAPTCv4BCTsh5VSxUCh04gT+c5GaXZxwBKGTwWuKenFJZC+9qJUlnMjo4uCXvpuXSGtKjqxdM4UaOfvpwq8pgTcdk7splTudf4ya2JaBIoCfej/zghSpKoTUsQAKHNpFlK+b3B1yRCeoDPYOkVK7bW1MGxcJrsz2XzwtZ1I5y4/2+V8JbuV6THSVCaB+63QJBGV0GzqI9yCw9BQAGicsWtUNK5tFCr2HoN/pIwJeCis3rlCtSTgUdsnihCWvaip8NJzbL0lo8T9lOYdEEPRfNNGoLRqkT1LVdhb2gnFjCFeMTVwsSLDeW8XVl1WO+6M73iUHQ/KwO8Ny5s062A9o2qfqHxrTRzMv7VZb5YSAO7g3X50XhfqL2dogA7CgCVMHmjimgxLvtoJHlXknI3QAc8qc9RMh1HtdW014SDWFocyBBJX3NIA+ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f12e0ca-08f7-4b5f-25c6-08dc564acd3e X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2024 15:04:06.9211 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR02MB7266 The "struct mana_cfg_rx_steer_req_v2" uses a dynamically sized set of trailing elements. Specifically, it uses a "mana_handle_t" array. So, use the preferred way in the kernel declaring a flexible array [1]. At the same time, prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). This is a previous step to refactor the two consumers of this structure. drivers/infiniband/hw/mana/qp.c drivers/net/ethernet/microsoft/mana/mana_en.c The ultimate goal is to avoid the open-coded arithmetic in the memory allocator functions [2] using the "struct_size" macro. Link: https://www.kernel.org/doc/html/next/process/deprecated.html#zero-length-and-one-element-arrays [1] Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [2] Signed-off-by: Erick Archer Reviewed-by: Long Li Reviewed-by: Justin Stitt --- include/net/mana/mana.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/net/mana/mana.h b/include/net/mana/mana.h index 4eeedf14711b..561f6719fb4e 100644 --- a/include/net/mana/mana.h +++ b/include/net/mana/mana.h @@ -670,6 +670,7 @@ struct mana_cfg_rx_steer_req_v2 { u8 hashkey[MANA_HASH_KEY_SIZE]; u8 cqe_coalescing_enable; u8 reserved2[7]; + mana_handle_t indir_tab[] __counted_by(num_indir_entries); }; /* HW DATA */ struct mana_cfg_rx_steer_resp { From patchwork Sat Apr 6 14:23:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13619858 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2028.outbound.protection.outlook.com [40.92.74.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7C7E33080; Sat, 6 Apr 2024 15:44:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.74.28 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712418259; cv=fail; b=kObzVkVcKP5tsrxFFjY0vbhNXHMH5S5lKw2HZEnzatfDhS7UjLdRoE2mIxhHlBLKiC1OK6VVdsLA6+LOFfigvX9dbTB9q1SvPcCbr7IUYnJ6p+LmeFYd1fbF+nL4AbsGk09fIRwZUmu9g3RjQA2BcsclCR738YD9DKZ9Lqfeh5Y= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712418259; c=relaxed/simple; bh=muAATVnktKmhuQXw0M3TndzIes0X+Ysq/z8ox9UmZAU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=o2A0qGs57oQhAarMy9OC/VWH88zUvaxG9F0L8KmdDvOAWMwNgdKax193Y3ed+Ec/vKcnx8WIxMUXCY4feZk4pceAwg7JTjD2uQzVDdE30Ao+kqUJNYsGbkoQcrxo3xmGo18GiE4p8+4pBH40ymPEONa6ZJ/+bqoJV9OYNn4nOAo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=ttGGsH4q; arc=fail smtp.client-ip=40.92.74.28 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="ttGGsH4q" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lhxvRcvm5cQfeRt6nCakd/xn4c1dbbuzC/tmml59UmnmeIUG3lTmSoRCKgt2q/vKdKEFa+dCoKWEoe8mFIZDAOvJtXWQ5z140YhfKabhgADfwgUUxfKXD/k6I/AsY5D3w396E2mvzMnqNfaJrwrm4l2SI94dhAEC5iymrutTG9KRF1vWp8oik3YJgXdc2NLv9rI1AUlYxNTrfF3NNn4tGokdvPPLrzeILVzrv0airXqpQAUnovUNG6hnns9zRM8bKgtSUP15LvbcHD/c0GmxGcFr8ENbZ2MMKZefyI1LnBzb6nUX+C/toH5EKyRF1kJw0VX4joaqSKg3Mwk4OGeEQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=eOzBCswJvl8184g//x7v9ru060DdT9pSGglnMTKJ1Kc=; b=ly7lluf/WNo5/tw9q0naet88N0MG3V88k7ehD2A+G+PG2m6FwkhUFHbztpMaoJHtHc+hB9zWZJeQVn4MVrtUHT0byzga1syoruC401n1oXctWdPwzTZGaaWgn6antxs474jk4n1gQ5phVKyKsrpU+3nsogQEoFutQlt2n68mY+G27v61nve7lto3DogYaqE2Key8PPfxfhqkqb57Ny9122yP/uwZPvtdzNQNUOivnkkS/OcnViGjHokZJKzTDCM8tMRRY8FFjQUVtbXdmAiMnAso0C/o2W4e4Wxn1VMpdag1orCXObPNKa9FOi7evGRFQ9dgQSvqkRsVBAp2GHDiqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=eOzBCswJvl8184g//x7v9ru060DdT9pSGglnMTKJ1Kc=; b=ttGGsH4qUa3E3a4qV52cUAM9l2Ae2OYVLzwW7/vyN01dwpZxrVeMGoRJXTjTo3/n+CFyFsUo7JI+s6H4XvPKR2AG7lxMesNM5HsUPn3jp/sGuld6ICXmj7Bb4qn2XZ8bpM0UoO7Xyxw+KyAOR5VJ5u8XKe8jqSDJTVKuD3N4kUoprVCiOq378Z2cGzArrigM92SMZsFeXcM5JDJCE8KcVzvmYwfpqgAnCVJlPr6l8S4Za60WUCIyqG/6uZjr1CacOL/2s1CNq27lkqr921sjjn0SYCKx6SYyGRY0PeAiagQFvsBhe4QTgt1DGogiztqoE6p8nESfu6s0kqUZW4HKSg== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by DB9PR02MB9777.eurprd02.prod.outlook.com (2603:10a6:10:459::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Sat, 6 Apr 2024 15:44:15 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486%4]) with mapi id 15.20.7409.049; Sat, 6 Apr 2024 15:44:14 +0000 From: Erick Archer To: 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 , Justin Stitt , Jason Gunthorpe , Leon Romanovsky , Shradha Gupta , Konstantin Taranov Cc: Erick Archer , 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: [PATCH v3 2/3] RDMA/mana_ib: Prefer struct_size over open coded arithmetic Date: Sat, 6 Apr 2024 16:23:36 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240406142337.16241-1-erick.archer@outlook.com> References: <20240406142337.16241-1-erick.archer@outlook.com> X-TMN: [zNZV2QuriLTH4cBB6X+2iUPSwKagj/JV] X-ClientProxiedBy: MA2P292CA0029.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::19) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240406142337.16241-3-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|DB9PR02MB9777:EE_ X-MS-Office365-Filtering-Correlation-Id: 1de0c22b-33d4-40e4-d44a-08dc5650692c X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Cl9NkR8WkPL4ovw/RK971JOxEbBLk8MHksjo/92/k0mOgYfgKMqFiQhtsx6pxB7y+lsRoQRr5eJ8+2fE3qhQknx6mFLhx23ypF1ziwcdbS1RpRTY4sUnaR4ImzENUhm3U069CRUNgeBhFGCXpfze8LFvCx/yglMwDXlOYV3SJpPXifVZf3UA/NGueXcOkGPnvs4CPc+/7xMQCzzIgUiPcoDafhMFddd48Ed690VSVsaSq4zi3Ga+tffiwGehKmYsxMC/erO7UZsMYuHSQqYxOBCX3fahopvl26yDeZAR98R6f2AH0duSDByjjkhG4Irdx0k0gMsKGahDXFc4ss7bOTxm7qK59Vfa8SMIlRCzVZZPDVlqNcZMqLk4kV2VJ9Essikr8cQApM9t1SFuiTRWIfWsvQpMDe/pjqRSz+T/k6RwEzccGKobLX4EZgNGomaYcZV/AioIKuwBEgJpEkKrhDgG6RhCeoX/FfrAuHgT3mF/x68unfaE+YdSM/5HkCKzJkwW2zdTnKg/SDPgUrcO+Onl0aqE5CEuFwopjZm6wVAVPZlrAKR05bYjEeKZFvO1JrWBH8h1o6gFLxUTleRDf5zdbl1WvzEm4yR9DFVeqwCtDrOWmIIOvlt3x/dPGv3rysxz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sGg/SKSXmKB9+UgTxihDh8sYj1tliPu3kkwAelQsbWdifleaFCvrjZZluxSUCsHNkFTnBjLc+t7AyPCxsiXe5Ax5tHTsvtQ1sCWZHZyHo6YoW6EagI26KbjMJWh3rbB2CVOcJLhAuaGm2nRfDA1I97whRK+9IL0NCSOI6wVKBfIMj2OM/aKjm1dIwzLNYuWf6ppvIZXrg7lhlBeEU/jN1rIoHIzclmvqJbpTg8gPTWRuwHSPv6xnQRp+kL6fucqDesGwI34zKOV9wWeZaDzBpnxVgrhhRPjUFZ/l5c24D2AxqJb9vj2k0JcybYYyJjTAWwl6ENu0YOEF5m/v6+f9F2WK5OaLSiBiD X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1de0c22b-33d4-40e4-d44a-08dc5650692c X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2024 15:44:14.7499 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR02MB9777 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. 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: Long Li Reviewed-by: Justin Stitt --- drivers/infiniband/hw/mana/qp.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/infiniband/hw/mana/qp.c b/drivers/infiniband/hw/mana/qp.c index ef0a6dc664d0..4cd8f8afe80d 100644 --- a/drivers/infiniband/hw/mana/qp.c +++ b/drivers/infiniband/hw/mana/qp.c @@ -15,15 +15,13 @@ static int mana_ib_cfg_vport_steering(struct mana_ib_dev *dev, struct mana_port_context *mpc = netdev_priv(ndev); struct mana_cfg_rx_steer_req_v2 *req; struct mana_cfg_rx_steer_resp resp = {}; - mana_handle_t *req_indir_tab; struct gdma_context *gc; u32 req_buf_size; int i, err; gc = mdev_to_gc(dev); - req_buf_size = - sizeof(*req) + sizeof(mana_handle_t) * MANA_INDIRECT_TABLE_SIZE; + req_buf_size = struct_size(req, indir_tab, MANA_INDIRECT_TABLE_SIZE); req = kzalloc(req_buf_size, GFP_KERNEL); if (!req) return -ENOMEM; @@ -44,20 +42,20 @@ static int mana_ib_cfg_vport_steering(struct mana_ib_dev *dev, req->rss_enable = true; req->num_indir_entries = MANA_INDIRECT_TABLE_SIZE; - req->indir_tab_offset = sizeof(*req); + req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2, + indir_tab); req->update_indir_tab = true; req->cqe_coalescing_enable = 1; - req_indir_tab = (mana_handle_t *)(req + 1); /* The ind table passed to the hardware must have * MANA_INDIRECT_TABLE_SIZE entries. Adjust the verb * ind_table to MANA_INDIRECT_TABLE_SIZE if required */ ibdev_dbg(&dev->ib_dev, "ind table size %u\n", 1 << log_ind_tbl_size); for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) { - req_indir_tab[i] = ind_table[i % (1 << log_ind_tbl_size)]; + req->indir_tab[i] = ind_table[i % (1 << log_ind_tbl_size)]; ibdev_dbg(&dev->ib_dev, "index %u handle 0x%llx\n", i, - req_indir_tab[i]); + req->indir_tab[i]); } req->update_hashkey = true; From patchwork Sat Apr 6 14:23:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13619869 Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2015.outbound.protection.outlook.com [40.92.90.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BA153C087; Sat, 6 Apr 2024 16:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712420668; cv=fail; b=TFia7+9dvLY/yDa2TRH53b8Lc6InsecpafUMVkRzcMuU5D++FI9NeFVv6CTWQrOJ6UZHBhPaqwkoAZ6BTTCFaFU1Vx7k/zmWOeaTI7YCIXnJDVkWbzG4VugLw3Q1TQpw3DWsdx7FH6ivzfjbQg/R7FKBF+a1mJw4+uLC0XjUYa0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712420668; c=relaxed/simple; bh=em0KuTuS5v17wCYbmf3zMkFaHtwR1H6btaoNK7MFnqo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=f7jjqAf7sNuUx4n2YstigdiNAHTq4IFHZlSgtP0/sykhaHAT0D/tBsXp0DAK+ZcxT5G3wBR5OJEP3yfX5rqXF4I32S16sLPQtONpJOsRBMCriPwQhoxCO7IgYhtSid3IoovD5pErG4ifZDtpzUXQDwpoR83VFycRz+NjBGF1Gf4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=MsGYFUNX; arc=fail smtp.client-ip=40.92.90.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="MsGYFUNX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DOAuPUI3GCFvdWS02y8lYX+Btl759fFtLz9XBkXpU6nc1U7x4A2adtJgW2HMOj6BmYHdZ/zzmjtEl/FaMwMYCSA7QNkx4qQYQpBO31bgo/zlpMBn4Iqb1oJxseHBIqioaHkBG4axX1fjF6cbpxlStIy7v6jKr/Qd4JDXA8pE+nV3ArBUVj8M97JU4ZOti2jnnCY/jLsDxBykGE54zkAZcVOVi6M8MvDl5QmDyIsN1MsORsUKgeMmSDB3VZmHi/F9mPPHW39Q/RSpn5Xl6/1G3R5Mo6p79EWsM+gASwIave6BCJ3BmXhawWTeTzHqRN9xyHciWGcEhS1kRtNB/DfPjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=78EOzeiJpMwIvqdyeb+jNXi711WZpDuzvqqWUG13Uhw=; b=YlR+qatxpUXpOsmAAdM8iRuaVnp1mFJ1HjV5gfLL3gzsScaI0zkykG4bEDieel81IvxdTNUkid3KcrCfPaxi+HU1eultff35hBflNmgsMg4tHE94zhKoW/rePopxiazz4XCeCoHM9t91UArZ/1pj46LzPfyu45DMgtKhj3/RSuAb05GGS1l9BvOX6si3n1NbQEhztyQ6h7weXuoofzzl5gx74WdYCxnpoX4SXxPBzNCWmsmmZ9CbaL91YK+k+QarikG4TjYfr9I929p5gjaD5biAduQesJeaLBNkTDBRyU5JnE4IUvAzZVbVyRODHVUS9QRRzFv/40ySnZ+knlpGiw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=78EOzeiJpMwIvqdyeb+jNXi711WZpDuzvqqWUG13Uhw=; b=MsGYFUNX9o4ieQ4Qn3OmFt9qnPNd4LxXPzY3+VmYbMiZnQvSGOZu2rQ+Us8GvaETy+RsZ54Do9ZLf6hhfJWwp/uGUArQxv3qG80QLVY4DD0xtV+myToh+uX6aaJxqc243qe44qXb5d7MXTFthsf49k22qOgez7XbU3nZPkIgN5R56E0WnH5LuvG3nCJ2v4e2JwoxvEsoY/1sRcrzWEyLNrxSGb2sshSKEEb/2HdzDdScDxQ6INBs+OoKMSKjdRf2iizhoaHD6hX93eq578gvz/gr5xyGxpObD2t82yaWLBmQ7KghN/WZ+Xbmq3JQp7gLHxx7uI/q1eQbdXR/kFE1Qg== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by DU0PR02MB9121.eurprd02.prod.outlook.com (2603:10a6:10:464::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Sat, 6 Apr 2024 16:24:22 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::9817:eaf5:e2a7:e486%4]) with mapi id 15.20.7409.049; Sat, 6 Apr 2024 16:24:22 +0000 From: Erick Archer To: 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 , Justin Stitt , Jason Gunthorpe , Leon Romanovsky , Shradha Gupta , Konstantin Taranov Cc: Erick Archer , 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: [PATCH v3 3/3] net: mana: Avoid open coded arithmetic Date: Sat, 6 Apr 2024 16:23:37 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240406142337.16241-1-erick.archer@outlook.com> References: <20240406142337.16241-1-erick.archer@outlook.com> X-TMN: [IjmoCuAyloIqhvoUYW9pNGyBpZ9aP3ZY] X-ClientProxiedBy: MA4P292CA0011.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250:2d::17) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240406142337.16241-4-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|DU0PR02MB9121:EE_ X-MS-Office365-Filtering-Correlation-Id: a83463e9-01d5-437c-88e7-08dc5656044e X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fmAByG4ma+dK8Rj55/hFD4vso/r9uC/2Bz1PS6+SQo77Bb0CnEgVXzR12W+2H2TMK6OvXWfMjf8t9RP0fuPXr8YDA1zzxjiP63yQ18gfRkf7GCSjyBoSUu4z8mCD6JxYudlL0ORmsJ8a27z3bsJxm1LPYfzv3Fhxgz1j1nY1tgQo7GdfDeksB0X4GeoJd+gNV/TU4XRZAZZNdq3BAMNhI+oHf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+bpIJfJzrw0LwR0HClZlc5IDKWvmLOExfF9y4JKQu6sAex7QoVZUGekG4wBtyVTFx0PnT3/gAecEE7dY8620K6CABFM7h5AyLogYw/K1g3S1ZgeuviP4Wg8LFAcAIWILZW/6i111xgtvqGXM4in4FoOG3nheijWC2unlrWfJGXgajODKjR4lfzAk7Sdj2/RTNvq+cx9HBTe7jtMOM59N3oiWrgQYDD/DDxPbWkLsmS1ePvbjpEWY0BgbwsdEhDjVmoi4V+cdB7cSDHye+kD6uUl4U47p5Pgc/ivUWRkaeunOCD6L9Qh/ulScSza5LorKy/Fk7ZQULG+LoTOnq5VLnO+VCIZuNz0CkV+Aje//Z6/zu/+CgpXjIJQUxQV/ZPZMU30qf/3Trmlr36jnDV7V9yU8Jtonv+69USUMU4+azbHKKb2Xvbjl4gaHLf80wwCjq9TJaXp0w3bvcMxsYXcw83Z1frjZ+7CVV4xVMqDRdiO5u/h/s41jmILwhRouySm6qrZsJ3akfrSW6hzs2t21fN04u1ZwTFnjs+lONtvLqEY1uj1WCyiVJr8eq+20UXfFIiopgkbYqFv4L3QFfL7xtuBaKNSNS9PsrYR12hqrnMDr6tkv4Qy8Z+ucYM8OAqkFIFMIPjfTm5iqkE1nLAWun9PBjwrA8ZA5WzCozx39uzDit2WWOb1Z/rzIxy9GZVEigJ8RvR6odiv8/uRyVyun0JY4UJBVyX3BIfFo6MsZGgLKiN3BzK5V9TfQ8exc/9kawPV6I8vrX/j7Z4bA X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a83463e9-01d5-437c-88e7-08dc5656044e X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2024 16:24:22.1362 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR02MB9121 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));