From patchwork Sun Sep 17 21:21:36 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: 141493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2827773vqi; Mon, 18 Sep 2023 10:28:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEukvzDU7RpOYkWajNB+tUndxVSRQNElRNBcAQ/rQ6jFzCVBJby5VhJrs9ufe4j662y37j3 X-Received: by 2002:a05:6a20:938e:b0:14d:5796:bccd with SMTP id x14-20020a056a20938e00b0014d5796bccdmr10219439pzh.24.1695058082267; Mon, 18 Sep 2023 10:28:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695058082; cv=none; d=google.com; s=arc-20160816; b=waa/Y68nI1yJrSSkR0J9wFLiT08v2zOWrj+moykfVE8BYcNZX+FQwXQOMGw3orAMQP OOKzwq/NuJJj79wyZ/iNwQOdjqxCpx47CM1rO7JCgvT1c7Z9c4jA9denLLiOZrupnQwX 7/DJFb+Gc4emS0x3pPCJBsUDxPONa7naqCKtYWVnEhQaXDRKncr/yc5EpcOIcfo7lgvC rl3awhCSGfY4mJ9yumhN474VW3dHNS1b5lN2YyRtlXxjmDsx2+NcMJTv9Hn8YMG9/zY0 5X1RvAdc40Cn3dzY+Exx9YuU+We6DS4MDeBks/mUrqcY/JMv30x5QNj0PTKejBKe0yQ2 ow2Q== 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=xSEVw4pus/5MHkNknSlQ8iHSxWjeDaET5xN4hqWltKQ=; fh=5qLkZAIBjb3Bsf3DyqZAbsotmUIxlYw74YZZmc5ntR8=; b=ZqXG6OL8FrxDtWlbk3FTLhJjsbWNEMl6Efd/VK9el1y1D569INo/ax7hE10N2djVVY 5LHyy76CAIqtdeqMnGvnllyyXucHs7ds+lafJlW09c/BCsTh2oQWj90SGG6SCDgTlnpi Vw9txeXbTMVGUhQ/xd8yUuN1iN6Re8UYwPPxps77cvPfmlD1EXmGBqrUyuX+I7gUJxIp DsMVRg/W1RznyFLapk7h8B9uNvNR3FunKQXgHT3l5bUb39aku2jYgbGmSiVgJsPieDQi xa3jkGhCsM40ssaer40cKVqYZejOyVbv+T9rEAkbDIFsvHNiz9y4AB85yVYQph9U/WzI 1JLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Bv9vTdNN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id mh7-20020a17090b4ac700b0027198fe5672si8784513pjb.129.2023.09.18.10.27.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 10:28:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Bv9vTdNN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 1505480C055D; Mon, 18 Sep 2023 10:02:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231419AbjIRRCu (ORCPT + 25 others); Mon, 18 Sep 2023 13:02:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231222AbjIRRCi (ORCPT ); Mon, 18 Sep 2023 13:02:38 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EEBC94; Mon, 18 Sep 2023 10:02:32 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64DE8C433CC; Mon, 18 Sep 2023 13:20:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695043232; bh=ShkpHFl+RnjUu34j0dVPCyZMpLLk1xaSBrq///1jfv4=; h=Date:From:To:Cc:Subject:From; b=Bv9vTdNNfsdCaguRGpM/oEQNdxFOSrxqi3Gh7tsGNqMepcLICp1RrGGF0o0jgkzCF Y5+qOUUX+8PcauW0H0WaeVXGB0043zzAW2fwGIE+8BlWsG/wAOWbG1G3egTlydbBHN HxjIZjqwPLfFBVuV6fIX5nZ8GYFitAf0M7w1nEHegy0nTdbf2UFf5Y9Zmx6otJ2oeD LH3HcQpfzw2fTKRfeN65S85E1G6dFGk+HiWcaexB24UoTFXC9clYxCIrQ+bOIhHbrv 5Mqxbuey0RjBeS+62gUUvKE4R3w6JzLW1WKs7cczxEl+j9DfK0l4F75O7efwyoyz6Z YmhrRIquvurVg== Date: Sun, 17 Sep 2023 15:21:36 -0600 From: "Gustavo A. R. Silva" To: Jason Gunthorpe , Leon Romanovsky , Parav Pandit , Jack Morgenstein , "Gustavo A. R. Silva" Cc: linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH v3][next] RDMA/core: Use size_{add,sub,mul}() in calls to struct_size() Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DATE_IN_PAST_12_24, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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, 18 Sep 2023 10:02:58 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777397223650177867 X-GMAIL-MSGID: 1777397223650177867 If, for any reason, the open-coded arithmetic causes a wraparound, the protection that `struct_size()` provides against potential integer overflows is defeated. Fix this by hardening calls to `struct_size()` with `size_add()`, `size_sub()` and `size_mul()`. Fixes: 467f432a521a ("RDMA/core: Split port and device counter sysfs attributes") Fixes: a4676388e2e2 ("RDMA/core: Simplify how the gid_attrs sysfs is created") Fixes: e9dd5daf884c ("IB/umad: Refactor code to use cdev_device_add()") Fixes: 324e227ea7c9 ("RDMA/device: Add ib_device_get_by_netdev()") Fixes: 5aad26a7eac5 ("IB/core: Use struct_size() in kzalloc()") Signed-off-by: Gustavo A. R. Silva --- Changes in v3: - Include changes to other files in drivers/infiniband/core/ - Update changelog text with a more descriptive argument for the changes. - Add more `Fixes:` tags. Changes in v2: - Update changelog text: remove the part about binary differences (it was added by mistake). Link: https://lore.kernel.org/linux-hardening/ZP+if342EMhModzZ@work/ v1: - Link: https://lore.kernel.org/linux-hardening/ZP+g+KfsEmEBAHmk@work/ drivers/infiniband/core/device.c | 2 +- drivers/infiniband/core/sa_query.c | 4 +++- drivers/infiniband/core/sysfs.c | 10 +++++----- drivers/infiniband/core/user_mad.c | 4 +++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index a666847bd714..010718738d04 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -804,7 +804,7 @@ static int alloc_port_data(struct ib_device *device) * empty slots at the beginning. */ pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata, - rdma_end_port(device) + 1), + size_add(rdma_end_port(device), 1)), GFP_KERNEL); if (!pdata_rcu) return -ENOMEM; diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c index 59179cfc20ef..8175dde60b0a 100644 --- a/drivers/infiniband/core/sa_query.c +++ b/drivers/infiniband/core/sa_query.c @@ -2159,7 +2159,9 @@ static int ib_sa_add_one(struct ib_device *device) s = rdma_start_port(device); e = rdma_end_port(device); - sa_dev = kzalloc(struct_size(sa_dev, port, e - s + 1), GFP_KERNEL); + sa_dev = kzalloc(struct_size(sa_dev, port, + size_add(size_sub(e, s), 1)), + GFP_KERNEL); if (!sa_dev) return -ENOMEM; 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; diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c index 7e5c33aad161..f5feca7fa9b9 100644 --- a/drivers/infiniband/core/user_mad.c +++ b/drivers/infiniband/core/user_mad.c @@ -1378,7 +1378,9 @@ static int ib_umad_add_one(struct ib_device *device) s = rdma_start_port(device); e = rdma_end_port(device); - umad_dev = kzalloc(struct_size(umad_dev, ports, e - s + 1), GFP_KERNEL); + umad_dev = kzalloc(struct_size(umad_dev, ports, + size_add(size_sub(e, s), 1)), + GFP_KERNEL); if (!umad_dev) return -ENOMEM;