From patchwork Sat Dec 9 12:36:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 176167 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6024473vqy; Sat, 9 Dec 2023 04:36:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFre0iM7GexoWK9RKxKPIjH70hKIDL8/vmrqhn6DQRtkiKmMwJF9lawrFgoX/zlnyOpY16n X-Received: by 2002:a05:6870:b025:b0:1fa:f541:5297 with SMTP id y37-20020a056870b02500b001faf5415297mr2377565oae.42.1702125410771; Sat, 09 Dec 2023 04:36:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702125410; cv=none; d=google.com; s=arc-20160816; b=J8ybtQrsonrtJ4NjFxrsF1to3uFUQh+2ue0VCuJ0COaKBz7qED0cUUccchj0PF/9Cc GYlNa+fKP8szTVjsdrouurTQf1sfsa+loHBRg9KNR9kPamoIbsB76y2Y3iPHjI+evUct C57iwX5Y2AezpVjObtDr1g5Ghf3XtKwH+FZB8rpxs4yA7iOC1EBgBI90MJ/75pxj+7Hn xdVfTAFoDOJPTdeUUrkqp+mTNLf6JBbn3FRdVE8cv7g67fP3dQkZUFfQHINpMF3S8Orb gh2JOoDOzf0+Xi3vb6aWvI2/PWJNf+Kt1gCLASL8Raqk5ASQTUpzA3gUHQcKYuwGPiqg FFCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=bXiXUI7HOibThetI/WgePYhHkYZjFZBA7O1RkI3V6uI=; fh=3wOtCVUcvBrsL51U+yUhlochyUa73CpX1FOSR+cseNE=; b=NY7ofl4cFtNrA1tsJCM3BFuEdolKxEsADJP3WZmOl65t9TTzLV3PxRYApQkG/e30eP aeGfnhTA0GNO9ECK7Nc0UN5S49sCV/wNs+4hMa5G8kP8zpqvIayLVrVLml5uh6s+WEfG hMnb/N2Cw4dzaC6VJo1AqYM6s6HVBkNjqX4G/MJdw+Dy5+qJGZfEZiUCNjZtUJCgqTTO kANyako1lBKA4Seo+z4BsqW9k8+N8XwjXPEk4BTUbGjww3lh7/Sg/96nSft7WsGmYtTC FImmOn9V/yLhc33MRW7XQ71HEsZgb7zR92xXXwzjyTdz3GNicso6jbpXBvSYLTSiCcvr cUwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=oKW5DHKs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id cm14-20020a056a020a0e00b005c6818b5a3esi3232493pgb.528.2023.12.09.04.36.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 04:36:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=oKW5DHKs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id B2564809269E; Sat, 9 Dec 2023 04:36:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229670AbjLIMgV (ORCPT + 99 others); Sat, 9 Dec 2023 07:36:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjLIMgU (ORCPT ); Sat, 9 Dec 2023 07:36:20 -0500 Received: from smtp.smtpout.orange.fr (smtp-26.smtpout.orange.fr [80.12.242.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AD6B1703 for ; Sat, 9 Dec 2023 04:36:21 -0800 (PST) Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id BwZ7rbI0E6wd8BwZ7re9vS; Sat, 09 Dec 2023 13:36:19 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1702125379; bh=bXiXUI7HOibThetI/WgePYhHkYZjFZBA7O1RkI3V6uI=; h=From:To:Cc:Subject:Date; b=oKW5DHKslOm34e9Qg3zWDWUgyjW5pxek3pqWPWHHPlsh8p0Jbyej5JRSHI2Zver/A RxfPEAMHE1/P/qAtqtuq8z3kkVfpxZ3dgj2EJ1Hz8DLop173gbjg1x3qYnPTBdrA4+ 8GyM41Z9wp1F4MaZnQ7CJ2uoKlmqIOSFjZYn+GUqNY9CB70XG7X59gcXGfH/QLQn8E pgH7t+HcjIlTGTaW3fGlBIoGyVBfbOb+NqAbWCitnOqDs4LN+KDqxZuwD7Wydx2ATQ RxgaPEH7InuWzBX8NzFtpuq2IMXwSqqpjo9Wx3usZWZlMq7EYq00CFTX0i+jjAkTUx wnv9+gv5yYlYg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 09 Dec 2023 13:36:19 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers , Arnd Bergmann , Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , Kees Cook Subject: [PATCH v2 1/2] VMCI: Remove handle_arr_calc_size() Date: Sat, 9 Dec 2023 13:36:14 +0100 Message-Id: <84e7f2d8e7c4c2eab68f958307d56546978f76e3.1702125347.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Sat, 09 Dec 2023 04:36:46 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784807854361512503 X-GMAIL-MSGID: 1784807854361512503 Use struct_size() instead of handle_arr_calc_size(). This is much more conventional. While at it, use size_add() when computing the needed size in vmci_handle_arr_append_entry(). This prevents from (unlikely) overflow when computing the new size to reallocate. Suggested-by: Kees Cook Signed-off-by: Christophe JAILLET Reviewed-by: Kees Cook --- Changes in v2: - use size_add() in vmci_handle_arr_append_entry() [Kees Cook] v1: https://lore.kernel.org/all/adf0c48a57d911be5509688614d2e00694ff57eb.1702068153.git.christophe.jaillet@wanadoo.fr/ --- drivers/misc/vmw_vmci/vmci_handle_array.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/drivers/misc/vmw_vmci/vmci_handle_array.c b/drivers/misc/vmw_vmci/vmci_handle_array.c index de7fee7ead1b..681b3500125a 100644 --- a/drivers/misc/vmw_vmci/vmci_handle_array.c +++ b/drivers/misc/vmw_vmci/vmci_handle_array.c @@ -8,12 +8,6 @@ #include #include "vmci_handle_array.h" -static size_t handle_arr_calc_size(u32 capacity) -{ - return VMCI_HANDLE_ARRAY_HEADER_SIZE + - capacity * sizeof(struct vmci_handle); -} - struct vmci_handle_arr *vmci_handle_arr_create(u32 capacity, u32 max_capacity) { struct vmci_handle_arr *array; @@ -25,7 +19,7 @@ struct vmci_handle_arr *vmci_handle_arr_create(u32 capacity, u32 max_capacity) capacity = min((u32)VMCI_HANDLE_ARRAY_DEFAULT_CAPACITY, max_capacity); - array = kmalloc(handle_arr_calc_size(capacity), GFP_ATOMIC); + array = kmalloc(struct_size(array, entries, capacity), GFP_ATOMIC); if (!array) return NULL; @@ -51,8 +45,8 @@ int vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr, struct vmci_handle_arr *new_array; u32 capacity_bump = min(array->max_capacity - array->capacity, array->capacity); - size_t new_size = handle_arr_calc_size(array->capacity + - capacity_bump); + size_t new_size = struct_size(array, entries, + size_add(array->capacity, capacity_bump)); if (array->size >= array->max_capacity) return VMCI_ERROR_NO_MEM; From patchwork Sat Dec 9 12:36:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 176168 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6024609vqy; Sat, 9 Dec 2023 04:37:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJJ0L81kHawMm4M5PPdb/c2kBf9K5etCUsQ+Cr8Tg0rQDgyKvIfS9qPD0VHbm5znJiT21J X-Received: by 2002:a05:6870:d90:b0:1fa:dd91:1839 with SMTP id mj16-20020a0568700d9000b001fadd911839mr2198662oab.13.1702125430520; Sat, 09 Dec 2023 04:37:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702125430; cv=none; d=google.com; s=arc-20160816; b=Efql3bcOQcXQxUCx0qsJFD1m/dmHcGjgVYlD03e7CZE086VBjUpoa7zfMDBNdA6yaV bTGxv7Hl1cV1LO06+099j1DbetvnUYryVf+u6Rn+mQgU+lyRn4bkFd30OJXOIQi3G4hk A6lttohaPtiXy+K/HfikQOYn7xMLC6SVUC2Mu0CPAtLzpRVsJXXVsEjnbEbqeUnrnpHJ 4DWB33uEwz6X4lm9fR/K1o17p3D2jHUxTICm/coVcbUQH8h9F6zMdMX4AgOhUxI/9jNX qnBzILgDkAGOQAurZVwGRSjfP3CSUVgZN8mLd3p1LgEWA0Asx2uIp6UJbvAUab+Dtw2B 61cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8H4ZD3O2zM53FOpfQ60afkiHrSEs0SVvh7G9x7C42wA=; fh=3wOtCVUcvBrsL51U+yUhlochyUa73CpX1FOSR+cseNE=; b=Sw+sFIhKhYDwBwF4KDsVy/0vkSngxKF5aW59hbS07RYhbD5jDuHaYaKh7Vfbdkd3WP nPiaBgsjJDZRBH/Qomlht93Dp0j/DG70l7sBh8s7HylKWjP5SF45+rj7P9ZMOXAnGWbG wOFlIYvMnz4rnCPhRTbGbdHUq4G1bbuaiQmaDgl+Kh70Iey72JYkzHsTpDc49/ILehjC 1TK+GMh5+awPyg/KZpIJoVezeRsymFAuINpd0/ov/b3Rg7n4tptqLvG551rO3bnTO/eS ZTWv6mt1fP13QAJ3NCAsXQzhdPTyXEZSXuROVXTKISXyG5ES0Wlpus0gP5fXr9nna5pT 4z8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=R0ivJdUO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id z23-20020a656657000000b005c6b544c3d0si2980939pgv.534.2023.12.09.04.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 04:37:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=R0ivJdUO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 3DAB28061CEC; Sat, 9 Dec 2023 04:37:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230044AbjLIMgk (ORCPT + 99 others); Sat, 9 Dec 2023 07:36:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230048AbjLIMg0 (ORCPT ); Sat, 9 Dec 2023 07:36:26 -0500 Received: from smtp.smtpout.orange.fr (smtp-25.smtpout.orange.fr [80.12.242.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB7DD10DF for ; Sat, 9 Dec 2023 04:36:31 -0800 (PST) Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id BwZ7rbI0E6wd8BwZFre9vu; Sat, 09 Dec 2023 13:36:28 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1702125391; bh=8H4ZD3O2zM53FOpfQ60afkiHrSEs0SVvh7G9x7C42wA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=R0ivJdUOrY6nEVb3Z2TcN+w3sallr2i/MWL/1LqnVQhXgXfiaYQPJTsEblBGh6LYw jCOhxqfxgkYyGsYcexTGlh+Gtg5aH+THs0dTjbssiif/t81aC6vuPLtzJTQWZaTB/8 iiCtD/mnGj7kUxMgASTZmka7MpYUB0J8sbHopyG9Am/VRPqRQKjOHSjfL8gRKkhMgo FpPy/OpDlOZ2SLxTejJ9TVw+JYWolOCknirLcQ610e3MRTgASocZ9TeCdbNyXK8WUs 49mJ1Bo5D1g8JUUyxDVY6XQDZGElEwL44q8mAghOKQibFDNB+Ry1dvd9sTv5LiV7yu PjHiT91ixwZng== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 09 Dec 2023 13:36:28 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers , Arnd Bergmann , Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , Kees Cook Subject: [PATCH v2 2/2] VMCI: Remove VMCI_HANDLE_ARRAY_HEADER_SIZE and VMCI_HANDLE_ARRAY_MAX_CAPACITY Date: Sat, 9 Dec 2023 13:36:15 +0100 Message-Id: <00547fe74efe329b266eb8074c41f286758a3c64.1702125347.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <84e7f2d8e7c4c2eab68f958307d56546978f76e3.1702125347.git.christophe.jaillet@wanadoo.fr> References: <84e7f2d8e7c4c2eab68f958307d56546978f76e3.1702125347.git.christophe.jaillet@wanadoo.fr> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Sat, 09 Dec 2023 04:37:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784807875667191371 X-GMAIL-MSGID: 1784807875667191371 Remove VMCI_HANDLE_ARRAY_HEADER_SIZE and VMCI_HANDLE_ARRAY_MAX_CAPACITY that are unused. Suggested-by: Kees Cook Signed-off-by: Christophe JAILLET Reviewed-by: Kees Cook --- Changes in v2: - no change in code - add R-b v1: https://lore.kernel.org/all/c2a231a5ea127b28b5d8c4e86ef471dd01069d47.1702068153.git.christophe.jaillet@wanadoo.fr/ --- drivers/misc/vmw_vmci/vmci_handle_array.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/drivers/misc/vmw_vmci/vmci_handle_array.h b/drivers/misc/vmw_vmci/vmci_handle_array.h index b0e6b1956014..27a38b97e8a8 100644 --- a/drivers/misc/vmw_vmci/vmci_handle_array.h +++ b/drivers/misc/vmw_vmci/vmci_handle_array.h @@ -20,14 +20,8 @@ struct vmci_handle_arr { struct vmci_handle entries[] __counted_by(capacity); }; -#define VMCI_HANDLE_ARRAY_HEADER_SIZE \ - offsetof(struct vmci_handle_arr, entries) /* Select a default capacity that results in a 64 byte sized array */ #define VMCI_HANDLE_ARRAY_DEFAULT_CAPACITY 6 -/* Make sure that the max array size can be expressed by a u32 */ -#define VMCI_HANDLE_ARRAY_MAX_CAPACITY \ - ((U32_MAX - VMCI_HANDLE_ARRAY_HEADER_SIZE - 1) / \ - sizeof(struct vmci_handle)) struct vmci_handle_arr *vmci_handle_arr_create(u32 capacity, u32 max_capacity); void vmci_handle_arr_destroy(struct vmci_handle_arr *array);