Skip to content

Commit cbd2314

Browse files
jpbruckerwilldeacon
authored andcommitted
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: 32784a9 ("iommu/arm-smmu-v3: Implement iommu_sva_bind/unbind()") Signed-off-by: Jean-Philippe Brucker <[email protected]> Tested-by: Zhangfei Gao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent b131fa8 commit cbd2314

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/mm.h>
77
#include <linux/mmu_context.h>
88
#include <linux/mmu_notifier.h>
9+
#include <linux/sched/mm.h>
910
#include <linux/slab.h>
1011

1112
#include "arm-smmu-v3.h"
@@ -96,9 +97,14 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
9697
struct arm_smmu_ctx_desc *cd;
9798
struct arm_smmu_ctx_desc *ret = NULL;
9899

100+
/* Don't free the mm until we release the ASID */
101+
mmgrab(mm);
102+
99103
asid = arm64_mm_context_get(mm);
100-
if (!asid)
101-
return ERR_PTR(-ESRCH);
104+
if (!asid) {
105+
err = -ESRCH;
106+
goto out_drop_mm;
107+
}
102108

103109
cd = kzalloc(sizeof(*cd), GFP_KERNEL);
104110
if (!cd) {
@@ -165,6 +171,8 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
165171
kfree(cd);
166172
out_put_context:
167173
arm64_mm_context_put(mm);
174+
out_drop_mm:
175+
mmdrop(mm);
168176
return err < 0 ? ERR_PTR(err) : ret;
169177
}
170178

@@ -173,6 +181,7 @@ static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
173181
if (arm_smmu_free_asid(cd)) {
174182
/* Unpin ASID */
175183
arm64_mm_context_put(cd->mm);
184+
mmdrop(cd->mm);
176185
kfree(cd);
177186
}
178187
}

0 commit comments

Comments
 (0)