arch/arm64/kernel/traps: Use find_vma_intersection() in traps for setting si_code
Subsystems (4)
PATCH 1/1 arch/arm64/kernel/traps: Use find_vma_intersection() in traps for setting si_code
find_vma() will continue to search upwards until the end of the virtual memory space. This means the si_code would almost never be set to SEGV_MAPERR even when the address falls outside of any VMA. The result is that the si_code is not reliable as it may or may not be set to the correct result, depending on where the address falls in the address space.
Using find_vma_intersection() allows for what is intended by only returning a VMA if it falls within the range provided, in this case a window of 1.
--- arch/arm64/kernel/traps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kernel/traps.c +2 -1
@@ -383,9 +383,10 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned ivoid arm64_notify_segfault (unsigned long addr) {int code; +unsigned long ut_addr =untagged_addr (addr);mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma_intersection (current->mm, ut_addr, ut_addr +1 ) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;
Signature · 1 line
2.30.0
Catalin Marinas
maintainerQuoted 22 lines · Liam Howlett
find_vma()will continue to search upwards until the end of the virtual memory space. This means thesi_codewould almost never be set toSEGV_MAPERReven when the address falls outside of any VMA. The result is that thesi_codeis not reliable as it may or may not be set to the correct result, depending on where the address falls in the address space.Using
find_vma_intersection()allows for what is intended by only returning a VMA if it falls within the range provided, in this case a window of 1.Signed-off-by Liam R. Howlett <[email protected]>--- arch/arm64/kernel/traps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kernel/traps.c +0
-0@@ -383,9 +383,10 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned ivoid arm64_notify_segfault (unsigned long addr)
arch/arm64/kernel/traps.c void force_signal_inject(int signal, int code, unsigned long address, unsigned i · line 392
Quoted 10 lines
arch/arm64/kernel/traps.c +2
-1{int code; +unsigned long ut_addr =untagged_addr (addr);mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma_intersection (current->mm, ut_addr, ut_addr +1 ) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;
I don't think your change is entirely correct either. We can have a fault below the vma of a stack (with VM_GROWSDOWN) and find_vma_intersection() would return NULL but it should be a SEGV_ACCERR instead.
Maybe this should employ similar checks as __do_page_fault() (with expand_stack() and VM_GROWSDOWN).
Signature · 1 line
Catalin
Quoted 27 lines · Catalin Marinas
Quoted 26 lines · Liam Howlett
find_vma()will continue to search upwards until the end of the virtual memory space. This means thesi_codewould almost never be set toSEGV_MAPERReven when the address falls outside of any VMA. The result is that thesi_codeis not reliable as it may or may not be set to the correct result, depending on where the address falls in the address space.Using
find_vma_intersection()allows for what is intended by only returning a VMA if it falls within the range provided, in this case a window of 1.Signed-off-by Liam R. Howlett <[email protected]>--- arch/arm64/kernel/traps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kernel/traps.c +1
-0@@ -383,9 +383,10 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned ivoid arm64_notify_segfault (unsigned long addr) {int code; +unsigned long ut_addr =untagged_addr (addr);
Quoted 6 lines
arch/arm64/kernel/traps.c void force_signal_inject(int signal, int code, unsigned long address, unsigned i · line 392
Quoted 6 lines
arch/arm64/kernel/traps.c +1
-1mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma_intersection (current->mm, ut_addr, ut_addr +1 ) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;
Thank you for taking the time to thoroughly review this patch.
Quoted 5 lines
I don't think your change is entirely correct either. We can have a fault below the vma of a stack (with
VM_GROWSDOWN) andfind_vma_intersection()would return NULL but it should be aSEGV_ACCERRinstead.
I'm pretty sure I am missing something. From what you said above, I think this means that there can be a user cache fault below the stack which should notify the user application that they are not allowed to expand the stack by sending a SIGV_ACCERR in the si_code? Is this expected behaviour or am I missing a code path to this function?
Quoted 3 lines
Maybe this should employ similar checks as
__do_page_fault()(withexpand_stack()andVM_GROWSDOWN).
You mean the code needs to detect endianness and to check if this is an attempt to expand the stack for both cases?
Thanks,
Liam
Catalin Marinas
maintainerQuoted 34 lines · Liam Howlett
Quoted 33 lines · Catalin Marinas
Quoted 32 lines · Liam Howlett
find_vma()will continue to search upwards until the end of the virtual memory space. This means thesi_codewould almost never be set toSEGV_MAPERReven when the address falls outside of any VMA. The result is that thesi_codeis not reliable as it may or may not be set to the correct result, depending on where the address falls in the address space.Using
find_vma_intersection()allows for what is intended by only returning a VMA if it falls within the range provided, in this case a window of 1.Signed-off-by Liam R. Howlett <[email protected]>--- arch/arm64/kernel/traps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kernel/traps.c +2
-1@@ -383,9 +383,10 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned ivoid arm64_notify_segfault (unsigned long addr) {int code; +unsigned long ut_addr =untagged_addr (addr);mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma_intersection (current->mm, ut_addr, ut_addr +1 ) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;
[...]
Quoted 10 lines
Quoted 4 lines
I don't think your change is entirely correct either. We can have a fault below the vma of a stack (with
VM_GROWSDOWN) andfind_vma_intersection()would return NULL but it should be aSEGV_ACCERRinstead.I'm pretty sure I am missing something. From what you said above, I think this means that there can be a user cache fault below the stack which should notify the user application that they are not allowed to expand the stack by sending a
SIGV_ACCERRin thesi_code? Is this expected behaviour or am I missing a code path to this function?
My point was that find_vma() may return a valid vma where addr < vm_end but also addr < vm_addr. It's the responsibility of the caller to check that that vma can be expanded (VM_GROWSDOWN) and we do something like this in __do_page_fault(). find_vma_intersection(), OTOH, requires addr
Quoted 1 line
=
vm_start.
If we hit this case (addr < vm_start), normally we'd first need to check whether it's expandable and, if not, return MAPERR. If it's expandable, it should be ACCERR since something else caused the fault.
Now, I think at least for user_cache_maint_handler(), we can assume that __do_page_fault() handled any expansion already, so we don't need to check it here. In this case, your find_vma_intersection() check should work.
Are there other cases where we invoke arm64_notify_segfault() without a prior fault? I think in swp_handler() we can bail out early before we even attempted the access so we may report MAPERR but ACCERR is a better indication. Also in sys_rt_sigreturn() we always call it as arm64_notify_segfault(regs->sp). I'm not sure that's correct in all cases, see restore_altstack().
I guess this code needs some tidying up.
Quoted 5 lines
Quoted 2 lines
Maybe this should employ similar checks as
__do_page_fault()(withexpand_stack()andVM_GROWSDOWN).You mean the code needs to detect endianness and to check if this is an attempt to expand the stack for both cases?
Nothing to do with endianness, just the relation between the address and the vma->vm_start and whether the vma can be expanded down.
Signature · 1 line
Catalin
Quoted 47 lines · Catalin Marinas
Quoted 34 lines · Liam Howlett
Quoted 33 lines · Catalin Marinas
Quoted 32 lines · Liam Howlett
find_vma()will continue to search upwards until the end of the virtual memory space. This means thesi_codewould almost never be set toSEGV_MAPERReven when the address falls outside of any VMA. The result is that thesi_codeis not reliable as it may or may not be set to the correct result, depending on where the address falls in the address space.Using
find_vma_intersection()allows for what is intended by only returning a VMA if it falls within the range provided, in this case a window of 1.Signed-off-by Liam R. Howlett <[email protected]>--- arch/arm64/kernel/traps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kernel/traps.c +2
-1@@ -383,9 +383,10 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned ivoid arm64_notify_segfault (unsigned long addr) {int code; +unsigned long ut_addr =untagged_addr (addr);mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma_intersection (current->mm, ut_addr, ut_addr +1 ) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;[...]
Quoted 10 lines
Quoted 4 lines
I don't think your change is entirely correct either. We can have a fault below the vma of a stack (with
VM_GROWSDOWN) andfind_vma_intersection()would return NULL but it should be aSEGV_ACCERRinstead.I'm pretty sure I am missing something. From what you said above, I think this means that there can be a user cache fault below the stack which should notify the user application that they are not allowed to expand the stack by sending a
SIGV_ACCERRin thesi_code? Is this expected behaviour or am I missing a code path to this function?
Quoted 5 lines
My point was that
find_vma()may return a valid vma where addr <vm_endbut also addr <vm_addr. It's the responsibility of the caller to check that that vma can be expanded (VM_GROWSDOWN) and we do something like this in__do_page_fault().find_vma_intersection(), OTOH, requires addrQuoted 1 line
=
vm_start.
Right. The find_vma() interface is not clear by the function name; returning a VMA that doesn't include the address of interest is unclear. I think this is why we ended up with the bug in the first place.
Quoted 10 lines
If we hit this case (addr <
vm_start), normally we'd first need to check whether it's expandable and, if not, return MAPERR. If it's expandable, it should be ACCERR since something else caused the fault.Now, I think at least for
user_cache_maint_handler(), we can assume that__do_page_fault()handled any expansion already, so we don't need to check it here. In this case, yourfind_vma_intersection()check should work.
Quoted 4 lines
Are there other cases where we invoke
arm64_notify_segfault()without a prior fault? I think inswp_handler()we can bail out early before we even attempted the access so we may report MAPERR but ACCERR is a better indication.
swp_handler() is also buggy. It is currently getting the ACCERR as long as the address being checked is > mm->highest_vm_end. If access_ok() fails, it should return ACCERR and not search VMAs for the address at all.
...
Quoted 3 lines
Also in
sys_rt_sigreturn()we always call it asarm64_notify_segfault(regs->sp). I'm not sure that's correct in all cases, seerestore_altstack().
Ditto for sys_rt_sigreturn() and sys_sigreturn(), they both suffer the same bug as swp_handler() outlined above.
In the case of restore_sigframe() or restore_altstack() failing, it seems that the signal shouldn't be dependent on where the address falls within the VMA at all. Should the signal still be SIGSEGV or something else? By the comments, I would have thought SIGBUS, si_code of BUS_ADRALN?
Quoted 2 lines
I guess this code needs some tidying up.
Indeed, there seems to be a few code paths that need to skip this function all together and just set the code to ACCERR - especially since the access_ok() just validates the number itself.
I don't think the right answer is to rewrite the function to check VM_GROWSDOWN, as I cannot see a way to reach this function when trying to expand the stack which should report back ACCERR. Do you agree?
I also see that my fix would expose other bugs which need to be addressed at the same time.
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Maybe this should employ similar checks as
__do_page_fault()(withexpand_stack()andVM_GROWSDOWN).You mean the code needs to detect endianness and to check if this is an attempt to expand the stack for both cases?
Nothing to do with endianness, just the relation between the address and the
vma->vm_startand whether the vma can be expanded down.
Okay, thanks for clarifying.
Cheers,
Liam