From patchwork Mon Oct 9 18:24:27 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: 150251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2048570vqo; Mon, 9 Oct 2023 11:24:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnvghh78OAsc2gnfTeZAyyHyQpa/NTfWFeSRbEFQ9tr2h5mUzF/gcs9fk6hgQvKE2n1XTt X-Received: by 2002:a17:902:e746:b0:1c7:3224:9137 with SMTP id p6-20020a170902e74600b001c732249137mr15544025plf.6.1696875880091; Mon, 09 Oct 2023 11:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696875880; cv=none; d=google.com; s=arc-20160816; b=NVRBE3btrtPyfHgzRH9GghA2yCaqC0wI0OGyPYVrbxREnR+syhT9C7t0cn+8S3fJEi iAknX78m4t7KFGVwqqK9Ns/gGnRfIp1cgnrC++tv0mtzolc21j+gdZ+MHPtUe3oQcbDZ jUujDpspsK5aCOo56GX4eqDUqKzFA6feShbQU4RXekP4ssH8vcD/MWYVGW/OVctMbbFF PGrLi/j5+necQWMI34Ck5nhec1bE3uC7iUi3MVmbfvmGlp3ihiqVXPTt1Yw5Uudpi9ca zxr5XkAIYco/nNmDiTEf3weosNFdY0rp3P/coPu/CQgeHBSDt5aEBzFilmK0cvLv9csd NRvw== 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.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 t11-20020a17090340cb00b001c72476c96fsi9832174pld.143.2023.10.09.11.24.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 11:24:40 -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="kfb/PFt1"; 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 1A50B801B9DD; Mon, 9 Oct 2023 11:24:39 -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 S1377557AbjJISYf (ORCPT + 18 others); Mon, 9 Oct 2023 14:24:35 -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-Disposition: inline X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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, 09 Oct 2023 11:24:39 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779303323211994796 X-GMAIL-MSGID: 1779303323211994796 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 Reviewed-by: Justin Stitt Reviewed-by: Jean-Philippe Brucker --- 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;