Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2069654rdb; Mon, 9 Oct 2023 11:24:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGMPEvFHI5WBXrrF/LK0SezIheBEyA4WX+pNySif8dc3XVDib5hinUYMjAS96vjSpPN8TLn X-Received: by 2002:a05:6a21:191:b0:155:5c28:ea67 with SMTP id le17-20020a056a21019100b001555c28ea67mr19378483pzb.38.1696875885275; Mon, 09 Oct 2023 11:24:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696875885; cv=none; d=google.com; s=arc-20160816; b=tLXdJ23bQVqIlyWin0DuQeoj4guuiZi7g7bICHFnPmOGrSlrAfnMlSER42A6eCjcCM fwSYb8CeN3bzhPyYop019vFC0Eh1U3q3TWbdDq64YozsSdE53dIoZgSzXaa/4AeGlCp6 uQEHwqd8eU9syWLvtcthjPHrQTjDtU7Xju+a+MrrRaPB3oGwuJTTdBxpCszXymIG0DER WvRHR1wP1SgL3QwL9OCJVQks5wPb/VyPViiBfe6B3jdq+RilBAovaJQH8AE3mA3vTFlg CABrxh2Rre0b7xBa34LrAn1hIu4XB299hBjqH0SWMv9RyXf8ClxEvYSkXybxqQpa+kBu Po9Q== 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=E0eI8sAd2nhYIsQ4tc4nWJ4mGntygIvpvHYnezHKTIs=; fh=j1jLUPxlO+RfftW++MwD8FG3WgZO+d/5Y7t2HTA0G9s=; b=d6Sym/4Zrwp+baguZ43+sKxarhMYGSjtTwJY4odpvUvOg8CpfsGtnQyCK0c5rYdOyo yQvcbKp/iRwcG5m5HUiDC+kf6tt0sOuvfhFRBNmGykWxOFmGSUOieEhnr120pr4IFMt0 e2jrwkbWFX6U+1b5K8l90t10fkMh5youGRBAiY8UKZ6lX9OCR0bS4PJ/1XxIRUxcoLht rtwxWg0JzK+lsCUzc7XZz89od+Am5bg+8ERgkRY4TDQOnZss+xj11vhDqBjwdOpDAnEg hNvXCxuig/Z/6Qjd530xs2x9T2nn78rHu+k8mxHtLArclCiCqHQ+vc3LO7Cc7DxxaylS pNoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="kfb/PFt1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id j9-20020a170902da8900b001bb95a5cb9fsi10438457plx.522.2023.10.09.11.24.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 11:24:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="kfb/PFt1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id BA5778092482; Mon, 9 Oct 2023 11:24:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377550AbjJISYe (ORCPT + 99 others); Mon, 9 Oct 2023 14:24:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377544AbjJISYc (ORCPT ); Mon, 9 Oct 2023 14:24:32 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 728E7D8 for ; Mon, 9 Oct 2023 11:24:31 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA41DC433C7; Mon, 9 Oct 2023 18:24:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696875871; bh=GX827MInhdg+9mFXDOrIyDM25nqfnAIqJdLH+cnFaYc=; h=Date:From:To:Cc:Subject:From; b=kfb/PFt1GOw5zC7CWQpYjFj1PXHizJBGvSfYLfMzu1Fu153G6IibmU7pIiE36MWaz Wq5Mbd63mgNvyj381SHoYS/kgRDRAwW4svc1+EjTpZytGPUC6BnHmjhe/1TbQKluOd lJwEvwm99bGucNKSkxo8yjWenMOkgH1X/PTJSUNlhE85Jmh4tatu/HsNRN2tGkdr8f n//Ox8QHbqjDlpowcP8WVokEry8qPiiQFpPH/FDZZnxgZuHZUJw0iYZbRVLeH7Bc6P 33Axz2LTWi6+hZU7n2PdSHI6Q19g4MdzRer4+pDnB+9NDTvZrWNKAMdz3katFpP7Xh pO6IoFkkMbihA== Date: Mon, 9 Oct 2023 12:24:27 -0600 From: "Gustavo A. R. Silva" To: Jean-Philippe Brucker , Joerg Roedel , Will Deacon , Robin Murphy Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] iommu/virtio: Add __counted_by for struct viommu_request and use struct_size() Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Mon, 09 Oct 2023 11:24:43 -0700 (PDT) X-Spam-Level: ** 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). While there, use struct_size() helper, instead of the open-coded version, to calculate the size for the allocation of the whole flexible structure, including of course, the flexible-array member. This code was found with the help of Coccinelle, and audited and fixed manually. Signed-off-by: Gustavo A. R. Silva --- drivers/iommu/virtio-iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 17dcd826f5c2..379ebe03efb6 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -85,7 +85,7 @@ struct viommu_request { void *writeback; unsigned int write_offset; unsigned int len; - char buf[]; + char buf[] __counted_by(len); }; #define VIOMMU_FAULT_RESV_MASK 0xffffff00 @@ -230,7 +230,7 @@ static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len, if (write_offset <= 0) return -EINVAL; - req = kzalloc(sizeof(*req) + len, GFP_ATOMIC); + req = kzalloc(struct_size(req, buf, len), GFP_ATOMIC); if (!req) return -ENOMEM; -- 2.34.1