From patchwork Mon Sep 11 23:21:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 138241 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp475565vqx; Tue, 12 Sep 2023 08:11:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJ19saDLRcxEVv3+seMuS6zN4R5xdOwN9PXTosFvi7LLN/Pjeo9B0B8gO4WrNt07oaCDSq X-Received: by 2002:aa7:8881:0:b0:68c:2be:67bb with SMTP id z1-20020aa78881000000b0068c02be67bbmr20475pfe.20.1694531501482; Tue, 12 Sep 2023 08:11:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694531501; cv=none; d=google.com; s=arc-20160816; b=XCKmovZyRPlfTDbsWaw7sVNH6XHDLDrVa+p3ZjJxBQWZakvRfCS5alWpuWFsYeFrrl ArChZ8PX1hXbmKrmrsJTeV9oxT+WEk5DkQB7UjwddxTky3I/BXgQ6FMprcDodTixkhwY Ej708U1k9wSmOguw6DaPkbTEii6RzoPA+bT1RLeGtAxJUQYWroxTfQ2UkqIFS+KjrhMm iN2l8P2I6iPWp+EAa/gU1TtrL561oIMV00KDKAh2lk6YUIJuWI2DvwuIVI/GwgeFu5Ez 1aAGh9WRxI5zxZSRUQO4M/mYz/2nSdxtTgWUfujz3fCCROcV/z+h3HcMrfEoJYpHi3wA nFRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=Jc+1XF69t3xYLZw93X+bqdqtl1jqJX12nGm9exCVBVc=; fh=1cHgEmDNMh32Nj/qSxwLpYUo5nRtD7wvP1+x8sexb8Y=; b=LnaPm7nWvXmjlnOv7OSrYgfw5Pw6+HRv/QFx3RAY3u02aiPgdoBY5oAqlO51u0w5vP xqjapbEjIMkzd7G8cRp3hePolEuJKuQG6F+GAFeKHQr5ovhAT1hCurBqi7OnGyhDJqxk DuH8FakQ8upaT5T54c6NHO01R8W0GAC9U81+IEKUwLUdycBqF0XmVwcKWfitmcoOJthC 5nSFNRHY06mmcNK08pARjqgG0zl5p7I9GlCCVC9nLepCfEPLTJ4kf3pJuu2eI0HZ9UgS BAqoRSwLhj621XY7nFETavvkslqaI3Jh0d9N7uNmG6tw1n4NJQFwxIIRTRi7zo7Fzb5K ZgwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aa3DyupX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id h1-20020a056a00230100b0068e4235b265si8387357pfh.20.2023.09.12.08.11.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 08:11:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aa3DyupX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 98791826750E; Mon, 11 Sep 2023 21:37:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240934AbjILC53 (ORCPT + 40 others); Mon, 11 Sep 2023 22:57:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240895AbjILC5J (ORCPT ); Mon, 11 Sep 2023 22:57:09 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C62110ADC; Mon, 11 Sep 2023 19:27:25 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29624C116A8; Mon, 11 Sep 2023 23:20:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694474432; bh=l1cAuCAFNIz6Fl5qFBOVmXB/EWQK3m8dwpuLOKjgIEI=; h=Date:From:To:Cc:Subject:From; b=aa3DyupXG32KVn0mp2BehxxtZGA8gqdDxOWHLZNORvS4j0w9CEsBrRhf83s2XJah8 iKdbsKWj6T4eGKCMiSCVFZCvwLbXiuIxShnus0WM+gPHKRK48O3oRu3PCpnAAhVO63 9G2lTXDg5gyQO8JxT6dVDmQzqeskbcCVQK4Ir92FKTJt50tcuOVb1uHk8LvEhSfI2+ S2FBJKHUgMvoL7CdL1j4hq3Jayy1sZfGBOwhqWxtRoHL06UgIu1eHguKS5StEIUwV0 QCbYtF9aPa0nKBQUd602HTJ5UsJ6O+N+ClncpSc3XdxFBXGMpbYr7EuD0G9A3go+Ct YvfyqBPrjTXRg== Date: Mon, 11 Sep 2023 17:21:28 -0600 From: "Gustavo A. R. Silva" To: Jason Gunthorpe , Leon Romanovsky Cc: linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] RDMA/core: Use size_{add,mul}() in calls to struct_size() Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 11 Sep 2023 21:37:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776845063315644347 X-GMAIL-MSGID: 1776845063315644347 Harden calls to struct_size() with size_add() and size_mul(). This results in no differences in binary output. Fixes: 467f432a521a ("RDMA/core: Split port and device counter sysfs attributes") Fixes: a4676388e2e2 ("RDMA/core: Simplify how the gid_attrs sysfs is created") Signed-off-by: Gustavo A. R. Silva --- drivers/infiniband/core/sysfs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c index ee59d7391568..ec5efdc16660 100644 --- a/drivers/infiniband/core/sysfs.c +++ b/drivers/infiniband/core/sysfs.c @@ -903,7 +903,7 @@ alloc_hw_stats_device(struct ib_device *ibdev) * Two extra attribue elements here, one for the lifespan entry and * one to NULL terminate the list for the sysfs core code */ - data = kzalloc(struct_size(data, attrs, stats->num_counters + 1), + data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)), GFP_KERNEL); if (!data) goto err_free_stats; @@ -1009,7 +1009,7 @@ alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group) * Two extra attribue elements here, one for the lifespan entry and * one to NULL terminate the list for the sysfs core code */ - data = kzalloc(struct_size(data, attrs, stats->num_counters + 1), + data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)), GFP_KERNEL); if (!data) goto err_free_stats; @@ -1140,7 +1140,7 @@ static int setup_gid_attrs(struct ib_port *port, int ret; gid_attr_group = kzalloc(struct_size(gid_attr_group, attrs_list, - attr->gid_tbl_len * 2), + size_mul(attr->gid_tbl_len, 2)), GFP_KERNEL); if (!gid_attr_group) return -ENOMEM; @@ -1205,8 +1205,8 @@ static struct ib_port *setup_port(struct ib_core_device *coredev, int port_num, int ret; p = kvzalloc(struct_size(p, attrs_list, - attr->gid_tbl_len + attr->pkey_tbl_len), - GFP_KERNEL); + size_add(attr->gid_tbl_len, attr->pkey_tbl_len)), + GFP_KERNEL); if (!p) return ERR_PTR(-ENOMEM); p->ibdev = device;