MIPS: Minor EVA related unwind fixes
Subsystems (2)
cover MIPS: Minor EVA related unwind fixes
Here are a couple of minor EVA related stack unwind fixes, which prevent the use of unwind_stack() (which unwinds using kallsyms knowledge of where kernel functions begin and end) to unwind userland code, particularly where the PC is a valid kernel address, which could happen innocently with EVA due to the overlapped user/kernel address spaces.
Note that unwind_stack() is defensive enough that it shouldn't do any harm, so I've only tagged stable back to v3.15, when EVA was added and it could conceivably happen under normal operation.
A rough audit shows no other uses of __kernel_text_address() in relation to possibly user mode register contexts.
The first patch fixes unwind_stack() itself not to unwind through an exception frame if it represents the user register context, preventing itself being called with that context next.
The second patch fixes show_backtrace() to fall back to raw backtrace for user mode register contexts, rather than using unwind_stack(). This is used on certain faults which can be triggered by userland (namely unaligned accesses when reporting is enabled in debugfs).
James Hogan (2):
MIPS: Don't unwind to user mode with EVA
MIPS: Avoid using unwind_stack() with usermode
arch/mips/kernel/process.c | 2 +- arch/mips/kernel/traps.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.4.10
PATCH 1/2 MIPS: Don't unwind to user mode with EVA
When unwinding through IRQs and exceptions, the unwinding only continues if the PC is a kernel text address, however since EVA it is possible for user and kernel address ranges to overlap, potentially allowing unwinding to continue to user mode if the user PC happens to be in the kernel text address range.
Adjust the check to also ensure that the register state from before the exception is actually running in kernel mode, i.e. !user_mode(regs).
I don't believe any harm can come of this problem, since the PC is only output, the stack pointer is checked to ensure it resides within the task's stack page before it is dereferenced in search of the return address, and the return address register is similarly only output (if the PC is in a leaf function or the beginning of a non-leaf function).
However unwind_stack() is only meant for unwinding kernel code, so to be correct the unwind should stop there.
--- arch/mips/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/kernel/process.c +1 -1
@@ -457,7 +457,7 @@ unsigned long notrace unwind_stack_by_address(unsigned long stack_page, *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) { regs = (struct pt_regs *)*sp; pc = regs->cp0_epc; -if (__kernel_text_address (pc)) { +if (!user_mode (regs) &&__kernel_text_address (pc)) { *sp = regs->regs[29 ]; *ra = regs->regs[31 ];return pc;
Signature · 1 line
2.4.10
Re: [PATCH 1/2] MIPS: Don't unwind to user mode with EVA
OK.
Re: [PATCH 1/2] MIPS: Don't unwind to user mode with EVA
Quoted 1 line · Leonid Yegoshin
OK.
Thanks Leonid. Can that be taken as a Reviewed-by?
Cheers
James
Re: [PATCH 1/2] MIPS: Don't unwind to user mode with EVA
Quoted 6 lines · James Hogan
Quoted 1 line · Leonid Yegoshin
OK.
Thanks Leonid. Can that be taken as a Reviewed-by?
Cheers
James
Yes, please.
- Leonid
PATCH 2/2 MIPS: Avoid using unwind_stack() with usermode
In reply to James HoganWhen showing backtraces in response to traps, for example crashes and address errors (usually unaligned accesses) when they are set in debugfs to be reported, unwind_stack will be used if the PC was in the kernel text address range. However since EVA it is possible for user and kernel address ranges to overlap, and even without EVA userland can still trigger an address error by jumping to a KSeg0 address.
Adjust the check to also ensure that it was running in kernel mode. I don't believe any harm can come of this problem, since unwind_stack() is sufficiently defensive, however it is only meant for unwinding kernel code, so to be correct it should use the raw backtracing instead.
--- arch/mips/kernel/traps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/kernel/traps.c +1 -1
@@ -144,7 +144,7 @@ static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)if (!task) task = current; -if (raw_show_trace || !__kernel_text_address (pc)) { +if (raw_show_trace ||user_mode (regs) || !__kernel_text_address (pc)) {show_raw_backtrace (sp);return ; }
Signature · 1 line
2.4.10
Re: [PATCH 2/2] MIPS: Avoid using unwind_stack() with usermode
OK.