Skip to contentSkip to conversation

iommu/arm-smmu-v3-sva: Fix mm use-after-free

PATCH Superseded Jean-Philippe Brucker 2 participants 3 messages Apr 26, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/arm-smmu-v3-sva: Fix mm use-after-free

We currently call arm64_mm_context_put() without holding a reference to the mm, which can result in use-after-free. Call mmgrab()/mmdrop() to ensure the mm only gets freed after we unpinned the ASID.

Fixes 32784a9562fb ("iommu/arm-smmu-v3: Implement iommu_sva_bind/unbind()")
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +10 -2
index 582114f94da0..c93477a2d7f1 100644
@@ -98,9 +98,14 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
 	struct arm_smmu_ctx_desc *cd;
 	struct arm_smmu_ctx_desc *ret = NULL;
 
+	/* Don't free the mm until we release the ASID */
+	mmgrab(mm);
+
 	asid = arm64_mm_context_get(mm);
-	if (!asid)
-		return ERR_PTR(-ESRCH);
+	if (!asid) {
+		err = -ESRCH;
+		goto out_drop_mm;
+	}
 
 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
 	if (!cd) {
@@ -167,6 +172,8 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
 	kfree(cd);
 out_put_context:
 	arm64_mm_context_put(mm);
+out_drop_mm:
+	mmdrop(mm);
 	return err < 0 ? ERR_PTR(err) : ret;
 }
 
@@ -175,6 +182,7 @@ static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
 	if (arm_smmu_free_asid(cd)) {
 		/* Unpin ASID */
 		arm64_mm_context_put(cd->mm);
+		mmdrop(cd->mm);
 		kfree(cd);
 	}
 }
Signature · 6 lines
2.35.1

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

Hi, Jean

Quoted 9 lines · Jean-Philippe Brucker

We currently call arm64_mm_context_put() without holding a reference to the mm, which can result in use-after-free. Call mmgrab()/mmdrop() to ensure the mm only gets freed after we unpinned the ASID.

Fixes 32784a9562fb ("iommu/arm-smmu-v3: Implement iommu_sva_bind/unbind()")
---
  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)

Missing +#include <linux/sched/mm.h> for compile.

We still need the fix move mm_pasid_drop from __mmput to __mmdrop, right?

1. Test OK with the mm_pasid_drop patch.

2. Test fail if revert the mm_pasid_drop patch,
uacce_fops_release

Unable to handle kernel paging request at virtual address ffff00083cc6ffc0

By the way, we use mmgrab in bind and mmput in unbind before, then the fops_release is not be called if exit without close(fd).

This patch does not have this issue, still not understand why.

Thanks

Quoted 31 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +9 -2
index 582114f94da0..c93477a2d7f1 100644
@@ -98,9 +98,14 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
  	struct arm_smmu_ctx_desc *cd;
  	struct arm_smmu_ctx_desc *ret = NULL;
 
+	/* Don't free the mm until we release the ASID */
+	mmgrab(mm);
+
  	asid = arm64_mm_context_get(mm);
-	if (!asid)
-		return ERR_PTR(-ESRCH);
+	if (!asid) {
+		err = -ESRCH;
+		goto out_drop_mm;
+	}
 
  	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  	if (!cd) {
@@ -167,6 +172,8 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
  	kfree(cd);
  out_put_context:
  	arm64_mm_context_put(mm);
+out_drop_mm:
+	mmdrop(mm);
  	return err < 0 ? ERR_PTR(err) : ret;
  }
 

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) · line 188

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -0
@@ -175,6 +182,7 @@ static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
  	if (arm_smmu_free_asid(cd)) {
  		/* Unpin ASID */
  		arm64_mm_context_put(cd->mm);
+		mmdrop(cd->mm);
  		kfree(cd);
  	}
  }

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

#2 lore raw
Quoted 7 lines · On Tue, Apr 26, 2022 at 08:20:52PM +0800, [email protected] wrote:

Hi, Jean

Quoted 4 lines · Jean-Philippe Brucker

We currently call arm64_mm_context_put() without holding a reference to the mm, which can result in use-after-free. Call mmgrab()/mmdrop() to ensure the mm only gets freed after we unpinned the ASID.

Quoted 6 lines
Quoted 5 lines
Fixes 32784a9562fb ("iommu/arm-smmu-v3: Implement iommu_sva_bind/unbind()")
---
  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)

Missing +#include <linux/sched/mm.h> for compile.

Ah thanks, I sent the wrong version.

Quoted 1 line

We still need the fix move mm_pasid_drop from __mmput to __mmdrop, right?

Yes, that's Fenghua's patch

Thanks,
Jean

Quoted 41 lines

1. Test OK with the mm_pasid_drop patch.

2. Test fail if revert the mm_pasid_drop patch,
uacce_fops_release

Unable to handle kernel paging request at virtual address ffff00083cc6ffc0

By the way, we use mmgrab in bind and mmput in unbind before, then the fops_release is not be called if exit without close(fd).

This patch does not have this issue, still not understand why.

Thanks

Quoted 27 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +9 -2
index 582114f94da0..c93477a2d7f1 100644
@@ -98,9 +98,14 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
  	struct arm_smmu_ctx_desc *cd;
  	struct arm_smmu_ctx_desc *ret = NULL;
+	/* Don't free the mm until we release the ASID */
+	mmgrab(mm);
+
  	asid = arm64_mm_context_get(mm);
-	if (!asid)
-		return ERR_PTR(-ESRCH);
+	if (!asid) {
+		err = -ESRCH;
+		goto out_drop_mm;
+	}
  	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  	if (!cd) {
@@ -167,6 +172,8 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
  	kfree(cd);
  out_put_context:
  	arm64_mm_context_put(mm);
+out_drop_mm:
+	mmdrop(mm);
  	return err < 0 ? ERR_PTR(err) : ret;
  }
Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) · line 188

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -0
@@ -175,6 +182,7 @@ static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
  	if (arm_smmu_free_asid(cd)) {
  		/* Unpin ASID */
  		arm64_mm_context_put(cd->mm);
+		mmdrop(cd->mm);
  		kfree(cd);
  	}
  }

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