All of lore.kernel.org
 help / color / mirror / Atom feed
From: Elkin Cruz <elkin@google.com>
To: "Joerg Roedel (AMD)" <joro@8bytes.org>, Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev,  linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev, irogers@google.com,
	 Elkin Cruz <elkin@google.com>,
	stable@vger.kernel.org
Subject: [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add()
Date: Wed, 25 Nov 2026 22:24:55 +0000	[thread overview]
Message-ID: <20261125222455.1445007-1-elkin@google.com> (raw)

In iommu_device_sysfs_add(), if device_add() or setting the kobject name
fails, put_device(iommu->dev) is called to release the allocated device.
However, iommu->dev is not cleared to NULL, leaving a dangling pointer.
Callers that inspect iommu->dev during error unwinding (such as
free_sysfs() in amd/init.c) can invoke iommu_device_sysfs_remove() on
the deallocated device pointer, causing a use-after-free and potential
double-free.

Set iommu->dev to NULL in the error path of iommu_device_sysfs_add(),
and guard iommu_device_sysfs_remove() against NULL pointers.

Fixes: 2926a2aa5c14 ("iommu: Fix wrong freeing of iommu_device->dev")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---

I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 drivers/iommu/iommu-sysfs.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/iommu/iommu-sysfs.c b/drivers/iommu/iommu-sysfs.c
index 937d1c749843..3b9e5578978e 100644
--- a/drivers/iommu/iommu-sysfs.c
+++ b/drivers/iommu/iommu-sysfs.c
@@ -85,12 +85,16 @@ int iommu_device_sysfs_add(struct iommu_device *iommu,
 
 error:
 	put_device(iommu->dev);
+	iommu->dev = NULL;
 	return ret;
 }
 EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
 
 void iommu_device_sysfs_remove(struct iommu_device *iommu)
 {
+	if (!iommu->dev)
+		return;
+
 	dev_set_drvdata(iommu->dev, NULL);
 	device_unregister(iommu->dev);
 	iommu->dev = NULL;
-- 
2.56.1.839.g22a8290db9-goog


             reply	other threads:[~2026-11-25 22:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 22:24 Elkin Cruz [this message]
2026-11-30  9:35 ` [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add() Joerg Roedel (AMD)

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=20261125222455.1445007-1-elkin@google.com \
    --to=elkin@google.com \
    --cc=iommu@lists.linux.dev \
    --cc=irogers@google.com \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=robin.murphy@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=will@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.