iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add()
Subsystems (2)
PATCH 1/1 iommu/sysfs: Clear iommu->dev on error in iommu_device_sysfs_add()
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.
---
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(+)
drivers/iommu/iommu-sysfs.c +4 -0
@@ -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 ;
Signature · 1 line
2.56.1.839.g22a8290db9-goog