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