Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1070368rwi; Thu, 13 Oct 2022 08:41:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7z7pHeeTY/n4fkuwWQrgugGDTs82CHv2RR6jstyKMlJfg07VKGKbPeZ2lAPNBzJrlLA5BN X-Received: by 2002:a17:907:3f94:b0:78d:9d2f:3002 with SMTP id hr20-20020a1709073f9400b0078d9d2f3002mr299342ejc.40.1665675710021; Thu, 13 Oct 2022 08:41:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665675710; cv=none; d=google.com; s=arc-20160816; b=T9lKMZvQhXwNF5D2oyOVfY0SJPPGbtobT5NRAJ7nn+Q8Fa/9E6UsNiYoUxNOYtes5g hGFEwm8EZhDD5aIsHKrSfguEcrFu8lJm12omrBG/x6X+C00xeDw+L6MpVAFYULiLdIMd iJr82plhySUuvHVnNF6t69jwdDGRqBluLjgS1PazN1kOmjhZb95haWVHBcnkn+toDJcl pH3mmbDrV5n7qhTQ/1olKquQ/A3NHfK8ys/VK7ibtXkfjdyGc7ZYwMN+5JmkA82+VOPo KE7tuSusmI3KAqKz+QVHgFfu3GQYPABHSDeF5a5c1Y9fTqiqTLHbIsV6hMGWQnrcoSdP WRdA== 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=b9+V6R7tFlbeiwac+o//rSY61pCcgVEQM77YrZbvp4g=; b=FLbqzNKNpskXLLd6NLhiHWWCwIqHdcLE1oGK2ZBfFV+RXg3E/YPcaREQ8DTGckZ4+P rs9MiaD9P0uz/WM7Ea8+cFnMqCZN/Qz0lmIPQ88p1ZYdsi/hm+ERvf18KmlrwM9hiv3R iBrB4xD/uZ92ceNTc5Y5zvBJ4sIoSO3/hPk+lvfmYUrrWLqkMLKAuqTZbF6SqTpb2eB0 lfjyt6rsRZuL39+OVgboTL9IbcJf8BgxX1BkZI+iVPRCaWdEZjO0aJOMTeWKaIn5s/As Tnjz5/qhicRtJ2BCMfDQpYsv48N9tS8AlGvBfijtHXAwVJcHcqJ9+Rurs5bjulVs2xTk GmOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R9MrUHYa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020a056402511100b0045938ab7129si52042edd.330.2022.10.13.08.41.24; Thu, 13 Oct 2022 08:41:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R9MrUHYa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbiJMPeM (ORCPT + 99 others); Thu, 13 Oct 2022 11:34:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229495AbiJMPeL (ORCPT ); Thu, 13 Oct 2022 11:34:11 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 345A089CF1 for ; Thu, 13 Oct 2022 08:34:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665675248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=b9+V6R7tFlbeiwac+o//rSY61pCcgVEQM77YrZbvp4g=; b=R9MrUHYaCTR94E9Rc3uLa4tg9GNUD37kRxdv8huKsqVHz0G0lUbHd7qH1qSfGuiaDp2L/b Kq6ImK7Hg1v2ybfvm2zFnOANC3uTEK+Q4v6p+WqoPp8CsEZayay7ePuW/YF0b8kyJ5rEvN JCwzR1VSv6YSCb84XFMiFp+nq/lN0d4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-658-mhXDiGpFPbyJlSVFSPWONA-1; Thu, 13 Oct 2022 11:34:07 -0400 X-MC-Unique: mhXDiGpFPbyJlSVFSPWONA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 58D8C2A5955A; Thu, 13 Oct 2022 15:34:06 +0000 (UTC) Received: from cantor.redhat.com (unknown [10.2.16.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id CAD6140C8461; Thu, 13 Oct 2022 15:33:57 +0000 (UTC) From: Jerry Snitselaar To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Will Deacon , Robin Murphy , Lu Baolu Subject: [RFC PATCH] iommu/vt-d: Add sanity check to iommu_sva_bind_device() Date: Thu, 13 Oct 2022 08:33:55 -0700 Message-Id: <20221013153355.2365865-1-jsnitsel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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_NONE, SPF_HELO_NONE,SPF_NONE 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 iommu_sva_bind_device() should only be called if iommu_dev_enable_feature() succeeded. There has been one case already where that hasn't been the case, which resulted in a null pointer deref in dev_iommu_ops(). To avoid that happening in the future if another driver makes that mistake, sanity check dev->iommu and dev->iommu->iommu_dev prior to calling dev_iommu_ops(). Cc: Joerg Roedel Cc: Will Deacon Cc: Robin Murphy Cc: Lu Baolu Signed-off-by: Jerry Snitselaar --- drivers/iommu/iommu.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 4893c2429ca5..20ec75667529 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2746,7 +2746,15 @@ iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) { struct iommu_group *group; struct iommu_sva *handle = ERR_PTR(-EINVAL); - const struct iommu_ops *ops = dev_iommu_ops(dev); + const struct iommu_ops *ops; + + if (!dev->iommu || !dev->iommu->iommu_dev) { + dev_warn(dev, "%s called without checking succes of iommu_dev_enable_feature?\n", + __func__); + return ERR_PTR(-ENODEV); + } + + ops = dev_iommu_ops(dev); if (!ops->sva_bind) return ERR_PTR(-ENODEV); -- 2.37.2