x86/mpx: fix recursive munmap() corruption
Subsystems (7)
Dave Hansen
maintainerPATCH 1/1 x86/mpx: fix recursive munmap() corruption
This is a bit of a mess, to put it mildly. But, it's a bug that seems to have gone unticked up to now, probably because nobody uses MPX. The other alternative to this fix is to just deprecate MPX, even in -stable kernels.
MPX has the arch_unmap() hook inside of munmap() because MPX uses bounds tables that protect other areas of memory. When memory is unmapped, there is also a need to unmap the MPX bounds tables. Barring this, unused bounds tables can eat 80% of the address space.
But, the recursive do_munmap() that gets called vi arch_unmap() wreaks havoc with __do_munmap()'s state. It can result in freeing populated page tables, accessing bogus VMA state, double-freed VMAs and more.
To fix this, call arch_unmap() before __do_unmap() has a chance to do anything meaningful. Also, remove the 'vma' argument and force the MPX code to do its own, independent VMA lookup.
For the common success case this is functionally identical to what was there before. For the munmap() failure case, it's possible that some MPX tables will be zapped for memory that continues to be in use. But, this is an extraordinarily unlikely scenario and the harm would be that MPX provides no protection since the bounds table got reset (zeroed).
I can't imagine anyone doing this:
ptr = mmap(); // use ptr ret = munmap(ptr); if (ret) // oh, there was an error, I'll // keep using ptr.
Because if you're doing munmap(), you are *done* with the memory. There's probably no good data in there _anyway_.
This passes the original reproducer from Richard Biener as well as the existing mpx selftests/.
====
The long story:
munmap() has a couple of pieces:
1. Find the affected VMA(s)
2. Split the start/end one(s) if neceesary
3. Pull the VMAs out of the rbtree
4. Actually zap the memory via unmap_region(), including freeing page tables (or queueing them to be freed).
5. Fixup some of the accounting (like fput()) and actually free the VMA itself.
I decided to put the arch_unmap() call right afer #3. This was *just* before mmap_sem looked like it might get downgraded (it won't in this context), but it looked right. It wasn't.
Richard Biener reported a test that shows this in dmesg:
[1216548.787498] BUG: Bad rss-counter state mm:0000000017ce560b idx:1 val:551 [1216548.787500] BUG: non-zero pgtables_bytes on freeing mm: 24576
What triggered this was the recursive do_munmap() called via arch_unmap(). It was freeing page tables that has not been properly zapped.
But, the problem was bigger than this. For one, arch_unmap() can free VMAs. But, the calling __do_munmap() has variables that *point* to VMAs and obviously can't handle them just getting freed while the pointer is still valid.
I tried a couple of things here. First, I tried to fix the page table freeing problem in isolation, but I then found the VMA issue. I also tried having the MPX code return a flag if it modified the rbtree which would force __do_munmap() to re-walk to restart. That spiralled out of control in complexity pretty fast.
Just moving arch_unmap() and accepting that the bonkers failure case might eat some bounds tables seems like the simplest viable fix.
---
b/arch/x86/include/asm/mmu_context.h | 6 +++--- b/arch/x86/include/asm/mpx.h | 5 ++--- b/arch/x86/mm/mpx.c | 10 ++++++---- b/include/asm-generic/mm_hooks.h | 1 - b/mm/mmap.c | 15 ++++++++------- 5 files changed, 19 insertions(+), 18 deletions(-)
mm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +8 -7
@@ -2731,9 +2731,17 @@ int __do_munmap(struct mm_struct *mm, unreturn -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end); +/* Find the first overlapping VMA */ vma =find_vma (mm, start);if (!vma)
@@ -2742,7 +2750,6 @@ int __do_munmap(struct mm_struct *mm, un/* we have start < vma->vm_end */ /* if it doesn't overlap, we have nothing.. */ - end = start + len;if (vma->vm_start >= end)return 0 ;
@@ -2812,12 +2819,6 @@ int __do_munmap(struct mm_struct *mm, un/* Detach vmas from rbtree */ detach_vmas_to_be_unmapped (mm, vma, prev, end); -/* -* mpx unmap needs to be called with mmap_sem held for write. -* It is safe to call it before unmap_region(). -*/ -arch_unmap (mm, vma, start, end); -if (downgrade)downgrade_write (&mm->mmap_sem);
arch/x86/include/asm/mmu_context.h renamed from arch/x86/include/asm/mmu_context.h~mpx-rss-pass-no-vma +3 -3
@@ -277,8 +277,8 @@ static inline void arch_bprm_mm_init(strmpx_mm_init (mm); } -static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) {/* * mpx_notify_unmap() goes and reads a rarely-hot
@@ -298,7 +298,7 @@ static inline void arch_unmap(struct mm_* consistently wrong. */ if (unlikely (cpu_feature_enabled (X86_FEATURE_MPX ))) -mpx_notify_unmap (mm, vma, start, end); +mpx_notify_unmap (mm, start, end); }/*
include/asm-generic/mm_hooks.h renamed from include/asm-generic/mm_hooks.h~mpx-rss-pass-no-vma +0 -1
@@ -18,7 +18,6 @@ static inline void arch_exit_mmap(struct }static inline void arch_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~mpx-rss-pass-no-vma +6 -4
@@ -881,9 +881,10 @@ static int mpx_unmap_tables(struct mm_st* the virtual address region start...end have already been split if * necessary, and the 'vma' is the first vma in this range (start -> end). */ -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) { +struct vm_area_struct *vma;int ret;/*
@@ -902,11 +903,12 @@ void mpx_notify_unmap(struct mm_struct ** which should not occur normally. Being strict about it here * helps ensure that we do not have an exploitable stack overflow. */ -do { + vma =find_vma (mm, start); +while (vma && vma->vm_start < end) {if (vma->vm_flags &VM_MPX )return ; vma = vma->vm_next; - }while (vma && vma->vm_start < end); + } ret =mpx_unmap_tables (mm, start, end);if (ret)
arch/x86/include/asm/mpx.h renamed from arch/x86/include/asm/mpx.h~mpx-rss-pass-no-vma +2 -3
@@ -78,8 +78,8 @@ static inline void mpx_mm_init(struct mm*/ mm->context.bd_addr =MPX_INVALID_BOUNDS_DIR ; } -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end); +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end);unsigned long mpx_unmapped_area_check (unsigned long addr,unsigned long len,unsigned long flags);
@@ -100,7 +100,6 @@ static inline void mpx_mm_init(struct mm { }static inline void mpx_notify_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
_
tip-bot for Dave Hansen
merge bot[tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
x86/mpx: Fix recursive munmap() corruption
Commit-ID: 9d812ad8fce2c556538f6efe8ab1ae78ab59ae8f Gitweb: https://git.kernel.org/tip/9d812ad8fce2c556538f6efe8ab1ae78ab59ae8f Author: Dave Hansen <[email protected]> AuthorDate: Mon, 1 Apr 2019 07:15:49 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 18 Apr 2019 14:37:04 +0200
x86/mpx: Fix recursive munmap() corruption
This is a bit of a mess, to put it mildly. But, it's a bug that seems to have gone unticked up to now, probably because nobody uses MPX. The other alternative to this fix is to just deprecate MPX, even in -stable kernels.
MPX has the arch_unmap() hook inside of munmap() because MPX uses bounds tables that protect other areas of memory. When memory is unmapped, there is also a need to unmap the MPX bounds tables. Barring this, unused bounds tables can eat 80% of the address space.
But, the recursive do_munmap() that gets called via arch_unmap() wreaks havoc with __do_munmap()'s state. It can result in freeing populated page tables, accessing bogus VMA state, double-freed VMAs and more.
To fix this, call arch_unmap() before __do_unmap() has a chance to do anything meaningful. Also, remove the 'vma' argument and force the MPX code to do its own, independent VMA lookup.
For the common success case this is functionally identical to what was there before. For the munmap() failure case, it's possible that some MPX tables will be zapped for memory that continues to be in use. But, this is an extraordinarily unlikely scenario and the harm would be that MPX provides no protection since the bounds table got reset (zeroed).
It's hard to imagine that anyone is doing this:
ptr = mmap(); // use ptr ret = munmap(ptr); if (ret) // oh, there was an error, I'll // keep using ptr.
Because if after doing munmap(), the applitcation is *done* with the memory. There's probably no good data in there _anyway_.
This passes the original reproducer from Richard Biener as well as the existing mpx selftests/.
Further details:
munmap() has a couple of pieces:
1. Find the affected VMA(s)
2. Split the start/end one(s) if neceesary
3. Pull the VMAs out of the rbtree
4. Actually zap the memory via unmap_region(), including freeing page tables (or queueing them to be freed).
5. Fixup some of the accounting (like fput()) and actually free the VMA itself.
The original MPX implementation chose to put the arch_unmap() call right afer #3. This was *just* before mmap_sem looked like it might get downgraded (it won't in this context), but it looked right. It wasn't.
Richard Biener reported a test that shows this in dmesg:
[1216548.787498] BUG: Bad rss-counter state mm:0000000017ce560b idx:1 val:551 [1216548.787500] BUG: non-zero pgtables_bytes on freeing mm: 24576
What triggered this was the recursive do_munmap() called via arch_unmap(). It was freeing page tables that has not been properly zapped.
But, the problem was bigger than this. For one, arch_unmap() can free VMAs. But, the calling __do_munmap() has variables that *point* to VMAs and obviously can't handle them just getting freed while the pointer is still valid.
A couple of attempts were made to fix this. First, was to fix the page table freeing problem in isolation, but this lead to the VMA issue. The next approach was to let the MPX code return a flag if it modified the rbtree which would force __do_munmap() to re-walk to restart. That spiralled out of control in complexity pretty fast.
Just moving arch_unmap() and accepting that the bonkers failure case might eat some bounds tables is the simplest viable fix.
--- arch/x86/include/asm/mmu_context.h | 6 +++--- arch/x86/include/asm/mpx.h | 5 ++--- arch/x86/mm/mpx.c | 10 ++++++---- include/asm-generic/mm_hooks.h | 1 - mm/mmap.c | 15 ++++++++------- 5 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/include/asm/mmu_context.h +3 -3
@@ -277,8 +277,8 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,mpx_mm_init (mm); } -static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) {/* * mpx_notify_unmap() goes and reads a rarely-hot
@@ -298,7 +298,7 @@ static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* consistently wrong. */ if (unlikely (cpu_feature_enabled (X86_FEATURE_MPX ))) -mpx_notify_unmap (mm, vma, start, end); +mpx_notify_unmap (mm, start, end); }/*
arch/x86/include/asm/mpx.h +2 -3
@@ -78,8 +78,8 @@ static inline void mpx_mm_init(struct mm_struct *mm)*/ mm->context.bd_addr =MPX_INVALID_BOUNDS_DIR ; } -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end); +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end);unsigned long mpx_unmapped_area_check (unsigned long addr,unsigned long len,unsigned long flags);
@@ -100,7 +100,6 @@ static inline void mpx_mm_init(struct mm_struct *mm) { }static inline void mpx_notify_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
arch/x86/mm/mpx.c +6 -4
@@ -881,9 +881,10 @@ static int mpx_unmap_tables(struct mm_struct *mm,* the virtual address region start...end have already been split if * necessary, and the 'vma' is the first vma in this range (start -> end). */ -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) { +struct vm_area_struct *vma;int ret;/*
@@ -902,11 +903,12 @@ void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* which should not occur normally. Being strict about it here * helps ensure that we do not have an exploitable stack overflow. */ -do { + vma =find_vma (mm, start); +while (vma && vma->vm_start < end) {if (vma->vm_flags &VM_MPX )return ; vma = vma->vm_next; - }while (vma && vma->vm_start < end); + } ret =mpx_unmap_tables (mm, start, end);if (ret)
include/asm-generic/mm_hooks.h +0 -1
@@ -18,7 +18,6 @@ static inline void arch_exit_mmap(struct mm_struct *mm) }static inline void arch_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
mm/mmap.c +8 -7
@@ -2730,9 +2730,17 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end); +/* Find the first overlapping VMA */ vma =find_vma (mm, start);if (!vma)
@@ -2741,7 +2749,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* we have start < vma->vm_end */ /* if it doesn't overlap, we have nothing.. */ - end = start + len;if (vma->vm_start >= end)return 0 ;
@@ -2811,12 +2818,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* Detach vmas from rbtree */ detach_vmas_to_be_unmapped (mm, vma, prev, end); -/* -* mpx unmap needs to be called with mmap_sem held for write. -* It is safe to call it before unmap_region(). -*/ -arch_unmap (mm, vma, start, end); -if (downgrade)downgrade_write (&mm->mmap_sem);
tip-bot for Dave Hansen
merge bot[tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
In reply to Dave Hansenx86/mpx: Fix recursive munmap() corruption
Commit-ID: 508b8482ea2227ba8695d1cf8311166a455c2ae0 Gitweb: https://git.kernel.org/tip/508b8482ea2227ba8695d1cf8311166a455c2ae0 Author: Dave Hansen <[email protected]> AuthorDate: Mon, 1 Apr 2019 07:15:49 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 18 Apr 2019 14:39:08 +0200
x86/mpx: Fix recursive munmap() corruption
This is a bit of a mess, to put it mildly. But, it's a bug that seems to have gone unticked up to now, probably because nobody uses MPX. The other alternative to this fix is to just deprecate MPX, even in -stable kernels.
MPX has the arch_unmap() hook inside of munmap() because MPX uses bounds tables that protect other areas of memory. When memory is unmapped, there is also a need to unmap the MPX bounds tables. Barring this, unused bounds tables can eat 80% of the address space.
But, the recursive do_munmap() that gets called via arch_unmap() wreaks havoc with __do_munmap()'s state. It can result in freeing populated page tables, accessing bogus VMA state, double-freed VMAs and more.
To fix this, call arch_unmap() before __do_unmap() has a chance to do anything meaningful. Also, remove the 'vma' argument and force the MPX code to do its own, independent VMA lookup.
For the common success case this is functionally identical to what was there before. For the munmap() failure case, it's possible that some MPX tables will be zapped for memory that continues to be in use. But, this is an extraordinarily unlikely scenario and the harm would be that MPX provides no protection since the bounds table got reset (zeroed).
It's hard to imagine that anyone is doing this:
ptr = mmap(); // use ptr ret = munmap(ptr); if (ret) // oh, there was an error, I'll // keep using ptr.
Because if after doing munmap(), the applitcation is *done* with the memory. There's probably no good data in there _anyway_.
This passes the original reproducer from Richard Biener as well as the existing mpx selftests/.
Further details:
munmap() has a couple of pieces:
1. Find the affected VMA(s)
2. Split the start/end one(s) if neceesary
3. Pull the VMAs out of the rbtree
4. Actually zap the memory via unmap_region(), including freeing page tables (or queueing them to be freed).
5. Fixup some of the accounting (like fput()) and actually free the VMA itself.
The original MPX implementation chose to put the arch_unmap() call right afer #3. This was *just* before mmap_sem looked like it might get downgraded (it won't in this context), but it looked right. It wasn't.
Richard Biener reported a test that shows this in dmesg:
[1216548.787498] BUG: Bad rss-counter state mm:0000000017ce560b idx:1 val:551 [1216548.787500] BUG: non-zero pgtables_bytes on freeing mm: 24576
What triggered this was the recursive do_munmap() called via arch_unmap(). It was freeing page tables that has not been properly zapped.
But, the problem was bigger than this. For one, arch_unmap() can free VMAs. But, the calling __do_munmap() has variables that *point* to VMAs and obviously can't handle them just getting freed while the pointer is still valid.
A couple of attempts were made to fix this. First, was to fix the page table freeing problem in isolation, but this lead to the VMA issue. The next approach was to let the MPX code return a flag if it modified the rbtree which would force __do_munmap() to re-walk to restart. That spiralled out of control in complexity pretty fast.
Just moving arch_unmap() and accepting that the bonkers failure case might eat some bounds tables is the simplest viable fix.
--- arch/x86/include/asm/mmu_context.h | 6 +++--- arch/x86/include/asm/mpx.h | 5 ++--- arch/x86/mm/mpx.c | 10 ++++++---- include/asm-generic/mm_hooks.h | 1 - mm/mmap.c | 15 ++++++++------- 5 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/include/asm/mmu_context.h +3 -3
@@ -277,8 +277,8 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,mpx_mm_init (mm); } -static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) {/* * mpx_notify_unmap() goes and reads a rarely-hot
@@ -298,7 +298,7 @@ static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* consistently wrong. */ if (unlikely (cpu_feature_enabled (X86_FEATURE_MPX ))) -mpx_notify_unmap (mm, vma, start, end); +mpx_notify_unmap (mm, start, end); }/*
arch/x86/include/asm/mpx.h +2 -3
@@ -78,8 +78,8 @@ static inline void mpx_mm_init(struct mm_struct *mm)*/ mm->context.bd_addr =MPX_INVALID_BOUNDS_DIR ; } -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end); +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end);unsigned long mpx_unmapped_area_check (unsigned long addr,unsigned long len,unsigned long flags);
@@ -100,7 +100,6 @@ static inline void mpx_mm_init(struct mm_struct *mm) { }static inline void mpx_notify_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
arch/x86/mm/mpx.c +6 -4
@@ -881,9 +881,10 @@ static int mpx_unmap_tables(struct mm_struct *mm,* the virtual address region start...end have already been split if * necessary, and the 'vma' is the first vma in this range (start -> end). */ -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) { +struct vm_area_struct *vma;int ret;/*
@@ -902,11 +903,12 @@ void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* which should not occur normally. Being strict about it here * helps ensure that we do not have an exploitable stack overflow. */ -do { + vma =find_vma (mm, start); +while (vma && vma->vm_start < end) {if (vma->vm_flags &VM_MPX )return ; vma = vma->vm_next; - }while (vma && vma->vm_start < end); + } ret =mpx_unmap_tables (mm, start, end);if (ret)
include/asm-generic/mm_hooks.h +0 -1
@@ -18,7 +18,6 @@ static inline void arch_exit_mmap(struct mm_struct *mm) }static inline void arch_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
mm/mmap.c +8 -7
@@ -2730,9 +2730,17 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end); +/* Find the first overlapping VMA */ vma =find_vma (mm, start);if (!vma)
@@ -2741,7 +2749,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* we have start < vma->vm_end */ /* if it doesn't overlap, we have nothing.. */ - end = start + len;if (vma->vm_start >= end)return 0 ;
@@ -2811,12 +2818,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* Detach vmas from rbtree */ detach_vmas_to_be_unmapped (mm, vma, prev, end); -/* -* mpx unmap needs to be called with mmap_sem held for write. -* It is safe to call it before unmap_region(). -*/ -arch_unmap (mm, vma, start, end); -if (downgrade)downgrade_write (&mm->mmap_sem);
Sasha Levin
maintainerRe: [tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
Hi,
[This is an automated email]
This commit has been processed because it contains a "Fixes:" tag, fixing commit: 1de4fa14ee25 x86, mpx: Cleanup unused bound tables.
The bot has tested the following trees: v5.0.8, v4.19.35, v4.14.112, v4.9.169, v4.4.178.
v5.0.8: Build OK!
v4.19.35: Failed to apply! Possible dependencies:
dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap")
v4.14.112: Failed to apply! Possible dependencies:
dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap")
v4.9.169: Failed to apply! Possible dependencies:
010426079ec1 ("sched/headers: Prepare for new header dependencies before moving more code to <linux/sched/mm.h>") 1b028f784e8c ("x86/mm: Introduce mmap_compat_base() for 32-bit mmap()")
39bc88e5e38e ("arm64: Disable TTBR0_EL1 during normal kernel execution")
3f07c0144132 ("sched/headers: Prepare for new header dependencies before moving code to <linux/sched/signal.h>") 44b04912fa72 ("x86/mpx: Do not allow MPX if we have mappings above 47-bit")
6a0b41d1e23d ("x86/mm: Introduce arch_rnd() to compute 32/64 mmap random base")
7c0f6ba682b9 ("Replace <asm/uaccess.h> with <linux/uaccess.h> globally")
8f3e474f3cea ("x86/mm: Add task_size parameter to mmap_base()")
9cf09d68b89a ("arm64: xen: Enable user access before a privcmd hvc call")
bd38967d406f ("arm64: Factor out PAN enabling/disabling into separate uaccess_* macros") e13b73dd9c80 ("x86/hugetlb: Adjust to the new native/compat mmap bases")
v4.4.178: Failed to apply! Possible dependencies:
1b028f784e8c ("x86/mm: Introduce mmap_compat_base() for 32-bit mmap()")
2b5e869ecfcb ("MIPS: ELF: Interpret the NAN2008 file header flag")
2ed02dd415ae ("MIPS: Use a union to access the ELF file header")
44b04912fa72 ("x86/mpx: Do not allow MPX if we have mappings above 47-bit")
5fa393c85719 ("MIPS: Break down cacheops.h definitions")
694977006a7b ("MIPS: Use enums to make asm/pgtable-bits.h readable")
745f35587846 ("MIPS: mm: Unify pte_page definition")
780602d740fc ("MIPS: mm: Standardise on _PAGE_NO_READ, drop _PAGE_READ")
7939469da29a ("MIPS64: signal: Fix o32 sigaction syscall")
7b2cb64f91f2 ("MIPS: mm: Fix MIPS32 36b physical addressing (alchemy, netlogic)")
8f3e474f3cea ("x86/mm: Add task_size parameter to mmap_base()")
97f2645f358b ("tree-wide: replace config_enabled() with IS_ENABLED()")
9e08f57d684a ("x86: mm: support ARCH_MMAP_RND_BITS")
a60ae81e5e59 ("MIPS: CM: Fix mips_cm_max_vp_width for UP kernels")
b1b4fad5cc67 ("MIPS: seccomp: Support compat with both O32 and N32")
b27873702b06 ("mips, thp: remove infrastructure for handling splitting PMDs")
b2edcfc81401 ("MIPS: Loongson: Add Loongson-3A R2 basic support")
d07e22597d1d ("mm: mmap: add new /proc tunable for mmap_base ASLR")
e13b73dd9c80 ("x86/hugetlb: Adjust to the new native/compat mmap bases")How should we proceed with this patch?
--
Thanks,
Sasha
Re: [tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
Quoted 8 lines · Sasha Levin
This commit has been processed because it contains a "Fixes:" tag, fixing commit:
1de4fa14ee25x86, mpx: Cleanup unused bound tables.The bot has tested the following trees: v5.0.8, v4.19.35, v4.14.112, v4.9.169, v4.4.178.
v5.0.8: Build OK!
v4.19.35: Failed to apply! Possible dependencies:
dd2283f2605e("mm: mmap: zap pages with readmmap_semin munmap")
I probably should have looked more closely at the state of the code before dd2283f2605e. A more correct Fixes: would probably have referred to dd2283f2605e. *It* appears to be the root cause rather than the original MPX code that I called out.
The pre-dd2283f2605e code does this:
Quoted 10 lines
/*
* Remove the vma's, and unmap the actual pages
*/
detach_vmas_to_be_unmapped(mm, vma, prev, end);
unmap_region(mm, vma, prev, start, end);
arch_unmap(mm, vma, start, end);
/* Fix up all other VM information */
remove_vma_list(mm, vma);But, this is actually safe. arch_unmap() can't see 'vma' in the rbtree because it's been detached, so it can't do anything to 'vma' that might be unsafe for remove_vma_list()'s use of 'vma'.
The bug in dd2283f2605e was moving unmap_region() to the after arch_unmap().
I confirmed this by running the reproducer on v4.19.35. It did not trigger anything there, even with a bunch of debugging enabled which detected the issue in 5.0.
Re: [tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
Quoted 32 lines · Dave Hansen
Quoted 8 lines · Sasha Levin
This commit has been processed because it contains a "Fixes:" tag, fixing commit:
1de4fa14ee25x86, mpx: Cleanup unused bound tables.The bot has tested the following trees: v5.0.8, v4.19.35, v4.14.112, v4.9.169, v4.4.178.
v5.0.8: Build OK!
v4.19.35: Failed to apply! Possible dependencies:
dd2283f2605e("mm: mmap: zap pages with readmmap_semin munmap")I probably should have looked more closely at the state of the code before
dd2283f2605e. A more correct Fixes: would probably have referred todd2283f2605e. *It* appears to be the root cause rather than the original MPX code that I called out.The pre-dd2283f2605e code does this:
Quoted 10 lines
/* * Remove the vma's, and unmap the actual pages */ detach_vmas_to_be_unmapped(mm, vma, prev, end); unmap_region(mm, vma, prev, start, end); arch_unmap(mm, vma, start, end); /* Fix up all other VM information */ remove_vma_list(mm, vma);But, this is actually safe.
arch_unmap()can't see 'vma' in the rbtree because it's been detached, so it can't do anything to 'vma' that might be unsafe for remove_vma_list()'s use of 'vma'.
Quoted 5 lines
The bug in
dd2283f2605ewas movingunmap_region()to the afterarch_unmap().I confirmed this by running the reproducer on v4.19.35. It did not trigger anything there, even with a bunch of debugging enabled which detected the issue in 5.0.
I'l amend the commit to avoid further confusion.
Thanks,
tglx
tip-bot for Dave Hansen
merge bot[tip:x86/urgent] x86/mpx: Fix recursive munmap() corruption
In reply to Dave Hansenx86/mpx: Fix recursive munmap() corruption
Commit-ID: 7d3bca521e76fd14922529a0ddd3ca7005eb2acf Gitweb: https://git.kernel.org/tip/7d3bca521e76fd14922529a0ddd3ca7005eb2acf Author: Dave Hansen <[email protected]> AuthorDate: Mon, 1 Apr 2019 07:15:49 -0700 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 18 Apr 2019 21:20:28 +0200
x86/mpx: Fix recursive munmap() corruption
This is a bit of a mess, to put it mildly. But, it's a bug that seems to have gone unticked up to now, probably because nobody uses MPX. The other alternative to this fix is to just deprecate MPX, even in -stable kernels.
MPX has the arch_unmap() hook inside of munmap() because MPX uses bounds tables that protect other areas of memory. When memory is unmapped, there is also a need to unmap the MPX bounds tables. Barring this, unused bounds tables can eat 80% of the address space.
But, the recursive do_munmap() that gets called via arch_unmap() wreaks havoc with __do_munmap()'s state. It can result in freeing populated page tables, accessing bogus VMA state, double-freed VMAs and more.
To fix this, call arch_unmap() before __do_unmap() has a chance to do anything meaningful. Also, remove the 'vma' argument and force the MPX code to do its own, independent VMA lookup.
For the common success case this is functionally identical to what was there before. For the munmap() failure case, it's possible that some MPX tables will be zapped for memory that continues to be in use. But, this is an extraordinarily unlikely scenario and the harm would be that MPX provides no protection since the bounds table got reset (zeroed).
It's hard to imagine that anyone is doing this:
ptr = mmap(); // use ptr ret = munmap(ptr); if (ret) // oh, there was an error, I'll // keep using ptr.
Because if after doing munmap(), the applitcation is *done* with the memory. There's probably no good data in there _anyway_.
This passes the original reproducer from Richard Biener as well as the existing mpx selftests/.
Further details:
munmap() has a couple of pieces:
1. Find the affected VMA(s)
2. Split the start/end one(s) if neceesary
3. Pull the VMAs out of the rbtree
4. Actually zap the memory via unmap_region(), including freeing page tables (or queueing them to be freed).
5. Fixup some of the accounting (like fput()) and actually free the VMA itself.
The original MPX implementation chose to put the arch_unmap() call right afer #3. This was *just* before mmap_sem looked like it might get downgraded (it won't in this context), but it looked right. It wasn't.
Richard Biener reported a test that shows this in dmesg:
[1216548.787498] BUG: Bad rss-counter state mm:0000000017ce560b idx:1 val:551 [1216548.787500] BUG: non-zero pgtables_bytes on freeing mm: 24576
What triggered this was the recursive do_munmap() called via arch_unmap(). It was freeing page tables that has not been properly zapped.
But, the problem was bigger than this. For one, arch_unmap() can free VMAs. But, the calling __do_munmap() has variables that *point* to VMAs and obviously can't handle them just getting freed while the pointer is still valid.
A couple of attempts were made to fix this. First, was to fix the page table freeing problem in isolation, but this lead to the VMA issue. The next approach was to let the MPX code return a flag if it modified the rbtree which would force __do_munmap() to re-walk to restart. That spiralled out of control in complexity pretty fast.
Just moving arch_unmap() and accepting that the bonkers failure case might eat some bounds tables is the simplest viable fix.
--- arch/x86/include/asm/mmu_context.h | 6 +++--- arch/x86/include/asm/mpx.h | 5 ++--- arch/x86/mm/mpx.c | 10 ++++++---- include/asm-generic/mm_hooks.h | 1 - mm/mmap.c | 15 ++++++++------- 5 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/include/asm/mmu_context.h +3 -3
@@ -277,8 +277,8 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,mpx_mm_init (mm); } -static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) {/* * mpx_notify_unmap() goes and reads a rarely-hot
@@ -298,7 +298,7 @@ static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* consistently wrong. */ if (unlikely (cpu_feature_enabled (X86_FEATURE_MPX ))) -mpx_notify_unmap (mm, vma, start, end); +mpx_notify_unmap (mm, start, end); }/*
arch/x86/include/asm/mpx.h +2 -3
@@ -78,8 +78,8 @@ static inline void mpx_mm_init(struct mm_struct *mm)*/ mm->context.bd_addr =MPX_INVALID_BOUNDS_DIR ; } -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end); +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end);unsigned long mpx_unmapped_area_check (unsigned long addr,unsigned long len,unsigned long flags);
@@ -100,7 +100,6 @@ static inline void mpx_mm_init(struct mm_struct *mm) { }static inline void mpx_notify_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
arch/x86/mm/mpx.c +6 -4
@@ -881,9 +881,10 @@ static int mpx_unmap_tables(struct mm_struct *mm,* the virtual address region start...end have already been split if * necessary, and the 'vma' is the first vma in this range (start -> end). */ -void mpx_notify_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +void mpx_notify_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end) { +struct vm_area_struct *vma;int ret;/*
@@ -902,11 +903,12 @@ void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,* which should not occur normally. Being strict about it here * helps ensure that we do not have an exploitable stack overflow. */ -do { + vma =find_vma (mm, start); +while (vma && vma->vm_start < end) {if (vma->vm_flags &VM_MPX )return ; vma = vma->vm_next; - }while (vma && vma->vm_start < end); + } ret =mpx_unmap_tables (mm, start, end);if (ret)
include/asm-generic/mm_hooks.h +0 -1
@@ -18,7 +18,6 @@ static inline void arch_exit_mmap(struct mm_struct *mm) }static inline void arch_unmap (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long start,unsigned long end) { }
mm/mmap.c +8 -7
@@ -2730,9 +2730,17 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end); +/* Find the first overlapping VMA */ vma =find_vma (mm, start);if (!vma)
@@ -2741,7 +2749,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* we have start < vma->vm_end */ /* if it doesn't overlap, we have nothing.. */ - end = start + len;if (vma->vm_start >= end)return 0 ;
@@ -2811,12 +2818,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* Detach vmas from rbtree */ detach_vmas_to_be_unmapped (mm, vma, prev, end); -/* -* mpx unmap needs to be called with mmap_sem held for write. -* It is safe to call it before unmap_region(). -*/ -arch_unmap (mm, vma, start, end); -if (downgrade)downgrade_write (&mm->mmap_sem);
Quoted 17 lines · Dave Hansen
diff -puN
mm/mmap.c~mpx-rss-pass-no-vmamm/mmap.cmm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +7
-0@@ -2731,9 +2731,17 @@ int __do_munmap(struct mm_struct *mm, unreturn -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end);
...
mm/mmap.c
Quoted 4 lines
mm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +2
-2-static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end)
While you fixed up the asm-generic thing, this breaks arch/um and arch/unicorn32. For those the fixup is trivial by removing the vma argument.
But itt also breaks powerpc and there I'm not sure whether moving arch_unmap() to the beginning of __do_munmap() is safe. Micheal???
Aside of that the powerpc variant looks suspicious:
static inline void arch_unmap(struct mm_struct *mm,
unsigned long start, unsigned long end)
{
if (start <= mm->context.vdso_base && mm->context.vdso_base < end)
mm->context.vdso_base = 0; }
Shouldn't that be:
if (start >= mm->context.vdso_base && mm->context.vdso_base < end)
Hmm?
Thanks,
tglx
Michael Ellerman
reviewerQuoted 30 lines · Thomas Gleixner
Quoted 17 lines · Dave Hansen
diff -puN
mm/mmap.c~mpx-rss-pass-no-vmamm/mmap.cmm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +7
-0@@ -2731,9 +2731,17 @@ int __do_munmap(struct mm_struct *mm, unreturn -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end);...
mm/mmap.c
Quoted 4 lines
mm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +2
-2-static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end)While you fixed up the asm-generic thing, this breaks
arch/umandarch/unicorn32. For those the fixup is trivial by removing the vma argument.
Quoted 2 lines
But itt also breaks powerpc and there I'm not sure whether moving
arch_unmap()to the beginning of__do_munmap()is safe. Micheal???
I don't know for sure but I think it should be fine. That code is just there to handle CRIU unmapping/remapping the VDSO. So that either needs to happen while the process is stopped or it needs to handle races anyway, so I don't see how the placement within the unmap path should matter.
Quoted 6 lines
Aside of that the powerpc variant looks suspicious:
static inline void arch_unmap(struct
mm_struct*mm,
unsigned long start, unsigned long end)
{
if (start <=mm->context.vdso_base&&mm->context.vdso_base< end)
Quoted 8 lines
mm->context.vdso_base = 0; }Shouldn't that be:
if (start >=
mm->context.vdso_base&&mm->context.vdso_base< end)Hmm?
Yeah looks pretty suspicious. I'll follow-up with Laurent who wrote it. Thanks for spotting it!
cheers
Le 20/04/2019 à 12:31, Michael Ellerman a écrit :
Quoted 31 lines
Quoted 30 lines · Thomas Gleixner
Quoted 17 lines · Dave Hansen
diff -puN
mm/mmap.c~mpx-rss-pass-no-vmamm/mmap.cmm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +7
-0@@ -2731,9 +2731,17 @@ int __do_munmap(struct mm_struct *mm, unreturn -EINVAL ; len =PAGE_ALIGN (len); + end = start + len;if (len ==0 )return -EINVAL ; +/* +* arch_unmap() might do unmaps itself. It must be called +* and finish any rbtree manipulation before this code +* runs and also starts to manipulate the rbtree. +*/ +arch_unmap (mm, start, end);...
mm/mmap.c
Quoted 4 lines
mm/mmap.c renamed from mm/mmap.c~mpx-rss-pass-no-vma +2
-2-static inline void arch_unmap (struct mm_struct *mm,struct vm_area_struct *vma, -unsigned long start,unsigned long end) +static inline void arch_unmap (struct mm_struct *mm,unsigned long start, +unsigned long end)While you fixed up the asm-generic thing, this breaks
arch/umandarch/unicorn32. For those the fixup is trivial by removing the vma argument.
Quoted 8 lines
Quoted 2 lines
But itt also breaks powerpc and there I'm not sure whether moving
arch_unmap()to the beginning of__do_munmap()is safe. Micheal???I don't know for sure but I think it should be fine. That code is just there to handle CRIU unmapping/remapping the VDSO. So that either needs to happen while the process is stopped or it needs to handle races anyway, so I don't see how the placement within the unmap path should matter.
My only concern is the error path.
Calling arch_unmap() before handling any error case means that it will have to be undo and there is no way to do so.
I don't know what is the rational to move arch_unmap() to the beginning of __do_munmap() but the error paths must be managed.
Quoted 9 lines
Quoted 9 lines
Aside of that the powerpc variant looks suspicious:
static inline void arch_unmap(struct
mm_struct*mm,
unsigned long start, unsigned long end)
{
if (start <=mm->context.vdso_base&&mm->context.vdso_base< end)mm->context.vdso_base = 0; }
Quoted 8 lines
Quoted 5 lines
Shouldn't that be:
if (start >= mm->context.vdso_base && mm->context.vdso_base < end)Hmm?
Yeah looks pretty suspicious. I'll follow-up with Laurent who wrote it. Thanks for spotting it!
I've to admit that I had to read that code carefully before answering.
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the union vdso_data_store
on powerpc).
The idea is to handle a munmap() call surrounding the VDSO area:
| VDSO | ^start ^end
This is covered by this test, as the munmap() matching the exact boundaries of the VDSO is handled too.
Am I missing something ?
Cheers,
Laurent.
Quoted 27 lines · Laurent Dufour
Le 20/04/2019 à 12:31, Michael Ellerman a écrit :
Quoted 10 lines
Quoted 9 lines · Thomas Gleixner
Aside of that the powerpc variant looks suspicious:
static inline void arch_unmap(struct
mm_struct*mm,
unsigned long start, unsigned long end)
{
if (start <=mm->context.vdso_base&&mm->context.vdso_base< end)mm->context.vdso_base = 0; }Quoted 8 lines
Quoted 5 lines
Shouldn't that be:
if (start >= mm->context.vdso_base && mm->context.vdso_base < end)Hmm?
Yeah looks pretty suspicious. I'll follow-up with Laurent who wrote it. Thanks for spotting it!
I've to admit that I had to read that code carefully before answering.
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon
powerpc).
Quoted 8 lines
The idea is to handle a
munmap()call surrounding the VDSO area:| VDSO | ^start ^endThis is covered by this test, as the
munmap()matching the exact boundaries of the VDSO is handled too.Am I missing something ?
Well if this is the intention, then you missed to add a comment explaining it :)
Thanks,
tglx
bos
Le 23/04/2019 à 15:34, Thomas Gleixner a écrit :
Quoted 35 lines · Laurent Dufour
Quoted 34 lines
Le 20/04/2019 à 12:31, Michael Ellerman a écrit :
Quoted 10 lines
Quoted 9 lines · Thomas Gleixner
Aside of that the powerpc variant looks suspicious:
static inline void arch_unmap(struct
mm_struct*mm,
unsigned long start, unsigned long end){ if (start <= mm->context.vdso_base && mm->context.vdso_base < end) mm->context.vdso_base = 0; }Quoted 8 lines
Quoted 5 lines
Shouldn't that be:
if (start >= mm->context.vdso_base && mm->context.vdso_base < end)Hmm?
Yeah looks pretty suspicious. I'll follow-up with Laurent who wrote it. Thanks for spotting it!
I've to admit that I had to read that code carefully before answering.
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon
powerpc).The idea is to handle a
munmap()call surrounding the VDSO area:| VDSO | ^start ^endThis is covered by this test, as the
munmap()matching the exact boundaries of the VDSO is handled too.
Quoted 7 lines
Quoted 1 line
Am I missing something ?
Well if this is the intention, then you missed to add a comment explaining it :)
Thanks,
tglx
You're right, and I was thinking the same when I read that code this morning ;)
I'll propose a patch to a add an explicit comment.
Thanks,
Laurent.
Quoted 3 lines · Laurent Dufour
My only concern is the error path.
Callingarch_unmap()before handling any error case means that it will have to be undo and there is no way to do so.
Is there a practical scenario where munmap() of the VDSO can split a VMA? If the VDSO is guaranteed to be a single page, it would have to be a scenario where munmap() was called on a range that included the VDSO *and* other VMA that we failed to split.
But, the scenario would have to be that someone tried to munmap() the VDSO and something adjacent, the munmap() failed, and they kept on using the VDSO and expected the special signal and perf behavior to be maintained.
BTW, what keeps the VDSO from merging with an adjacent VMA? Is it just the vm_ops->close that comes from special_mapping_vmops?
Quoted 2 lines
I don't know what is the rational to move
arch_unmap()to the beginning of__do_munmap()but the error paths must be managed.
It's in the changelog:
https://patchwork.kernel.org/patch/10909727/
But, the tl;dr version is: x86 is recursively calling __do_unmap() (via arch_unmap()) in a spot where the internal rbtree data is inconsistent, which causes all kinds of fun. If we move arch_unmap() to before __do_munmap() does any data structure manipulation, the recursive call doesn't get confused any more.
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)
Are you sure about #2? The 'vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)
Le 23/04/2019 à 18:04, Dave Hansen a écrit :
Quoted 5 lines · Laurent Dufour
Quoted 3 lines
My only concern is the error path.
Callingarch_unmap()before handling any error case means that it will have to be undo and there is no way to do so.
Quoted 8 lines
Is there a practical scenario where
munmap()of the VDSO can split a VMA? If the VDSO is guaranteed to be a single page, it would have to be a scenario wheremunmap()was called on a range that included the VDSO *and* other VMA that we failed to split.But, the scenario would have to be that someone tried to
munmap()the VDSO and something adjacent, themunmap()failed, and they kept on using the VDSO and expected the special signal and perf behavior to be maintained.
I've to admit that this should not be a common scenario, and unmapping the VDSO is not so common anyway.
Quoted 2 lines
BTW, what keeps the VDSO from merging with an adjacent VMA? Is it just the
vm_ops->closethat comes fromspecial_mapping_vmops?
I'd think so.
Quoted 5 lines
Quoted 2 lines
I don't know what is the rational to move
arch_unmap()to the beginning of__do_munmap()but the error paths must be managed.It's in the changelog:
Quoted 7 lines
https://patchwork.kernel.org/patch/10909727/But, the tl;dr version is: x86 is recursively calling
__do_unmap()(viaarch_unmap()) in a spot where the internal rbtree data is inconsistent, which causes all kinds of fun. If we movearch_unmap()to before__do_munmap()does any data structure manipulation, the recursive call doesn't get confused any more.
If only Powerpc is impacted I guess this would be fine but what about the other architectures?
Quoted 6 lines
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)Are you sure about #2? The '
vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)
Hum, not so sure now ;)
I got confused, only the header is one page.
The test is working as a best effort, and don't cover the case where only few pages inside the VDSO are unmmapped (start > mm->context.vdso_base). This is not what CRIU is doing and so this was enough for CRIU support.
Michael, do you think there is a need to manage all the possibility here, since the only user is CRIU and unmapping the VDSO is not a so good idea for other processes ?
Michael Ellerman
reviewerQuoted 2 lines · Laurent Dufour
Le 23/04/2019 à 18:04, Dave Hansen a écrit :
Quoted 1 line · Laurent Dufour
...
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)Are you sure about #2? The '
vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)Hum, not so sure now ;)
I got confused, only the header is one page.
The test is working as a best effort, and don't cover the case where only few pages inside the VDSO are unmmapped (start >mm->context.vdso_base). This is not what CRIU is doing and so this was enough for CRIU support.
Quoted 3 lines
Michael, do you think there is a need to manage all the possibility here, since the only user is CRIU and unmapping the VDSO is not a so good idea for other processes ?
Couldn't we implement the semantic that if any part of the VDSO is unmapped then vdso_base is set to zero? That should be fairly easy, eg:
if (start < vdso_end && end >= mm->context.vdso_base) mm->context.vdso_base = 0;
We might need to add vdso_end to the mm->context, but that should be OK.
That seems like it would work for CRIU and make sense in general?
cheers
Le 01/05/2019 à 12:32, Michael Ellerman a écrit :
Quoted 25 lines
Quoted 2 lines · Laurent Dufour
Le 23/04/2019 à 18:04, Dave Hansen a écrit :
Quoted 1 line · Laurent Dufour
...
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)Are you sure about #2? The '
vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)Hum, not so sure now ;)
I got confused, only the header is one page.
The test is working as a best effort, and don't cover the case where only few pages inside the VDSO are unmmapped (start >mm->context.vdso_base). This is not what CRIU is doing and so this was enough for CRIU support.Quoted 3 lines
Michael, do you think there is a need to manage all the possibility here, since the only user is CRIU and unmapping the VDSO is not a so good idea for other processes ?
Couldn't we implement the semantic that if any part of the VDSO is unmapped then
vdso_baseis set to zero? That should be fairly easy, eg:
Quoted 7 lines
if (start < vdso_end && end >= mm->context.vdso_base) mm->context.vdso_base = 0;We might need to add
vdso_endto themm->context, but that should be OK.That seems like it would work for CRIU and make sense in general?
Sorry for the late answer, yes this would make more sense.
Here is a patch doing that.
Cheers,
Laurent
From 5b64a86c2a8042c7785c3d3f5e58e954a2c8c843 Mon Sep 17 00:00:00 2001
From: Laurent Dufour <[email protected]>
Date: Tue, 7 May 2019 16:29:46 +0200
Subject: [PATCH] powerpc/vdso: handle generic unmap of the VDSO
Make the unmap of the VDSO more generic by checking for the start and end of the VDSO.
This implies to add the vdso_end address in the mm_context_t structure.
--- arch/powerpc/include/asm/book3s/32/mmu-hash.h | 3 ++- arch/powerpc/include/asm/book3s/64/mmu.h | 2 +- arch/powerpc/include/asm/mm-arch-hooks.h | 5 ++++- arch/powerpc/include/asm/mmu_context.h | 21 +++++++++++++++++-- arch/powerpc/include/asm/nohash/32/mmu-40x.h | 2 +- arch/powerpc/include/asm/nohash/32/mmu-44x.h | 2 +- arch/powerpc/include/asm/nohash/32/mmu-8xx.h | 2 +- arch/powerpc/include/asm/nohash/mmu-book3e.h | 2 +- arch/powerpc/kernel/vdso.c | 2 ++ 9 files changed, 32 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/book3s/32/mmu-hash.h +2 -1
@@ -29,6 +29,7 @@#define BPP_RX 0x01/* Read only */ #define BPP_RW 0x02/* Read/write */ +#ifndef __ASSEMBLY__/* Contort a phys_addr_t into the right format/bits for a BAT */ #ifdef CONFIG_PHYS_64BIT
@@ -90,7 +91,7 @@ struct hash_pte {typedef struct {unsigned long id; -unsigned long vdso_base; +unsigned long vdso_base, vdso_end; }mm_context_t ;void update_bats (void );
arch/powerpc/include/asm/book3s/64/mmu.h +1 -1
@@ -120,7 +120,7 @@ typedef struct {struct npu_context *npu_context;struct hash_mm_context *hash_context; -unsigned long vdso_base; +unsigned long vdso_base, vdso_end;/* * pagetable fragment support */
arch/powerpc/include/asm/mm-arch-hooks.h +4 -1
@@ -16,12 +16,15 @@ static inline void arch_remap(struct mm_struct *mm, unsigned long old_start, unsigned long old_end, unsigned long new_start, unsigned long new_end) { + unsigned long length = mm->context.vdso_end - mm->context.vdso_base;/* * mremap() doesn't allow moving multiple vmas so we can limit the * check to old_start == vdso_base. */ - if (old_start == mm->context.vdso_base) +if (old_start == mm->context.vdso_base) { + mm->context.vdso_end = new_start + length; mm->context.vdso_base = new_start; + } }#define arch_remap arch_remap
arch/powerpc/include/asm/mmu_context.h +19 -2
@@ -235,8 +235,25 @@ static inline void arch_unmap(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long start, unsigned long end) { - if (start <= mm->context.vdso_base && mm->context.vdso_base < end) - mm->context.vdso_base = 0; + unsigned long vdso_base, vdso_end; + + vdso_base = mm->context.vdso_base; + vdso_end = mm->context.vdso_end; + +/* +* Partial unmapping of pages inside the VDSO, is consider equivalent +* to unmapping the VDSO. +* +* case 1 > | VDSO | < +* case 2 > | < | +* case 3 | > < | +* case 4 | > | < +*/ + +if ((start <= vdso_base && vdso_end <= end) ||/* 1 */ + (vdso_base <= start && start < vdso_end) ||/* 3,4 */ + (vdso_base < end && end <= vdso_end))/* 2,3 */ + mm->context.vdso_base = mm->context.vdso_end =0 ; } static inline void arch_bprm_mm_init(struct mm_struct *mm,
arch/powerpc/include/asm/nohash/32/mmu-40x.h +1 -1
@@ -57,7 +57,7 @@typedef struct {unsigned int id;unsigned int active; -unsigned long vdso_base; +unsigned long vdso_base, vdso_end; }mm_context_t ;#endif /* !__ASSEMBLY__ */
arch/powerpc/include/asm/nohash/32/mmu-44x.h +1 -1
@@ -108,7 +108,7 @@ extern unsigned int tlb_44x_index;typedef struct {unsigned int id;unsigned int active; -unsigned long vdso_base; +unsigned long vdso_base, vdso_end; }mm_context_t ;/* patch sites */
arch/powerpc/include/asm/nohash/32/mmu-8xx.h +1 -1
@@ -209,7 +209,7 @@ struct slice_mask { typedef struct { unsigned int id; unsigned int active; - unsigned long vdso_base; + unsigned long vdso_base, vdso_end; #ifdef CONFIG_PPC_MM_SLICES u16 user_psize; /* page size index */ unsigned char low_slices_psize[SLICE_ARRAY_SIZE];
arch/powerpc/include/asm/nohash/mmu-book3e.h +1 -1
@@ -229,7 +229,7 @@ extern unsigned int tlbcam_index;typedef struct {unsigned int id;unsigned int active; -unsigned long vdso_base; +unsigned long vdso_base, vdso_end; }mm_context_t ;/* Page size definitions, common between 32 and 64-bit
arch/powerpc/kernel/vdso.c +2 -0
@@ -182,6 +182,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)#endif current->mm->context.vdso_base =0 ; + current->mm->context.vdso_end =0 ;/* vDSO has a problem and was disabled, just don't "enable" it for the * process
@@ -217,6 +218,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)* will fail to recognise it as a vDSO (since arch_vma_name fails). */ current->mm->context.vdso_base = vdso_base; + current->mm->context.vdso_end = vdso_base + (vdso_pages <<PAGE_SHIFT );/* * our vma flags don't have VM_WRITE so by default, the process isn't
Signature · 1 line
2.21.0
Hi Laurent
Le 07/05/2019 à 18:35, Laurent Dufour a écrit :
Quoted 30 lines
Le 01/05/2019 à 12:32, Michael Ellerman a écrit :
Quoted 29 lines
Quoted 2 lines · Laurent Dufour
Le 23/04/2019 à 18:04, Dave Hansen a écrit :
Quoted 1 line · Laurent Dufour
...
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)Are you sure about #2? The '
vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)Hum, not so sure now ;)
I got confused, only the header is one page.
The test is working as a best effort, and don't cover the case where only few pages inside the VDSO are unmmapped (start >mm->context.vdso_base). This is not what CRIU is doing and so this was enough for CRIU support.Quoted 3 lines
Michael, do you think there is a need to manage all the possibility here, since the only user is CRIU and unmapping the VDSO is not a so good idea for other processes ?
Couldn't we implement the semantic that if any part of the VDSO is unmapped then
vdso_baseis set to zero? That should be fairly easy, eg:if (start <
vdso_end&& end >=mm->context.vdso_base)mm->context.vdso_base= 0;
Quoted 8 lines
Quoted 3 lines
We might need to add
vdso_endto themm->context, but that should be OK.That seems like it would work for CRIU and make sense in general?
Sorry for the late answer, yes this would make more sense.
Here is a patch doing that.
In your patch, the test seems overkill:
+ if ((start <= vdso_base && vdso_end <= end) || /* 1 */
+ (vdso_base <= start && start < vdso_end) || /* 3,4 */
+ (vdso_base < end && end <= vdso_end)) /* 2,3 */
+ mm->context.vdso_base = mm->context.vdso_end = 0;
What about
if (start < vdso_end && vdso_start < end) mm->context.vdso_base = mm->context.vdso_end = 0;
This should cover all cases, or am I missing something ?
And do we really need to store vdso_end in the context ?
I think it should be possible to re-calculate it: the size of the VDSO should be (&vdso32_end - &vdso32_start) + PAGE_SIZE for 32 bits VDSO, and (&vdso64_end - &vdso64_start) + PAGE_SIZE for the 64 bits VDSO.
Christophe
Le 23/10/2020 à 14:28, Christophe Leroy a écrit :
Quoted 51 lines
Hi Laurent
Le 07/05/2019 à 18:35, Laurent Dufour a écrit :
Quoted 30 lines
Le 01/05/2019 à 12:32, Michael Ellerman a écrit :
Quoted 29 lines
Quoted 2 lines · Laurent Dufour
Le 23/04/2019 à 18:04, Dave Hansen a écrit :
Quoted 1 line · Laurent Dufour
...
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
There are 2 assumptions here:
1. 'start' and 'end' are page aligned (this is guaranteed by__do_munmap().
2. the VDSO is 1 page (this is guaranteed by the unionvdso_data_storeon powerpc)Are you sure about #2? The '
vdso64_pages' variable seems rather unnecessary if the VDSO is only 1 page. ;)Hum, not so sure now ;)
I got confused, only the header is one page.
The test is working as a best effort, and don't cover the case where only few pages inside the VDSO are unmmapped (start >mm->context.vdso_base). This is not what CRIU is doing and so this was enough for CRIU support.Quoted 3 lines
Michael, do you think there is a need to manage all the possibility here, since the only user is CRIU and unmapping the VDSO is not a so good idea for other processes ?
Couldn't we implement the semantic that if any part of the VDSO is unmapped then
vdso_baseis set to zero? That should be fairly easy, eg:if (start <
vdso_end&& end >=mm->context.vdso_base)mm->context.vdso_base= 0;Quoted 8 lines
Quoted 3 lines
We might need to add
vdso_endto themm->context, but that should be OK.That seems like it would work for CRIU and make sense in general?
Sorry for the late answer, yes this would make more sense.
Here is a patch doing that.
In your patch, the test seems overkill:
+ if ((start <=
vdso_base&&vdso_end<= end) || /* 1 */ + (vdso_base<= start && start <vdso_end) || /* 3,4 */ + (vdso_base< end && end <= vdso_end)) /* 2,3 */ +mm->context.vdso_base=mm->context.vdso_end= 0;What about
Quoted 8 lines
if (start <
vdso_end&&vdso_start< end)mm->context.vdso_base=mm->context.vdso_end= 0;This should cover all cases, or am I missing something ?
And do we really need to store
vdso_endin the context ?
I think it should be possible to re-calculate it: the size of the VDSO should be (&vdso32_end-&vdso32_start) +PAGE_SIZEfor 32 bits VDSO, and (&vdso64_end-&vdso64_start) +PAGE_SIZEfor the 64 bits VDSO.
Thanks Christophe for the advise.
That is covering all the cases, and indeed is similar to the Michael's proposal I missed last year.
I'll send a patch fixing this issue following your proposal.
Cheers,
Laurent.
Hi Laurent, Christophe, Michael, all,
Quoted 1 line · Laurent Dufour
Le 23/10/2020 à 14:28, Christophe Leroy a écrit :
[..]
Quoted 27 lines
Quoted 19 lines
Quoted 6 lines
Quoted 1 line
That seems like it would work for CRIU and make sense in general?
Sorry for the late answer, yes this would make more sense.
Here is a patch doing that.
In your patch, the test seems overkill:
+ if ((start <=
vdso_base&&vdso_end<= end) || /* 1 */ + (vdso_base<= start && start <vdso_end) || /* 3,4 */ + (vdso_base< end && end <= vdso_end)) /* 2,3 */ +mm->context.vdso_base=mm->context.vdso_end= 0;What about
if (start <
vdso_end&&vdso_start< end)mm->context.vdso_base=mm->context.vdso_end= 0;Quoted 7 lines
This should cover all cases, or am I missing something ?
And do we really need to store
vdso_endin the context ?
I think it should be possible to re-calculate it: the size of the VDSO should be (&vdso32_end-&vdso32_start) +PAGE_SIZEfor 32 bits VDSO, and (&vdso64_end-&vdso64_start) +PAGE_SIZEfor the 64 bits VDSO.
Quoted 6 lines
Thanks Christophe for the advise.
That is covering all the cases, and indeed is similar to the Michael's proposal I missed last year.
I'll send a patch fixing this issue following your proposal.
It's probably not necessary anymore. I've sent patches [1], currently in akpm, the last one forbids splitting of vm_special_mapping. So, a user is able munmap() or mremap() vdso as a whole, but not partly.
[1]:
https://lore.kernel.org/linux-mm/[email protected]/
Thanks,
Dmitry
Le 03/11/2020 à 22:08, Dmitry Safonov a écrit :
Quoted 34 lines
Hi Laurent, Christophe, Michael, all,
Quoted 1 line · Laurent Dufour
Le 23/10/2020 à 14:28, Christophe Leroy a écrit :
[..]
Quoted 29 lines
Quoted 19 lines
Quoted 6 lines
Quoted 1 line
That seems like it would work for CRIU and make sense in general?
Sorry for the late answer, yes this would make more sense.
Here is a patch doing that.
In your patch, the test seems overkill:
+ if ((start <=
vdso_base&&vdso_end<= end) || /* 1 */ + (vdso_base<= start && start <vdso_end) || /* 3,4 */ + (vdso_base< end && end <= vdso_end)) /* 2,3 */ +mm->context.vdso_base=mm->context.vdso_end= 0;What about
if (start <
vdso_end&&vdso_start< end)mm->context.vdso_base=mm->context.vdso_end= 0;Quoted 7 lines
This should cover all cases, or am I missing something ?
And do we really need to store
vdso_endin the context ?
I think it should be possible to re-calculate it: the size of the VDSO should be (&vdso32_end-&vdso32_start) +PAGE_SIZEfor 32 bits VDSO, and (&vdso64_end-&vdso64_start) +PAGE_SIZEfor the 64 bits VDSO.Thanks Christophe for the advise.
Quoted 8 lines
Quoted 4 lines
That is covering all the cases, and indeed is similar to the Michael's proposal I missed last year.
I'll send a patch fixing this issue following your proposal.
It's probably not necessary anymore. I've sent patches [1], currently in akpm, the last one forbids splitting of
vm_special_mapping. So, a user is ablemunmap()ormremap()vdso as a whole, but not partly.
Hi Dmitry,
That's a good thing too, but I think my patch is still valid in the PowerPC code, fixing a bad check, even if some corner cases are handled earlier in the code.
Quoted 6 lines
[1]:
https://lore.kernel.org/linux-mm/[email protected]/Thanks,
Dmitry