iommu/io-pgfault: reject partial faults after device removal
Subsystems (2)
cover iommu/io-pgfault: reject partial faults after device removal
iopf_queue_remove_device() kfree()s the entries on fault_param->partial without unlinking them, leaving the list head pointing into freed memory. Since a74c077b9021 the fault parameter is refcounted and can outlive the drain, a report that took its reference earlier can still list_add() through those pointers. Unlinking instead turns that into a leak: the late node lands on a parameter that is no longer on queue->devices, whose final put does not free partial faults.
This needs a device removed from its iopf queue while a non-last (partial) request is in flight, with the caller not blocking new reports for the duration.
Patch 1 unlinks each partial before freeing it, matching iopf_queue_discard_partial().
Patch 2 adds fault_param->removing, set and tested under fault_param->lock: a late partial is either drained, or rejected and freed by report_partial_fault() (-ENODEV). It covers partial-list admission only; callers must still quiesce reports for the remaining lifetimes.
Both patches are scoped to fault_param->partial, not the group path, the attach handle, or paths that never call iopf_queue_remove_device().
Verified on v7.4-rc3 with KASAN + DEBUG_LIST and the iommufd mock driver, its caller-side IOPF serialization deliberately removed and test-only delays added; this shows the mechanism, not production reachability. The runs cover queue-before-removal, removal-before-insertion, concurrent late reporters and re-enable. Each round queues one partial before the removal and then races the late reporters against the drain, so a clean run ends with every node accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated = drained + rejected), counted at a final removal with no reporter in flight. Nothing was left on fault_param->partial and KASAN stayed quiet.
With patch 1 reverted, the report is:
BUG: KASAN: slab-use-after-free in __list_add_valid_or_report Freed by task 1: kfree <- iopf_queue_remove_device 56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)
With patch 1 only, the same reproducer runs 1460 rounds without a KASAN report and leaks one late node per round, which patch 2 addresses.
Reachability on an unmodified production kernel, and any security impact, remain unconfirmed: the test shows memory corruption and a KASAN panic on the modified mock driver, not a user-triggerable bug on a stock kernel.
An AI coding assistant helped find this defect and build the reproducer and instrumentation, which are available on request along with the KASAN logs.
PATCH 1/2 iommu/io-pgfault: unlink partial faults before freeing them
iopf_queue_remove_device() frees the pending partial faults without unlinking them, so fault_param->partial keeps pointing into freed memory. A report that is still in flight can then list_add() through those dangling pointers. The unlink has been missing since iopf_queue_remove_device() was first added, but the race only became reachable with a74c077b9021, which made the fault parameter refcounted and able to outlive the removal.
Unlink each entry before freeing it, matchingiopf_queue_discard_partial().
This fixes the dangling-list access only: a partial that arrives after the drain now leaks, because the parameter is no longer on queue->devices and its final put does not free partial faults. The next patch gives such a partial a defined fate.
---iopf_queue_discard_partial() cannot be called directly from here: it takes queue->lock, which iopf_queue_remove_device() already holds.
drivers/iommu/io-pgfault.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/io-pgfault.c +3 -1
@@ -479,8 +479,10 @@ void iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) goto unlock;mutex_lock (&fault_param->lock); -list_for_each_entry_safe (partial_iopf, next, &fault_param->partial, list) +list_for_each_entry_safe (partial_iopf, next, &fault_param->partial, list) { +list_del (&partial_iopf->list);kfree (partial_iopf); + }list_for_each_entry_safe (group, temp, &fault_param->faults, pending_node) {struct iopf_fault *iopf = &group->last_fault;
PATCH 2/2 iommu/io-pgfault: reject partial faults after the device is removed
In reply to QihangA report that already holds a reference to the fault_param can still reach report_partial_fault() after iopf_queue_remove_device() drained the partial list. The node it queues there leaks: the parameter is no longer on queue->devices and its final put does not free partial faults.
Reject late partial insertions under fault_param->lock: add fault_param->removing, set it in iopf_queue_remove_device() before the drain, and test it in report_partial_fault() under the same lock that the insertion takes, so "check" and "link" cannot be separated. A partial is then either drained or rejected and freed by report_partial_fault() itself, which returns -ENODEV; as for any non-last request, no response is sent. removing is never cleared, since IOPF re-enable allocates a new fault_param.
This only protects partial-list admission; callers must still quiesce reports for the remaining lifetimes. It is not a general teardown fix; the cover letter lists the paths it does not cover.
---
This applies on top of "iommu/io-pgfault: unlink partial faults before freeing them".
drivers/iommu/io-pgfault.c | 12 ++++++++++-- include/linux/iommu.h | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/iommu/io-pgfault.c +10 -2
@@ -64,6 +64,7 @@ static int report_partial_fault(struct iommu_fault_param *fault_param, struct iommu_fault *fault) {struct iopf_fault *iopf; +int ret =0 ; iopf =kzalloc_obj (*iopf);if (!iopf)
@@ -72,10 +73,16 @@ static int report_partial_fault(struct iommu_fault_param *fault_param, iopf->fault = *fault;mutex_lock (&fault_param->lock); -list_add (&iopf->list, &fault_param->partial); +if (fault_param->removing) + ret = -ENODEV ; +else +list_add (&iopf->list, &fault_param->partial);mutex_unlock (&fault_param->lock); -return 0 ; +if (ret) +kfree (iopf); + +return ret; } static struct iopf_group *iopf_group_alloc(struct iommu_fault_param *iopf_param,
@@ -479,6 +486,7 @@ void iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) goto unlock;mutex_lock (&fault_param->lock); + fault_param->removing = true;list_for_each_entry_safe (partial_iopf, next, &fault_param->partial, list) {list_del (&partial_iopf->list);kfree (partial_iopf);
include/linux/iommu.h +3 -0
@@ -846,6 +846,8 @@ struct iommu_device {* @partial: faults that are part of a Page Request Group for which the last * request hasn't been submitted yet. * @faults: holds the pending faults which need response +* @removing: the device is being removed from the queue, no new partial +* faults may be queued */ struct iommu_fault_param {struct mutex lock;
@@ -858,6 +860,7 @@ struct iommu_fault_param {struct list_head partial;struct list_head faults; +bool removing; };/**
Quoted 46 lines · Qihang
iopf_queue_remove_device()kfree()s the entries onfault_param->partialwithout unlinking them, leaving the list head pointing into freed memory. Sincea74c077b9021the fault parameter is refcounted and can outlive the drain, a report that took its reference earlier can stilllist_add()through those pointers. Unlinking instead turns that into a leak: the late node lands on a parameter that is no longer onqueue->devices, whose final put does not free partial faults.This needs a device removed from its iopf queue while a non-last (partial) request is in flight, with the caller not blocking new reports for the duration.
Patch 1 unlinks each partial before freeing it, matching
iopf_queue_discard_partial().Patch 2 adds
fault_param->removing, set and tested underfault_param->lock: a late partial is either drained, or rejected and freed byreport_partial_fault()(-ENODEV). It covers partial-list admission only; callers must still quiesce reports for the remaining lifetimes.Both patches are scoped to
fault_param->partial, not the group path, the attach handle, or paths that never calliopf_queue_remove_device().Verified on v7.4-rc3 with KASAN +
DEBUG_LISTand the iommufd mock driver, its caller-side IOPF serialization deliberately removed and test-only delays added; this shows the mechanism, not production reachability. The runs cover queue-before-removal, removal-before-insertion, concurrent late reporters and re-enable. Each round queues one partial before the removal and then races the late reporters against the drain, so a clean run ends with every node accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated = drained + rejected), counted at a final removal with no reporter in flight. Nothing was left onfault_param->partialand KASAN stayed quiet.With patch 1 reverted, the report is:
BUG: KASAN: slab-use-after-free in __list_add_valid_or_report Freed by task 1: kfree <- iopf_queue_remove_device 56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)With patch 1 only, the same reproducer runs 1460 rounds without a KASAN report and leaks one late node per round, which patch 2 addresses.
Quoted 8 lines
Reachability on an unmodified production kernel, and any security impact, remain unconfirmed: the test shows memory corruption and a KASAN panic on the modified mock driver, not a user-triggerable bug on a stock kernel.
An AI coding assistant helped find this defect and build the reproducer and instrumentation, which are available on request along with the KASAN logs.
+Jason
The issues addressed by both patches are false positives. They rely on a fault being reported for a device after iopf_queue_remove_device() has been called for that device. The iommu driver must not allow that. Pending i/o page faults must be drained before the device is removed from its iommu's io page fault queue.
Thanks,
baolu
Hi Baolu,
Thanks. I agree that the driver must drain pending faults before calling iopf_queue_remove_device(). My selftest removed the mock driver's serialization, so it doesn't establish that fault reporting can overlap removal in an unmodified driver. I'm withdrawing this series and won't resend it.
I still have two questions about where the drivers establish that guarantee.
On VT-d, blocking_domain_attach_dev() calls iopf_for_domain_remove(old, dev) before device_block_translation(dev). When that drops the last IOPF reference, it calls iopf_queue_remove_device(). On an SM-capable IOMMU, for a device that isn't a real DMA subdevice, the only PRQ drain on this path is reached through device_block_translation() → intel_pasid_tear_down_entry() → intel_iommu_drain_pasid_prq(). By then iopf_refcount is zero, so the drain returns early.
In the same file, identity_domain_attach_dev() calls device_block_translation(dev) first and releases the old domain's IOPF reference afterwards. The comment there explains that putting the device in the blocking state leaves the IOPF reference count unchanged. Should blocking_domain_attach_dev() use the same order? If the current order is intentional, what ensures that pending PRQ reports for this device have drained before the last reference is dropped?
On AMD, detach_device() calls iopf_queue_flush_dev() immediately before amd_iommu_iopf_remove_device(). The flush helper waits for queued work and documents that the caller must have flushed the low-level queue first. The PPR interrupt thread calls iommu_report_device_fault() directly, so waiting for queued work alone doesn't establish that the thread has finished reporting faults for this device. What in the detach path ensures that PPR reporting for this device has quiesced before the device is removed from the IOPF queue?
I may be missing a step in both cases. A pointer would be enough.
Thanks,
Qihang
Quoted 67 lines · Baolu Lu
Quoted 46 lines · Qihang
iopf_queue_remove_device()kfree()s the entries onfault_param->partialwithout unlinking them, leaving the list head pointing into freed memory. Sincea74c077b9021the fault parameter is refcounted and can outlive the drain, a report that took its reference earlier can stilllist_add()through those pointers. Unlinking instead turns that into a leak: the late node lands on a parameter that is no longer onqueue->devices, whose final put does not free partial faults.This needs a device removed from its iopf queue while a non-last (partial) request is in flight, with the caller not blocking new reports for the duration.
Patch 1 unlinks each partial before freeing it, matching
iopf_queue_discard_partial().Patch 2 adds
fault_param->removing, set and tested underfault_param->lock: a late partial is either drained, or rejected and freed byreport_partial_fault()(-ENODEV). It covers partial-list admission only; callers must still quiesce reports for the remaining lifetimes.Both patches are scoped to
fault_param->partial, not the group path, the attach handle, or paths that never calliopf_queue_remove_device().Verified on v7.4-rc3 with KASAN +
DEBUG_LISTand the iommufd mock driver, its caller-side IOPF serialization deliberately removed and test-only delays added; this shows the mechanism, not production reachability. The runs cover queue-before-removal, removal-before-insertion, concurrent late reporters and re-enable. Each round queues one partial before the removal and then races the late reporters against the drain, so a clean run ends with every node accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated = drained + rejected), counted at a final removal with no reporter in flight. Nothing was left onfault_param->partialand KASAN stayed quiet.With patch 1 reverted, the report is:
BUG: KASAN: slab-use-after-free in __list_add_valid_or_report Freed by task 1: kfree <- iopf_queue_remove_device 56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)With patch 1 only, the same reproducer runs 1460 rounds without a KASAN report and leaks one late node per round, which patch 2 addresses.
Quoted 8 lines
Reachability on an unmodified production kernel, and any security impact, remain unconfirmed: the test shows memory corruption and a KASAN panic on the modified mock driver, not a user-triggerable bug on a stock kernel.
An AI coding assistant helped find this defect and build the reproducer and instrumentation, which are available on request along with the KASAN logs.
+Jason
The issues addressed by both patches are false positives. They rely on a fault being reported for a device after
iopf_queue_remove_device()has been called for that device. The iommu driver must not allow that. Pending i/o page faults must be drained before the device is removed from its iommu's io page fault queue.Thanks,
baolu
Quoted 19 lines · Qihang
Hi Baolu,
Thanks. I agree that the driver must drain pending faults before calling
iopf_queue_remove_device(). My selftest removed the mock driver's serialization, so it doesn't establish that fault reporting can overlap removal in an unmodified driver. I'm withdrawing this series and won't resend it.I still have two questions about where the drivers establish that guarantee.
On VT-d,
blocking_domain_attach_dev()callsiopf_for_domain_remove(old, dev)beforedevice_block_translation(dev). When that drops the last IOPF reference, it callsiopf_queue_remove_device(). On an SM-capable IOMMU, for a device that isn't a real DMA subdevice, the only PRQ drain on this path is reached throughdevice_block_translation()→intel_pasid_tear_down_entry()→intel_iommu_drain_pasid_prq(). By theniopf_refcountis zero, so the drain returns early.
Quoted 8 lines
In the same file,
identity_domain_attach_dev()callsdevice_block_translation(dev)first and releases the old domain's IOPF reference afterwards. The comment there explains that putting the device in the blocking state leaves the IOPF reference count unchanged. Shouldblocking_domain_attach_dev()use the same order? If the current order is intentional, what ensures that pending PRQ reports for this device have drained before the last reference is dropped?
Good catch. This is a bug in the vt-d driver. I already have a patch that changes the order in blocking_domain_attach_dev(). I will submit it for discussion after the necessary assessment.
Quoted 10 lines
On AMD,
detach_device()callsiopf_queue_flush_dev()immediately beforeamd_iommu_iopf_remove_device(). The flush helper waits for queued work and documents that the caller must have flushed the low-level queue first. The PPR interrupt thread callsiommu_report_device_fault()directly, so waiting for queued work alone doesn't establish that the thread has finished reporting faults for this device. What in the detach path ensures that PPR reporting for this device has quiesced before the device is removed from the IOPF queue?
Quoted 73 lines
I may be missing a step in both cases. A pointer would be enough.
Thanks,
QihangQuoted 67 lines · Baolu Lu
Quoted 46 lines · Qihang
iopf_queue_remove_device()kfree()s the entries onfault_param->partialwithout unlinking them, leaving the list head pointing into freed memory. Sincea74c077b9021the fault parameter is refcounted and can outlive the drain, a report that took its reference earlier can stilllist_add()through those pointers. Unlinking instead turns that into a leak: the late node lands on a parameter that is no longer onqueue->devices, whose final put does not free partial faults.This needs a device removed from its iopf queue while a non-last (partial) request is in flight, with the caller not blocking new reports for the duration.
Patch 1 unlinks each partial before freeing it, matching
iopf_queue_discard_partial().Patch 2 adds
fault_param->removing, set and tested underfault_param->lock: a late partial is either drained, or rejected and freed byreport_partial_fault()(-ENODEV). It covers partial-list admission only; callers must still quiesce reports for the remaining lifetimes.Both patches are scoped to
fault_param->partial, not the group path, the attach handle, or paths that never calliopf_queue_remove_device().Verified on v7.4-rc3 with KASAN +
DEBUG_LISTand the iommufd mock driver, its caller-side IOPF serialization deliberately removed and test-only delays added; this shows the mechanism, not production reachability. The runs cover queue-before-removal, removal-before-insertion, concurrent late reporters and re-enable. Each round queues one partial before the removal and then races the late reporters against the drain, so a clean run ends with every node accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated = drained + rejected), counted at a final removal with no reporter in flight. Nothing was left onfault_param->partialand KASAN stayed quiet.With patch 1 reverted, the report is:
BUG: KASAN: slab-use-after-free in __list_add_valid_or_report Freed by task 1: kfree <- iopf_queue_remove_device 56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)With patch 1 only, the same reproducer runs 1460 rounds without a KASAN report and leaks one late node per round, which patch 2 addresses.
Quoted 8 lines
Reachability on an unmodified production kernel, and any security impact, remain unconfirmed: the test shows memory corruption and a KASAN panic on the modified mock driver, not a user-triggerable bug on a stock kernel.
An AI coding assistant helped find this defect and build the reproducer and instrumentation, which are available on request along with the KASAN logs.
+Jason
The issues addressed by both patches are false positives. They rely on a fault being reported for a device after
iopf_queue_remove_device()has been called for that device. The iommu driver must not allow that. Pending i/o page faults must be drained before the device is removed from its iommu's io page fault queue.Thanks,
baolu
Thanks,
baolu
Hi Baolu,
Thanks for confirming the VT-d ordering bug. I'll keep this series withdrawn and won't resend it.
I still have one question about AMD. detach_device() calls iopf_queue_flush_dev() just before amd_iommu_iopf_remove_device(). That flush only waits on the iopf workqueue, while the PPR IRQ thread runs amd_iommu_poll_ppr_log() and reports the fault through iommu_report_device_fault() directly. What ensures that the thread has finished reporting faults for this device, and cannot report another one, before the device is removed from the IOPF queue?
Thanks,
Qihang
Quoted 120 lines · Baolu Lu
Quoted 19 lines · Qihang
Hi Baolu,
Thanks. I agree that the driver must drain pending faults before calling
iopf_queue_remove_device(). My selftest removed the mock driver's serialization, so it doesn't establish that fault reporting can overlap removal in an unmodified driver. I'm withdrawing this series and won't resend it.I still have two questions about where the drivers establish that guarantee.
On VT-d,
blocking_domain_attach_dev()callsiopf_for_domain_remove(old, dev)beforedevice_block_translation(dev). When that drops the last IOPF reference, it callsiopf_queue_remove_device(). On an SM-capable IOMMU, for a device that isn't a real DMA subdevice, the only PRQ drain on this path is reached throughdevice_block_translation()→intel_pasid_tear_down_entry()→intel_iommu_drain_pasid_prq(). By theniopf_refcountis zero, so the drain returns early.Quoted 8 lines
In the same file,
identity_domain_attach_dev()callsdevice_block_translation(dev)first and releases the old domain's IOPF reference afterwards. The comment there explains that putting the device in the blocking state leaves the IOPF reference count unchanged. Shouldblocking_domain_attach_dev()use the same order? If the current order is intentional, what ensures that pending PRQ reports for this device have drained before the last reference is dropped?Good catch. This is a bug in the vt-d driver. I already have a patch that changes the order in
blocking_domain_attach_dev(). I will submit it for discussion after the necessary assessment.Quoted 10 lines
On AMD,
detach_device()callsiopf_queue_flush_dev()immediately beforeamd_iommu_iopf_remove_device(). The flush helper waits for queued work and documents that the caller must have flushed the low-level queue first. The PPR interrupt thread callsiommu_report_device_fault()directly, so waiting for queued work alone doesn't establish that the thread has finished reporting faults for this device. What in the detach path ensures that PPR reporting for this device has quiesced before the device is removed from the IOPF queue?Quoted 73 lines
I may be missing a step in both cases. A pointer would be enough.
Thanks,
QihangQuoted 67 lines · Baolu Lu
Quoted 46 lines · Qihang
iopf_queue_remove_device()kfree()s the entries onfault_param->partialwithout unlinking them, leaving the list head pointing into freed memory. Sincea74c077b9021the fault parameter is refcounted and can outlive the drain, a report that took its reference earlier can stilllist_add()through those pointers. Unlinking instead turns that into a leak: the late node lands on a parameter that is no longer onqueue->devices, whose final put does not free partial faults.This needs a device removed from its iopf queue while a non-last (partial) request is in flight, with the caller not blocking new reports for the duration.
Patch 1 unlinks each partial before freeing it, matching
iopf_queue_discard_partial().Patch 2 adds
fault_param->removing, set and tested underfault_param->lock: a late partial is either drained, or rejected and freed byreport_partial_fault()(-ENODEV). It covers partial-list admission only; callers must still quiesce reports for the remaining lifetimes.Both patches are scoped to
fault_param->partial, not the group path, the attach handle, or paths that never calliopf_queue_remove_device().Verified on v7.4-rc3 with KASAN +
DEBUG_LISTand the iommufd mock driver, its caller-side IOPF serialization deliberately removed and test-only delays added; this shows the mechanism, not production reachability. The runs cover queue-before-removal, removal-before-insertion, concurrent late reporters and re-enable. Each round queues one partial before the removal and then races the late reporters against the drain, so a clean run ends with every node accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated = drained + rejected), counted at a final removal with no reporter in flight. Nothing was left onfault_param->partialand KASAN stayed quiet.With patch 1 reverted, the report is:
BUG: KASAN: slab-use-after-free in __list_add_valid_or_report Freed by task 1: kfree <- iopf_queue_remove_device 56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)With patch 1 only, the same reproducer runs 1460 rounds without a KASAN report and leaks one late node per round, which patch 2 addresses.
Quoted 8 lines
Reachability on an unmodified production kernel, and any security impact, remain unconfirmed: the test shows memory corruption and a KASAN panic on the modified mock driver, not a user-triggerable bug on a stock kernel.
An AI coding assistant helped find this defect and build the reproducer and instrumentation, which are available on request along with the KASAN logs.
+Jason
The issues addressed by both patches are false positives. They rely on a fault being reported for a device after
iopf_queue_remove_device()has been called for that device. The iommu driver must not allow that. Pending i/o page faults must be drained before the device is removed from its iommu's io page fault queue.Thanks,
baoluThanks,
baolu
Quoted 5 lines · Qihang
Hi Baolu,
Thanks for confirming the VT-d ordering bug. I'll keep this series withdrawn and won't resend it.
Quoted 7 lines
I still have one question about AMD.
detach_device()callsiopf_queue_flush_dev()just beforeamd_iommu_iopf_remove_device(). That flush only waits on the iopf workqueue, while the PPR IRQ thread runsamd_iommu_poll_ppr_log()and reports the fault throughiommu_report_device_fault()directly. What ensures that the thread has finished reporting faults for this device, and cannot report another one, before the device is removed from the IOPF queue?
+ Vasant
Quoted 2 lines
Thanks,
Qihang
Thanks,
baolu