Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/ptrace.c |
| 3 | * |
| 4 | * (C) Copyright 1999 Linus Torvalds |
| 5 | * |
| 6 | * Common interfaces for "ptrace()" which we do not want |
| 7 | * to continually duplicate across every architecture. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/capability.h> |
| 11 | #include <linux/export.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/highmem.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/ptrace.h> |
| 18 | #include <linux/security.h> |
| 19 | #include <linux/signal.h> |
| 20 | #include <linux/uio.h> |
| 21 | #include <linux/audit.h> |
| 22 | #include <linux/pid_namespace.h> |
| 23 | #include <linux/syscalls.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/regset.h> |
| 26 | #include <linux/hw_breakpoint.h> |
| 27 | #include <linux/cn_proc.h> |
| 28 | #include <linux/compat.h> |
| 29 | |
| 30 | |
| 31 | void __ptrace_link(struct task_struct *child, struct task_struct *new_parent, |
| 32 | const struct cred *ptracer_cred) |
| 33 | { |
| 34 | BUG_ON(!list_empty(&child->ptrace_entry)); |
| 35 | list_add(&child->ptrace_entry, &new_parent->ptraced); |
| 36 | child->parent = new_parent; |
| 37 | child->ptracer_cred = get_cred(ptracer_cred); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * ptrace a task: make the debugger its new parent and |
| 42 | * move it to the ptrace list. |
| 43 | * |
| 44 | * Must be called with the tasklist lock write-held. |
| 45 | */ |
| 46 | static void ptrace_link(struct task_struct *child, struct task_struct *new_parent) |
| 47 | { |
| 48 | rcu_read_lock(); |
| 49 | __ptrace_link(child, new_parent, __task_cred(new_parent)); |
| 50 | rcu_read_unlock(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * __ptrace_unlink - unlink ptracee and restore its execution state |
| 55 | * @child: ptracee to be unlinked |
| 56 | * |
| 57 | * Remove @child from the ptrace list, move it back to the original parent, |
| 58 | * and restore the execution state so that it conforms to the group stop |
| 59 | * state. |
| 60 | * |
| 61 | * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer |
| 62 | * exiting. For PTRACE_DETACH, unless the ptracee has been killed between |
| 63 | * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. |
| 64 | * If the ptracer is exiting, the ptracee can be in any state. |
| 65 | * |
| 66 | * After detach, the ptracee should be in a state which conforms to the |
| 67 | * group stop. If the group is stopped or in the process of stopping, the |
| 68 | * ptracee should be put into TASK_STOPPED; otherwise, it should be woken |
| 69 | * up from TASK_TRACED. |
| 70 | * |
| 71 | * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, |
| 72 | * it goes through TRACED -> RUNNING -> STOPPED transition which is similar |
| 73 | * to but in the opposite direction of what happens while attaching to a |
| 74 | * stopped task. However, in this direction, the intermediate RUNNING |
| 75 | * state is not hidden even from the current ptracer and if it immediately |
| 76 | * re-attaches and performs a WNOHANG wait(2), it may fail. |
| 77 | * |
| 78 | * CONTEXT: |
| 79 | * write_lock_irq(tasklist_lock) |
| 80 | */ |
| 81 | void __ptrace_unlink(struct task_struct *child) |
| 82 | { |
| 83 | const struct cred *old_cred; |
| 84 | BUG_ON(!child->ptrace); |
| 85 | |
| 86 | child->ptrace = 0; |
| 87 | child->parent = child->real_parent; |
| 88 | list_del_init(&child->ptrace_entry); |
| 89 | old_cred = child->ptracer_cred; |
| 90 | child->ptracer_cred = NULL; |
| 91 | put_cred(old_cred); |
| 92 | |
| 93 | spin_lock(&child->sighand->siglock); |
| 94 | |
| 95 | /* |
| 96 | * Clear all pending traps and TRAPPING. TRAPPING should be |
| 97 | * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. |
| 98 | */ |
| 99 | task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); |
| 100 | task_clear_jobctl_trapping(child); |
| 101 | |
| 102 | /* |
| 103 | * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and |
| 104 | * @child isn't dead. |
| 105 | */ |
| 106 | if (!(child->flags & PF_EXITING) && |
| 107 | (child->signal->flags & SIGNAL_STOP_STOPPED || |
| 108 | child->signal->group_stop_count)) { |
| 109 | child->jobctl |= JOBCTL_STOP_PENDING; |
| 110 | |
| 111 | /* |
| 112 | * This is only possible if this thread was cloned by the |
| 113 | * traced task running in the stopped group, set the signal |
| 114 | * for the future reports. |
| 115 | * FIXME: we should change ptrace_init_task() to handle this |
| 116 | * case. |
| 117 | */ |
| 118 | if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) |
| 119 | child->jobctl |= SIGSTOP; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick |
| 124 | * @child in the butt. Note that @resume should be used iff @child |
| 125 | * is in TASK_TRACED; otherwise, we might unduly disrupt |
| 126 | * TASK_KILLABLE sleeps. |
| 127 | */ |
| 128 | if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) |
| 129 | ptrace_signal_wake_up(child, true); |
| 130 | |
| 131 | spin_unlock(&child->sighand->siglock); |
| 132 | } |
| 133 | |
| 134 | /* Ensure that nothing can wake it up, even SIGKILL */ |
| 135 | static bool ptrace_freeze_traced(struct task_struct *task) |
| 136 | { |
| 137 | bool ret = false; |
| 138 | |
| 139 | /* Lockless, nobody but us can set this flag */ |
| 140 | if (task->jobctl & JOBCTL_LISTENING) |
| 141 | return ret; |
| 142 | |
| 143 | spin_lock_irq(&task->sighand->siglock); |
| 144 | if (task_is_traced(task) && !__fatal_signal_pending(task)) { |
| 145 | task->state = __TASK_TRACED; |
| 146 | ret = true; |
| 147 | } |
| 148 | spin_unlock_irq(&task->sighand->siglock); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static void ptrace_unfreeze_traced(struct task_struct *task) |
| 154 | { |
| 155 | if (task->state != __TASK_TRACED) |
| 156 | return; |
| 157 | |
| 158 | WARN_ON(!task->ptrace || task->parent != current); |
| 159 | |
| 160 | /* |
| 161 | * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely. |
| 162 | * Recheck state under the lock to close this race. |
| 163 | */ |
| 164 | spin_lock_irq(&task->sighand->siglock); |
| 165 | if (task->state == __TASK_TRACED) { |
| 166 | if (__fatal_signal_pending(task)) |
| 167 | wake_up_state(task, __TASK_TRACED); |
| 168 | else |
| 169 | task->state = TASK_TRACED; |
| 170 | } |
| 171 | spin_unlock_irq(&task->sighand->siglock); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * ptrace_check_attach - check whether ptracee is ready for ptrace operation |
| 176 | * @child: ptracee to check for |
| 177 | * @ignore_state: don't check whether @child is currently %TASK_TRACED |
| 178 | * |
| 179 | * Check whether @child is being ptraced by %current and ready for further |
| 180 | * ptrace operations. If @ignore_state is %false, @child also should be in |
| 181 | * %TASK_TRACED state and on return the child is guaranteed to be traced |
| 182 | * and not executing. If @ignore_state is %true, @child can be in any |
| 183 | * state. |
| 184 | * |
| 185 | * CONTEXT: |
| 186 | * Grabs and releases tasklist_lock and @child->sighand->siglock. |
| 187 | * |
| 188 | * RETURNS: |
| 189 | * 0 on success, -ESRCH if %child is not ready. |
| 190 | */ |
| 191 | static int ptrace_check_attach(struct task_struct *child, bool ignore_state) |
| 192 | { |
| 193 | int ret = -ESRCH; |
| 194 | |
| 195 | /* |
| 196 | * We take the read lock around doing both checks to close a |
| 197 | * possible race where someone else was tracing our child and |
| 198 | * detached between these two checks. After this locked check, |
| 199 | * we are sure that this is our traced child and that can only |
| 200 | * be changed by us so it's not changing right after this. |
| 201 | */ |
| 202 | read_lock(&tasklist_lock); |
| 203 | if (child->ptrace && child->parent == current) { |
| 204 | WARN_ON(child->state == __TASK_TRACED); |
| 205 | /* |
| 206 | * child->sighand can't be NULL, release_task() |
| 207 | * does ptrace_unlink() before __exit_signal(). |
| 208 | */ |
| 209 | if (ignore_state || ptrace_freeze_traced(child)) |
| 210 | ret = 0; |
| 211 | } |
| 212 | read_unlock(&tasklist_lock); |
| 213 | |
| 214 | if (!ret && !ignore_state) { |
| 215 | if (!wait_task_inactive(child, __TASK_TRACED)) { |
| 216 | /* |
| 217 | * This can only happen if may_ptrace_stop() fails and |
| 218 | * ptrace_stop() changes ->state back to TASK_RUNNING, |
| 219 | * so we should not worry about leaking __TASK_TRACED. |
| 220 | */ |
| 221 | WARN_ON(child->state == __TASK_TRACED); |
| 222 | ret = -ESRCH; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode) |
| 230 | { |
| 231 | if (mode & PTRACE_MODE_NOAUDIT) |
| 232 | return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE); |
| 233 | else |
| 234 | return has_ns_capability(current, ns, CAP_SYS_PTRACE); |
| 235 | } |
| 236 | |
| 237 | /* Returns 0 on success, -errno on denial. */ |
| 238 | static int __ptrace_may_access(struct task_struct *task, unsigned int mode) |
| 239 | { |
| 240 | const struct cred *cred = current_cred(), *tcred; |
| 241 | struct mm_struct *mm; |
| 242 | kuid_t caller_uid; |
| 243 | kgid_t caller_gid; |
| 244 | |
| 245 | if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { |
| 246 | WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); |
| 247 | return -EPERM; |
| 248 | } |
| 249 | |
| 250 | /* May we inspect the given task? |
| 251 | * This check is used both for attaching with ptrace |
| 252 | * and for allowing access to sensitive information in /proc. |
| 253 | * |
| 254 | * ptrace_attach denies several cases that /proc allows |
| 255 | * because setting up the necessary parent/child relationship |
| 256 | * or halting the specified task is impossible. |
| 257 | */ |
| 258 | |
| 259 | /* Don't let security modules deny introspection */ |
| 260 | if (same_thread_group(task, current)) |
| 261 | return 0; |
| 262 | rcu_read_lock(); |
| 263 | if (mode & PTRACE_MODE_FSCREDS) { |
| 264 | caller_uid = cred->fsuid; |
| 265 | caller_gid = cred->fsgid; |
| 266 | } else { |
| 267 | /* |
| 268 | * Using the euid would make more sense here, but something |
| 269 | * in userland might rely on the old behavior, and this |
| 270 | * shouldn't be a security problem since |
| 271 | * PTRACE_MODE_REALCREDS implies that the caller explicitly |
| 272 | * used a syscall that requests access to another process |
| 273 | * (and not a filesystem syscall to procfs). |
| 274 | */ |
| 275 | caller_uid = cred->uid; |
| 276 | caller_gid = cred->gid; |
| 277 | } |
| 278 | tcred = __task_cred(task); |
| 279 | if (uid_eq(caller_uid, tcred->euid) && |
| 280 | uid_eq(caller_uid, tcred->suid) && |
| 281 | uid_eq(caller_uid, tcred->uid) && |
| 282 | gid_eq(caller_gid, tcred->egid) && |
| 283 | gid_eq(caller_gid, tcred->sgid) && |
| 284 | gid_eq(caller_gid, tcred->gid)) |
| 285 | goto ok; |
| 286 | if (ptrace_has_cap(tcred->user_ns, mode)) |
| 287 | goto ok; |
| 288 | rcu_read_unlock(); |
| 289 | return -EPERM; |
| 290 | ok: |
| 291 | rcu_read_unlock(); |
| 292 | mm = task->mm; |
| 293 | if (mm && |
| 294 | ((get_dumpable(mm) != SUID_DUMP_USER) && |
| 295 | !ptrace_has_cap(mm->user_ns, mode))) |
| 296 | return -EPERM; |
| 297 | |
| 298 | return security_ptrace_access_check(task, mode); |
| 299 | } |
| 300 | |
| 301 | bool ptrace_may_access(struct task_struct *task, unsigned int mode) |
| 302 | { |
| 303 | int err; |
| 304 | task_lock(task); |
| 305 | err = __ptrace_may_access(task, mode); |
| 306 | task_unlock(task); |
| 307 | return !err; |
| 308 | } |
| 309 | |
| 310 | static int ptrace_attach(struct task_struct *task, long request, |
| 311 | unsigned long addr, |
| 312 | unsigned long flags) |
| 313 | { |
| 314 | bool seize = (request == PTRACE_SEIZE); |
| 315 | int retval; |
| 316 | |
| 317 | retval = -EIO; |
| 318 | if (seize) { |
| 319 | if (addr != 0) |
| 320 | goto out; |
| 321 | if (flags & ~(unsigned long)PTRACE_O_MASK) |
| 322 | goto out; |
| 323 | flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); |
| 324 | } else { |
| 325 | flags = PT_PTRACED; |
| 326 | } |
| 327 | |
| 328 | audit_ptrace(task); |
| 329 | |
| 330 | retval = -EPERM; |
| 331 | if (unlikely(task->flags & PF_KTHREAD)) |
| 332 | goto out; |
| 333 | if (same_thread_group(task, current)) |
| 334 | goto out; |
| 335 | |
| 336 | /* |
| 337 | * Protect exec's credential calculations against our interference; |
| 338 | * SUID, SGID and LSM creds get determined differently |
| 339 | * under ptrace. |
| 340 | */ |
| 341 | retval = -ERESTARTNOINTR; |
| 342 | if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) |
| 343 | goto out; |
| 344 | |
| 345 | task_lock(task); |
| 346 | retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); |
| 347 | task_unlock(task); |
| 348 | if (retval) |
| 349 | goto unlock_creds; |
| 350 | |
| 351 | write_lock_irq(&tasklist_lock); |
| 352 | retval = -EPERM; |
| 353 | if (unlikely(task->exit_state)) |
| 354 | goto unlock_tasklist; |
| 355 | if (task->ptrace) |
| 356 | goto unlock_tasklist; |
| 357 | |
| 358 | if (seize) |
| 359 | flags |= PT_SEIZED; |
| 360 | task->ptrace = flags; |
| 361 | |
| 362 | ptrace_link(task, current); |
| 363 | |
| 364 | /* SEIZE doesn't trap tracee on attach */ |
| 365 | if (!seize) |
| 366 | send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); |
| 367 | |
| 368 | spin_lock(&task->sighand->siglock); |
| 369 | |
| 370 | /* |
| 371 | * If the task is already STOPPED, set JOBCTL_TRAP_STOP and |
| 372 | * TRAPPING, and kick it so that it transits to TRACED. TRAPPING |
| 373 | * will be cleared if the child completes the transition or any |
| 374 | * event which clears the group stop states happens. We'll wait |
| 375 | * for the transition to complete before returning from this |
| 376 | * function. |
| 377 | * |
| 378 | * This hides STOPPED -> RUNNING -> TRACED transition from the |
| 379 | * attaching thread but a different thread in the same group can |
| 380 | * still observe the transient RUNNING state. IOW, if another |
| 381 | * thread's WNOHANG wait(2) on the stopped tracee races against |
| 382 | * ATTACH, the wait(2) may fail due to the transient RUNNING. |
| 383 | * |
| 384 | * The following task_is_stopped() test is safe as both transitions |
| 385 | * in and out of STOPPED are protected by siglock. |
| 386 | */ |
| 387 | if (task_is_stopped(task) && |
| 388 | task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) |
| 389 | signal_wake_up_state(task, __TASK_STOPPED); |
| 390 | |
| 391 | spin_unlock(&task->sighand->siglock); |
| 392 | |
| 393 | retval = 0; |
| 394 | unlock_tasklist: |
| 395 | write_unlock_irq(&tasklist_lock); |
| 396 | unlock_creds: |
| 397 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 398 | out: |
| 399 | if (!retval) { |
| 400 | wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, |
| 401 | TASK_UNINTERRUPTIBLE); |
| 402 | proc_ptrace_connector(task, PTRACE_ATTACH); |
| 403 | } |
| 404 | |
| 405 | return retval; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * ptrace_traceme -- helper for PTRACE_TRACEME |
| 410 | * |
| 411 | * Performs checks and sets PT_PTRACED. |
| 412 | * Should be used by all ptrace implementations for PTRACE_TRACEME. |
| 413 | */ |
| 414 | static int ptrace_traceme(void) |
| 415 | { |
| 416 | int ret = -EPERM; |
| 417 | |
| 418 | write_lock_irq(&tasklist_lock); |
| 419 | /* Are we already being traced? */ |
| 420 | if (!current->ptrace) { |
| 421 | ret = security_ptrace_traceme(current->parent); |
| 422 | /* |
| 423 | * Check PF_EXITING to ensure ->real_parent has not passed |
| 424 | * exit_ptrace(). Otherwise we don't report the error but |
| 425 | * pretend ->real_parent untraces us right after return. |
| 426 | */ |
| 427 | if (!ret && !(current->real_parent->flags & PF_EXITING)) { |
| 428 | current->ptrace = PT_PTRACED; |
| 429 | ptrace_link(current, current->real_parent); |
| 430 | } |
| 431 | } |
| 432 | write_unlock_irq(&tasklist_lock); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Called with irqs disabled, returns true if childs should reap themselves. |
| 439 | */ |
| 440 | static int ignoring_children(struct sighand_struct *sigh) |
| 441 | { |
| 442 | int ret; |
| 443 | spin_lock(&sigh->siglock); |
| 444 | ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || |
| 445 | (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); |
| 446 | spin_unlock(&sigh->siglock); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Called with tasklist_lock held for writing. |
| 452 | * Unlink a traced task, and clean it up if it was a traced zombie. |
| 453 | * Return true if it needs to be reaped with release_task(). |
| 454 | * (We can't call release_task() here because we already hold tasklist_lock.) |
| 455 | * |
| 456 | * If it's a zombie, our attachedness prevented normal parent notification |
| 457 | * or self-reaping. Do notification now if it would have happened earlier. |
| 458 | * If it should reap itself, return true. |
| 459 | * |
| 460 | * If it's our own child, there is no notification to do. But if our normal |
| 461 | * children self-reap, then this child was prevented by ptrace and we must |
| 462 | * reap it now, in that case we must also wake up sub-threads sleeping in |
| 463 | * do_wait(). |
| 464 | */ |
| 465 | static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) |
| 466 | { |
| 467 | bool dead; |
| 468 | |
| 469 | __ptrace_unlink(p); |
| 470 | |
| 471 | if (p->exit_state != EXIT_ZOMBIE) |
| 472 | return false; |
| 473 | |
| 474 | dead = !thread_group_leader(p); |
| 475 | |
| 476 | if (!dead && thread_group_empty(p)) { |
| 477 | if (!same_thread_group(p->real_parent, tracer)) |
| 478 | dead = do_notify_parent(p, p->exit_signal); |
| 479 | else if (ignoring_children(tracer->sighand)) { |
| 480 | __wake_up_parent(p, tracer); |
| 481 | dead = true; |
| 482 | } |
| 483 | } |
| 484 | /* Mark it as in the process of being reaped. */ |
| 485 | if (dead) |
| 486 | p->exit_state = EXIT_DEAD; |
| 487 | return dead; |
| 488 | } |
| 489 | |
| 490 | static int ptrace_detach(struct task_struct *child, unsigned int data) |
| 491 | { |
| 492 | if (!valid_signal(data)) |
| 493 | return -EIO; |
| 494 | |
| 495 | /* Architecture-specific hardware disable .. */ |
| 496 | ptrace_disable(child); |
| 497 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 498 | |
| 499 | write_lock_irq(&tasklist_lock); |
| 500 | /* |
| 501 | * We rely on ptrace_freeze_traced(). It can't be killed and |
| 502 | * untraced by another thread, it can't be a zombie. |
| 503 | */ |
| 504 | WARN_ON(!child->ptrace || child->exit_state); |
| 505 | /* |
| 506 | * tasklist_lock avoids the race with wait_task_stopped(), see |
| 507 | * the comment in ptrace_resume(). |
| 508 | */ |
| 509 | child->exit_code = data; |
| 510 | __ptrace_detach(current, child); |
| 511 | write_unlock_irq(&tasklist_lock); |
| 512 | |
| 513 | proc_ptrace_connector(child, PTRACE_DETACH); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Detach all tasks we were using ptrace on. Called with tasklist held |
| 520 | * for writing. |
| 521 | */ |
| 522 | void exit_ptrace(struct task_struct *tracer, struct list_head *dead) |
| 523 | { |
| 524 | struct task_struct *p, *n; |
| 525 | |
| 526 | list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { |
| 527 | if (unlikely(p->ptrace & PT_EXITKILL)) |
| 528 | send_sig_info(SIGKILL, SEND_SIG_FORCED, p); |
| 529 | |
| 530 | if (__ptrace_detach(tracer, p)) |
| 531 | list_add(&p->ptrace_entry, dead); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) |
| 536 | { |
| 537 | int copied = 0; |
| 538 | |
| 539 | while (len > 0) { |
| 540 | char buf[128]; |
| 541 | int this_len, retval; |
| 542 | |
| 543 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 544 | retval = access_process_vm(tsk, src, buf, this_len, 0); |
| 545 | if (!retval) { |
| 546 | if (copied) |
| 547 | break; |
| 548 | return -EIO; |
| 549 | } |
| 550 | if (copy_to_user(dst, buf, retval)) |
| 551 | return -EFAULT; |
| 552 | copied += retval; |
| 553 | src += retval; |
| 554 | dst += retval; |
| 555 | len -= retval; |
| 556 | } |
| 557 | return copied; |
| 558 | } |
| 559 | |
| 560 | int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) |
| 561 | { |
| 562 | int copied = 0; |
| 563 | |
| 564 | while (len > 0) { |
| 565 | char buf[128]; |
| 566 | int this_len, retval; |
| 567 | |
| 568 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 569 | if (copy_from_user(buf, src, this_len)) |
| 570 | return -EFAULT; |
| 571 | retval = access_process_vm(tsk, dst, buf, this_len, 1); |
| 572 | if (!retval) { |
| 573 | if (copied) |
| 574 | break; |
| 575 | return -EIO; |
| 576 | } |
| 577 | copied += retval; |
| 578 | src += retval; |
| 579 | dst += retval; |
| 580 | len -= retval; |
| 581 | } |
| 582 | return copied; |
| 583 | } |
| 584 | |
| 585 | static int ptrace_setoptions(struct task_struct *child, unsigned long data) |
| 586 | { |
| 587 | unsigned flags; |
| 588 | |
| 589 | if (data & ~(unsigned long)PTRACE_O_MASK) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { |
| 593 | if (!config_enabled(CONFIG_CHECKPOINT_RESTORE) || |
| 594 | !config_enabled(CONFIG_SECCOMP)) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | if (!capable(CAP_SYS_ADMIN)) |
| 598 | return -EPERM; |
| 599 | |
| 600 | if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || |
| 601 | current->ptrace & PT_SUSPEND_SECCOMP) |
| 602 | return -EPERM; |
| 603 | } |
| 604 | |
| 605 | /* Avoid intermediate state when all opts are cleared */ |
| 606 | flags = child->ptrace; |
| 607 | flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); |
| 608 | flags |= (data << PT_OPT_FLAG_SHIFT); |
| 609 | child->ptrace = flags; |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) |
| 615 | { |
| 616 | unsigned long flags; |
| 617 | int error = -ESRCH; |
| 618 | |
| 619 | if (lock_task_sighand(child, &flags)) { |
| 620 | error = -EINVAL; |
| 621 | if (likely(child->last_siginfo != NULL)) { |
| 622 | *info = *child->last_siginfo; |
| 623 | error = 0; |
| 624 | } |
| 625 | unlock_task_sighand(child, &flags); |
| 626 | } |
| 627 | return error; |
| 628 | } |
| 629 | |
| 630 | static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) |
| 631 | { |
| 632 | unsigned long flags; |
| 633 | int error = -ESRCH; |
| 634 | |
| 635 | if (lock_task_sighand(child, &flags)) { |
| 636 | error = -EINVAL; |
| 637 | if (likely(child->last_siginfo != NULL)) { |
| 638 | *child->last_siginfo = *info; |
| 639 | error = 0; |
| 640 | } |
| 641 | unlock_task_sighand(child, &flags); |
| 642 | } |
| 643 | return error; |
| 644 | } |
| 645 | |
| 646 | static int ptrace_peek_siginfo(struct task_struct *child, |
| 647 | unsigned long addr, |
| 648 | unsigned long data) |
| 649 | { |
| 650 | struct ptrace_peeksiginfo_args arg; |
| 651 | struct sigpending *pending; |
| 652 | struct sigqueue *q; |
| 653 | int ret, i; |
| 654 | |
| 655 | ret = copy_from_user(&arg, (void __user *) addr, |
| 656 | sizeof(struct ptrace_peeksiginfo_args)); |
| 657 | if (ret) |
| 658 | return -EFAULT; |
| 659 | |
| 660 | if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) |
| 661 | return -EINVAL; /* unknown flags */ |
| 662 | |
| 663 | if (arg.nr < 0) |
| 664 | return -EINVAL; |
| 665 | |
| 666 | if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) |
| 667 | pending = &child->signal->shared_pending; |
| 668 | else |
| 669 | pending = &child->pending; |
| 670 | |
| 671 | for (i = 0; i < arg.nr; ) { |
| 672 | siginfo_t info; |
| 673 | s32 off = arg.off + i; |
| 674 | |
| 675 | spin_lock_irq(&child->sighand->siglock); |
| 676 | list_for_each_entry(q, &pending->list, list) { |
| 677 | if (!off--) { |
| 678 | copy_siginfo(&info, &q->info); |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | spin_unlock_irq(&child->sighand->siglock); |
| 683 | |
| 684 | if (off >= 0) /* beyond the end of the list */ |
| 685 | break; |
| 686 | |
| 687 | #ifdef CONFIG_COMPAT |
| 688 | if (unlikely(is_compat_task())) { |
| 689 | compat_siginfo_t __user *uinfo = compat_ptr(data); |
| 690 | |
| 691 | if (copy_siginfo_to_user32(uinfo, &info) || |
| 692 | __put_user(info.si_code, &uinfo->si_code)) { |
| 693 | ret = -EFAULT; |
| 694 | break; |
| 695 | } |
| 696 | |
| 697 | } else |
| 698 | #endif |
| 699 | { |
| 700 | siginfo_t __user *uinfo = (siginfo_t __user *) data; |
| 701 | |
| 702 | if (copy_siginfo_to_user(uinfo, &info) || |
| 703 | __put_user(info.si_code, &uinfo->si_code)) { |
| 704 | ret = -EFAULT; |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | data += sizeof(siginfo_t); |
| 710 | i++; |
| 711 | |
| 712 | if (signal_pending(current)) |
| 713 | break; |
| 714 | |
| 715 | cond_resched(); |
| 716 | } |
| 717 | |
| 718 | if (i > 0) |
| 719 | return i; |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | #ifdef PTRACE_SINGLESTEP |
| 725 | #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) |
| 726 | #else |
| 727 | #define is_singlestep(request) 0 |
| 728 | #endif |
| 729 | |
| 730 | #ifdef PTRACE_SINGLEBLOCK |
| 731 | #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) |
| 732 | #else |
| 733 | #define is_singleblock(request) 0 |
| 734 | #endif |
| 735 | |
| 736 | #ifdef PTRACE_SYSEMU |
| 737 | #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) |
| 738 | #else |
| 739 | #define is_sysemu_singlestep(request) 0 |
| 740 | #endif |
| 741 | |
| 742 | static int ptrace_resume(struct task_struct *child, long request, |
| 743 | unsigned long data) |
| 744 | { |
| 745 | bool need_siglock; |
| 746 | |
| 747 | if (!valid_signal(data)) |
| 748 | return -EIO; |
| 749 | |
| 750 | if (request == PTRACE_SYSCALL) |
| 751 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 752 | else |
| 753 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 754 | |
| 755 | #ifdef TIF_SYSCALL_EMU |
| 756 | if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) |
| 757 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 758 | else |
| 759 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 760 | #endif |
| 761 | |
| 762 | if (is_singleblock(request)) { |
| 763 | if (unlikely(!arch_has_block_step())) |
| 764 | return -EIO; |
| 765 | user_enable_block_step(child); |
| 766 | } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { |
| 767 | if (unlikely(!arch_has_single_step())) |
| 768 | return -EIO; |
| 769 | user_enable_single_step(child); |
| 770 | } else { |
| 771 | user_disable_single_step(child); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Change ->exit_code and ->state under siglock to avoid the race |
| 776 | * with wait_task_stopped() in between; a non-zero ->exit_code will |
| 777 | * wrongly look like another report from tracee. |
| 778 | * |
| 779 | * Note that we need siglock even if ->exit_code == data and/or this |
| 780 | * status was not reported yet, the new status must not be cleared by |
| 781 | * wait_task_stopped() after resume. |
| 782 | * |
| 783 | * If data == 0 we do not care if wait_task_stopped() reports the old |
| 784 | * status and clears the code too; this can't race with the tracee, it |
| 785 | * takes siglock after resume. |
| 786 | */ |
| 787 | need_siglock = data && !thread_group_empty(current); |
| 788 | if (need_siglock) |
| 789 | spin_lock_irq(&child->sighand->siglock); |
| 790 | child->exit_code = data; |
| 791 | wake_up_state(child, __TASK_TRACED); |
| 792 | if (need_siglock) |
| 793 | spin_unlock_irq(&child->sighand->siglock); |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 799 | |
| 800 | static const struct user_regset * |
| 801 | find_regset(const struct user_regset_view *view, unsigned int type) |
| 802 | { |
| 803 | const struct user_regset *regset; |
| 804 | int n; |
| 805 | |
| 806 | for (n = 0; n < view->n; ++n) { |
| 807 | regset = view->regsets + n; |
| 808 | if (regset->core_note_type == type) |
| 809 | return regset; |
| 810 | } |
| 811 | |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | static int ptrace_regset(struct task_struct *task, int req, unsigned int type, |
| 816 | struct iovec *kiov) |
| 817 | { |
| 818 | const struct user_regset_view *view = task_user_regset_view(task); |
| 819 | const struct user_regset *regset = find_regset(view, type); |
| 820 | int regset_no; |
| 821 | |
| 822 | if (!regset || (kiov->iov_len % regset->size) != 0) |
| 823 | return -EINVAL; |
| 824 | |
| 825 | regset_no = regset - view->regsets; |
| 826 | kiov->iov_len = min(kiov->iov_len, |
| 827 | (__kernel_size_t) (regset->n * regset->size)); |
| 828 | |
| 829 | if (req == PTRACE_GETREGSET) |
| 830 | return copy_regset_to_user(task, view, regset_no, 0, |
| 831 | kiov->iov_len, kiov->iov_base); |
| 832 | else |
| 833 | return copy_regset_from_user(task, view, regset_no, 0, |
| 834 | kiov->iov_len, kiov->iov_base); |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * This is declared in linux/regset.h and defined in machine-dependent |
| 839 | * code. We put the export here, near the primary machine-neutral use, |
| 840 | * to ensure no machine forgets it. |
| 841 | */ |
| 842 | EXPORT_SYMBOL_GPL(task_user_regset_view); |
| 843 | #endif |
| 844 | |
| 845 | int ptrace_request(struct task_struct *child, long request, |
| 846 | unsigned long addr, unsigned long data) |
| 847 | { |
| 848 | bool seized = child->ptrace & PT_SEIZED; |
| 849 | int ret = -EIO; |
| 850 | siginfo_t siginfo, *si; |
| 851 | void __user *datavp = (void __user *) data; |
| 852 | unsigned long __user *datalp = datavp; |
| 853 | unsigned long flags; |
| 854 | |
| 855 | switch (request) { |
| 856 | case PTRACE_PEEKTEXT: |
| 857 | case PTRACE_PEEKDATA: |
| 858 | return generic_ptrace_peekdata(child, addr, data); |
| 859 | case PTRACE_POKETEXT: |
| 860 | case PTRACE_POKEDATA: |
| 861 | return generic_ptrace_pokedata(child, addr, data); |
| 862 | |
| 863 | #ifdef PTRACE_OLDSETOPTIONS |
| 864 | case PTRACE_OLDSETOPTIONS: |
| 865 | #endif |
| 866 | case PTRACE_SETOPTIONS: |
| 867 | ret = ptrace_setoptions(child, data); |
| 868 | break; |
| 869 | case PTRACE_GETEVENTMSG: |
| 870 | ret = put_user(child->ptrace_message, datalp); |
| 871 | break; |
| 872 | |
| 873 | case PTRACE_PEEKSIGINFO: |
| 874 | ret = ptrace_peek_siginfo(child, addr, data); |
| 875 | break; |
| 876 | |
| 877 | case PTRACE_GETSIGINFO: |
| 878 | ret = ptrace_getsiginfo(child, &siginfo); |
| 879 | if (!ret) |
| 880 | ret = copy_siginfo_to_user(datavp, &siginfo); |
| 881 | break; |
| 882 | |
| 883 | case PTRACE_SETSIGINFO: |
| 884 | if (copy_from_user(&siginfo, datavp, sizeof siginfo)) |
| 885 | ret = -EFAULT; |
| 886 | else |
| 887 | ret = ptrace_setsiginfo(child, &siginfo); |
| 888 | break; |
| 889 | |
| 890 | case PTRACE_GETSIGMASK: |
| 891 | if (addr != sizeof(sigset_t)) { |
| 892 | ret = -EINVAL; |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t))) |
| 897 | ret = -EFAULT; |
| 898 | else |
| 899 | ret = 0; |
| 900 | |
| 901 | break; |
| 902 | |
| 903 | case PTRACE_SETSIGMASK: { |
| 904 | sigset_t new_set; |
| 905 | |
| 906 | if (addr != sizeof(sigset_t)) { |
| 907 | ret = -EINVAL; |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { |
| 912 | ret = -EFAULT; |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 917 | |
| 918 | /* |
| 919 | * Every thread does recalc_sigpending() after resume, so |
| 920 | * retarget_shared_pending() and recalc_sigpending() are not |
| 921 | * called here. |
| 922 | */ |
| 923 | spin_lock_irq(&child->sighand->siglock); |
| 924 | child->blocked = new_set; |
| 925 | spin_unlock_irq(&child->sighand->siglock); |
| 926 | |
| 927 | ret = 0; |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | case PTRACE_INTERRUPT: |
| 932 | /* |
| 933 | * Stop tracee without any side-effect on signal or job |
| 934 | * control. At least one trap is guaranteed to happen |
| 935 | * after this request. If @child is already trapped, the |
| 936 | * current trap is not disturbed and another trap will |
| 937 | * happen after the current trap is ended with PTRACE_CONT. |
| 938 | * |
| 939 | * The actual trap might not be PTRACE_EVENT_STOP trap but |
| 940 | * the pending condition is cleared regardless. |
| 941 | */ |
| 942 | if (unlikely(!seized || !lock_task_sighand(child, &flags))) |
| 943 | break; |
| 944 | |
| 945 | /* |
| 946 | * INTERRUPT doesn't disturb existing trap sans one |
| 947 | * exception. If ptracer issued LISTEN for the current |
| 948 | * STOP, this INTERRUPT should clear LISTEN and re-trap |
| 949 | * tracee into STOP. |
| 950 | */ |
| 951 | if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) |
| 952 | ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); |
| 953 | |
| 954 | unlock_task_sighand(child, &flags); |
| 955 | ret = 0; |
| 956 | break; |
| 957 | |
| 958 | case PTRACE_LISTEN: |
| 959 | /* |
| 960 | * Listen for events. Tracee must be in STOP. It's not |
| 961 | * resumed per-se but is not considered to be in TRACED by |
| 962 | * wait(2) or ptrace(2). If an async event (e.g. group |
| 963 | * stop state change) happens, tracee will enter STOP trap |
| 964 | * again. Alternatively, ptracer can issue INTERRUPT to |
| 965 | * finish listening and re-trap tracee into STOP. |
| 966 | */ |
| 967 | if (unlikely(!seized || !lock_task_sighand(child, &flags))) |
| 968 | break; |
| 969 | |
| 970 | si = child->last_siginfo; |
| 971 | if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { |
| 972 | child->jobctl |= JOBCTL_LISTENING; |
| 973 | /* |
| 974 | * If NOTIFY is set, it means event happened between |
| 975 | * start of this trap and now. Trigger re-trap. |
| 976 | */ |
| 977 | if (child->jobctl & JOBCTL_TRAP_NOTIFY) |
| 978 | ptrace_signal_wake_up(child, true); |
| 979 | ret = 0; |
| 980 | } |
| 981 | unlock_task_sighand(child, &flags); |
| 982 | break; |
| 983 | |
| 984 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 985 | ret = ptrace_detach(child, data); |
| 986 | break; |
| 987 | |
| 988 | #ifdef CONFIG_BINFMT_ELF_FDPIC |
| 989 | case PTRACE_GETFDPIC: { |
| 990 | struct mm_struct *mm = get_task_mm(child); |
| 991 | unsigned long tmp = 0; |
| 992 | |
| 993 | ret = -ESRCH; |
| 994 | if (!mm) |
| 995 | break; |
| 996 | |
| 997 | switch (addr) { |
| 998 | case PTRACE_GETFDPIC_EXEC: |
| 999 | tmp = mm->context.exec_fdpic_loadmap; |
| 1000 | break; |
| 1001 | case PTRACE_GETFDPIC_INTERP: |
| 1002 | tmp = mm->context.interp_fdpic_loadmap; |
| 1003 | break; |
| 1004 | default: |
| 1005 | break; |
| 1006 | } |
| 1007 | mmput(mm); |
| 1008 | |
| 1009 | ret = put_user(tmp, datalp); |
| 1010 | break; |
| 1011 | } |
| 1012 | #endif |
| 1013 | |
| 1014 | #ifdef PTRACE_SINGLESTEP |
| 1015 | case PTRACE_SINGLESTEP: |
| 1016 | #endif |
| 1017 | #ifdef PTRACE_SINGLEBLOCK |
| 1018 | case PTRACE_SINGLEBLOCK: |
| 1019 | #endif |
| 1020 | #ifdef PTRACE_SYSEMU |
| 1021 | case PTRACE_SYSEMU: |
| 1022 | case PTRACE_SYSEMU_SINGLESTEP: |
| 1023 | #endif |
| 1024 | case PTRACE_SYSCALL: |
| 1025 | case PTRACE_CONT: |
| 1026 | return ptrace_resume(child, request, data); |
| 1027 | |
| 1028 | case PTRACE_KILL: |
| 1029 | if (child->exit_state) /* already dead */ |
| 1030 | return 0; |
| 1031 | return ptrace_resume(child, request, SIGKILL); |
| 1032 | |
| 1033 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 1034 | case PTRACE_GETREGSET: |
| 1035 | case PTRACE_SETREGSET: { |
| 1036 | struct iovec kiov; |
| 1037 | struct iovec __user *uiov = datavp; |
| 1038 | |
| 1039 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) |
| 1040 | return -EFAULT; |
| 1041 | |
| 1042 | if (__get_user(kiov.iov_base, &uiov->iov_base) || |
| 1043 | __get_user(kiov.iov_len, &uiov->iov_len)) |
| 1044 | return -EFAULT; |
| 1045 | |
| 1046 | ret = ptrace_regset(child, request, addr, &kiov); |
| 1047 | if (!ret) |
| 1048 | ret = __put_user(kiov.iov_len, &uiov->iov_len); |
| 1049 | break; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
| 1053 | case PTRACE_SECCOMP_GET_FILTER: |
| 1054 | ret = seccomp_get_filter(child, addr, datavp); |
| 1055 | break; |
| 1056 | |
| 1057 | default: |
| 1058 | break; |
| 1059 | } |
| 1060 | |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static struct task_struct *ptrace_get_task_struct(pid_t pid) |
| 1065 | { |
| 1066 | struct task_struct *child; |
| 1067 | |
| 1068 | rcu_read_lock(); |
| 1069 | child = find_task_by_vpid(pid); |
| 1070 | if (child) |
| 1071 | get_task_struct(child); |
| 1072 | rcu_read_unlock(); |
| 1073 | |
| 1074 | if (!child) |
| 1075 | return ERR_PTR(-ESRCH); |
| 1076 | return child; |
| 1077 | } |
| 1078 | |
| 1079 | #ifndef arch_ptrace_attach |
| 1080 | #define arch_ptrace_attach(child) do { } while (0) |
| 1081 | #endif |
| 1082 | |
| 1083 | SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, |
| 1084 | unsigned long, data) |
| 1085 | { |
| 1086 | struct task_struct *child; |
| 1087 | long ret; |
| 1088 | |
| 1089 | if (request == PTRACE_TRACEME) { |
| 1090 | ret = ptrace_traceme(); |
| 1091 | if (!ret) |
| 1092 | arch_ptrace_attach(current); |
| 1093 | goto out; |
| 1094 | } |
| 1095 | |
| 1096 | child = ptrace_get_task_struct(pid); |
| 1097 | if (IS_ERR(child)) { |
| 1098 | ret = PTR_ERR(child); |
| 1099 | goto out; |
| 1100 | } |
| 1101 | |
| 1102 | if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { |
| 1103 | ret = ptrace_attach(child, request, addr, data); |
| 1104 | /* |
| 1105 | * Some architectures need to do book-keeping after |
| 1106 | * a ptrace attach. |
| 1107 | */ |
| 1108 | if (!ret) |
| 1109 | arch_ptrace_attach(child); |
| 1110 | goto out_put_task_struct; |
| 1111 | } |
| 1112 | |
| 1113 | ret = ptrace_check_attach(child, request == PTRACE_KILL || |
| 1114 | request == PTRACE_INTERRUPT); |
| 1115 | if (ret < 0) |
| 1116 | goto out_put_task_struct; |
| 1117 | |
| 1118 | ret = arch_ptrace(child, request, addr, data); |
| 1119 | if (ret || request != PTRACE_DETACH) |
| 1120 | ptrace_unfreeze_traced(child); |
| 1121 | |
| 1122 | out_put_task_struct: |
| 1123 | put_task_struct(child); |
| 1124 | out: |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, |
| 1129 | unsigned long data) |
| 1130 | { |
| 1131 | unsigned long tmp; |
| 1132 | int copied; |
| 1133 | |
| 1134 | copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); |
| 1135 | if (copied != sizeof(tmp)) |
| 1136 | return -EIO; |
| 1137 | return put_user(tmp, (unsigned long __user *)data); |
| 1138 | } |
| 1139 | |
| 1140 | int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, |
| 1141 | unsigned long data) |
| 1142 | { |
| 1143 | int copied; |
| 1144 | |
| 1145 | copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); |
| 1146 | return (copied == sizeof(data)) ? 0 : -EIO; |
| 1147 | } |
| 1148 | |
| 1149 | #if defined CONFIG_COMPAT |
| 1150 | |
| 1151 | int compat_ptrace_request(struct task_struct *child, compat_long_t request, |
| 1152 | compat_ulong_t addr, compat_ulong_t data) |
| 1153 | { |
| 1154 | compat_ulong_t __user *datap = compat_ptr(data); |
| 1155 | compat_ulong_t word; |
| 1156 | siginfo_t siginfo; |
| 1157 | int ret; |
| 1158 | |
| 1159 | switch (request) { |
| 1160 | case PTRACE_PEEKTEXT: |
| 1161 | case PTRACE_PEEKDATA: |
| 1162 | ret = access_process_vm(child, addr, &word, sizeof(word), 0); |
| 1163 | if (ret != sizeof(word)) |
| 1164 | ret = -EIO; |
| 1165 | else |
| 1166 | ret = put_user(word, datap); |
| 1167 | break; |
| 1168 | |
| 1169 | case PTRACE_POKETEXT: |
| 1170 | case PTRACE_POKEDATA: |
| 1171 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); |
| 1172 | ret = (ret != sizeof(data) ? -EIO : 0); |
| 1173 | break; |
| 1174 | |
| 1175 | case PTRACE_GETEVENTMSG: |
| 1176 | ret = put_user((compat_ulong_t) child->ptrace_message, datap); |
| 1177 | break; |
| 1178 | |
| 1179 | case PTRACE_GETSIGINFO: |
| 1180 | ret = ptrace_getsiginfo(child, &siginfo); |
| 1181 | if (!ret) |
| 1182 | ret = copy_siginfo_to_user32( |
| 1183 | (struct compat_siginfo __user *) datap, |
| 1184 | &siginfo); |
| 1185 | break; |
| 1186 | |
| 1187 | case PTRACE_SETSIGINFO: |
| 1188 | memset(&siginfo, 0, sizeof siginfo); |
| 1189 | if (copy_siginfo_from_user32( |
| 1190 | &siginfo, (struct compat_siginfo __user *) datap)) |
| 1191 | ret = -EFAULT; |
| 1192 | else |
| 1193 | ret = ptrace_setsiginfo(child, &siginfo); |
| 1194 | break; |
| 1195 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 1196 | case PTRACE_GETREGSET: |
| 1197 | case PTRACE_SETREGSET: |
| 1198 | { |
| 1199 | struct iovec kiov; |
| 1200 | struct compat_iovec __user *uiov = |
| 1201 | (struct compat_iovec __user *) datap; |
| 1202 | compat_uptr_t ptr; |
| 1203 | compat_size_t len; |
| 1204 | |
| 1205 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) |
| 1206 | return -EFAULT; |
| 1207 | |
| 1208 | if (__get_user(ptr, &uiov->iov_base) || |
| 1209 | __get_user(len, &uiov->iov_len)) |
| 1210 | return -EFAULT; |
| 1211 | |
| 1212 | kiov.iov_base = compat_ptr(ptr); |
| 1213 | kiov.iov_len = len; |
| 1214 | |
| 1215 | ret = ptrace_regset(child, request, addr, &kiov); |
| 1216 | if (!ret) |
| 1217 | ret = __put_user(kiov.iov_len, &uiov->iov_len); |
| 1218 | break; |
| 1219 | } |
| 1220 | #endif |
| 1221 | |
| 1222 | default: |
| 1223 | ret = ptrace_request(child, request, addr, data); |
| 1224 | } |
| 1225 | |
| 1226 | return ret; |
| 1227 | } |
| 1228 | |
| 1229 | COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, |
| 1230 | compat_long_t, addr, compat_long_t, data) |
| 1231 | { |
| 1232 | struct task_struct *child; |
| 1233 | long ret; |
| 1234 | |
| 1235 | if (request == PTRACE_TRACEME) { |
| 1236 | ret = ptrace_traceme(); |
| 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | child = ptrace_get_task_struct(pid); |
| 1241 | if (IS_ERR(child)) { |
| 1242 | ret = PTR_ERR(child); |
| 1243 | goto out; |
| 1244 | } |
| 1245 | |
| 1246 | if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { |
| 1247 | ret = ptrace_attach(child, request, addr, data); |
| 1248 | /* |
| 1249 | * Some architectures need to do book-keeping after |
| 1250 | * a ptrace attach. |
| 1251 | */ |
| 1252 | if (!ret) |
| 1253 | arch_ptrace_attach(child); |
| 1254 | goto out_put_task_struct; |
| 1255 | } |
| 1256 | |
| 1257 | ret = ptrace_check_attach(child, request == PTRACE_KILL || |
| 1258 | request == PTRACE_INTERRUPT); |
| 1259 | if (!ret) { |
| 1260 | ret = compat_arch_ptrace(child, request, addr, data); |
| 1261 | if (ret || request != PTRACE_DETACH) |
| 1262 | ptrace_unfreeze_traced(child); |
| 1263 | } |
| 1264 | |
| 1265 | out_put_task_struct: |
| 1266 | put_task_struct(child); |
| 1267 | out: |
| 1268 | return ret; |
| 1269 | } |
| 1270 | #endif /* CONFIG_COMPAT */ |