mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add()
@ 2026-11-25 22:24 Elkin Cruz
  2026-11-30  9:35 ` Joerg Roedel (AMD)
  0 siblings, 1 reply; 2+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:24 UTC (permalink / raw)
  To: Joerg Roedel (AMD), Will Deacon
  Cc: Robin Murphy, iommu, linux-kernel, llvm, irogers, Elkin Cruz, stable

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add()
  2026-11-25 22:24 [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add() Elkin Cruz
@ 2026-11-30  9:35 ` Joerg Roedel (AMD)
  0 siblings, 0 replies; 2+ messages in thread
From: Joerg Roedel (AMD) @ 2026-11-30  9:35 UTC (permalink / raw)
  To: Elkin Cruz
  Cc: Will Deacon, Robin Murphy, iommu, linux-kernel, llvm, irogers, stable

On Wed, Nov 25, 2026 at 10:24:55PM +0000, Elkin Cruz wrote:
>  drivers/iommu/iommu-sysfs.c | 4 ++++
>  1 file changed, 4 insertions(+)

Applied, thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  9:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 22:24 [PATCH] iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add() Elkin Cruz
2026-11-30  9:35 ` Joerg Roedel (AMD)

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®