^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/siginfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <frame_kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) EXPORT_SYMBOL(block_signals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) EXPORT_SYMBOL(unblock_signals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) void block_signals_trace(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) block_signals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (current_thread_info())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) trace_hardirqs_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void unblock_signals_trace(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (current_thread_info())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) trace_hardirqs_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unblock_signals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void um_trace_signals_on(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (current_thread_info())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) trace_hardirqs_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void um_trace_signals_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (current_thread_info())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) trace_hardirqs_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * OK, we're invoking a handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sigset_t *oldset = sigmask_to_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int singlestep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned long sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) singlestep = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Did we come from a system call? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (PT_REGS_SYSCALL_NR(regs) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* If so, check system call restarting.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) switch (PT_REGS_SYSCALL_RET(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) case -ERESTART_RESTARTBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) case -ERESTARTNOHAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) PT_REGS_SYSCALL_RET(regs) = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) PT_REGS_SYSCALL_RET(regs) = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case -ERESTARTNOINTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) PT_REGS_RESTART_SYSCALL(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) sp = PT_REGS_SP(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if ((ksig->ka.sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sp = current->sas_ss_sp + current->sas_ss_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!(ksig->ka.sa.sa_flags & SA_SIGINFO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = setup_signal_stack_sc(sp, ksig, regs, oldset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err = setup_signal_stack_si(sp, ksig, regs, oldset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) signal_setup_done(err, ksig, singlestep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void do_signal(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct ksignal ksig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int handled_sig = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) while (get_signal(&ksig)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) handled_sig = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Whee! Actually deliver the signal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) handle_signal(&ksig, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Did we come from a system call? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Restart the system call - no handlers present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) switch (PT_REGS_SYSCALL_RET(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case -ERESTARTNOHAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) case -ERESTARTNOINTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) PT_REGS_RESTART_SYSCALL(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) case -ERESTART_RESTARTBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) PT_REGS_RESTART_SYSCALL(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * This closes a way to execute a system call on the host. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * you set a breakpoint on a system call instruction and singlestep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * from it, the tracing thread used to PTRACE_SINGLESTEP the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * rather than PTRACE_SYSCALL it, allowing the system call to execute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * on the host. The tracing thread will check this flag and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * PTRACE_SYSCALL if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (current->ptrace & PT_DTRACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) current->thread.singlestep_syscall =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) is_syscall(PT_REGS_IP(¤t->thread.regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * if there's no signal to deliver, we just put the saved sigmask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!handled_sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) restore_saved_sigmask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }