mm/mprotect: Call arch_validate_prot under mmap_lock and with length
Subsystems (5)
Jann Horn
reviewerPATCH 1/2 mm/mprotect: Call arch_validate_prot under mmap_lock and with length
arch_validate_prot() is a hook that can validate whether a given set of protection flags is valid in an mprotect() operation. It is given the set of protection flags and the address being modified.
However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. The mmap_lock is not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.
Currently, custom arch_validate_prot() handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc's arch_validate_prot() attempts to look at the VMA, but doesn't take the mmap_lock.
Change the function signature to also take a length, and move the arch_validate_prot() call in mm/mprotect.c down into the locked region.
--- arch/arm64/include/asm/mman.h | 4 ++-- arch/powerpc/include/asm/mman.h | 3 ++- arch/powerpc/kernel/syscalls.c | 2 +- arch/sparc/include/asm/mman.h | 6 ++++-- include/linux/mman.h | 3 ++- mm/mprotect.c | 6 ++++-- 6 files changed, 15 insertions(+), 9 deletions(-)
arch/arm64/include/asm/mman.h +2 -2
@@ -23,7 +23,7 @@ static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags)static inline bool arch_validate_prot (unsigned long prot, -unsigned long addr__always_unused ) +unsigned long addr__always_unused ,unsigned long len__always_unused ) {unsigned long supported =PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM ;
@@ -32,6 +32,6 @@ static inline bool arch_validate_prot(unsigned long prot, return (prot & ~supported) = 0; } -#define arch_validate_prot (prot, addr) arch_validate_prot(prot, addr) +#define arch_validate_prot (prot, addr, len) arch_validate_prot(prot, addr, len)#endif /* ! __ASM_MMAN_H__ */
arch/powerpc/include/asm/mman.h +2 -1
@@ -36,7 +36,8 @@ static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags) }#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags) -static inline bool arch_validate_prot (unsigned long prot,unsigned long addr) +static inline bool arch_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) {if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_SAO ))return false;
arch/powerpc/kernel/syscalls.c +1 -1
@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len)) goto out;if (shift) {
arch/sparc/include/asm/mman.h +4 -2
@@ -52,9 +52,11 @@ static inline pgprot_t sparc_vm_get_page_prot(unsigned long vm_flags)return (vm_flags &VM_SPARC_ADI ) ?__pgprot (_PAGE_MCD_4V) :__pgprot (0 ); } -#define arch_validate_prot (prot, addr) sparc_validate_prot(prot, addr) -static inline int sparc_validate_prot (unsigned long prot,unsigned long addr) +#define arch_validate_prot (prot, addr, len) sparc_validate_prot(prot, addr, len) +static inline int sparc_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) { +mmap_assert_write_locked (current->mm);if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_ADI ))return 0 ;if (prot &PROT_ADI ) {
include/linux/mman.h +2 -1
@@ -96,7 +96,8 @@ static inline void vm_unacct_memory(long pages)* * Returns true if the prot flags are valid */ -static inline bool arch_validate_prot (unsigned long prot,unsigned long addr) +static inline bool arch_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) {return (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM )) =0 ; }
mm/mprotect.c +4 -2
@@ -533,14 +533,16 @@ static int do_mprotect_pkey(unsigned long start, size_t len, end = start + len;if (end <= start)return -ENOMEM ; -if (!arch_validate_prot (prot, start)) -return -EINVAL ; reqprot = prot;if (mmap_write_lock_killable (current->mm))return -EINTR ; + error = -EINVAL ; +if (!arch_validate_prot (prot, start, len)) + goto out; +/* * If userspace did not allocate the pkey, do not let * them use it here.
base-commit: c85fb28b6f999db9928b841f63f1beeb3074eeca
Signature · 1 line
2.28.0.806.g8561365e88-goog
Jann Horn
reviewerPATCH 2/2 sparc: Check VMA range in sparc_validate_prot()
sparc_validate_prot() is called from do_mprotect_pkey() as arch_validate_prot(); it tries to ensure that an mprotect() call can't enable ADI on incompatible VMAs.
The current implementation only checks that the VMA at the start address matches the rules for ADI mappings; instead, check all VMAs that will be affected by mprotect().
(This hook is called before mprotect() makes sure that the specified range is actually covered by VMAs, and mprotect() returns specific error codes when that's not the case. In order for mprotect() to still generate the same error codes for mprotect(<unmapped_ptr>, <len>, ...|PROT_ADI), we need to *accept* cases where the range is not fully covered by VMAs.)
---
compile-tested only, I don't have a Sparc ADI setup - might be nice if some Sparc person could test this?
arch/sparc/include/asm/mman.h | 50 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 20 deletions(-)
arch/sparc/include/asm/mman.h +30 -20
@@ -60,31 +60,41 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr,if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_ADI ))return 0 ;if (prot &PROT_ADI ) { +struct vm_area_struct *vma, *next; +if (!adi_capable ())return 0 ; -if (addr) { -struct vm_area_struct *vma; + vma =find_vma (current->mm, addr); +/* if @addr is unmapped, let mprotect() deal with it */ +if (!vma || vma->vm_start > addr) +return 1 ; +while (1 ) { +/* ADI can not be enabled on PFN +* mapped pages +*/ +if (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )) +return 0 ; - vma =find_vma (current->mm, addr); -if (vma) { -/* ADI can not be enabled on PFN -* mapped pages -*/ -if (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )) -return 0 ; +/* Mergeable pages can become unmergeable +* if ADI is enabled on them even if they +* have identical data on them. This can be +* because ADI enabled pages with identical +* data may still not have identical ADI +* tags on them. Disallow ADI on mergeable +* pages. +*/ +if (vma->vm_flags &VM_MERGEABLE ) +return 0 ; -/* Mergeable pages can become unmergeable -* if ADI is enabled on them even if they -* have identical data on them. This can be -* because ADI enabled pages with identical -* data may still not have identical ADI -* tags on them. Disallow ADI on mergeable -* pages. -*/ -if (vma->vm_flags &VM_MERGEABLE ) -return 0 ; - } +/* reached the end of the range without errors? */ +if (addr+len <= vma->vm_end) +return 1 ; + next = vma->vm_next; +/* if a VMA hole follows, let mprotect() deal with it */ +if (!next || next->vm_start != vma->vm_end) +return 1 ; + vma = next; } }return 1 ;
Signature · 1 line
2.28.0.806.g8561365e88-goog
Re: [PATCH 2/2] sparc: Check VMA range in sparc_validate_prot()
arch/sparc/include/asm/mman.h static inline int sparc_validate_prot(unsigned long prot, unsigned long addr, · line 64
Quoted 7 lines
+++
b/arch/sparc/include/asm/mman.harch/sparc/include/asm/mman.h +2
-0@@ -60,31 +60,41 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr,if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_ADI ))return 0 ;if (prot &PROT_ADI ) { +struct vm_area_struct *vma, *next; +
I'd split all the ADI logic into a separate, preferable out of line helper.
arch/sparc/include/asm/mman.h static inline int sparc_validate_prot(unsigned long prot, unsigned long addr, · line 91
Quoted 2 lines
arch/sparc/include/asm/mman.h +2
-0+/* reached the end of the range without errors? */ +if (addr+len <= vma->vm_end)
missing whitespaces around the arithmetic operator.
Re: [PATCH 2/2] sparc: Check VMA range in sparc_validate_prot()
In reply to Jann HornQuoted 16 lines · Jann Horn
sparc_validate_prot()is called fromdo_mprotect_pkey()asarch_validate_prot(); it tries to ensure that anmprotect()call can't enable ADI on incompatible VMAs.
The current implementation only checks that the VMA at the start address matches the rules for ADI mappings; instead, check all VMAs that will be affected bymprotect().(This hook is called before
mprotect()makes sure that the specified range is actually covered by VMAs, andmprotect()returns specific error codes when that's not the case. In order formprotect()to still generate the same error codes formprotect(<unmapped_ptr>, <len>, ...|PROT_ADI), we need to *accept* cases where the range is not fully covered by VMAs.)Fixes 74a04967482f ("sparc64: Add support for ADI (Application Data Integrity)")Signed-off-by Jann Horn <[email protected]>
Quoted 6 lines
---
compile-tested only, I don't have a Sparc ADI setup - might be nice if some Sparc person could test this?arch/sparc/include/asm/mman.h | 50 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 20 deletions(-)
Looks good to me.
Quoted 68 lines
arch/sparc/include/asm/mman.h +30
-20@@ -60,31 +60,41 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr,if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_ADI ))return 0 ;if (prot &PROT_ADI ) { +struct vm_area_struct *vma, *next; +if (!adi_capable ())return 0 ; -if (addr) { -struct vm_area_struct *vma; + vma =find_vma (current->mm, addr); +/* if @addr is unmapped, let mprotect() deal with it */ +if (!vma || vma->vm_start > addr) +return 1 ; +while (1 ) { +/* ADI can not be enabled on PFN +* mapped pages +*/ +if (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )) +return 0 ; - vma =find_vma (current->mm, addr); -if (vma) { -/* ADI can not be enabled on PFN -* mapped pages -*/ -if (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )) -return 0 ; +/* Mergeable pages can become unmergeable +* if ADI is enabled on them even if they +* have identical data on them. This can be +* because ADI enabled pages with identical +* data may still not have identical ADI +* tags on them. Disallow ADI on mergeable +* pages. +*/ +if (vma->vm_flags &VM_MERGEABLE ) +return 0 ; -/* Mergeable pages can become unmergeable -* if ADI is enabled on them even if they -* have identical data on them. This can be -* because ADI enabled pages with identical -* data may still not have identical ADI -* tags on them. Disallow ADI on mergeable -* pages. -*/ -if (vma->vm_flags &VM_MERGEABLE ) -return 0 ; - } +/* reached the end of the range without errors? */ +if (addr+len <= vma->vm_end) +return 1 ; + next = vma->vm_next; +/* if a VMA hole follows, let mprotect() deal with it */ +if (!next || next->vm_start != vma->vm_end) +return 1 ; + vma = next; } }return 1 ;
arch/powerpc/kernel/syscalls.c static inline long do_mmap2(unsigned long addr, size_t len, · line 46
Quoted 10 lines · Jann Horn
arch/powerpc/kernel/syscalls.c +1
-1@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len))
This call isn't under mmap lock. I also find it rather weird as the generic code only calls arch_validate_prot from mprotect, only powerpc also calls it from mmap.
This seems to go back to commit ef3d3246a0d0 ("powerpc/mm: Add Strong Access Ordering support")
Jann Horn
reviewerQuoted 16 lines · Christoph Hellwig
arch/powerpc/kernel/syscalls.c static inline long do_mmap2(unsigned long addr, size_t len, · line 46
Quoted 10 lines · Jann Horn
arch/powerpc/kernel/syscalls.c +1
-1@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len))This call isn't under mmap lock. I also find it rather weird as the generic code only calls
arch_validate_protfrom mprotect, only powerpc also calls it from mmap.
Quoted 2 lines
This seems to go back to commit
ef3d3246a0d0("powerpc/mm: Add Strong Access Ordering support")
I'm _guessing_ the idea in the generic case might be that mmap() doesn't check unknown bits in the protection flags, and therefore maybe people wanted to avoid adding new error cases that could be caused by random high bits being set? So while the mprotect() case checks the flags and refuses unknown values, the mmap() code just lets the architecture figure out which bits are actually valid to set (via arch_calc_vm_prot_bits()) and silently ignores the rest?
And powerpc apparently decided that they do want to error out on bogus prot values passed to their version of mmap(), and in exchange, assume in arch_calc_vm_prot_bits() that the protection bits are valid?
powerpc's arch_validate_prot() doesn't actually need the mmap lock, so I think this is fine-ish for now (as in, while the code is a bit unclean, I don't think I'm making it worse, and I don't think it's actually buggy). In theory, we could move the arch_validate_prot() call over into the mmap guts, where we're holding the lock, and gate it on the architecture or on some feature CONFIG that powerpc can activate in its Kconfig. But I'm not sure whether that'd be helping or making things worse, so when I sent this patch, I deliberately left the powerpc stuff as-is.
Quoted 22 lines · Jann Horn
Quoted 11 lines
arch/powerpc/kernel/syscalls.c static inline long do_mmap2(unsigned long addr, size_t len, · line 46
Quoted 6 lines
arch/powerpc/kernel/syscalls.c +1
-1@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len))This call isn't under mmap lock. I also find it rather weird as the generic code only calls
arch_validate_protfrom mprotect, only powerpc also calls it from mmap.Quoted 2 lines
This seems to go back to commit
ef3d3246a0d0("powerpc/mm: Add Strong Access Ordering support")I'm _guessing_ the idea in the generic case might be that
mmap()doesn't check unknown bits in the protection flags, and therefore maybe people wanted to avoid adding new error cases that could be caused by random high bits being set? So while themprotect()case checks the flags and refuses unknown values, themmap()code just lets the architecture figure out which bits are actually valid to set (viaarch_calc_vm_prot_bits()) and silently ignores the rest?
Quoted 3 lines
And powerpc apparently decided that they do want to error out on bogus prot values passed to their version of
mmap(), and in exchange, assume inarch_calc_vm_prot_bits()that the protection bits are valid?
The problem really is that now programs behave different on powerpc compared to all other architectures.
Quoted 9 lines
powerpc's
arch_validate_prot()doesn't actually need the mmap lock, so I think this is fine-ish for now (as in, while the code is a bit unclean, I don't think I'm making it worse, and I don't think it's actually buggy). In theory, we could move thearch_validate_prot()call over into the mmap guts, where we're holding the lock, and gate it on the architecture or on some feature CONFIG that powerpc can activate in itsKconfig. But I'm not sure whether that'd be helping or making things worse, so when I sent this patch, I deliberately left the powerpc stuff as-is.
For now I'd just duplicate the trivial logic from arch_validate_prot in the powerpc version of do_mmap2 and add a comment that this check causes a gratious incompatibility to all other architectures. And then hope that the powerpc maintainers fix it up :)
Michael Ellerman
reviewerQuoted 17 lines · Jann Horn
Quoted 16 lines · Christoph Hellwig
arch/powerpc/kernel/syscalls.c static inline long do_mmap2(unsigned long addr, size_t len, · line 46
Quoted 10 lines · Jann Horn
arch/powerpc/kernel/syscalls.c +1
-1@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len))This call isn't under mmap lock. I also find it rather weird as the generic code only calls
arch_validate_protfrom mprotect, only powerpc also calls it from mmap.
Quoted 7 lines
Quoted 2 lines
This seems to go back to commit
ef3d3246a0d0("powerpc/mm: Add Strong Access Ordering support")I'm _guessing_ the idea in the generic case might be that
mmap()doesn't check unknown bits in the protection flags, and therefore maybe people wanted to avoid adding new error cases that could be caused by random high bits being set?
I suspect it's just that when we added it we updated our do_mmap2() and didn't touch the generic version because we didn't need to. ie. it's not intentional it's just a buglet.
I think this is the original submission:
https://lore.kernel.org/linuxppc-dev/[email protected]/
Which only calls arch_validate_prot() from mprotect and the powerpc code, and there was no discussion about adding it elsewhere.
Quoted 8 lines
So while the
mprotect()case
checks the flags and refuses unknown values, themmap()code just lets the architecture figure out which bits are actually valid to set (viaarch_calc_vm_prot_bits()) and silently ignores the rest?And powerpc apparently decided that they do want to error out on bogus prot values passed to their version of
mmap(), and in exchange, assume inarch_calc_vm_prot_bits()that the protection bits are valid?
I don't think we really decided that, it just happened by accident and no one noticed/complained.
Seems userspace is pretty well behaved when it comes to passing prot values to mmap().
Quoted 9 lines
powerpc's
arch_validate_prot()doesn't actually need the mmap lock, so I think this is fine-ish for now (as in, while the code is a bit unclean, I don't think I'm making it worse, and I don't think it's actually buggy). In theory, we could move thearch_validate_prot()call over into the mmap guts, where we're holding the lock, and gate it on the architecture or on some feature CONFIG that powerpc can activate in itsKconfig. But I'm not sure whether that'd be helping or making things worse, so when I sent this patch, I deliberately left the powerpc stuff as-is.
I think what you've done is fine, and anything more elaborate is not worth the effort.
cheers
Catalin Marinas
maintainerQuoted 10 lines · Michael Ellerman
Quoted 8 lines · Jann Horn
So while the
mprotect()case
checks the flags and refuses unknown values, themmap()code just lets the architecture figure out which bits are actually valid to set (viaarch_calc_vm_prot_bits()) and silently ignores the rest?And powerpc apparently decided that they do want to error out on bogus prot values passed to their version of
mmap(), and in exchange, assume inarch_calc_vm_prot_bits()that the protection bits are valid?
Quoted 5 lines
I don't think we really decided that, it just happened by accident and no one noticed/complained.
Seems userspace is pretty well behaved when it comes to passing prot values to
mmap().
It's not necessarily about well behaved but whether it can have security implications. On arm64, if the underlying memory does not support MTE (say some DAX mmap) but we still allow PROT_MTE driven by user, it will lead to an SError which brings the whole machine down.
Not sure whether ADI has similar requirements but at least for arm64 we addressed the mmap() case as well (see my other email on the details; I think the approach would work on SPARC as well).
Signature · 1 line
Catalin
Quoted 29 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.Fixes 9035cf9a97e4 ("mm: Add address parameter to arch_validate_prot()")Suggested-by Khalid Aziz <[email protected]>Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Jann Horn <[email protected]>
Quoted 8 lines
--- arch/arm64/include/asm/mman.h | 4 ++-- arch/powerpc/include/asm/mman.h | 3 ++- arch/powerpc/kernel/syscalls.c | 2 +- arch/sparc/include/asm/mman.h | 6 ++++-- include/linux/mman.h | 3 ++- mm/mprotect.c | 6 ++++-- 6 files changed, 15 insertions(+), 9 deletions(-)
This looks good to me.
As Chris pointed out, the call to arch_validate_prot() from do_mmap2() is made without holding mmap_lock. Lock is not acquired until vm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations of validate_prot to require mmap_lock be held when non-sparc implementations do not have such need yet. Since do_mmap2() is in powerpc specific code, for now this patch solves a current problem. That leaves open the question of should generic mmap call arch_validate_prot and return EINVAL for invalid combination of protection bits, but that is better addressed in a separate patch.
Quoted 107 lines
arch/arm64/include/asm/mman.h +2
-2@@ -23,7 +23,7 @@ static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags)static inline bool arch_validate_prot (unsigned long prot, -unsigned long addr__always_unused ) +unsigned long addr__always_unused ,unsigned long len__always_unused ) {unsigned long supported =PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM ;@@ -32,6 +32,6 @@ static inline bool arch_validate_prot(unsigned long prot, return (prot & ~supported) == 0; } -#define arch_validate_prot (prot, addr) arch_validate_prot(prot, addr) +#define arch_validate_prot (prot, addr, len) arch_validate_prot(prot, addr, len)#endif /* ! __ASM_MMAN_H__ */ arch/powerpc/include/asm/mman.h +2
-1@@ -36,7 +36,8 @@ static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags) }#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags) -static inline bool arch_validate_prot (unsigned long prot,unsigned long addr) +static inline bool arch_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) {if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_SAO ))return false;arch/powerpc/kernel/syscalls.c +1
-1@@ -43,7 +43,7 @@ static inline long do_mmap2(unsigned long addr, size_t len, {long ret = -EINVAL ; -if (!arch_validate_prot (prot, addr)) +if (!arch_validate_prot (prot, addr, len)) goto out;if (shift) {arch/sparc/include/asm/mman.h +4
-2@@ -52,9 +52,11 @@ static inline pgprot_t sparc_vm_get_page_prot(unsigned long vm_flags)return (vm_flags &VM_SPARC_ADI ) ?__pgprot (_PAGE_MCD_4V) :__pgprot (0 ); } -#define arch_validate_prot (prot, addr) sparc_validate_prot(prot, addr) -static inline int sparc_validate_prot (unsigned long prot,unsigned long addr) +#define arch_validate_prot (prot, addr, len) sparc_validate_prot(prot, addr, len) +static inline int sparc_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) { +mmap_assert_write_locked (current->mm);if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM |PROT_ADI ))return 0 ;if (prot &PROT_ADI ) {include/linux/mman.h +2
-1@@ -96,7 +96,8 @@ static inline void vm_unacct_memory(long pages)* * Returns true if the prot flags are valid */ -static inline bool arch_validate_prot (unsigned long prot,unsigned long addr) +static inline bool arch_validate_prot (unsigned long prot,unsigned long addr, +unsigned long len) {return (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM )) ==0 ; }mm/mprotect.c +4
-2@@ -533,14 +533,16 @@ static int do_mprotect_pkey(unsigned long start, size_t len, end = start + len;if (end <= start)return -ENOMEM ; -if (!arch_validate_prot (prot, start)) -return -EINVAL ; reqprot = prot;if (mmap_write_lock_killable (current->mm))return -EINTR ; + error = -EINVAL ; +if (!arch_validate_prot (prot, start, len)) + goto out; +/* * If userspace did not allocate the pkey, do not let * them use it here. base-commit:
c85fb28b6f999db9928b841f63f1beeb3074eeca
Catalin Marinas
maintainerHi Khalid,
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.
[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.
I still think sparc should avoid walking the vmas in arch_validate_prot(). The core code already has the vmas, though not when calling arch_validate_prot(). That's one of the reasons I added arch_validate_flags() with the MTE patches. For sparc, this could be (untested, just copied the arch_validate_prot() code):
static inline bool arch_validate_flags(unsigned long vm_flags)
{
if (!(vm_flags & VM_SPARC_ADI))
return true;
if (!adi_capable())
return false;
/* ADI can not be enabled on PFN mapped pages */
if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
return false;
/*
* Mergeable pages can become unmergeable if ADI is enabled on
* them even if they have identical data on them. This can be
* because ADI enabled pages with identical data may still not
* have identical ADI tags on them. Disallow ADI on mergeable
* pages.
*/
if (vma->vm_flags & VM_MERGEABLE)
return false;
return true;
}Quoted 4 lines
That leaves open the question of should
generic mmap callarch_validate_protand return EINVAL for invalid combination of protection bits, but that is better addressed in a separate patch.
The above would cover mmap() as well.
The current sparc_validate_prot() relies on finding the vma for the corresponding address. However, if you call this early in the mmap() path, there's no such vma. It is only created later in mmap_region() which no longer has the original PROT_* flags (all converted to VM_* flags).
Calling arch_validate_flags() on mmap() has a small side-effect on the user ABI: if the CPU is not adi_capable(), PROT_ADI is currently ignored on mmap() but rejected by sparc_validate_prot(). Powerpc already does this already and I think it should be fine for arm64 (it needs checking though as we have another flag, PROT_BTI, hopefully dynamic loaders don't pass this flag unconditionally).
However, as I said above, it doesn't solve the mmap() PROT_ADI checking for sparc since there's no vma yet. I'd strongly recommend the arch_validate_flags() approach and reverting the "start" parameter added to arch_validate_prot() if you go for the flags route.
Thanks.
Signature · 1 line
Catalin
Quoted 36 lines · Catalin Marinas
Hi Khalid,
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.
Quoted 5 lines
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in do_mprotect_pkey(). When ADI is being enabled across multiple VMAs and arch_validate_flags() fails on a VMA later, do_mprotect_pkey() will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure of mprotect() call. I think it should be all or nothing with mprotect() - when one calls mprotect() from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.
--
Khalid
Quoted 53 lines
static inline bool
arch_validate_flags(unsigned long vm_flags){ if (!(vm_flags & VM_SPARC_ADI)) return true; if (!adi_capable()) return false; /* ADI can not be enabled on PFN mapped pages */ if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) return false; /* * Mergeable pages can become unmergeable if ADI is enabled on * them even if they have identical data on them. This can be * because ADI enabled pages with identical data may still not * have identical ADI tags on them. Disallow ADI on mergeable * pages. */ if (vma->vm_flags & VM_MERGEABLE) return false; return true; }Quoted 4 lines
That leaves open the question of should
generic mmap callarch_validate_protand return EINVAL for invalid combination of protection bits, but that is better addressed in a separate patch.The above would cover
mmap()as well.The current
sparc_validate_prot()relies on finding the vma for the corresponding address. However, if you call this early in themmap()path, there's no such vma. It is only created later inmmap_region()which no longer has the original PROT_* flags (all converted to VM_* flags).Calling
arch_validate_flags()onmmap()has a small side-effect on the user ABI: if the CPU is notadi_capable(),PROT_ADIis currently ignored onmmap()but rejected bysparc_validate_prot(). Powerpc already does this already and I think it should be fine for arm64 (it needs checking though as we have another flag,PROT_BTI, hopefully dynamic loaders don't pass this flag unconditionally).However, as I said above, it doesn't solve the
mmap()PROT_ADIchecking for sparc since there's no vma yet. I'd strongly recommend thearch_validate_flags()approach and reverting the "start" parameter added toarch_validate_prot()if you go for the flags route.Thanks.
Catalin Marinas
maintainerQuoted 41 lines · Khalid Aziz
Quoted 34 lines · Catalin Marinas
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.Quoted 5 lines
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):
Quoted 11 lines
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in
do_mprotect_pkey(). When ADI is being enabled across multiple VMAs andarch_validate_flags()fails on a VMA later,do_mprotect_pkey()will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure ofmprotect()call. I think it should be all or nothing withmprotect()- when one callsmprotect()from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.
I thought the same initially but mprotect() already does this with the VM_MAY* flag checking. If you ask it for an mprotect() that crosses multiple vmas and one of them fails, it doesn't roll back the changes to the prior ones. I considered that a similar approach is fine for MTE (it's most likely a user error).
Signature · 1 line
Catalin
Quoted 54 lines · Catalin Marinas
Quoted 41 lines · Khalid Aziz
Quoted 34 lines · Catalin Marinas
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.Quoted 5 lines
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):Quoted 11 lines
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in
do_mprotect_pkey(). When ADI is being enabled across multiple VMAs andarch_validate_flags()fails on a VMA later,do_mprotect_pkey()will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure ofmprotect()call. I think it should be all or nothing withmprotect()- when one callsmprotect()from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.
Quoted 6 lines
I thought the same initially but
mprotect()already does this with the VM_MAY* flag checking. If you ask it for anmprotect()that crosses multiple vmas and one of them fails, it doesn't roll back the changes to the prior ones. I considered that a similar approach is fine for MTE (it's most likely a user error).
You are right about the current behavior with VM_MAY* flags, but that is not the right behavior. Adding more cases to this just perpetuates incorrect behavior. It is not easy to roll back changes after VMAs have potentially been split/merged which is probably why the current code simply throws in the towel and returns with partially modified address space. It is lot easier to do all the checks upfront and then proceed or not proceed with modifying VMAs. One approach might be to call arch_validate_flags() in a loop before modifying VMAs and walk all VMAs with a read lock held. Current code also bails out with ENOMEM if it finds a hole in the address range and leaves any modifications already made in place. This is another case where a hole could have been detected earlier.
--
Khalid
Catalin Marinas
maintainerQuoted 61 lines · Khalid Aziz
Quoted 54 lines · Catalin Marinas
Quoted 41 lines · Khalid Aziz
Quoted 34 lines · Catalin Marinas
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.Quoted 5 lines
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):Quoted 11 lines
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in
do_mprotect_pkey(). When ADI is being enabled across multiple VMAs andarch_validate_flags()fails on a VMA later,do_mprotect_pkey()will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure ofmprotect()call. I think it should be all or nothing withmprotect()- when one callsmprotect()from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.Quoted 5 lines
I thought the same initially but
mprotect()already does this with the VM_MAY* flag checking. If you ask it for anmprotect()that crosses multiple vmas and one of them fails, it doesn't roll back the changes to the prior ones. I considered that a similar approach is fine for MTE (it's most likely a user error).
Quoted 12 lines
You are right about the current behavior with VM_MAY* flags, but that is not the right behavior. Adding more cases to this just perpetuates incorrect behavior. It is not easy to roll back changes after VMAs have potentially been split/merged which is probably why the current code simply throws in the towel and returns with partially modified address space. It is lot easier to do all the checks upfront and then proceed or not proceed with modifying VMAs. One approach might be to call
arch_validate_flags()in a loop before modifying VMAs and walk all VMAs with a read lock held. Current code also bails out with ENOMEM if it finds a hole in the address range and leaves any modifications already made in place. This is another case where a hole could have been detected earlier.
This should be ideal indeed though with the risk of breaking the current ABI (FWIW, FreeBSD seems to do a first pass to check for violations:
https://github.com/freebsd/freebsd/blob/master/sys/vm/vm_map.c#L2630).
However, I'm not sure it's worth the hassle. Do we expect the user to call mprotect() across multiple mixed type mappings while relying on no change if an error is returned? We should probably at least document the current behaviour in the mprotect man page.
Signature · 1 line
Catalin
Quoted 75 lines · Catalin Marinas
Quoted 61 lines · Khalid Aziz
Quoted 54 lines · Catalin Marinas
Quoted 41 lines · Khalid Aziz
Quoted 34 lines · Catalin Marinas
Quoted 24 lines · Khalid Aziz
Quoted 23 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.[...]
Quoted 7 lines
As Chris pointed out, the call to
arch_validate_prot()fromdo_mmap2()is made without holdingmmap_lock. Lock is not acquired untilvm_mmap_pgoff(). This variance is uncomfortable but I am more uncomfortable forcing all implementations ofvalidate_protto requiremmap_lockbe held when non-sparc implementations do not have such need yet. Sincedo_mmap2()is in powerpc specific code, for now this patch solves a current problem.Quoted 5 lines
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):Quoted 11 lines
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in
do_mprotect_pkey(). When ADI is being enabled across multiple VMAs andarch_validate_flags()fails on a VMA later,do_mprotect_pkey()will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure ofmprotect()call. I think it should be all or nothing withmprotect()- when one callsmprotect()from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.Quoted 5 lines
I thought the same initially but
mprotect()already does this with the VM_MAY* flag checking. If you ask it for anmprotect()that crosses multiple vmas and one of them fails, it doesn't roll back the changes to the prior ones. I considered that a similar approach is fine for MTE (it's most likely a user error).Quoted 12 lines
You are right about the current behavior with VM_MAY* flags, but that is not the right behavior. Adding more cases to this just perpetuates incorrect behavior. It is not easy to roll back changes after VMAs have potentially been split/merged which is probably why the current code simply throws in the towel and returns with partially modified address space. It is lot easier to do all the checks upfront and then proceed or not proceed with modifying VMAs. One approach might be to call
arch_validate_flags()in a loop before modifying VMAs and walk all VMAs with a read lock held. Current code also bails out with ENOMEM if it finds a hole in the address range and leaves any modifications already made in place. This is another case where a hole could have been detected earlier.
Quoted 3 lines
This should be ideal indeed though with the risk of breaking the current ABI (FWIW, FreeBSD seems to do a first pass to check for violations:
https://github.com/freebsd/freebsd/blob/master/sys/vm/vm_map.c#L2630).
I am not sure I understand where the ABI breakage would be. Are we aware of apps that intentionally modify address space partially using the current code? What FreeBSD does seems like a reasonable thing to do. Any way first thing to do is to update sparc to use arch_validate_flags() and update sparc_validate_prot() to not peek into vma without lock. I can do that unless Jann wants to rework this 2 patch series with these changes.
Quoted 6 lines
However, I'm not sure it's worth the hassle. Do we expect the user to call
mprotect()across multiple mixed type mappings while relying on no change if an error is returned? We should probably at least document the current behaviour in the mprotect man page.
Yes, documenting current behavior is definitely a good thing to do.
--
Khalid
Jann Horn
reviewerOn Wed, Oct 14, 2020 at 11:24 PM Khalid Aziz <[email protected]> wrote:
[...]
Quoted 5 lines
current code? What FreeBSD does seems like a reasonable thing to do. Any way first thing to do is to update sparc to use
arch_validate_flags()and updatesparc_validate_prot()to not peek into vma without lock. I can do that unless Jann wants to rework this 2 patch series with these changes.
Ah, if you're willing to take care of that, that'd be nice, please do. :)
Catalin Marinas
maintainerQuoted 42 lines · Khalid Aziz
Quoted 41 lines · Catalin Marinas
Quoted 39 lines · Khalid Aziz
Quoted 20 lines · Catalin Marinas
Quoted 7 lines · Khalid Aziz
Quoted 5 lines · Catalin Marinas
I still think sparc should avoid walking the vmas in
arch_validate_prot(). The core code already has the vmas, though not when callingarch_validate_prot(). That's one of the reasons I addedarch_validate_flags()with the MTE patches. For sparc, this could be (untested, just copied thearch_validate_prot()code):Quoted 11 lines
I am little uncomfortable with the idea of validating protection bits inside the VMA walk loop in
do_mprotect_pkey(). When ADI is being enabled across multiple VMAs andarch_validate_flags()fails on a VMA later,do_mprotect_pkey()will bail out with error leaving ADI enabled on earlier VMAs. This will apply to protection bits other than ADI as well of course. This becomes a partial failure ofmprotect()call. I think it should be all or nothing withmprotect()- when one callsmprotect()from userspace, either the entire address range passed in gets its protection bits updated or none of it does. That requires validating protection bits upfront or undoing what earlier iterations of VMA walk loop might have done.Quoted 5 lines
I thought the same initially but
mprotect()already does this with the VM_MAY* flag checking. If you ask it for anmprotect()that crosses multiple vmas and one of them fails, it doesn't roll back the changes to the prior ones. I considered that a similar approach is fine for MTE (it's most likely a user error).You are right about the current behavior with VM_MAY* flags, but that is not the right behavior. Adding more cases to this just perpetuates incorrect behavior. It is not easy to roll back changes after VMAs have potentially been split/merged which is probably why the current code simply throws in the towel and returns with partially modified address space. It is lot easier to do all the checks upfront and then proceed or not proceed with modifying VMAs. One approach might be to call
arch_validate_flags()in a loop before modifying VMAs and walk all VMAs with a read lock held. Current code also bails out with ENOMEM if it finds a hole in the address range and leaves any modifications already made in place. This is another case where a hole could have been detected earlier.
Quoted 7 lines
Quoted 3 lines
This should be ideal indeed though with the risk of breaking the current ABI (FWIW, FreeBSD seems to do a first pass to check for violations:
https://github.com/freebsd/freebsd/blob/master/sys/vm/vm_map.c#L2630).I am not sure I understand where the ABI breakage would be. Are we aware of apps that intentionally modify address space partially using the current code?
I hope there aren't any but you never know until you make the change and someone complains. Arguably, such user code is already broken since mprotect() doesn't even tell where the failure occurred.
Quoted 3 lines
What FreeBSD does seems like a reasonable thing to do. Any way first thing to do is to update sparc to use
arch_validate_flags()and updatesparc_validate_prot()to not peek into vma without lock.
If you go for arch_validate_flags(), I think sparc_validate_prot() doesn't need the vma at all.
BTW, on the ADI topic, I think you have a race in do_swap_page() since set_pte_at() is called before arch_do_swap_page(). So a thread in the same process would see the new mapping but the tags have not been updated yet. Unless sparc relies on the new user pte to be set, I think you can just swap the two calls.
Signature · 1 line
Catalin
Quoted 7 lines · Catalin Marinas
Quoted 3 lines · Khalid Aziz
What FreeBSD does seems like a reasonable thing to do. Any way first thing to do is to update sparc to use
arch_validate_flags()and updatesparc_validate_prot()to not peek into vma without lock.If you go for
arch_validate_flags(), I thinksparc_validate_prot()doesn't need the vma at all.
Yes, the plan is to move vma flag check from sparc_validate_prot() to arch_validate_flags()..
Quoted 7 lines
BTW, on the ADI topic, I think you have a race in
do_swap_page()sinceset_pte_at()is called beforearch_do_swap_page(). So a thread in the same process would see the new mapping but the tags have not been updated yet. Unless sparc relies on the new user pte to be set, I think you can just swap the two calls.
Thanks for pointing that out. I will take a look at it.
--
Khalid
Catalin Marinas
maintainerQuoted 21 lines · Jann Horn
arch_validate_prot()is a hook that can validate whether a given set of protection flags is valid in anmprotect()operation. It is given the set of protection flags and the address being modified.However, the address being modified can currently not actually be used in a meaningful way because:
1. Only the address is given, but not the length, and the operation can span multiple VMAs. Therefore, the callee can't actually tell which virtual address range, or which VMAs, are being targeted.
2. Themmap_lockis not held, meaning that if the callee were to check the VMA at @addr, that VMA would be unrelated to the one the operation is performed on.Currently, custom
arch_validate_prot()handlers are defined by arm64, powerpc and sparc.
arm64 and powerpc don't care about the address range, they just check the flags against CPU support masks.
sparc'sarch_validate_prot()attempts to look at the VMA, but doesn't take themmap_lock.
Quoted 2 lines
Change the function signature to also take a length, and move the
arch_validate_prot()call inmm/mprotect.cdown into the locked region.
For arm64 mte, I noticed the arch_validate_prot() issue with multiple vmas and addressed this in a different way:
https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/commit/?h=for-next/mte&idÄ62ac288f2c97e0c1d9ff6a65955317e799f958