mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jerry Snitselaar <jsnitsel@redhat.com>
To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2] iommu: Add sanity check to iommu_sva_bind_device()
Date: Thu, 13 Oct 2022 10:13:57 -0700	[thread overview]
Message-ID: <20221013171357.2379415-1-jsnitsel@redhat.com> (raw)
In-Reply-To: <20221013153355.2365865-1-jsnitsel@redhat.com>

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().

Signed-off-by: Jerry Snitselaar <jsnitsel@redhat.com>
---
v2: Fix summary, and typo in dev_warn()

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..c745e935f26a 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 success 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


  parent reply	other threads:[~2022-10-13 17:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 15:33 [RFC PATCH] iommu/vt-d: " Jerry Snitselaar
2022-10-13 15:37 ` Jerry Snitselaar
2022-10-13 16:33 ` Jerry Snitselaar
2022-10-13 17:13 ` Jerry Snitselaar [this message]
2022-10-14  1:52 ` Baolu Lu
2022-10-14  2:10   ` Jerry Snitselaar
2022-10-14  2:22     ` Baolu Lu
2022-10-14  6:52       ` Jerry Snitselaar
2022-10-14 11:01         ` Robin Murphy
2022-10-14 15:25           ` Jerry Snitselaar
2022-10-14 16:50             ` Jerry Snitselaar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221013171357.2379415-1-jsnitsel@redhat.com \
    --to=jsnitsel@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®