1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/ptrace.c 4 * 5 * (C) Copyright 1999 Linus Torvalds 6 * 7 * Common interfaces for "ptrace()" which we do not want 8 * to continually duplicate across every architecture. 9 */ 10 11 #include <linux/capability.h> 12 #include <linux/export.h> 13 #include <linux/sched.h> 14 #include <linux/sched/mm.h> 15 #include <linux/sched/coredump.h> 16 #include <linux/sched/task.h> 17 #include <linux/errno.h> 18 #include <linux/mm.h> 19 #include <linux/highmem.h> 20 #include <linux/pagemap.h> 21 #include <linux/ptrace.h> 22 #include <linux/security.h> 23 #include <linux/signal.h> 24 #include <linux/uio.h> 25 #include <linux/audit.h> 26 #include <linux/pid_namespace.h> 27 #include <linux/syscalls.h> 28 #include <linux/uaccess.h> 29 #include <linux/regset.h> 30 #include <linux/hw_breakpoint.h> 31 #include <linux/cn_proc.h> 32 #include <linux/compat.h> 33 #include <linux/sched/signal.h> 34 #include <linux/minmax.h> 35 #include <linux/syscall_user_dispatch.h> 36 37 #include <asm/syscall.h> /* for syscall_get_* */ 38 39 /* 40 * Access another process' address space via ptrace. 41 * Source/target buffer must be kernel space, 42 * Do not walk the page table directly, use get_user_pages 43 */ 44 int ptrace_access_vm(struct task_struct *tsk, unsigned long addr, 45 void *buf, int len, unsigned int gup_flags) 46 { 47 struct mm_struct *mm; 48 int ret; 49 50 mm = get_task_mm(tsk); 51 if (!mm) 52 return 0; 53 54 if (!tsk->ptrace || 55 (current != tsk->parent) || 56 ((get_dumpable(mm) != SUID_DUMP_USER) && 57 !ptracer_capable(tsk, mm->user_ns))) { 58 mmput(mm); 59 return 0; 60 } 61 62 ret = access_remote_vm(mm, addr, buf, len, gup_flags); 63 mmput(mm); 64 65 return ret; 66 } 67 68 69 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent, 70 const struct cred *ptracer_cred) 71 { 72 BUG_ON(!list_empty(&child->ptrace_entry)); 73 list_add(&child->ptrace_entry, &new_parent->ptraced); 74 child->parent = new_parent; 75 child->ptracer_cred = get_cred(ptracer_cred); 76 } 77 78 /* 79 * ptrace a task: make the debugger its new parent and 80 * move it to the ptrace list. 81 * 82 * Must be called with the tasklist lock write-held. 83 */ 84 static void ptrace_link(struct task_struct *child, struct task_struct *new_parent) 85 { 86 __ptrace_link(child, new_parent, current_cred()); 87 } 88 89 /** 90 * __ptrace_unlink - unlink ptracee and restore its execution state 91 * @child: ptracee to be unlinked 92 * 93 * Remove @child from the ptrace list, move it back to the original parent, 94 * and restore the execution state so that it conforms to the group stop 95 * state. 96 * 97 * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 98 * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 99 * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 100 * If the ptracer is exiting, the ptracee can be in any state. 101 * 102 * After detach, the ptracee should be in a state which conforms to the 103 * group stop. If the group is stopped or in the process of stopping, the 104 * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 105 * up from TASK_TRACED. 106 * 107 * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 108 * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 109 * to but in the opposite direction of what happens while attaching to a 110 * stopped task. However, in this direction, the intermediate RUNNING 111 * state is not hidden even from the current ptracer and if it immediately 112 * re-attaches and performs a WNOHANG wait(2), it may fail. 113 * 114 * CONTEXT: 115 * write_lock_irq(tasklist_lock) 116 */ 117 void __ptrace_unlink(struct task_struct *child) 118 { 119 const struct cred *old_cred; 120 BUG_ON(!child->ptrace); 121 122 clear_task_syscall_work(child, SYSCALL_TRACE); 123 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 124 clear_task_syscall_work(child, SYSCALL_EMU); 125 #endif 126 127 child->parent = child->real_parent; 128 list_del_init(&child->ptrace_entry); 129 old_cred = child->ptracer_cred; 130 child->ptracer_cred = NULL; 131 put_cred(old_cred); 132 133 spin_lock(&child->sighand->siglock); 134 child->ptrace = 0; 135 /* 136 * Clear all pending traps and TRAPPING. TRAPPING should be 137 * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 138 */ 139 task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 140 task_clear_jobctl_trapping(child); 141 142 /* 143 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 144 * @child isn't dead. 145 */ 146 if (!(child->flags & PF_EXITING) && 147 (child->signal->flags & SIGNAL_STOP_STOPPED || 148 child->signal->group_stop_count)) 149 child->jobctl |= JOBCTL_STOP_PENDING; 150 151 /* 152 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 153 * @child in the butt. Note that @resume should be used iff @child 154 * is in TASK_TRACED; otherwise, we might unduly disrupt 155 * TASK_KILLABLE sleeps. 156 */ 157 if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 158 ptrace_signal_wake_up(child, true); 159 160 spin_unlock(&child->sighand->siglock); 161 } 162 163 static bool looks_like_a_spurious_pid(struct task_struct *task) 164 { 165 if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP)) 166 return false; 167 168 if (task_pid_vnr(task) == task->ptrace_message) 169 return false; 170 /* 171 * The tracee changed its pid but the PTRACE_EVENT_EXEC event 172 * was not wait()'ed, most probably debugger targets the old 173 * leader which was destroyed in de_thread(). 174 */ 175 return true; 176 } 177 178 /* 179 * Ensure that nothing can wake it up, even SIGKILL 180 * 181 * A task is switched to this state while a ptrace operation is in progress; 182 * such that the ptrace operation is uninterruptible. 183 */ 184 static bool ptrace_freeze_traced(struct task_struct *task) 185 { 186 bool ret = false; 187 188 /* Lockless, nobody but us can set this flag */ 189 if (task->jobctl & JOBCTL_LISTENING) 190 return ret; 191 192 spin_lock_irq(&task->sighand->siglock); 193 if (task_is_traced(task) && !looks_like_a_spurious_pid(task) && 194 !__fatal_signal_pending(task)) { 195 task->jobctl |= JOBCTL_PTRACE_FROZEN; 196 ret = true; 197 } 198 spin_unlock_irq(&task->sighand->siglock); 199 200 return ret; 201 } 202 203 static void ptrace_unfreeze_traced(struct task_struct *task) 204 { 205 unsigned long flags; 206 207 /* 208 * The child may be awake and may have cleared 209 * JOBCTL_PTRACE_FROZEN (see ptrace_resume). The child will 210 * not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew. 211 */ 212 if (lock_task_sighand(task, &flags)) { 213 task->jobctl &= ~JOBCTL_PTRACE_FROZEN; 214 if (__fatal_signal_pending(task)) { 215 task->jobctl &= ~JOBCTL_TRACED; 216 wake_up_state(task, __TASK_TRACED); 217 } 218 unlock_task_sighand(task, &flags); 219 } 220 } 221 222 /** 223 * ptrace_check_attach - check whether ptracee is ready for ptrace operation 224 * @child: ptracee to check for 225 * @ignore_state: don't check whether @child is currently %TASK_TRACED 226 * 227 * Check whether @child is being ptraced by %current and ready for further 228 * ptrace operations. If @ignore_state is %false, @child also should be in 229 * %TASK_TRACED state and on return the child is guaranteed to be traced 230 * and not executing. If @ignore_state is %true, @child can be in any 231 * state. 232 * 233 * CONTEXT: 234 * Grabs and releases tasklist_lock and @child->sighand->siglock. 235 * 236 * RETURNS: 237 * 0 on success, -ESRCH if %child is not ready. 238 */ 239 static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 240 { 241 int ret = -ESRCH; 242 243 /* 244 * We take the read lock around doing both checks to close a 245 * possible race where someone else was tracing our child and 246 * detached between these two checks. After this locked check, 247 * we are sure that this is our traced child and that can only 248 * be changed by us so it's not changing right after this. 249 */ 250 read_lock(&tasklist_lock); 251 if (child->ptrace && child->parent == current) { 252 /* 253 * child->sighand can't be NULL, release_task() 254 * does ptrace_unlink() before __exit_signal(). 255 */ 256 if (ignore_state || ptrace_freeze_traced(child)) 257 ret = 0; 258 } 259 read_unlock(&tasklist_lock); 260 261 if (!ret && !ignore_state && 262 WARN_ON_ONCE(!wait_task_inactive(child, __TASK_TRACED|TASK_FROZEN))) 263 ret = -ESRCH; 264 265 return ret; 266 } 267 268 static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode) 269 { 270 if (mode & PTRACE_MODE_NOAUDIT) 271 return ns_capable_noaudit(ns, CAP_SYS_PTRACE); 272 return ns_capable(ns, CAP_SYS_PTRACE); 273 } 274 275 /* Returns 0 on success, -errno on denial. */ 276 static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 277 { 278 const struct cred *cred = current_cred(), *tcred; 279 struct mm_struct *mm; 280 kuid_t caller_uid; 281 kgid_t caller_gid; 282 283 if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { 284 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); 285 return -EPERM; 286 } 287 288 /* May we inspect the given task? 289 * This check is used both for attaching with ptrace 290 * and for allowing access to sensitive information in /proc. 291 * 292 * ptrace_attach denies several cases that /proc allows 293 * because setting up the necessary parent/child relationship 294 * or halting the specified task is impossible. 295 */ 296 297 /* Don't let security modules deny introspection */ 298 if (same_thread_group(task, current)) 299 return 0; 300 rcu_read_lock(); 301 if (mode & PTRACE_MODE_FSCREDS) { 302 caller_uid = cred->fsuid; 303 caller_gid = cred->fsgid; 304 } else { 305 /* 306 * Using the euid would make more sense here, but something 307 * in userland might rely on the old behavior, and this 308 * shouldn't be a security problem since 309 * PTRACE_MODE_REALCREDS implies that the caller explicitly 310 * used a syscall that requests access to another process 311 * (and not a filesystem syscall to procfs). 312 */ 313 caller_uid = cred->uid; 314 caller_gid = cred->gid; 315 } 316 tcred = __task_cred(task); 317 if (uid_eq(caller_uid, tcred->euid) && 318 uid_eq(caller_uid, tcred->suid) && 319 uid_eq(caller_uid, tcred->uid) && 320 gid_eq(caller_gid, tcred->egid) && 321 gid_eq(caller_gid, tcred->sgid) && 322 gid_eq(caller_gid, tcred->gid)) 323 goto ok; 324 if (ptrace_has_cap(tcred->user_ns, mode)) 325 goto ok; 326 rcu_read_unlock(); 327 return -EPERM; 328 ok: 329 rcu_read_unlock(); 330 /* 331 * If a task drops privileges and becomes nondumpable (through a syscall 332 * like setresuid()) while we are trying to access it, we must ensure 333 * that the dumpability is read after the credentials; otherwise, 334 * we may be able to attach to a task that we shouldn't be able to 335 * attach to (as if the task had dropped privileges without becoming 336 * nondumpable). 337 * Pairs with a write barrier in commit_creds(). 338 */ 339 smp_rmb(); 340 mm = task->mm; 341 if (mm && 342 ((get_dumpable(mm) != SUID_DUMP_USER) && 343 !ptrace_has_cap(mm->user_ns, mode))) 344 return -EPERM; 345 346 return security_ptrace_access_check(task, mode); 347 } 348 349 bool ptrace_may_access(struct task_struct *task, unsigned int mode) 350 { 351 int err; 352 task_lock(task); 353 err = __ptrace_may_access(task, mode); 354 task_unlock(task); 355 return !err; 356 } 357 358 static int check_ptrace_options(unsigned long data) 359 { 360 if (data & ~(unsigned long)PTRACE_O_MASK) 361 return -EINVAL; 362 363 if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { 364 if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) || 365 !IS_ENABLED(CONFIG_SECCOMP)) 366 return -EINVAL; 367 368 if (!capable(CAP_SYS_ADMIN)) 369 return -EPERM; 370 371 if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || 372 current->ptrace & PT_SUSPEND_SECCOMP) 373 return -EPERM; 374 } 375 return 0; 376 } 377 378 static inline void ptrace_set_stopped(struct task_struct *task, bool seize) 379 { 380 guard(spinlock)(&task->sighand->siglock); 381 382 /* SEIZE doesn't trap tracee on attach */ 383 if (!seize) 384 send_signal_locked(SIGSTOP, SEND_SIG_PRIV, task, PIDTYPE_PID); 385 /* 386 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 387 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 388 * will be cleared if the child completes the transition or any 389 * event which clears the group stop states happens. We'll wait 390 * for the transition to complete before returning from this 391 * function. 392 * 393 * This hides STOPPED -> RUNNING -> TRACED transition from the 394 * attaching thread but a different thread in the same group can 395 * still observe the transient RUNNING state. IOW, if another 396 * thread's WNOHANG wait(2) on the stopped tracee races against 397 * ATTACH, the wait(2) may fail due to the transient RUNNING. 398 * 399 * The following task_is_stopped() test is safe as both transitions 400 * in and out of STOPPED are protected by siglock. 401 */ 402 if (task_is_stopped(task) && 403 task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) { 404 task->jobctl &= ~JOBCTL_STOPPED; 405 signal_wake_up_state(task, __TASK_STOPPED); 406 } 407 } 408 409 static int ptrace_attach(struct task_struct *task, long request, 410 unsigned long addr, 411 unsigned long flags) 412 { 413 bool seize = (request == PTRACE_SEIZE); 414 int retval; 415 416 if (seize) { 417 if (addr != 0) 418 return -EIO; 419 /* 420 * This duplicates the check in check_ptrace_options() because 421 * ptrace_attach() and ptrace_setoptions() have historically 422 * used different error codes for unknown ptrace options. 423 */ 424 if (flags & ~(unsigned long)PTRACE_O_MASK) 425 return -EIO; 426 427 retval = check_ptrace_options(flags); 428 if (retval) 429 return retval; 430 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 431 } else { 432 flags = PT_PTRACED; 433 } 434 435 audit_ptrace(task); 436 437 if (unlikely(task->flags & PF_KTHREAD)) 438 return -EPERM; 439 if (same_thread_group(task, current)) 440 return -EPERM; 441 442 /* 443 * Protect exec's credential calculations against our interference; 444 * SUID, SGID and LSM creds get determined differently 445 * under ptrace. 446 */ 447 scoped_cond_guard (mutex_intr, return -ERESTARTNOINTR, 448 &task->signal->cred_guard_mutex) { 449 450 scoped_guard (task_lock, task) { 451 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); 452 if (retval) 453 return retval; 454 } 455 456 scoped_guard (write_lock_irq, &tasklist_lock) { 457 if (unlikely(task->exit_state)) 458 return -EPERM; 459 if (task->ptrace) 460 return -EPERM; 461 462 task->ptrace = flags; 463 ptrace_link(task, current); 464 ptrace_set_stopped(task, seize); 465 } 466 } 467 468 /* 469 * We do not bother to change retval or clear JOBCTL_TRAPPING 470 * if wait_on_bit() was interrupted by SIGKILL. The tracer will 471 * not return to user-mode, it will exit and clear this bit in 472 * __ptrace_unlink() if it wasn't already cleared by the tracee; 473 * and until then nobody can ptrace this task. 474 */ 475 wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE); 476 proc_ptrace_connector(task, PTRACE_ATTACH); 477 478 return 0; 479 } 480 481 /** 482 * ptrace_traceme -- helper for PTRACE_TRACEME 483 * 484 * Performs checks and sets PT_PTRACED. 485 * Should be used by all ptrace implementations for PTRACE_TRACEME. 486 */ 487 static int ptrace_traceme(void) 488 { 489 int ret = -EPERM; 490 491 write_lock_irq(&tasklist_lock); 492 /* Are we already being traced? */ 493 if (!current->ptrace) { 494 ret = security_ptrace_traceme(current->parent); 495 /* 496 * Check PF_EXITING to ensure ->real_parent has not passed 497 * exit_ptrace(). Otherwise we don't report the error but 498 * pretend ->real_parent untraces us right after return. 499 */ 500 if (!ret && !(current->real_parent->flags & PF_EXITING)) { 501 current->ptrace = PT_PTRACED; 502 ptrace_link(current, current->real_parent); 503 } 504 } 505 write_unlock_irq(&tasklist_lock); 506 507 return ret; 508 } 509 510 /* 511 * Called with irqs disabled, returns true if childs should reap themselves. 512 */ 513 static int ignoring_children(struct sighand_struct *sigh) 514 { 515 int ret; 516 spin_lock(&sigh->siglock); 517 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 518 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 519 spin_unlock(&sigh->siglock); 520 return ret; 521 } 522 523 /* 524 * Called with tasklist_lock held for writing. 525 * Unlink a traced task, and clean it up if it was a traced zombie. 526 * Return true if it needs to be reaped with release_task(). 527 * (We can't call release_task() here because we already hold tasklist_lock.) 528 * 529 * If it's a zombie, our attachedness prevented normal parent notification 530 * or self-reaping. Do notification now if it would have happened earlier. 531 * If it should reap itself, return true. 532 * 533 * If it's our own child, there is no notification to do. But if our normal 534 * children self-reap, then this child was prevented by ptrace and we must 535 * reap it now, in that case we must also wake up sub-threads sleeping in 536 * do_wait(). 537 */ 538 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 539 { 540 bool dead; 541 542 __ptrace_unlink(p); 543 544 if (p->exit_state != EXIT_ZOMBIE) 545 return false; 546 547 dead = !thread_group_leader(p); 548 549 if (!dead && thread_group_empty(p)) { 550 if (!same_thread_group(p->real_parent, tracer)) 551 dead = do_notify_parent(p, p->exit_signal); 552 else if (ignoring_children(tracer->sighand) || 553 p->signal->autoreap) { 554 __wake_up_parent(p, tracer); 555 dead = true; 556 } 557 } 558 /* Mark it as in the process of being reaped. */ 559 if (dead) 560 p->exit_state = EXIT_DEAD; 561 return dead; 562 } 563 564 static int ptrace_detach(struct task_struct *child, unsigned int data) 565 { 566 if (!valid_signal(data)) 567 return -EIO; 568 569 /* Architecture-specific hardware disable .. */ 570 ptrace_disable(child); 571 572 write_lock_irq(&tasklist_lock); 573 /* 574 * We rely on ptrace_freeze_traced(). It can't be killed and 575 * untraced by another thread, it can't be a zombie. 576 */ 577 WARN_ON(!child->ptrace || child->exit_state); 578 /* 579 * tasklist_lock avoids the race with wait_task_stopped(), see 580 * the comment in ptrace_resume(). 581 */ 582 child->exit_code = data; 583 __ptrace_detach(current, child); 584 write_unlock_irq(&tasklist_lock); 585 586 proc_ptrace_connector(child, PTRACE_DETACH); 587 588 return 0; 589 } 590 591 /* 592 * Detach all tasks we were using ptrace on. Called with tasklist held 593 * for writing. 594 */ 595 void exit_ptrace(struct task_struct *tracer, struct list_head *dead) 596 { 597 struct task_struct *p, *n; 598 599 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 600 if (unlikely(p->ptrace & PT_EXITKILL)) 601 send_sig_info(SIGKILL, SEND_SIG_PRIV, p); 602 603 if (__ptrace_detach(tracer, p)) 604 list_add(&p->ptrace_entry, dead); 605 } 606 } 607 608 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 609 { 610 int copied = 0; 611 612 while (len > 0) { 613 char buf[128]; 614 int this_len, retval; 615 616 this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 617 retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE); 618 619 if (!retval) { 620 if (copied) 621 break; 622 return -EIO; 623 } 624 if (copy_to_user(dst, buf, retval)) 625 return -EFAULT; 626 copied += retval; 627 src += retval; 628 dst += retval; 629 len -= retval; 630 } 631 return copied; 632 } 633 634 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 635 { 636 int copied = 0; 637 638 while (len > 0) { 639 char buf[128]; 640 int this_len, retval; 641 642 this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 643 if (copy_from_user(buf, src, this_len)) 644 return -EFAULT; 645 retval = ptrace_access_vm(tsk, dst, buf, this_len, 646 FOLL_FORCE | FOLL_WRITE); 647 if (!retval) { 648 if (copied) 649 break; 650 return -EIO; 651 } 652 copied += retval; 653 src += retval; 654 dst += retval; 655 len -= retval; 656 } 657 return copied; 658 } 659 660 static int ptrace_setoptions(struct task_struct *child, unsigned long data) 661 { 662 unsigned flags; 663 int ret; 664 665 ret = check_ptrace_options(data); 666 if (ret) 667 return ret; 668 669 /* Avoid intermediate state when all opts are cleared */ 670 flags = child->ptrace; 671 flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 672 flags |= (data << PT_OPT_FLAG_SHIFT); 673 child->ptrace = flags; 674 675 return 0; 676 } 677 678 static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info) 679 { 680 unsigned long flags; 681 int error = -ESRCH; 682 683 if (lock_task_sighand(child, &flags)) { 684 error = -EINVAL; 685 if (likely(child->last_siginfo != NULL)) { 686 copy_siginfo(info, child->last_siginfo); 687 error = 0; 688 } 689 unlock_task_sighand(child, &flags); 690 } 691 return error; 692 } 693 694 static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info) 695 { 696 unsigned long flags; 697 int error = -ESRCH; 698 699 if (lock_task_sighand(child, &flags)) { 700 error = -EINVAL; 701 if (likely(child->last_siginfo != NULL)) { 702 copy_siginfo(child->last_siginfo, info); 703 error = 0; 704 } 705 unlock_task_sighand(child, &flags); 706 } 707 return error; 708 } 709 710 static int ptrace_peek_siginfo(struct task_struct *child, 711 unsigned long addr, 712 unsigned long data) 713 { 714 struct ptrace_peeksiginfo_args arg; 715 struct sigpending *pending; 716 struct sigqueue *q; 717 int ret, i; 718 719 ret = copy_from_user(&arg, (void __user *) addr, 720 sizeof(struct ptrace_peeksiginfo_args)); 721 if (ret) 722 return -EFAULT; 723 724 if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 725 return -EINVAL; /* unknown flags */ 726 727 if (arg.nr < 0) 728 return -EINVAL; 729 730 /* Ensure arg.off fits in an unsigned long */ 731 if (arg.off > ULONG_MAX) 732 return 0; 733 734 if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 735 pending = &child->signal->shared_pending; 736 else 737 pending = &child->pending; 738 739 for (i = 0; i < arg.nr; ) { 740 kernel_siginfo_t info; 741 unsigned long off = arg.off + i; 742 bool found = false; 743 744 spin_lock_irq(&child->sighand->siglock); 745 list_for_each_entry(q, &pending->list, list) { 746 if (!off--) { 747 found = true; 748 copy_siginfo(&info, &q->info); 749 break; 750 } 751 } 752 spin_unlock_irq(&child->sighand->siglock); 753 754 if (!found) /* beyond the end of the list */ 755 break; 756 757 #ifdef CONFIG_COMPAT 758 if (unlikely(in_compat_syscall())) { 759 compat_siginfo_t __user *uinfo = compat_ptr(data); 760 761 if (copy_siginfo_to_user32(uinfo, &info)) { 762 ret = -EFAULT; 763 break; 764 } 765 766 } else 767 #endif 768 { 769 siginfo_t __user *uinfo = (siginfo_t __user *) data; 770 771 if (copy_siginfo_to_user(uinfo, &info)) { 772 ret = -EFAULT; 773 break; 774 } 775 } 776 777 data += sizeof(siginfo_t); 778 i++; 779 780 if (signal_pending(current)) 781 break; 782 783 cond_resched(); 784 } 785 786 if (i > 0) 787 return i; 788 789 return ret; 790 } 791 792 #ifdef CONFIG_RSEQ 793 static long ptrace_get_rseq_configuration(struct task_struct *task, 794 unsigned long size, void __user *data) 795 { 796 struct ptrace_rseq_configuration conf = { 797 .rseq_abi_pointer = (u64)(uintptr_t)task->rseq.usrptr, 798 .rseq_abi_size = task->rseq.len, 799 .signature = task->rseq.sig, 800 .flags = 0, 801 }; 802 803 size = min_t(unsigned long, size, sizeof(conf)); 804 if (copy_to_user(data, &conf, size)) 805 return -EFAULT; 806 return sizeof(conf); 807 } 808 #endif 809 810 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 811 812 #ifdef PTRACE_SINGLEBLOCK 813 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 814 #else 815 #define is_singleblock(request) 0 816 #endif 817 818 #ifdef PTRACE_SYSEMU 819 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 820 #else 821 #define is_sysemu_singlestep(request) 0 822 #endif 823 824 static int ptrace_resume(struct task_struct *child, long request, 825 unsigned long data) 826 { 827 if (!valid_signal(data)) 828 return -EIO; 829 830 if (request == PTRACE_SYSCALL) 831 set_task_syscall_work(child, SYSCALL_TRACE); 832 else 833 clear_task_syscall_work(child, SYSCALL_TRACE); 834 835 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 836 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 837 set_task_syscall_work(child, SYSCALL_EMU); 838 else 839 clear_task_syscall_work(child, SYSCALL_EMU); 840 #endif 841 842 if (is_singleblock(request)) { 843 if (unlikely(!arch_has_block_step())) 844 return -EIO; 845 user_enable_block_step(child); 846 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 847 if (unlikely(!arch_has_single_step())) 848 return -EIO; 849 user_enable_single_step(child); 850 } else { 851 user_disable_single_step(child); 852 } 853 854 /* 855 * Change ->exit_code and ->state under siglock to avoid the race 856 * with wait_task_stopped() in between; a non-zero ->exit_code will 857 * wrongly look like another report from tracee. 858 * 859 * Note that we need siglock even if ->exit_code == data and/or this 860 * status was not reported yet, the new status must not be cleared by 861 * wait_task_stopped() after resume. 862 */ 863 spin_lock_irq(&child->sighand->siglock); 864 child->exit_code = data; 865 child->jobctl &= ~JOBCTL_TRACED; 866 wake_up_state(child, __TASK_TRACED); 867 spin_unlock_irq(&child->sighand->siglock); 868 869 return 0; 870 } 871 872 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 873 874 static const struct user_regset * 875 find_regset(const struct user_regset_view *view, unsigned int type) 876 { 877 const struct user_regset *regset; 878 int n; 879 880 for (n = 0; n < view->n; ++n) { 881 regset = view->regsets + n; 882 if (regset->core_note_type == type) 883 return regset; 884 } 885 886 return NULL; 887 } 888 889 static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 890 struct iovec *kiov) 891 { 892 const struct user_regset_view *view = task_user_regset_view(task); 893 const struct user_regset *regset = find_regset(view, type); 894 int regset_no; 895 896 if (!regset || (kiov->iov_len % regset->size) != 0) 897 return -EINVAL; 898 899 regset_no = regset - view->regsets; 900 kiov->iov_len = min(kiov->iov_len, 901 (__kernel_size_t) (regset->n * regset->size)); 902 903 if (req == PTRACE_GETREGSET) 904 return copy_regset_to_user(task, view, regset_no, 0, 905 kiov->iov_len, kiov->iov_base); 906 else 907 return copy_regset_from_user(task, view, regset_no, 0, 908 kiov->iov_len, kiov->iov_base); 909 } 910 911 /* 912 * This is declared in linux/regset.h and defined in machine-dependent 913 * code. We put the export here, near the primary machine-neutral use, 914 * to ensure no machine forgets it. 915 */ 916 EXPORT_SYMBOL_GPL(task_user_regset_view); 917 918 static unsigned long 919 ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs, 920 struct ptrace_syscall_info *info) 921 { 922 unsigned long args[ARRAY_SIZE(info->entry.args)]; 923 int i; 924 925 info->entry.nr = syscall_get_nr(child, regs); 926 syscall_get_arguments(child, regs, args); 927 for (i = 0; i < ARRAY_SIZE(args); i++) 928 info->entry.args[i] = args[i]; 929 930 /* args is the last field in struct ptrace_syscall_info.entry */ 931 return offsetofend(struct ptrace_syscall_info, entry.args); 932 } 933 934 static unsigned long 935 ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs, 936 struct ptrace_syscall_info *info) 937 { 938 /* 939 * As struct ptrace_syscall_info.entry is currently a subset 940 * of struct ptrace_syscall_info.seccomp, it makes sense to 941 * initialize that subset using ptrace_get_syscall_info_entry(). 942 * This can be reconsidered in the future if these structures 943 * diverge significantly enough. 944 */ 945 ptrace_get_syscall_info_entry(child, regs, info); 946 info->seccomp.ret_data = child->ptrace_message; 947 948 /* 949 * ret_data is the last non-reserved field 950 * in struct ptrace_syscall_info.seccomp 951 */ 952 return offsetofend(struct ptrace_syscall_info, seccomp.ret_data); 953 } 954 955 static unsigned long 956 ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs, 957 struct ptrace_syscall_info *info) 958 { 959 info->exit.rval = syscall_get_error(child, regs); 960 info->exit.is_error = !!info->exit.rval; 961 if (!info->exit.is_error) 962 info->exit.rval = syscall_get_return_value(child, regs); 963 964 /* is_error is the last field in struct ptrace_syscall_info.exit */ 965 return offsetofend(struct ptrace_syscall_info, exit.is_error); 966 } 967 968 static int 969 ptrace_get_syscall_info_op(struct task_struct *child) 970 { 971 /* 972 * This does not need lock_task_sighand() to access 973 * child->last_siginfo because ptrace_freeze_traced() 974 * called earlier by ptrace_check_attach() ensures that 975 * the tracee cannot go away and clear its last_siginfo. 976 */ 977 switch (child->last_siginfo ? child->last_siginfo->si_code : 0) { 978 case SIGTRAP | 0x80: 979 switch (child->ptrace_message) { 980 case PTRACE_EVENTMSG_SYSCALL_ENTRY: 981 return PTRACE_SYSCALL_INFO_ENTRY; 982 case PTRACE_EVENTMSG_SYSCALL_EXIT: 983 return PTRACE_SYSCALL_INFO_EXIT; 984 default: 985 return PTRACE_SYSCALL_INFO_NONE; 986 } 987 case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8): 988 return PTRACE_SYSCALL_INFO_SECCOMP; 989 default: 990 return PTRACE_SYSCALL_INFO_NONE; 991 } 992 } 993 994 static int 995 ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size, 996 void __user *datavp) 997 { 998 struct pt_regs *regs = task_pt_regs(child); 999 struct ptrace_syscall_info info = { 1000 .op = ptrace_get_syscall_info_op(child), 1001 .arch = syscall_get_arch(child), 1002 .instruction_pointer = instruction_pointer(regs), 1003 .stack_pointer = user_stack_pointer(regs), 1004 }; 1005 unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry); 1006 unsigned long write_size; 1007 1008 switch (info.op) { 1009 case PTRACE_SYSCALL_INFO_ENTRY: 1010 actual_size = ptrace_get_syscall_info_entry(child, regs, &info); 1011 break; 1012 case PTRACE_SYSCALL_INFO_EXIT: 1013 actual_size = ptrace_get_syscall_info_exit(child, regs, &info); 1014 break; 1015 case PTRACE_SYSCALL_INFO_SECCOMP: 1016 actual_size = ptrace_get_syscall_info_seccomp(child, regs, &info); 1017 break; 1018 } 1019 1020 write_size = min(actual_size, user_size); 1021 return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size; 1022 } 1023 1024 static int 1025 ptrace_set_syscall_info_entry(struct task_struct *child, struct pt_regs *regs, 1026 struct ptrace_syscall_info *info) 1027 { 1028 unsigned long args[ARRAY_SIZE(info->entry.args)]; 1029 int nr = info->entry.nr; 1030 int i; 1031 1032 /* 1033 * Check that the syscall number specified in info->entry.nr 1034 * is either a value of type "int" or a sign-extended value 1035 * of type "int". 1036 */ 1037 if (nr != info->entry.nr) 1038 return -ERANGE; 1039 1040 for (i = 0; i < ARRAY_SIZE(args); i++) { 1041 args[i] = info->entry.args[i]; 1042 /* 1043 * Check that the syscall argument specified in 1044 * info->entry.args[i] is either a value of type 1045 * "unsigned long" or a sign-extended value of type "long". 1046 */ 1047 if (args[i] != info->entry.args[i]) 1048 return -ERANGE; 1049 } 1050 1051 syscall_set_nr(child, regs, nr); 1052 /* 1053 * If the syscall number is set to -1, setting syscall arguments is not 1054 * just pointless, it would also clobber the syscall return value on 1055 * those architectures that share the same register both for the first 1056 * argument of syscall and its return value. 1057 */ 1058 if (nr != -1) 1059 syscall_set_arguments(child, regs, args); 1060 1061 return 0; 1062 } 1063 1064 static int 1065 ptrace_set_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs, 1066 struct ptrace_syscall_info *info) 1067 { 1068 /* 1069 * info->entry is currently a subset of info->seccomp, 1070 * info->seccomp.ret_data is currently ignored. 1071 */ 1072 return ptrace_set_syscall_info_entry(child, regs, info); 1073 } 1074 1075 static int 1076 ptrace_set_syscall_info_exit(struct task_struct *child, struct pt_regs *regs, 1077 struct ptrace_syscall_info *info) 1078 { 1079 long rval = info->exit.rval; 1080 1081 /* 1082 * Check that the return value specified in info->exit.rval 1083 * is either a value of type "long" or a sign-extended value 1084 * of type "long". 1085 */ 1086 if (rval != info->exit.rval) 1087 return -ERANGE; 1088 1089 if (info->exit.is_error) 1090 syscall_set_return_value(child, regs, rval, 0); 1091 else 1092 syscall_set_return_value(child, regs, 0, rval); 1093 1094 return 0; 1095 } 1096 1097 static int 1098 ptrace_set_syscall_info(struct task_struct *child, unsigned long user_size, 1099 const void __user *datavp) 1100 { 1101 struct pt_regs *regs = task_pt_regs(child); 1102 struct ptrace_syscall_info info; 1103 1104 if (user_size < sizeof(info)) 1105 return -EINVAL; 1106 1107 /* 1108 * The compatibility is tracked by info.op and info.flags: if user-space 1109 * does not instruct us to use unknown extra bits from future versions 1110 * of ptrace_syscall_info, we are not going to read them either. 1111 */ 1112 if (copy_from_user(&info, datavp, sizeof(info))) 1113 return -EFAULT; 1114 1115 /* Reserved for future use. */ 1116 if (info.flags || info.reserved) 1117 return -EINVAL; 1118 1119 /* Changing the type of the system call stop is not supported yet. */ 1120 if (ptrace_get_syscall_info_op(child) != info.op) 1121 return -EINVAL; 1122 1123 switch (info.op) { 1124 case PTRACE_SYSCALL_INFO_ENTRY: 1125 return ptrace_set_syscall_info_entry(child, regs, &info); 1126 case PTRACE_SYSCALL_INFO_EXIT: 1127 return ptrace_set_syscall_info_exit(child, regs, &info); 1128 case PTRACE_SYSCALL_INFO_SECCOMP: 1129 return ptrace_set_syscall_info_seccomp(child, regs, &info); 1130 default: 1131 /* Other types of system call stops are not supported yet. */ 1132 return -EINVAL; 1133 } 1134 } 1135 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 1136 1137 int ptrace_request(struct task_struct *child, long request, 1138 unsigned long addr, unsigned long data) 1139 { 1140 bool seized = child->ptrace & PT_SEIZED; 1141 int ret = -EIO; 1142 kernel_siginfo_t siginfo, *si; 1143 void __user *datavp = (void __user *) data; 1144 unsigned long __user *datalp = datavp; 1145 unsigned long flags; 1146 1147 switch (request) { 1148 case PTRACE_PEEKTEXT: 1149 case PTRACE_PEEKDATA: 1150 return generic_ptrace_peekdata(child, addr, data); 1151 case PTRACE_POKETEXT: 1152 case PTRACE_POKEDATA: 1153 return generic_ptrace_pokedata(child, addr, data); 1154 1155 #ifdef PTRACE_OLDSETOPTIONS 1156 case PTRACE_OLDSETOPTIONS: 1157 #endif 1158 case PTRACE_SETOPTIONS: 1159 ret = ptrace_setoptions(child, data); 1160 break; 1161 case PTRACE_GETEVENTMSG: 1162 ret = put_user(child->ptrace_message, datalp); 1163 break; 1164 1165 case PTRACE_PEEKSIGINFO: 1166 ret = ptrace_peek_siginfo(child, addr, data); 1167 break; 1168 1169 case PTRACE_GETSIGINFO: 1170 ret = ptrace_getsiginfo(child, &siginfo); 1171 if (!ret) 1172 ret = copy_siginfo_to_user(datavp, &siginfo); 1173 break; 1174 1175 case PTRACE_SETSIGINFO: 1176 ret = copy_siginfo_from_user(&siginfo, datavp); 1177 if (!ret) 1178 ret = ptrace_setsiginfo(child, &siginfo); 1179 break; 1180 1181 case PTRACE_GETSIGMASK: { 1182 sigset_t *mask; 1183 1184 if (addr != sizeof(sigset_t)) { 1185 ret = -EINVAL; 1186 break; 1187 } 1188 1189 if (test_tsk_restore_sigmask(child)) 1190 mask = &child->saved_sigmask; 1191 else 1192 mask = &child->blocked; 1193 1194 if (copy_to_user(datavp, mask, sizeof(sigset_t))) 1195 ret = -EFAULT; 1196 else 1197 ret = 0; 1198 1199 break; 1200 } 1201 1202 case PTRACE_SETSIGMASK: { 1203 sigset_t new_set; 1204 1205 if (addr != sizeof(sigset_t)) { 1206 ret = -EINVAL; 1207 break; 1208 } 1209 1210 if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 1211 ret = -EFAULT; 1212 break; 1213 } 1214 1215 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 1216 1217 /* 1218 * Every thread does recalc_sigpending() after resume, so 1219 * retarget_shared_pending() and recalc_sigpending() are not 1220 * called here. 1221 */ 1222 spin_lock_irq(&child->sighand->siglock); 1223 child->blocked = new_set; 1224 spin_unlock_irq(&child->sighand->siglock); 1225 1226 clear_tsk_restore_sigmask(child); 1227 1228 ret = 0; 1229 break; 1230 } 1231 1232 case PTRACE_INTERRUPT: 1233 /* 1234 * Stop tracee without any side-effect on signal or job 1235 * control. At least one trap is guaranteed to happen 1236 * after this request. If @child is already trapped, the 1237 * current trap is not disturbed and another trap will 1238 * happen after the current trap is ended with PTRACE_CONT. 1239 * 1240 * The actual trap might not be PTRACE_EVENT_STOP trap but 1241 * the pending condition is cleared regardless. 1242 */ 1243 if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1244 break; 1245 1246 /* 1247 * INTERRUPT doesn't disturb existing trap sans one 1248 * exception. If ptracer issued LISTEN for the current 1249 * STOP, this INTERRUPT should clear LISTEN and re-trap 1250 * tracee into STOP. 1251 */ 1252 if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 1253 ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 1254 1255 unlock_task_sighand(child, &flags); 1256 ret = 0; 1257 break; 1258 1259 case PTRACE_LISTEN: 1260 /* 1261 * Listen for events. Tracee must be in STOP. It's not 1262 * resumed per-se but is not considered to be in TRACED by 1263 * wait(2) or ptrace(2). If an async event (e.g. group 1264 * stop state change) happens, tracee will enter STOP trap 1265 * again. Alternatively, ptracer can issue INTERRUPT to 1266 * finish listening and re-trap tracee into STOP. 1267 */ 1268 if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1269 break; 1270 1271 si = child->last_siginfo; 1272 if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 1273 child->jobctl |= JOBCTL_LISTENING; 1274 /* 1275 * If NOTIFY is set, it means event happened between 1276 * start of this trap and now. Trigger re-trap. 1277 */ 1278 if (child->jobctl & JOBCTL_TRAP_NOTIFY) 1279 ptrace_signal_wake_up(child, true); 1280 ret = 0; 1281 } 1282 unlock_task_sighand(child, &flags); 1283 break; 1284 1285 case PTRACE_DETACH: /* detach a process that was attached. */ 1286 ret = ptrace_detach(child, data); 1287 break; 1288 1289 #ifdef CONFIG_BINFMT_ELF_FDPIC 1290 case PTRACE_GETFDPIC: { 1291 struct mm_struct *mm = get_task_mm(child); 1292 unsigned long tmp = 0; 1293 1294 ret = -ESRCH; 1295 if (!mm) 1296 break; 1297 1298 switch (addr) { 1299 case PTRACE_GETFDPIC_EXEC: 1300 tmp = mm->context.exec_fdpic_loadmap; 1301 break; 1302 case PTRACE_GETFDPIC_INTERP: 1303 tmp = mm->context.interp_fdpic_loadmap; 1304 break; 1305 default: 1306 break; 1307 } 1308 mmput(mm); 1309 1310 ret = put_user(tmp, datalp); 1311 break; 1312 } 1313 #endif 1314 1315 case PTRACE_SINGLESTEP: 1316 #ifdef PTRACE_SINGLEBLOCK 1317 case PTRACE_SINGLEBLOCK: 1318 #endif 1319 #ifdef PTRACE_SYSEMU 1320 case PTRACE_SYSEMU: 1321 case PTRACE_SYSEMU_SINGLESTEP: 1322 #endif 1323 case PTRACE_SYSCALL: 1324 case PTRACE_CONT: 1325 return ptrace_resume(child, request, data); 1326 1327 case PTRACE_KILL: 1328 send_sig_info(SIGKILL, SEND_SIG_NOINFO, child); 1329 return 0; 1330 1331 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 1332 case PTRACE_GETREGSET: 1333 case PTRACE_SETREGSET: { 1334 struct iovec kiov; 1335 struct iovec __user *uiov = datavp; 1336 1337 if (!access_ok(uiov, sizeof(*uiov))) 1338 return -EFAULT; 1339 1340 if (__get_user(kiov.iov_base, &uiov->iov_base) || 1341 __get_user(kiov.iov_len, &uiov->iov_len)) 1342 return -EFAULT; 1343 1344 ret = ptrace_regset(child, request, addr, &kiov); 1345 if (!ret) 1346 ret = __put_user(kiov.iov_len, &uiov->iov_len); 1347 break; 1348 } 1349 1350 case PTRACE_GET_SYSCALL_INFO: 1351 ret = ptrace_get_syscall_info(child, addr, datavp); 1352 break; 1353 1354 case PTRACE_SET_SYSCALL_INFO: 1355 ret = ptrace_set_syscall_info(child, addr, datavp); 1356 break; 1357 #endif 1358 1359 case PTRACE_SECCOMP_GET_FILTER: 1360 ret = seccomp_get_filter(child, addr, datavp); 1361 break; 1362 1363 case PTRACE_SECCOMP_GET_METADATA: 1364 ret = seccomp_get_metadata(child, addr, datavp); 1365 break; 1366 1367 #ifdef CONFIG_RSEQ 1368 case PTRACE_GET_RSEQ_CONFIGURATION: 1369 ret = ptrace_get_rseq_configuration(child, addr, datavp); 1370 break; 1371 #endif 1372 1373 case PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG: 1374 ret = syscall_user_dispatch_set_config(child, addr, datavp); 1375 break; 1376 1377 case PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG: 1378 ret = syscall_user_dispatch_get_config(child, addr, datavp); 1379 break; 1380 1381 default: 1382 break; 1383 } 1384 1385 return ret; 1386 } 1387 1388 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 1389 unsigned long, data) 1390 { 1391 struct task_struct *child; 1392 long ret; 1393 1394 if (request == PTRACE_TRACEME) { 1395 ret = ptrace_traceme(); 1396 goto out; 1397 } 1398 1399 child = find_get_task_by_vpid(pid); 1400 if (!child) { 1401 ret = -ESRCH; 1402 goto out; 1403 } 1404 1405 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1406 ret = ptrace_attach(child, request, addr, data); 1407 goto out_put_task_struct; 1408 } 1409 1410 ret = ptrace_check_attach(child, request == PTRACE_KILL || 1411 request == PTRACE_INTERRUPT); 1412 if (ret < 0) 1413 goto out_put_task_struct; 1414 1415 ret = arch_ptrace(child, request, addr, data); 1416 if (ret || request != PTRACE_DETACH) 1417 ptrace_unfreeze_traced(child); 1418 1419 out_put_task_struct: 1420 put_task_struct(child); 1421 out: 1422 return ret; 1423 } 1424 1425 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 1426 unsigned long data) 1427 { 1428 unsigned long tmp; 1429 int copied; 1430 1431 copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE); 1432 if (copied != sizeof(tmp)) 1433 return -EIO; 1434 return put_user(tmp, (unsigned long __user *)data); 1435 } 1436 1437 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 1438 unsigned long data) 1439 { 1440 int copied; 1441 1442 copied = ptrace_access_vm(tsk, addr, &data, sizeof(data), 1443 FOLL_FORCE | FOLL_WRITE); 1444 return (copied == sizeof(data)) ? 0 : -EIO; 1445 } 1446 1447 #if defined CONFIG_COMPAT 1448 1449 int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1450 compat_ulong_t addr, compat_ulong_t data) 1451 { 1452 compat_ulong_t __user *datap = compat_ptr(data); 1453 compat_ulong_t word; 1454 kernel_siginfo_t siginfo; 1455 int ret; 1456 1457 switch (request) { 1458 case PTRACE_PEEKTEXT: 1459 case PTRACE_PEEKDATA: 1460 ret = ptrace_access_vm(child, addr, &word, sizeof(word), 1461 FOLL_FORCE); 1462 if (ret != sizeof(word)) 1463 ret = -EIO; 1464 else 1465 ret = put_user(word, datap); 1466 break; 1467 1468 case PTRACE_POKETEXT: 1469 case PTRACE_POKEDATA: 1470 ret = ptrace_access_vm(child, addr, &data, sizeof(data), 1471 FOLL_FORCE | FOLL_WRITE); 1472 ret = (ret != sizeof(data) ? -EIO : 0); 1473 break; 1474 1475 case PTRACE_GETEVENTMSG: 1476 ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1477 break; 1478 1479 case PTRACE_GETSIGINFO: 1480 ret = ptrace_getsiginfo(child, &siginfo); 1481 if (!ret) 1482 ret = copy_siginfo_to_user32( 1483 (struct compat_siginfo __user *) datap, 1484 &siginfo); 1485 break; 1486 1487 case PTRACE_SETSIGINFO: 1488 ret = copy_siginfo_from_user32( 1489 &siginfo, (struct compat_siginfo __user *) datap); 1490 if (!ret) 1491 ret = ptrace_setsiginfo(child, &siginfo); 1492 break; 1493 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 1494 case PTRACE_GETREGSET: 1495 case PTRACE_SETREGSET: 1496 { 1497 struct iovec kiov; 1498 struct compat_iovec __user *uiov = 1499 (struct compat_iovec __user *) datap; 1500 compat_uptr_t ptr; 1501 compat_size_t len; 1502 1503 if (!access_ok(uiov, sizeof(*uiov))) 1504 return -EFAULT; 1505 1506 if (__get_user(ptr, &uiov->iov_base) || 1507 __get_user(len, &uiov->iov_len)) 1508 return -EFAULT; 1509 1510 kiov.iov_base = compat_ptr(ptr); 1511 kiov.iov_len = len; 1512 1513 ret = ptrace_regset(child, request, addr, &kiov); 1514 if (!ret) 1515 ret = __put_user(kiov.iov_len, &uiov->iov_len); 1516 break; 1517 } 1518 #endif 1519 1520 default: 1521 ret = ptrace_request(child, request, addr, data); 1522 } 1523 1524 return ret; 1525 } 1526 1527 COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, 1528 compat_long_t, addr, compat_long_t, data) 1529 { 1530 struct task_struct *child; 1531 long ret; 1532 1533 if (request == PTRACE_TRACEME) { 1534 ret = ptrace_traceme(); 1535 goto out; 1536 } 1537 1538 child = find_get_task_by_vpid(pid); 1539 if (!child) { 1540 ret = -ESRCH; 1541 goto out; 1542 } 1543 1544 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1545 ret = ptrace_attach(child, request, addr, data); 1546 goto out_put_task_struct; 1547 } 1548 1549 ret = ptrace_check_attach(child, request == PTRACE_KILL || 1550 request == PTRACE_INTERRUPT); 1551 if (!ret) { 1552 ret = compat_arch_ptrace(child, request, addr, data); 1553 if (ret || request != PTRACE_DETACH) 1554 ptrace_unfreeze_traced(child); 1555 } 1556 1557 out_put_task_struct: 1558 put_task_struct(child); 1559 out: 1560 return ret; 1561 } 1562 #endif /* CONFIG_COMPAT */ 1563