Skip to contentSkip to conversation

iommu/sva: Fix PASID use-after-free issue

PATCH Applied Fenghua Yu 4 participants 5 messages Apr 28, 2022 – May 6, 2022

Subsystems (3)
lore
Share & tools
lore

Fenghua Yu

author
#0 lore raw

PATCH v2 1/1 iommu/sva: Fix PASID use-after-free issue

The PASID is being freed too early. It needs to stay around until after device drivers that might be using it have had a chance to clear it out of the hardware.

As a reminder:

mmget() /mmput() refcount the mm's address space
mmgrab()/mmdrop() refcount the mm itself

The PASID is currently tied to the life of the mm's address space and freed in __mmput(). This makes logical sense because the PASID can't be used once the address space is gone.

But, this misses an important point: even after the address space is gone, the PASID will still be programmed into a device. Device drivers might, for instance, still need to flush operations that are outstanding and need to use that PASID. They do this at file->release() time.

Device drivers call the IOMMU driver to hold a reference on the mm itself and drop it at file->release() time. But, the IOMMU driver holds a reference on the mm itself, not the address space. The address space (and the PASID) is long gone by the time the driver tries to clean up. This is effectively a use-after-free bug on the PASID.

To fix this, move the PASID free operation from __mmput() to __mmdrop(). This ensures that the IOMMU driver's existing mmgrab() keeps the PASID allocated until it drops its mm reference.

Fixes 701fac40384f ("iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit")

---

v2:
- Dave Hansen rewrites the change log.
- Add Tested-by: Zhangfei Gao <[email protected]>
- Add Reviewed-by: Jean-Philippe Brucker <[email protected]>

The original patch was posted and discussed in:
https://lore.kernel.org/lkml/[email protected]/

 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index 9796897560ab..35a3beff140b 100644
@@ -792,6 +792,7 @@ void __mmdrop(struct mm_struct *mm)
 	mmu_notifier_subscriptions_destroy(mm);
 	check_mm(mm);
 	put_user_ns(mm->user_ns);
+	mm_pasid_drop(mm);
 	free_mm(mm);
 }
 EXPORT_SYMBOL_GPL(__mmdrop);
@@ -1190,7 +1191,6 @@ static inline void __mmput(struct mm_struct *mm)
 	}
 	if (mm->binfmt)
 		module_put(mm->binfmt->module);
-	mm_pasid_drop(mm);
 	mmdrop(mm);
 }
 
Signature · 1 line
2.32.0
Tested-by on v2 1/1
#1 lore raw
Quoted 24 lines · Fenghua Yu

The PASID is being freed too early. It needs to stay around until after device drivers that might be using it have had a chance to clear it out of the hardware.

As a reminder:

mmget() /mmput() refcount the mm's address space
mmgrab()/mmdrop() refcount the mm itself

The PASID is currently tied to the life of the mm's address space and freed in __mmput(). This makes logical sense because the PASID can't be used once the address space is gone.

But, this misses an important point: even after the address space is gone, the PASID will still be programmed into a device. Device drivers might, for instance, still need to flush operations that are outstanding and need to use that PASID. They do this at file->release() time.

Device drivers call the IOMMU driver to hold a reference on the mm itself and drop it at file->release() time. But, the IOMMU driver holds a reference on the mm itself, not the address space. The address space (and the PASID) is long gone by the time the driver tries to clean up. This is effectively a use-after-free bug on the PASID.

Quoted 8 lines

To fix this, move the PASID free operation from __mmput() to __mmdrop(). This ensures that the IOMMU driver's existing mmgrab() keeps the PASID allocated until it drops its mm reference.

Fixes 701fac40384f ("iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit")

Use the formal email, thanks

Quoted 37 lines

---

v2:
- Dave Hansen rewrites the change log.
- Add Tested-by: Zhangfei Gao <[email protected]>
- Add Reviewed-by: Jean-Philippe Brucker <[email protected]>

The original patch was posted and discussed in:
https://lore.kernel.org/lkml/[email protected]/

  kernel/fork.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index 9796897560ab..35a3beff140b 100644
@@ -792,6 +792,7 @@ void __mmdrop(struct mm_struct *mm)
  	mmu_notifier_subscriptions_destroy(mm);
  	check_mm(mm);
  	put_user_ns(mm->user_ns);
+	mm_pasid_drop(mm);
  	free_mm(mm);
  }
  EXPORT_SYMBOL_GPL(__mmdrop);
@@ -1190,7 +1191,6 @@ static inline void __mmput(struct mm_struct *mm)
  	}
  	if (mm->binfmt)
  		module_put(mm->binfmt->module);
-	mm_pasid_drop(mm);
  	mmdrop(mm);
  }
 
#2 lore raw
Quoted 31 lines · Zhangfei Gao
Quoted 28 lines · Fenghua Yu

The PASID is being freed too early.  It needs to stay around until after device drivers that might be using it have had a chance to clear it out of the hardware.

As a reminder:

mmget() /mmput()  refcount the mm's address space
mmgrab()/mmdrop() refcount the mm itself

The PASID is currently tied to the life of the mm's address space and freed in __mmput().  This makes logical sense because the PASID can't be used once the address space is gone.

But, this misses an important point: even after the address space is gone, the PASID will still be programmed into a device.  Device drivers might, for instance, still need to flush operations that are outstanding and need to use that PASID.  They do this at file->release() time.

Device drivers call the IOMMU driver to hold a reference on the mm itself and drop it at file->release() time.  But, the IOMMU driver holds a reference on the mm itself, not the address space.  The address space (and the PASID) is long gone by the time the driver tries to clean up. This is effectively a use-after-free bug on the PASID.

To fix this, move the PASID free operation from __mmput() to __mmdrop(). This ensures that the IOMMU driver's existing mmgrab() keeps the PASID allocated until it drops its mm reference.

Quoted 13 lines
Quoted 4 lines
Fixes 701fac40384f ("iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit")

Use the formal email, thanks

Quoted 4 lines

Hi,

Will this be taken for 5.18?

Thanks

Quoted 24 lines
Quoted 24 lines

---

v2:
- Dave Hansen rewrites the change log.
- Add Tested-by: Zhangfei Gao <[email protected]>
- Add Reviewed-by: Jean-Philippe Brucker <[email protected]>

The original patch was posted and discussed in:
https://lore.kernel.org/lkml/[email protected]/

  kernel/fork.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -0
index 9796897560ab..35a3beff140b 100644
@@ -792,6 +792,7 @@ void __mmdrop(struct mm_struct *mm)
       mmu_notifier_subscriptions_destroy(mm);
       check_mm(mm);
       put_user_ns(mm->user_ns);
+    mm_pasid_drop(mm);
       free_mm(mm);
   }
Quoted 9 lines

kernel/fork.c static inline void __mmput(struct mm_struct *mm) · line 1195

Quoted 8 lines
kernel/fork.c +0 -1
   EXPORT_SYMBOL_GPL(__mmdrop);
@@ -1190,7 +1191,6 @@ static inline void __mmput(struct mm_struct *mm)
       }
       if (mm->binfmt)
           module_put(mm->binfmt->module);
-    mm_pasid_drop(mm);
       mmdrop(mm);
   }

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw
Quoted 1 line · Zhangfei Gao

Will this be taken for 5.18?

It's queued in tip core/urgent and will go to Linus this week.

merge bot Applied
#4 lore raw

[tip: core/urgent] mm: Fix PASID use-after-free issue

In reply to Fenghua Yu
The following commit has been merged into the core/urgent branch of tip:

The following commit has been merged into the core/urgent branch of tip:

Commit-ID:     2667ed10d9f01e250ba806276740782c89d77fda
Gitweb:        https://git.kernel.org/tip/2667ed10d9f01e250ba806276740782c89d77fda
Author:        Fenghua Yu <[email protected]>
AuthorDate:    Thu, 28 Apr 2022 11:00:41 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sun, 01 May 2022 10:17:17 +02:00

mm: Fix PASID use-after-free issue

The PASID is being freed too early. It needs to stay around until after device drivers that might be using it have had a chance to clear it out of the hardware.

The relevant refcounts are:

mmget() /mmput() refcount the mm's address space mmgrab()/mmdrop() refcount the mm itself

The PASID is currently tied to the life of the mm's address space and freed in __mmput(). This makes logical sense because the PASID can't be used once the address space is gone.

But, this misses an important point: even after the address space is gone, the PASID will still be programmed into a device. Device drivers might, for instance, still need to flush operations that are outstanding and need to use that PASID. They do this at file->release() time.

Device drivers call the IOMMU driver to hold a reference on the mm itself and drop it at file->release() time. But, the IOMMU driver holds a reference on the mm itself, not the address space. The address space (and the PASID) is long gone by the time the driver tries to clean up. This is effectively a use-after-free bug on the PASID.

To fix this, move the PASID free operation from __mmput() to __mmdrop(). This ensures that the IOMMU driver's existing mmgrab() keeps the PASID allocated until it drops its mm reference.

---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index 9796897..35a3bef 100644
@@ -792,6 +792,7 @@ void __mmdrop(struct mm_struct *mm)
 	mmu_notifier_subscriptions_destroy(mm);
 	check_mm(mm);
 	put_user_ns(mm->user_ns);
+	mm_pasid_drop(mm);
 	free_mm(mm);
 }
 EXPORT_SYMBOL_GPL(__mmdrop);
@@ -1190,7 +1191,6 @@ static inline void __mmput(struct mm_struct *mm)
 	}
 	if (mm->binfmt)
 		module_put(mm->binfmt->module);
-	mm_pasid_drop(mm);
 	mmdrop(mm);
 }