MIPS: Avoid to cause watchpoint exception in kernel mode
PATCH 1/1 MIPS: Avoid to cause watchpoint exception in kernel mode
From: Heiher <[email protected]>
The following program cause an endless loop in kernel space:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int
main (int argc, char *argv[])
{
char buf[16];
printf ("%p\n", buf);
raise (SIGINT);
write (1, buf, 16);
return 0;
}
# gcc -O0 -o t t.c
# gdb ./t
(gdb) r
(gdb) watch *<printed buf address>
(gdb) cSigned-off-by Heiher <[email protected]>
--- arch/mips/kernel/entry.S | 23 +++++++++++++++++++++++ arch/mips/kernel/traps.c | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-)
arch/mips/kernel/entry.S +23 -0
@@ -49,6 +49,13 @@ resume_userspace: # interrupt setting need_resched # between sampling and return LONG_L a2, TI_FLAGS($28) # current->work + li t0, _TIF_LOAD_WATCH + and t0, a2 + beqz t0, 1f + move a0, $28 + jal mips_install_watch_registers + LONG_L a2, TI_FLAGS($28) # current->work +1: andi t0, a2, _TIF_WORK_MASK # (ignoring syscall_trace) bnez t0, work_pending j restore_all
@@ -82,7 +89,15 @@ FEXPORT(syscall_exit) local_irq_disable # make sure need_resched and # signals dont change between # sampling and return + + LONG_L a2, TI_FLAGS($28) # current->work + li t0, _TIF_LOAD_WATCH + and t0, a2 + beqz t0, 1f + move a0, $28 + jal mips_install_watch_registers LONG_L a2, TI_FLAGS($28) # current->work +1: li t0, _TIF_ALLWORK_MASK and t0, a2, t0 bnez t0, syscall_exit_work
@@ -143,7 +158,15 @@ work_notifysig: # deal with pending signals and FEXPORT(syscall_exit_partial) local_irq_disable # make sure need_resched doesn't # change between and return + + LONG_L a2, TI_FLAGS($28) # current->work + li t0, _TIF_LOAD_WATCH + and t0, a2 + beqz t0, 1f + move a0, $28 + jal mips_install_watch_registers LONG_L a2, TI_FLAGS($28) # current->work +1: li t0, _TIF_ALLWORK_MASK and t0, a2 beqz t0, restore_partial
arch/mips/kernel/traps.c +1 -1
@@ -1525,7 +1525,7 @@ asmlinkage void do_watch(struct pt_regs *regs)* their values and send SIGTRAP. Otherwise another thread * left the registers set, clear them and continue. */ -if (test_tsk_thread_flag (current,TIF_LOAD_WATCH )) { +if (user_mode (regs) &&test_tsk_thread_flag (current,TIF_LOAD_WATCH )) {mips_read_watch_registers ();local_irq_enable ();force_sig_info (SIGTRAP , &info, current);
Signature · 1 line
2.16.3
Quoted 18 lines · On Fri, Mar 30, 2018 at 05:17:21PM +0800, [email protected] wrote:
From: Heiher <[email protected]>
The following program cause an endless loop in kernel space:
#include <stdio.h> #include <unistd.h> #include <signal.h> int main (int argc, char *argv[]) { char buf[16]; printf ("%p\n", buf); raise (SIGINT); write (1, buf, 16);
Quoted 8 lines
return 0; } # gcc -O0 -o t t.c # gdb ./t (gdb) r (gdb) watch *<printed buf address> (gdb) c
Please add more explanation so that a future reader can see what the problem was and how you fixed it.
Thanks
James