Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1001843rdg; Wed, 11 Oct 2023 11:15:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IENJJ4E7cZ1jdbsrPukDUVejohUBtCVDRSPleoNVhoWwlwaUwXa5ETkafNlYLoxmInF5GwX X-Received: by 2002:a17:902:9b8e:b0:1c6:2780:3adc with SMTP id y14-20020a1709029b8e00b001c627803adcmr16906534plp.24.1697048158139; Wed, 11 Oct 2023 11:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697048158; cv=none; d=google.com; s=arc-20160816; b=ol6XkrbGsRBcTO2DKC7vIRZgP7KMz5P+/0rW71zE1APPVuScnFZTPl5zGFgSZEcU9L 0Fu6SIgng7kK8Ub8MXXrFE1m8tkOJNZquICm6Y9a5CAMW4PN8c+OEfMl8Tj5zIuj43sR K2DrNNqB4E5JPAda4w3mqoWt4eWU/f+GAA4Vhwza6o2+5788x6UcL4fQvcLWu4Y8Hm2a hCntdPEYBswyQY1PKzu5phfYN+FrWoEamGGjk8U0g02AgT7SlPVm3NhQDpWgE+5Nt4C3 iSHWUmv2p0mcdUIC7jlN3JcwTf6jSzpN43dTPP3XU8powOeBn5rnfs9oP+ZLeMtXgZnU VADQ== 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; bh=CKjT39HyXUXX35XjrQdaeLewbIAa0J4e6UCDvFr4kwc=; fh=3nQM0QufZPSZ94JO+WDmHkrFziuG1/NJr0OD1jy0Wvc=; b=vlFSQkb1Zu5q/NY79yxEOvguoRXAwUL1PNUyVZ4OVAIYSEfRaPQy2C8vg0Ei04OXCj Ii8qRBDmHd3g+tzq5AFuPfUn1EIq/lxTvplWs1eNceDRhxptzFgkxS0vUvNJhYbHPtWd TZ+msv9UIRSyIRTGFNDRzfkYY0X4HF0Oj19bP2/WdP+JeNb7pzoknpUO7Y4JGt46NxAg o9t4+Z2vXH8g7Er1Q0P9pPxVxHlDx4Q5d5hHirLAje9ObQT1F23I07wd32o+CBC7xBTI sZAKF64gElcE+W9T+7hx6Q0oEBoZRqCz5ktnJRvMKQtQyV3awK4bTBqL5XvHzPgz5JSq xEbw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id h5-20020a170902f2c500b001c9d4f08c3asi231838plc.277.2023.10.11.11.15.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 11:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5439480EFC70; Wed, 11 Oct 2023 11:15:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235076AbjJKSPS (ORCPT + 99 others); Wed, 11 Oct 2023 14:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233335AbjJKSPH (ORCPT ); Wed, 11 Oct 2023 14:15:07 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BCB489D for ; Wed, 11 Oct 2023 11:15:05 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 43AB01516; Wed, 11 Oct 2023 11:15:46 -0700 (PDT) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A50F23F5A1; Wed, 11 Oct 2023 11:15:04 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, jgg@nvidia.com, baolu.lu@linux.intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v5 3/7] iommu: Validate that devices match domains Date: Wed, 11 Oct 2023 19:14:50 +0100 Message-Id: <4e8bda33aac4021b444e40389648deccf61c1f37.1697047261.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, 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 morse.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 (morse.vger.email [0.0.0.0]); Wed, 11 Oct 2023 11:15:52 -0700 (PDT) X-Spam-Level: ** Before we can allow drivers to coexist, we need to make sure that one driver's domain ops can't misinterpret another driver's dev_iommu_priv data. To that end, add a token to the domain so we can remember how it was allocated - for now this may as well be the device ops, since they still correlate 1:1 with drivers. We can trust ourselves for internal default domain attachment, so add checks to cover all the public attach interfaces. Reviewed-by: Lu Baolu Reviewed-by: Jason Gunthorpe Signed-off-by: Robin Murphy --- v4: Cover iommu_attach_device_pasid() as well, and improve robustness against theoretical attempts to attach a noiommu group. --- drivers/iommu/iommu.c | 10 ++++++++++ include/linux/iommu.h | 1 + 2 files changed, 11 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 7bb92e8b7a49..578292d3b152 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2114,6 +2114,7 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops, return NULL; domain->type = type; + domain->owner = ops; /* * If not already set, assume all sizes by default; the driver * may override this later @@ -2279,10 +2280,16 @@ struct iommu_domain *iommu_get_dma_domain(struct device *dev) static int __iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) { + struct device *dev; + if (group->domain && group->domain != group->default_domain && group->domain != group->blocking_domain) return -EBUSY; + dev = iommu_group_first_dev(group); + if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) + return -EINVAL; + return __iommu_group_set_domain(group, domain); } @@ -3480,6 +3487,9 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, if (!group) return -ENODEV; + if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) + return -EINVAL; + mutex_lock(&group->mutex); curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); if (curr) { diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 2d2802fb2c74..5c9560813d05 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -99,6 +99,7 @@ struct iommu_domain_geometry { struct iommu_domain { unsigned type; const struct iommu_domain_ops *ops; + const struct iommu_ops *owner; /* Whose domain_alloc we came from */ unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; -- 2.39.2.101.g768bb238c484.dirty