Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Al Viro | 1bfa231 | 2012-05-23 00:18:33 -0400 | [diff] [blame] | 6 | #include <linux/audit.h> |
| 7 | #include <linux/ptrace.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/tracehook.h> |
| 10 | #include <asm/uaccess.h> |
Richard Weinberger | da028d5 | 2015-06-25 22:44:11 +0200 | [diff] [blame] | 11 | #include <asm/ptrace-abi.h> |
Christoph Hellwig | 1bd0950 | 2010-03-10 15:22:56 -0800 | [diff] [blame] | 12 | |
| 13 | void user_enable_single_step(struct task_struct *child) |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 14 | { |
Christoph Hellwig | 1bd0950 | 2010-03-10 15:22:56 -0800 | [diff] [blame] | 15 | child->ptrace |= PT_DTRACE; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 16 | child->thread.singlestep_syscall = 0; |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef SUBARCH_SET_SINGLESTEPPING |
Christoph Hellwig | 1bd0950 | 2010-03-10 15:22:56 -0800 | [diff] [blame] | 19 | SUBARCH_SET_SINGLESTEPPING(child, 1); |
| 20 | #endif |
| 21 | } |
| 22 | |
| 23 | void user_disable_single_step(struct task_struct *child) |
| 24 | { |
| 25 | child->ptrace &= ~PT_DTRACE; |
| 26 | child->thread.singlestep_syscall = 0; |
| 27 | |
| 28 | #ifdef SUBARCH_SET_SINGLESTEPPING |
| 29 | SUBARCH_SET_SINGLESTEPPING(child, 0); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 30 | #endif |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 31 | } |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Called by kernel/ptrace.c when detaching.. |
| 35 | */ |
| 36 | void ptrace_disable(struct task_struct *child) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 37 | { |
Christoph Hellwig | 1bd0950 | 2010-03-10 15:22:56 -0800 | [diff] [blame] | 38 | user_disable_single_step(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 41 | extern int peek_user(struct task_struct * child, long addr, long data); |
| 42 | extern int poke_user(struct task_struct * child, long addr, long data); |
| 43 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 44 | long arch_ptrace(struct task_struct *child, long request, |
| 45 | unsigned long addr, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | int i, ret; |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 48 | unsigned long __user *p = (void __user *)data; |
Namhyung Kim | 0a3d763 | 2010-10-27 15:34:04 -0700 | [diff] [blame] | 49 | void __user *vp = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | switch (request) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* read the word at location addr in the USER area. */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 53 | case PTRACE_PEEKUSR: |
| 54 | ret = peek_user(child, addr, data); |
| 55 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 57 | /* write the word at location addr in the USER area */ |
| 58 | case PTRACE_POKEUSR: |
| 59 | ret = poke_user(child, addr, data); |
| 60 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Renzo Davoli | 86d6f2b | 2009-03-12 14:31:23 -0700 | [diff] [blame] | 62 | case PTRACE_SYSEMU: |
| 63 | case PTRACE_SYSEMU_SINGLESTEP: |
| 64 | ret = -EIO; |
| 65 | break; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #ifdef PTRACE_GETREGS |
| 68 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 69 | if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | ret = -EIO; |
| 71 | break; |
| 72 | } |
| 73 | for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 74 | __put_user(getreg(child, i), p); |
| 75 | p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | ret = 0; |
| 78 | break; |
| 79 | } |
| 80 | #endif |
| 81 | #ifdef PTRACE_SETREGS |
| 82 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 83 | unsigned long tmp = 0; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 84 | if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | ret = -EIO; |
| 86 | break; |
| 87 | } |
| 88 | for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 89 | __get_user(tmp, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | putreg(child, i, tmp); |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 91 | p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | ret = 0; |
| 94 | break; |
| 95 | } |
| 96 | #endif |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 97 | case PTRACE_GET_THREAD_AREA: |
Namhyung Kim | 0a3d763 | 2010-10-27 15:34:04 -0700 | [diff] [blame] | 98 | ret = ptrace_get_thread_area(child, addr, vp); |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 99 | break; |
| 100 | |
| 101 | case PTRACE_SET_THREAD_AREA: |
Richard Weinberger | 8818b67 | 2010-11-11 14:05:04 -0800 | [diff] [blame] | 102 | ret = ptrace_set_thread_area(child, addr, vp); |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 103 | break; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | default: |
| 106 | ret = ptrace_request(child, request, addr, data); |
Jeff Dike | e8012b5 | 2007-10-16 01:27:16 -0700 | [diff] [blame] | 107 | if (ret == -EIO) |
| 108 | ret = subarch_ptrace(child, request, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return ret; |
| 113 | } |
| 114 | |
WANG Cong | 99764fa | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 115 | static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | int error_code) |
| 117 | { |
| 118 | struct siginfo info; |
| 119 | |
| 120 | memset(&info, 0, sizeof(info)); |
| 121 | info.si_signo = SIGTRAP; |
| 122 | info.si_code = TRAP_BRKPT; |
| 123 | |
| 124 | /* User-mode eip? */ |
| 125 | info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL; |
| 126 | |
Simon Arlott | b60745b | 2007-10-20 01:23:03 +0200 | [diff] [blame] | 127 | /* Send us the fake SIGTRAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | force_sig_info(SIGTRAP, &info, tsk); |
| 129 | } |
| 130 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 131 | /* |
| 132 | * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check |
| 134 | */ |
Richard Weinberger | 5334cda | 2015-05-31 22:59:03 +0200 | [diff] [blame] | 135 | int syscall_trace_enter(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Eric Paris | 9139740 | 2014-03-11 13:29:28 -0400 | [diff] [blame] | 137 | audit_syscall_entry(UPT_SYSCALL_NR(®s->regs), |
Al Viro | 1bfa231 | 2012-05-23 00:18:33 -0400 | [diff] [blame] | 138 | UPT_SYSCALL_ARG1(®s->regs), |
| 139 | UPT_SYSCALL_ARG2(®s->regs), |
| 140 | UPT_SYSCALL_ARG3(®s->regs), |
| 141 | UPT_SYSCALL_ARG4(®s->regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
Richard Weinberger | 5334cda | 2015-05-31 22:59:03 +0200 | [diff] [blame] | 144 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Richard Weinberger | 5334cda | 2015-05-31 22:59:03 +0200 | [diff] [blame] | 146 | return tracehook_report_syscall_entry(regs); |
Al Viro | 1bfa231 | 2012-05-23 00:18:33 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void syscall_trace_leave(struct pt_regs *regs) |
| 150 | { |
| 151 | int ptraced = current->ptrace; |
| 152 | |
| 153 | audit_syscall_exit(regs); |
| 154 | |
| 155 | /* Fake a debug trap */ |
| 156 | if (ptraced & PT_DTRACE) |
| 157 | send_sigtrap(current, ®s->regs, 0); |
| 158 | |
| 159 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return; |
| 161 | |
Al Viro | 1bfa231 | 2012-05-23 00:18:33 -0400 | [diff] [blame] | 162 | tracehook_report_syscall_exit(regs, 0); |
| 163 | /* force do_signal() --> is_syscall() */ |
| 164 | if (ptraced & PT_PTRACED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | set_thread_flag(TIF_SIGPENDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |