iommu/vt-d: Fix issues on probe error path
Subsystems (2)
cover iommu/vt-d: Fix issues on probe error path
This series addresses two pre-existing issues in the Intel VT-d driver's probe error path. These issues were identified by Sashiko during the review of the ATS series [1].
The first patch fixes an RB-tree corruption that occurs when probing non-ATS devices. The second patch fixes a UAF by ensuring the per-device private data pointer is cleared before freeing memory on failure.
[1] https://sashiko.dev/#/patchset/[email protected]?part=4
Thanks,
Praan
Pranjal Shrivastava (2):
iommu/vt-d: Fix RB-tree corruption in probe error path iommu/vt-d: Fix Use-After-Free in probe error path
drivers/iommu/intel/iommu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH 1/2 iommu/vt-d: Fix RB-tree corruption in probe error path
The info->node RB-tree member is zero-initialized via kzalloc. If a device does not support ATS, the device_rbtree_insert() call is skipped. If a subsequent probe step fails, the error path jumps to device_rbtree_remove(), which misinterprets the zeroed node as a tree root and corrupts the device RB-tree.
Fix this by explicitly initializing the RB-node as empty using RB_CLEAR_NODE() during initialization and guarding the removal with RB_EMPTY_NODE().
--- drivers/iommu/intel/iommu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +5 -1
@@ -157,7 +157,10 @@ static void device_rbtree_remove(struct device_domain_info *info)unsigned long flags;spin_lock_irqsave (&iommu->device_rbtree_lock, flags); -rb_erase (&info->node, &iommu->device_rbtree); +if (!RB_EMPTY_NODE (&info->node)) { +rb_erase (&info->node, &iommu->device_rbtree); +RB_CLEAR_NODE (&info->node); + }spin_unlock_irqrestore (&iommu->device_rbtree_lock, flags); }
@@ -3254,6 +3257,7 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) info->dev = dev; info->iommu = iommu; +RB_CLEAR_NODE (&info->node);if (dev_is_pci (dev)) { if (ecap_dev_iotlb_support(iommu->ecap) && pci_ats_supported(pdev) &&
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
PATCH 2/2 iommu/vt-d: Fix Use-After-Free in probe error path
In reply to Pranjal ShrivastavaWhen intel_iommu_probe_device() fails after the info structure has been linked to the device via dev_iommu_priv_set(), the error path calls kfree(info) but does not clear the pointer in the device structure.
This results in a Use-After-Free regression if the pointer is accessed by a subsequent IOMMU core call or a re-probe.
Fix this by ensuring dev_iommu_priv_set(dev, NULL) is called before freeing the info structure in the error path.
--- drivers/iommu/intel/iommu.c | 1 + 1 file changed, 1 insertion(+)
drivers/iommu/intel/iommu.c +1 -0
@@ -3320,6 +3320,7 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) clear_rbtree:device_rbtree_remove (info); free: +dev_iommu_priv_set (dev,NULL );kfree (info);return ERR_PTR (ret);
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Baolu Lu
maintainerRe: [PATCH 2/2] iommu/vt-d: Fix Use-After-Free in probe error path
Quoted 5 lines · Pranjal Shrivastava
When
intel_iommu_probe_device()fails after the info structure has been linked to the device viadev_iommu_priv_set(), the error path calls kfree(info) but does not clear the pointer in the device structure.
Quoted 7 lines
This results in a Use-After-Free regression if the pointer is accessed by a subsequent IOMMU core call or a re-probe.
Fix this by ensuring
dev_iommu_priv_set(dev, NULL)is called before freeing the info structure in the error path.Fixes 89436f4f5412 ("iommu/vt-d: Fix WARN_ON in iommu probe path")
Quoted 12 lines
Reported-by [email protected]Signed-off-by Pranjal Shrivastava <[email protected]>--- drivers/iommu/intel/iommu.c | 1 + 1 file changed, 1 insertion(+)drivers/iommu/intel/iommu.c +0
-0
drivers/iommu/intel/iommu.c static struct iommu_device *intel_iommu_probe_device(struct device *dev) · line 3326
Quoted 7 lines
drivers/iommu/intel/iommu.c +1
-0@@ -3320,6 +3320,7 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) clear_rbtree:device_rbtree_remove (info); free: +dev_iommu_priv_set (dev,NULL );kfree (info);return ERR_PTR (ret);
Thanks,
baolu
Re: [PATCH 2/2] iommu/vt-d: Fix Use-After-Free in probe error path
Quoted 9 lines · Baolu Lu
Quoted 8 lines · Pranjal Shrivastava
When
intel_iommu_probe_device()fails after the info structure has been linked to the device viadev_iommu_priv_set(), the error path calls kfree(info) but does not clear the pointer in the device structure.This results in a Use-After-Free regression if the pointer is accessed by a subsequent IOMMU core call or a re-probe.
Quoted 6 lines
Quoted 4 lines
Fix this by ensuring
dev_iommu_priv_set(dev, NULL)is called before freeing the info structure in the error path.Fixes 89436f4f5412 ("iommu/vt-d: Fix WARN_ON in iommu probe path")Fixes eda1a94caf6b ("iommu: Mark dev_iommu_priv_set() with a lockdep")
Ack. Sending a v2 with this.
Thanks,
Praan