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;