Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include "linux/sched.h" |
| 7 | #include "linux/mm.h" |
| 8 | #include "linux/errno.h" |
| 9 | #include "linux/smp_lock.h" |
| 10 | #include "linux/security.h" |
| 11 | #include "linux/ptrace.h" |
| 12 | #include "linux/audit.h" |
| 13 | #ifdef CONFIG_PROC_MM |
| 14 | #include "linux/proc_mm.h" |
| 15 | #endif |
| 16 | #include "asm/ptrace.h" |
| 17 | #include "asm/uaccess.h" |
| 18 | #include "kern_util.h" |
| 19 | #include "skas_ptrace.h" |
| 20 | #include "sysdep/ptrace.h" |
Jeff Dike | 6e6d74c | 2007-02-10 01:44:30 -0800 | [diff] [blame] | 21 | #include "os.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 23 | static inline void set_singlestepping(struct task_struct *child, int on) |
| 24 | { |
| 25 | if (on) |
| 26 | child->ptrace |= PT_DTRACE; |
| 27 | else |
| 28 | child->ptrace &= ~PT_DTRACE; |
| 29 | child->thread.singlestep_syscall = 0; |
| 30 | |
| 31 | #ifdef SUBARCH_SET_SINGLESTEPPING |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 32 | SUBARCH_SET_SINGLESTEPPING(child, on); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 33 | #endif |
Jeff Dike | ba9950c | 2005-05-20 13:59:07 -0700 | [diff] [blame] | 34 | } |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Called by kernel/ptrace.c when detaching.. |
| 38 | */ |
| 39 | void ptrace_disable(struct task_struct *child) |
| 40 | { |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 41 | set_singlestepping(child,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 44 | extern int peek_user(struct task_struct * child, long addr, long data); |
| 45 | extern int poke_user(struct task_struct * child, long addr, long data); |
| 46 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 47 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int i, ret; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 50 | unsigned long __user *p = (void __user *)(unsigned long)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | switch (request) { |
| 53 | /* when I and D space are separate, these will need to be fixed. */ |
| 54 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 55 | case PTRACE_PEEKDATA: |
| 56 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* read the word at location addr in the USER area. */ |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 60 | case PTRACE_PEEKUSR: |
| 61 | ret = peek_user(child, addr, data); |
| 62 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* when I and D space are separate, this will have to be fixed. */ |
| 65 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 66 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame^] | 67 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | break; |
| 69 | |
| 70 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 71 | ret = poke_user(child, addr, data); |
| 72 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 75 | case PTRACE_CONT: { /* restart after signal. */ |
| 76 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 77 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 80 | set_singlestepping(child, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (request == PTRACE_SYSCALL) { |
| 82 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 83 | } |
| 84 | else { |
| 85 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 86 | } |
| 87 | child->exit_code = data; |
| 88 | wake_up_process(child); |
| 89 | ret = 0; |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * make the child exit. Best I can do is send it a sigkill. |
| 95 | * perhaps it should be put in the status that it wants to |
| 96 | * exit. |
| 97 | */ |
| 98 | case PTRACE_KILL: { |
| 99 | ret = 0; |
| 100 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 101 | break; |
| 102 | |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 103 | set_singlestepping(child, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | child->exit_code = SIGKILL; |
| 105 | wake_up_process(child); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | case PTRACE_SINGLESTEP: { /* set the trap flag. */ |
| 110 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 111 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | break; |
| 113 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Bodo Stroesser | 82c1c11 | 2005-05-06 21:30:46 -0700 | [diff] [blame] | 114 | set_singlestepping(child, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | child->exit_code = data; |
| 116 | /* give it a chance to run. */ |
| 117 | wake_up_process(child); |
| 118 | ret = 0; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | case PTRACE_DETACH: |
| 123 | /* detach a process that was attached. */ |
| 124 | ret = ptrace_detach(child, data); |
| 125 | break; |
| 126 | |
| 127 | #ifdef PTRACE_GETREGS |
| 128 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 129 | if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | ret = -EIO; |
| 131 | break; |
| 132 | } |
| 133 | for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 134 | __put_user(getreg(child, i), p); |
| 135 | p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | ret = 0; |
| 138 | break; |
| 139 | } |
| 140 | #endif |
| 141 | #ifdef PTRACE_SETREGS |
| 142 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ |
| 143 | unsigned long tmp = 0; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 144 | if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | ret = -EIO; |
| 146 | break; |
| 147 | } |
| 148 | for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 149 | __get_user(tmp, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | putreg(child, i, tmp); |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 151 | p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | ret = 0; |
| 154 | break; |
| 155 | } |
| 156 | #endif |
| 157 | #ifdef PTRACE_GETFPREGS |
| 158 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 159 | ret = get_fpregs(data, child); |
| 160 | break; |
| 161 | #endif |
| 162 | #ifdef PTRACE_SETFPREGS |
| 163 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 164 | ret = set_fpregs(data, child); |
| 165 | break; |
| 166 | #endif |
| 167 | #ifdef PTRACE_GETFPXREGS |
| 168 | case PTRACE_GETFPXREGS: /* Get the child FPU state. */ |
| 169 | ret = get_fpxregs(data, child); |
| 170 | break; |
| 171 | #endif |
| 172 | #ifdef PTRACE_SETFPXREGS |
| 173 | case PTRACE_SETFPXREGS: /* Set the child FPU state. */ |
| 174 | ret = set_fpxregs(data, child); |
| 175 | break; |
| 176 | #endif |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 177 | case PTRACE_GET_THREAD_AREA: |
| 178 | ret = ptrace_get_thread_area(child, addr, |
| 179 | (struct user_desc __user *) data); |
| 180 | break; |
| 181 | |
| 182 | case PTRACE_SET_THREAD_AREA: |
| 183 | ret = ptrace_set_thread_area(child, addr, |
| 184 | (struct user_desc __user *) data); |
| 185 | break; |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | case PTRACE_FAULTINFO: { |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 188 | /* Take the info from thread->arch->faultinfo, |
| 189 | * but transfer max. sizeof(struct ptrace_faultinfo). |
| 190 | * On i386, ptrace_faultinfo is smaller! |
| 191 | */ |
| 192 | ret = copy_to_user(p, &child->thread.arch.faultinfo, |
| 193 | sizeof(struct ptrace_faultinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if(ret) |
| 195 | break; |
| 196 | break; |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Bodo Stroesser | c578455 | 2005-05-05 16:15:31 -0700 | [diff] [blame] | 199 | #ifdef PTRACE_LDT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | case PTRACE_LDT: { |
| 201 | struct ptrace_ldt ldt; |
| 202 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 203 | if(copy_from_user(&ldt, p, sizeof(ldt))){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | ret = -EIO; |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | /* This one is confusing, so just punt and return -EIO for |
| 209 | * now |
| 210 | */ |
| 211 | ret = -EIO; |
| 212 | break; |
| 213 | } |
Bodo Stroesser | c578455 | 2005-05-05 16:15:31 -0700 | [diff] [blame] | 214 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | #ifdef CONFIG_PROC_MM |
| 216 | case PTRACE_SWITCH_MM: { |
| 217 | struct mm_struct *old = child->mm; |
| 218 | struct mm_struct *new = proc_mm_get_mm(data); |
| 219 | |
| 220 | if(IS_ERR(new)){ |
| 221 | ret = PTR_ERR(new); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | atomic_inc(&new->mm_users); |
| 226 | child->mm = new; |
| 227 | child->active_mm = new; |
| 228 | mmput(old); |
| 229 | ret = 0; |
| 230 | break; |
| 231 | } |
| 232 | #endif |
Jeff Dike | 6e6d74c | 2007-02-10 01:44:30 -0800 | [diff] [blame] | 233 | #ifdef PTRACE_ARCH_PRCTL |
| 234 | case PTRACE_ARCH_PRCTL: |
| 235 | /* XXX Calls ptrace on the host - needs some SMP thinking */ |
| 236 | ret = arch_prctl_skas(child, data, (void *) addr); |
| 237 | break; |
| 238 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | default: |
| 240 | ret = ptrace_request(child, request, addr, data); |
| 241 | break; |
| 242 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs, |
| 248 | int error_code) |
| 249 | { |
| 250 | struct siginfo info; |
| 251 | |
| 252 | memset(&info, 0, sizeof(info)); |
| 253 | info.si_signo = SIGTRAP; |
| 254 | info.si_code = TRAP_BRKPT; |
| 255 | |
| 256 | /* User-mode eip? */ |
| 257 | info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL; |
| 258 | |
| 259 | /* Send us the fakey SIGTRAP */ |
| 260 | force_sig_info(SIGTRAP, &info, tsk); |
| 261 | } |
| 262 | |
| 263 | /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and |
| 264 | * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check |
| 265 | */ |
| 266 | void syscall_trace(union uml_pt_regs *regs, int entryexit) |
| 267 | { |
| 268 | int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit; |
| 269 | int tracesysgood; |
| 270 | |
| 271 | if (unlikely(current->audit_context)) { |
| 272 | if (!entryexit) |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 273 | audit_syscall_entry(HOST_AUDIT_ARCH, |
Jeff Dike | 79d20b1 | 2005-05-03 07:54:51 +0100 | [diff] [blame] | 274 | UPT_SYSCALL_NR(regs), |
| 275 | UPT_SYSCALL_ARG1(regs), |
| 276 | UPT_SYSCALL_ARG2(regs), |
| 277 | UPT_SYSCALL_ARG3(regs), |
| 278 | UPT_SYSCALL_ARG4(regs)); |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 279 | else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)), |
Jeff Dike | b9e0d069 | 2005-05-28 15:51:53 -0700 | [diff] [blame] | 280 | UPT_SYSCALL_RET(regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* Fake a debug trap */ |
| 284 | if (is_singlestep) |
| 285 | send_sigtrap(current, regs, 0); |
| 286 | |
| 287 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 288 | return; |
| 289 | |
| 290 | if (!(current->ptrace & PT_PTRACED)) |
| 291 | return; |
| 292 | |
| 293 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 294 | between a syscall stop and SIGTRAP delivery */ |
| 295 | tracesysgood = (current->ptrace & PT_TRACESYSGOOD); |
| 296 | ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0)); |
| 297 | |
| 298 | if (entryexit) /* force do_signal() --> is_syscall() */ |
| 299 | set_thread_flag(TIF_SIGPENDING); |
| 300 | |
| 301 | /* this isn't the same as continuing with a signal, but it will do |
| 302 | * for normal use. strace only continues with a signal if the |
| 303 | * stopping signal is not SIGTRAP. -brl |
| 304 | */ |
| 305 | if (current->exit_code) { |
| 306 | send_sig(current->exit_code, current, 1); |
| 307 | current->exit_code = 0; |
| 308 | } |
| 309 | } |