Skip to contentSkip to conversation

arm64: mm: assign PXN permission in trans_pgd

PATCH In review Pingfan Liu 5 participants 10 messages Nov 11, 2021 – Nov 16, 2021 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mm: assign PXN permission in trans_pgd

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

p?d_populate() helpers resort to the input parameter mm_struct to decide the attribute: UXN or PXN. And plus the fact, either hibernation or kexec runs in privilege mode. So the bug can be fixed by assigning &init_mm to the callsites of these helpers.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

---
 arch/arm64/mm/trans_pgd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/trans_pgd.c +3 -3
index d7da8ca40d2e..7b43d179e94d 100644
@@ -67,7 +67,7 @@ static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
 	dst_ptep = trans_alloc(info);
 	if (!dst_ptep)
 		return -ENOMEM;
-	pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
+	pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
 	dst_ptep = pte_offset_kernel(dst_pmdp, start);
 
 	src_ptep = pte_offset_kernel(src_pmdp, start);
@@ -90,7 +90,7 @@ static int copy_pmd(struct trans_pgd_info *info, pud_t *dst_pudp,
 		dst_pmdp = trans_alloc(info);
 		if (!dst_pmdp)
 			return -ENOMEM;
-		pud_populate(NULL, dst_pudp, dst_pmdp);
+		pud_populate(&init_mm, dst_pudp, dst_pmdp);
 	}
 	dst_pmdp = pmd_offset(dst_pudp, start);
 
@@ -126,7 +126,7 @@ static int copy_pud(struct trans_pgd_info *info, p4d_t *dst_p4dp,
 		dst_pudp = trans_alloc(info);
 		if (!dst_pudp)
 			return -ENOMEM;
-		p4d_populate(NULL, dst_p4dp, dst_pudp);
+		p4d_populate(&init_mm, dst_p4dp, dst_pudp);
 	}
 	dst_pudp = pud_offset(dst_p4dp, start);
 
Signature · 7 lines
2.31.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#1 lore raw
Quoted 2 lines · Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

I think that's more important to fix than the PXN. You may want to change the subject to something like "Fix VM_BUG_ON ..."

Quoted 4 lines

p?d_populate() helpers resort to the input parameter mm_struct to decide the attribute: UXN or PXN. And plus the fact, either hibernation or kexec runs in privilege mode. So the bug can be fixed by assigning &init_mm to the callsites of these helpers.

That's the table PXN, so it's not a big deal if we miss it.

Quoted 7 lines
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

A Fixes: tag would be nice, we just need to decide which.

Quoted 9 lines
---
 arch/arm64/mm/trans_pgd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/trans_pgd.c +0 -0
index d7da8ca40d2e..7b43d179e94d 100644

arch/arm64/mm/trans_pgd.c static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp, · line 71

Quoted 6 lines
arch/arm64/mm/trans_pgd.c +1 -1
@@ -67,7 +67,7 @@ static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
 	dst_ptep = trans_alloc(info);
 	if (!dst_ptep)
 		return -ENOMEM;
-	pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
+	pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
 	dst_ptep = pte_offset_kernel(dst_pmdp, start);

Commit 5de59884ac0e ("arm64: trans_pgd: pass NULL instead of init_mm to *_populate functions") actually replaced &init_mm with NULL. Commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings") introduced the VM_BUG_ON.

So we either revert Pasha's trans_pgd commit (as per your patch) or we fix Ard's commit to make it aware of a NULL mm and either skip the table UXN altogether or treat it as init_mm.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#2 lore raw
Quoted 46 lines · Catalin Marinas
Quoted 2 lines · Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

I think that's more important to fix than the PXN. You may want to change the subject to something like "Fix VM_BUG_ON ..."

Quoted 4 lines

p?d_populate() helpers resort to the input parameter mm_struct to decide the attribute: UXN or PXN. And plus the fact, either hibernation or kexec runs in privilege mode. So the bug can be fixed by assigning &init_mm to the callsites of these helpers.

That's the table PXN, so it's not a big deal if we miss it.

Quoted 7 lines
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

A Fixes: tag would be nice, we just need to decide which.

Quoted 9 lines
---
 arch/arm64/mm/trans_pgd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/trans_pgd.c +0 -0
index d7da8ca40d2e..7b43d179e94d 100644

arch/arm64/mm/trans_pgd.c static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp, · line 71

Quoted 6 lines
arch/arm64/mm/trans_pgd.c +1 -1
@@ -67,7 +67,7 @@ static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
      dst_ptep = trans_alloc(info);
      if (!dst_ptep)
              return -ENOMEM;
-     pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
+     pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
      dst_ptep = pte_offset_kernel(dst_pmdp, start);

Commit 5de59884ac0e ("arm64: trans_pgd: pass NULL instead of init_mm to *_populate functions") actually replaced &init_mm with NULL. Commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings") introduced the VM_BUG_ON.

Quoted 4 lines

So we either revert Pasha's trans_pgd commit (as per your patch) or we fix Ard's commit to make it aware of a NULL mm and either skip the table UXN altogether or treat it as init_mm.

That code in pmd_populate_kernel() does not actually *use* the mm argument at all, it just BUGs if its value != init_mm. So we can just relax the BUG() to disregard mm == NULL.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#3 lore raw
Quoted 19 lines · Ard Biesheuvel
Quoted 18 lines · Catalin Marinas

arch/arm64/mm/trans_pgd.c static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp, · line 71

Quoted 11 lines · Pingfan Liu
arch/arm64/mm/trans_pgd.c +1 -1
index d7da8ca40d2e..7b43d179e94d 100644
@@ -67,7 +67,7 @@ static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
      dst_ptep = trans_alloc(info);
      if (!dst_ptep)
              return -ENOMEM;
-     pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
+     pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
      dst_ptep = pte_offset_kernel(dst_pmdp, start);

Commit 5de59884ac0e ("arm64: trans_pgd: pass NULL instead of init_mm to *_populate functions") actually replaced &init_mm with NULL. Commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings") introduced the VM_BUG_ON.

Quoted 8 lines
Quoted 4 lines

So we either revert Pasha's trans_pgd commit (as per your patch) or we fix Ard's commit to make it aware of a NULL mm and either skip the table UXN altogether or treat it as init_mm.

That code in pmd_populate_kernel() does not actually *use* the mm argument at all, it just BUGs if its value != init_mm. So we can just relax the BUG() to disregard mm == NULL.

We also have pud_populate() that ends up setting PUD_TABLE_PXN when mm is NULL. Fortunately we don't execute anything from those mappings, they are just a copy of the linear map.

BTW, in line with PAGE_KERNEL, pud_populate() could set both UXN and PXN for init_mm mappings.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#4 lore raw
Quoted 2 lines · Catalin Marinas

BTW, in line with PAGE_KERNEL, pud_populate() could set both UXN and PXN for init_mm mappings.

Ignore this, I got confused. The UXN/PXN choice is correct as it is.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw
In reply to Catalin Marinas
Quoted 19 lines · Catalin Marinas
Quoted 18 lines · Ard Biesheuvel
Quoted 17 lines · Catalin Marinas

arch/arm64/mm/trans_pgd.c static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp, · line 71

Quoted 11 lines · Pingfan Liu
arch/arm64/mm/trans_pgd.c +1 -1
index d7da8ca40d2e..7b43d179e94d 100644
@@ -67,7 +67,7 @@ static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
      dst_ptep = trans_alloc(info);
      if (!dst_ptep)
              return -ENOMEM;
-     pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
+     pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
      dst_ptep = pte_offset_kernel(dst_pmdp, start);

Commit 5de59884ac0e ("arm64: trans_pgd: pass NULL instead of init_mm to *_populate functions") actually replaced &init_mm with NULL. Commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings") introduced the VM_BUG_ON.

I will include this history in V2.

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

So we either revert Pasha's trans_pgd commit (as per your patch) or we fix Ard's commit to make it aware of a NULL mm and either skip the table UXN altogether or treat it as init_mm.

That code in pmd_populate_kernel() does not actually *use* the mm argument at all, it just BUGs if its value != init_mm. So we can just relax the BUG() to disregard mm == NULL.

This seems to be more reasonable solution than my V1.

Quoted 5 lines

We also have pud_populate() that ends up setting PUD_TABLE_PXN when mm is NULL. Fortunately we don't execute anything from those mappings, they are just a copy of the linear map.

Also included to V2.

Thank both of you for kindly review.

Regards,

Pingfan

Quoted 10 lines

BTW, in line with PAGE_KERNEL, pud_populate() could set both UXN and PXN for init_mm mappings.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#6 lore raw

PATCH v2 1/1 arm64: mm: Fix VM_BUG_ON(mm != &init_mm) for trans_pgd

In reply to Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

This is the combined consequence of commit 5de59884ac0e ("arm64:
trans_pgd: pass NULL instead of init_mm to *_populate functions"), which replaced &init_mm with NULL and commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings"), which introduced the VM_BUG_ON.

Since the former sounds reasonable, it is better to work on the later. From the perspective of trans_pgd, two groups of functions are considered in the later one:

pmd_populate_kernel()

    mm == NULL should be fixed, else it hits VM_BUG_ON()

p?d_populate()
mm == NULL means PXN, that is OK, since trans_pgd only copies a linear map, no execution will happen on the map.

So it is good enough to just relax VM_BUG_ON() to disregard mm == NULL

Fixes 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings")
Cc Catalin Marinas <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

---
 arch/arm64/include/asm/pgalloc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/pgalloc.h +1 -1
index 8433a2058eb1..237224484d0f 100644
@@ -76,7 +76,7 @@ static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
 static inline void
 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
 {
-	VM_BUG_ON(mm != &init_mm);
+	VM_BUG_ON(mm && mm != &init_mm);
 	__pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE | PMD_TABLE_UXN);
 }
 
Signature · 7 lines
2.31.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Reviewed-by on v2 1/1
#7 lore raw

Re: [PATCHv2] arm64: mm: Fix VM_BUG_ON(mm != &init_mm) for trans_pgd

Quoted 14 lines · Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

This is the combined consequence of commit 5de59884ac0e ("arm64:
trans_pgd: pass NULL instead of init_mm to *_populate functions"), which replaced &init_mm with NULL and commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings"), which introduced the VM_BUG_ON.

Since the former sounds reasonable, it is better to work on the later. From the perspective of trans_pgd, two groups of functions are considered in the later one:

pmd_populate_kernel()

Quoted 8 lines
    mm == NULL should be fixed, else it hits VM_BUG_ON()

p?d_populate()
mm == NULL means PXN, that is OK, since trans_pgd only copies a linear map, no execution will happen on the map.

So it is good enough to just relax VM_BUG_ON() to disregard mm == NULL

Fixes 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings")
Cc <[email protected]> # 5.13.x
Quoted 8 lines
Cc Catalin Marinas <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

It looks fine to me.

I'll leave it to Will to queue as a fix.

Thanks.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v2 1/1
#8 lore raw

Re: [PATCHv2] arm64: mm: Fix VM_BUG_ON(mm != &init_mm) for trans_pgd

In reply to Pingfan Liu
Quoted 40 lines · Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

This is the combined consequence of commit 5de59884ac0e ("arm64:
trans_pgd: pass NULL instead of init_mm to *_populate functions"), which replaced &init_mm with NULL and commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings"), which introduced the VM_BUG_ON.

Since the former sounds reasonable, it is better to work on the later. From the perspective of trans_pgd, two groups of functions are considered in the later one:

pmd_populate_kernel()

    mm == NULL should be fixed, else it hits VM_BUG_ON()

p?d_populate()
mm == NULL means PXN, that is OK, since trans_pgd only copies a linear map, no execution will happen on the map.

So it is good enough to just relax VM_BUG_ON() to disregard mm == NULL

Fixes 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings")
Cc Catalin Marinas <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Will Deacon <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc James Morse <[email protected]>
Cc Matthias Brugger <[email protected]>

To: [email protected]

---
 arch/arm64/include/asm/pgalloc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/pgalloc.h +0 -0
index 8433a2058eb1..237224484d0f 100644

arch/arm64/include/asm/pgalloc.h static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep, · line 79

Quoted 5 lines
arch/arm64/include/asm/pgalloc.h +1 -1
@@ -76,7 +76,7 @@ static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
 static inline void
 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
 {
-       VM_BUG_ON(mm != &init_mm);
+       VM_BUG_ON(mm && mm != &init_mm);

LGTM

Pasha

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer Applied
#9 lore raw

Re: [PATCHv2] arm64: mm: Fix VM_BUG_ON(mm != &init_mm) for trans_pgd

In reply to Pingfan Liu
Quoted 10 lines · Pingfan Liu

trans_pgd_create_copy() can hit "VM_BUG_ON(mm != &init_mm)" in the function pmd_populate_kernel().

This is the combined consequence of commit 5de59884ac0e ("arm64:
trans_pgd: pass NULL instead of init_mm to *_populate functions"), which replaced &init_mm with NULL and commit 59511cfd08f3 ("arm64: mm: use XN table mapping attributes for user/kernel mappings"), which introduced the VM_BUG_ON.

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64: mm: Fix VM_BUG_ON(mm != &init_mm) for trans_pgd

      https://git.kernel.org/arm64/c/d3eb70ead647

Cheers,

Signature · 10 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel