1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/ptrace.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Common interfaces for "ptrace()" which we do not want 81da177e4SLinus Torvalds * to continually duplicate across every architecture. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11c59ede7bSRandy.Dunlap #include <linux/capability.h> 129984de1aSPaul Gortmaker #include <linux/export.h> 131da177e4SLinus Torvalds #include <linux/sched.h> 146e84f315SIngo Molnar #include <linux/sched/mm.h> 15f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 1629930025SIngo Molnar #include <linux/sched/task.h> 171da177e4SLinus Torvalds #include <linux/errno.h> 181da177e4SLinus Torvalds #include <linux/mm.h> 191da177e4SLinus Torvalds #include <linux/highmem.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/ptrace.h> 221da177e4SLinus Torvalds #include <linux/security.h> 237ed20e1aSJesper Juhl #include <linux/signal.h> 24a27bb332SKent Overstreet #include <linux/uio.h> 25a5cb013dSAl Viro #include <linux/audit.h> 26b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 27f17d30a8SAdrian Bunk #include <linux/syscalls.h> 283a709703SRoland McGrath #include <linux/uaccess.h> 292225a122SSuresh Siddha #include <linux/regset.h> 30bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 31f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h> 3284c751bdSAndrey Vagin #include <linux/compat.h> 33fcfc2aa0SAndrei Vagin #include <linux/sched/signal.h> 341da177e4SLinus Torvalds 35201766a2SElvira Khabirova #include <asm/syscall.h> /* for syscall_get_* */ 36201766a2SElvira Khabirova 3784d77d3fSEric W. Biederman /* 3884d77d3fSEric W. Biederman * Access another process' address space via ptrace. 3984d77d3fSEric W. Biederman * Source/target buffer must be kernel space, 4084d77d3fSEric W. Biederman * Do not walk the page table directly, use get_user_pages 4184d77d3fSEric W. Biederman */ 4284d77d3fSEric W. Biederman int ptrace_access_vm(struct task_struct *tsk, unsigned long addr, 4384d77d3fSEric W. Biederman void *buf, int len, unsigned int gup_flags) 4484d77d3fSEric W. Biederman { 4584d77d3fSEric W. Biederman struct mm_struct *mm; 4684d77d3fSEric W. Biederman int ret; 4784d77d3fSEric W. Biederman 4884d77d3fSEric W. Biederman mm = get_task_mm(tsk); 4984d77d3fSEric W. Biederman if (!mm) 5084d77d3fSEric W. Biederman return 0; 5184d77d3fSEric W. Biederman 5284d77d3fSEric W. Biederman if (!tsk->ptrace || 5384d77d3fSEric W. Biederman (current != tsk->parent) || 5484d77d3fSEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 5584d77d3fSEric W. Biederman !ptracer_capable(tsk, mm->user_ns))) { 5684d77d3fSEric W. Biederman mmput(mm); 5784d77d3fSEric W. Biederman return 0; 5884d77d3fSEric W. Biederman } 5984d77d3fSEric W. Biederman 6084d77d3fSEric W. Biederman ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 6184d77d3fSEric W. Biederman mmput(mm); 6284d77d3fSEric W. Biederman 6384d77d3fSEric W. Biederman return ret; 6484d77d3fSEric W. Biederman } 6584d77d3fSEric W. Biederman 66bf53de90SMarkus Metzger 67c70d9d80SEric W. Biederman void __ptrace_link(struct task_struct *child, struct task_struct *new_parent, 68c70d9d80SEric W. Biederman const struct cred *ptracer_cred) 69c70d9d80SEric W. Biederman { 70c70d9d80SEric W. Biederman BUG_ON(!list_empty(&child->ptrace_entry)); 71c70d9d80SEric W. Biederman list_add(&child->ptrace_entry, &new_parent->ptraced); 72c70d9d80SEric W. Biederman child->parent = new_parent; 73c70d9d80SEric W. Biederman child->ptracer_cred = get_cred(ptracer_cred); 74c70d9d80SEric W. Biederman } 75c70d9d80SEric W. Biederman 76bf53de90SMarkus Metzger /* 771da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 781da177e4SLinus Torvalds * move it to the ptrace list. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 811da177e4SLinus Torvalds */ 82c70d9d80SEric W. Biederman static void ptrace_link(struct task_struct *child, struct task_struct *new_parent) 831da177e4SLinus Torvalds { 846994eefbSJann Horn __ptrace_link(child, new_parent, current_cred()); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 87e3bd058fSTejun Heo /** 88e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 89e3bd058fSTejun Heo * @child: ptracee to be unlinked 901da177e4SLinus Torvalds * 910e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 920e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 930e9f0a4aSTejun Heo * state. 940e9f0a4aSTejun Heo * 950e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 960e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 970e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 980e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 990e9f0a4aSTejun Heo * 1000e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 1010e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 1020e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 1030e9f0a4aSTejun Heo * up from TASK_TRACED. 1040e9f0a4aSTejun Heo * 1050e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 1060e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 1070e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 1080e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 1090e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 1100e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 111e3bd058fSTejun Heo * 112e3bd058fSTejun Heo * CONTEXT: 113e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 1141da177e4SLinus Torvalds */ 11536c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 1161da177e4SLinus Torvalds { 11764b875f7SEric W. Biederman const struct cred *old_cred; 1185ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 1195ecfbae0SOleg Nesterov 12064c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_TRACE); 121*64eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 122*64eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_EMU); 12315532fd6SSudeep Holla #endif 1240a5bf409SAles Novak 1251da177e4SLinus Torvalds child->parent = child->real_parent; 126f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 12764b875f7SEric W. Biederman old_cred = child->ptracer_cred; 12864b875f7SEric W. Biederman child->ptracer_cred = NULL; 12964b875f7SEric W. Biederman put_cred(old_cred); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 1321333ab03SOleg Nesterov child->ptrace = 0; 1331da177e4SLinus Torvalds /* 13473ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 13573ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 13673ddff2bSTejun Heo */ 13773ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 13873ddff2bSTejun Heo task_clear_jobctl_trapping(child); 13973ddff2bSTejun Heo 14073ddff2bSTejun Heo /* 141a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 1420e9f0a4aSTejun Heo * @child isn't dead. 1431da177e4SLinus Torvalds */ 1440e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 1450e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 1468a88951bSOleg Nesterov child->signal->group_stop_count)) { 147a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1480e9f0a4aSTejun Heo 1490e9f0a4aSTejun Heo /* 1508a88951bSOleg Nesterov * This is only possible if this thread was cloned by the 1518a88951bSOleg Nesterov * traced task running in the stopped group, set the signal 1528a88951bSOleg Nesterov * for the future reports. 1538a88951bSOleg Nesterov * FIXME: we should change ptrace_init_task() to handle this 1548a88951bSOleg Nesterov * case. 1558a88951bSOleg Nesterov */ 1568a88951bSOleg Nesterov if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) 1578a88951bSOleg Nesterov child->jobctl |= SIGSTOP; 1588a88951bSOleg Nesterov } 1598a88951bSOleg Nesterov 1608a88951bSOleg Nesterov /* 1610e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1620e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1630e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1640e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1650e9f0a4aSTejun Heo */ 166a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 167910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1680e9f0a4aSTejun Heo 1691da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1729899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */ 1739899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task) 1749899d11fSOleg Nesterov { 1759899d11fSOleg Nesterov bool ret = false; 1769899d11fSOleg Nesterov 1779899d11fSOleg Nesterov /* Lockless, nobody but us can set this flag */ 1789899d11fSOleg Nesterov if (task->jobctl & JOBCTL_LISTENING) 1799899d11fSOleg Nesterov return ret; 1809899d11fSOleg Nesterov 1819899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1829899d11fSOleg Nesterov if (task_is_traced(task) && !__fatal_signal_pending(task)) { 1839899d11fSOleg Nesterov task->state = __TASK_TRACED; 1849899d11fSOleg Nesterov ret = true; 1859899d11fSOleg Nesterov } 1869899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1879899d11fSOleg Nesterov 1889899d11fSOleg Nesterov return ret; 1899899d11fSOleg Nesterov } 1909899d11fSOleg Nesterov 1919899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task) 1929899d11fSOleg Nesterov { 1939899d11fSOleg Nesterov if (task->state != __TASK_TRACED) 1949899d11fSOleg Nesterov return; 1959899d11fSOleg Nesterov 1969899d11fSOleg Nesterov WARN_ON(!task->ptrace || task->parent != current); 1979899d11fSOleg Nesterov 1985402e97aSbsegall@google.com /* 1995402e97aSbsegall@google.com * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely. 2005402e97aSbsegall@google.com * Recheck state under the lock to close this race. 2015402e97aSbsegall@google.com */ 2029899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 2035402e97aSbsegall@google.com if (task->state == __TASK_TRACED) { 2049899d11fSOleg Nesterov if (__fatal_signal_pending(task)) 2059899d11fSOleg Nesterov wake_up_state(task, __TASK_TRACED); 2069899d11fSOleg Nesterov else 2079899d11fSOleg Nesterov task->state = TASK_TRACED; 2085402e97aSbsegall@google.com } 2099899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 2109899d11fSOleg Nesterov } 2119899d11fSOleg Nesterov 212755e276bSTejun Heo /** 213755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 214755e276bSTejun Heo * @child: ptracee to check for 215755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 216755e276bSTejun Heo * 217755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 218755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 219755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 220755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 221755e276bSTejun Heo * state. 222755e276bSTejun Heo * 223755e276bSTejun Heo * CONTEXT: 224755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 225755e276bSTejun Heo * 226755e276bSTejun Heo * RETURNS: 227755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 2281da177e4SLinus Torvalds */ 229edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds int ret = -ESRCH; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* 2341da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 2351da177e4SLinus Torvalds * possible race where someone else was tracing our child and 2361da177e4SLinus Torvalds * detached between these two checks. After this locked check, 2371da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 2381da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 2391da177e4SLinus Torvalds */ 2401da177e4SLinus Torvalds read_lock(&tasklist_lock); 2419899d11fSOleg Nesterov if (child->ptrace && child->parent == current) { 2429899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 243c0c0b649SOleg Nesterov /* 244c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 245c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 246c0c0b649SOleg Nesterov */ 2479899d11fSOleg Nesterov if (ignore_state || ptrace_freeze_traced(child)) 248321fb561SOleg Nesterov ret = 0; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds read_unlock(&tasklist_lock); 2511da177e4SLinus Torvalds 2529899d11fSOleg Nesterov if (!ret && !ignore_state) { 2539899d11fSOleg Nesterov if (!wait_task_inactive(child, __TASK_TRACED)) { 2549899d11fSOleg Nesterov /* 2559899d11fSOleg Nesterov * This can only happen if may_ptrace_stop() fails and 2569899d11fSOleg Nesterov * ptrace_stop() changes ->state back to TASK_RUNNING, 2579899d11fSOleg Nesterov * so we should not worry about leaking __TASK_TRACED. 2589899d11fSOleg Nesterov */ 2599899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 2609899d11fSOleg Nesterov ret = -ESRCH; 2619899d11fSOleg Nesterov } 2629899d11fSOleg Nesterov } 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds return ret; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2676b3ad664SChristian Brauner static bool ptrace_has_cap(const struct cred *cred, struct user_namespace *ns, 2686b3ad664SChristian Brauner unsigned int mode) 26969f594a3SEric Paris { 2706b3ad664SChristian Brauner int ret; 2716b3ad664SChristian Brauner 27269f594a3SEric Paris if (mode & PTRACE_MODE_NOAUDIT) 2736b3ad664SChristian Brauner ret = security_capable(cred, ns, CAP_SYS_PTRACE, CAP_OPT_NOAUDIT); 27469f594a3SEric Paris else 2756b3ad664SChristian Brauner ret = security_capable(cred, ns, CAP_SYS_PTRACE, CAP_OPT_NONE); 2766b3ad664SChristian Brauner 2776b3ad664SChristian Brauner return ret == 0; 27869f594a3SEric Paris } 27969f594a3SEric Paris 2809f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */ 2819f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 282ab8d11beSMiklos Szeredi { 283c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 284bfedb589SEric W. Biederman struct mm_struct *mm; 285caaee623SJann Horn kuid_t caller_uid; 286caaee623SJann Horn kgid_t caller_gid; 287caaee623SJann Horn 288caaee623SJann Horn if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { 289caaee623SJann Horn WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); 290caaee623SJann Horn return -EPERM; 291caaee623SJann Horn } 292b6dff3ecSDavid Howells 293df26c40eSEric W. Biederman /* May we inspect the given task? 294df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 295df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 296df26c40eSEric W. Biederman * 297df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 298df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 299df26c40eSEric W. Biederman * or halting the specified task is impossible. 300df26c40eSEric W. Biederman */ 301caaee623SJann Horn 302df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 30373af963fSMark Grondona if (same_thread_group(task, current)) 304df26c40eSEric W. Biederman return 0; 305c69e8d9cSDavid Howells rcu_read_lock(); 306caaee623SJann Horn if (mode & PTRACE_MODE_FSCREDS) { 307caaee623SJann Horn caller_uid = cred->fsuid; 308caaee623SJann Horn caller_gid = cred->fsgid; 309caaee623SJann Horn } else { 310caaee623SJann Horn /* 311caaee623SJann Horn * Using the euid would make more sense here, but something 312caaee623SJann Horn * in userland might rely on the old behavior, and this 313caaee623SJann Horn * shouldn't be a security problem since 314caaee623SJann Horn * PTRACE_MODE_REALCREDS implies that the caller explicitly 315caaee623SJann Horn * used a syscall that requests access to another process 316caaee623SJann Horn * (and not a filesystem syscall to procfs). 317caaee623SJann Horn */ 318caaee623SJann Horn caller_uid = cred->uid; 319caaee623SJann Horn caller_gid = cred->gid; 320caaee623SJann Horn } 321c69e8d9cSDavid Howells tcred = __task_cred(task); 322caaee623SJann Horn if (uid_eq(caller_uid, tcred->euid) && 323caaee623SJann Horn uid_eq(caller_uid, tcred->suid) && 324caaee623SJann Horn uid_eq(caller_uid, tcred->uid) && 325caaee623SJann Horn gid_eq(caller_gid, tcred->egid) && 326caaee623SJann Horn gid_eq(caller_gid, tcred->sgid) && 327caaee623SJann Horn gid_eq(caller_gid, tcred->gid)) 3288409cca7SSerge E. Hallyn goto ok; 3296b3ad664SChristian Brauner if (ptrace_has_cap(cred, tcred->user_ns, mode)) 3308409cca7SSerge E. Hallyn goto ok; 331c69e8d9cSDavid Howells rcu_read_unlock(); 332ab8d11beSMiklos Szeredi return -EPERM; 3338409cca7SSerge E. Hallyn ok: 334c69e8d9cSDavid Howells rcu_read_unlock(); 335f6581f5bSJann Horn /* 336f6581f5bSJann Horn * If a task drops privileges and becomes nondumpable (through a syscall 337f6581f5bSJann Horn * like setresuid()) while we are trying to access it, we must ensure 338f6581f5bSJann Horn * that the dumpability is read after the credentials; otherwise, 339f6581f5bSJann Horn * we may be able to attach to a task that we shouldn't be able to 340f6581f5bSJann Horn * attach to (as if the task had dropped privileges without becoming 341f6581f5bSJann Horn * nondumpable). 342f6581f5bSJann Horn * Pairs with a write barrier in commit_creds(). 343f6581f5bSJann Horn */ 344f6581f5bSJann Horn smp_rmb(); 345bfedb589SEric W. Biederman mm = task->mm; 346bfedb589SEric W. Biederman if (mm && 347bfedb589SEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 3486b3ad664SChristian Brauner !ptrace_has_cap(cred, mm->user_ns, mode))) 349ab8d11beSMiklos Szeredi return -EPERM; 350ab8d11beSMiklos Szeredi 3519e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 352ab8d11beSMiklos Szeredi } 353ab8d11beSMiklos Szeredi 354006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 355ab8d11beSMiklos Szeredi { 356ab8d11beSMiklos Szeredi int err; 357ab8d11beSMiklos Szeredi task_lock(task); 358006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 359ab8d11beSMiklos Szeredi task_unlock(task); 3603a709703SRoland McGrath return !err; 361ab8d11beSMiklos Szeredi } 362ab8d11beSMiklos Szeredi 3633544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 364aa9147c9SDenys Vlasenko unsigned long addr, 3653544d72aSTejun Heo unsigned long flags) 3661da177e4SLinus Torvalds { 3673544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 3681da177e4SLinus Torvalds int retval; 369f5b40e36SLinus Torvalds 3703544d72aSTejun Heo retval = -EIO; 371aa9147c9SDenys Vlasenko if (seize) { 372aa9147c9SDenys Vlasenko if (addr != 0) 3733544d72aSTejun Heo goto out; 374aa9147c9SDenys Vlasenko if (flags & ~(unsigned long)PTRACE_O_MASK) 375aa9147c9SDenys Vlasenko goto out; 376aa9147c9SDenys Vlasenko flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 377aa9147c9SDenys Vlasenko } else { 378aa9147c9SDenys Vlasenko flags = PT_PTRACED; 379aa9147c9SDenys Vlasenko } 3803544d72aSTejun Heo 381a5cb013dSAl Viro audit_ptrace(task); 382a5cb013dSAl Viro 3831da177e4SLinus Torvalds retval = -EPERM; 384b79b7ba9SOleg Nesterov if (unlikely(task->flags & PF_KTHREAD)) 385b79b7ba9SOleg Nesterov goto out; 386bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 387f5b40e36SLinus Torvalds goto out; 388f5b40e36SLinus Torvalds 389f2f0b00aSOleg Nesterov /* 390f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 39186b6c1f3SDenys Vlasenko * SUID, SGID and LSM creds get determined differently 3925e751e99SDavid Howells * under ptrace. 393d84f4f99SDavid Howells */ 394793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 3959b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 396d84f4f99SDavid Howells goto out; 3974b105cbbSOleg Nesterov 398f5b40e36SLinus Torvalds task_lock(task); 399caaee623SJann Horn retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); 4004b105cbbSOleg Nesterov task_unlock(task); 4011da177e4SLinus Torvalds if (retval) 4024b105cbbSOleg Nesterov goto unlock_creds; 4031da177e4SLinus Torvalds 4044b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 405b79b7ba9SOleg Nesterov retval = -EPERM; 406b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 4074b105cbbSOleg Nesterov goto unlock_tasklist; 408f2f0b00aSOleg Nesterov if (task->ptrace) 4094b105cbbSOleg Nesterov goto unlock_tasklist; 410b79b7ba9SOleg Nesterov 4113544d72aSTejun Heo if (seize) 412aa9147c9SDenys Vlasenko flags |= PT_SEIZED; 413aa9147c9SDenys Vlasenko task->ptrace = flags; 4141da177e4SLinus Torvalds 415c70d9d80SEric W. Biederman ptrace_link(task, current); 4163544d72aSTejun Heo 4173544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 4183544d72aSTejun Heo if (!seize) 419079b22dcSEric W. Biederman send_sig_info(SIGSTOP, SEND_SIG_PRIV, task); 420b79b7ba9SOleg Nesterov 421d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 422d79fdd6dSTejun Heo 423d79fdd6dSTejun Heo /* 42473ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 425d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 426d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 427d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 428d79fdd6dSTejun Heo * for the transition to complete before returning from this 429d79fdd6dSTejun Heo * function. 430d79fdd6dSTejun Heo * 431d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 432d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 433d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 434d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 435d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 436d79fdd6dSTejun Heo * 437d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 438d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 439d79fdd6dSTejun Heo */ 4407dd3db54STejun Heo if (task_is_stopped(task) && 44173ddff2bSTejun Heo task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) 442910ffdb1SOleg Nesterov signal_wake_up_state(task, __TASK_STOPPED); 443d79fdd6dSTejun Heo 444d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 445d79fdd6dSTejun Heo 446b79b7ba9SOleg Nesterov retval = 0; 4474b105cbbSOleg Nesterov unlock_tasklist: 4484b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4494b105cbbSOleg Nesterov unlock_creds: 4509b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 451f5b40e36SLinus Torvalds out: 452f701e5b7SVladimir Zapolskiy if (!retval) { 4537c3b00e0SOleg Nesterov /* 4547c3b00e0SOleg Nesterov * We do not bother to change retval or clear JOBCTL_TRAPPING 4557c3b00e0SOleg Nesterov * if wait_on_bit() was interrupted by SIGKILL. The tracer will 4567c3b00e0SOleg Nesterov * not return to user-mode, it will exit and clear this bit in 4577c3b00e0SOleg Nesterov * __ptrace_unlink() if it wasn't already cleared by the tracee; 4587c3b00e0SOleg Nesterov * and until then nobody can ptrace this task. 4597c3b00e0SOleg Nesterov */ 4607c3b00e0SOleg Nesterov wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE); 461f701e5b7SVladimir Zapolskiy proc_ptrace_connector(task, PTRACE_ATTACH); 462f701e5b7SVladimir Zapolskiy } 463f701e5b7SVladimir Zapolskiy 4641da177e4SLinus Torvalds return retval; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 467f2f0b00aSOleg Nesterov /** 468f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 469f2f0b00aSOleg Nesterov * 470f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 471f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 472f2f0b00aSOleg Nesterov */ 473e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 474f2f0b00aSOleg Nesterov { 475f2f0b00aSOleg Nesterov int ret = -EPERM; 476f2f0b00aSOleg Nesterov 4774b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 4784b105cbbSOleg Nesterov /* Are we already being traced? */ 479f2f0b00aSOleg Nesterov if (!current->ptrace) { 480f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 481f2f0b00aSOleg Nesterov /* 482f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 483f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 484f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 485f2f0b00aSOleg Nesterov */ 486f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 487f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 488c70d9d80SEric W. Biederman ptrace_link(current, current->real_parent); 489f2f0b00aSOleg Nesterov } 490f2f0b00aSOleg Nesterov } 4914b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4924b105cbbSOleg Nesterov 493f2f0b00aSOleg Nesterov return ret; 494f2f0b00aSOleg Nesterov } 495f2f0b00aSOleg Nesterov 49639c626aeSOleg Nesterov /* 49739c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 49839c626aeSOleg Nesterov */ 49939c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 50039c626aeSOleg Nesterov { 50139c626aeSOleg Nesterov int ret; 50239c626aeSOleg Nesterov spin_lock(&sigh->siglock); 50339c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 50439c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 50539c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 50639c626aeSOleg Nesterov return ret; 50739c626aeSOleg Nesterov } 50839c626aeSOleg Nesterov 50939c626aeSOleg Nesterov /* 51039c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 51139c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 51239c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 51339c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 51439c626aeSOleg Nesterov * 51539c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 51639c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 51739c626aeSOleg Nesterov * If it should reap itself, return true. 51839c626aeSOleg Nesterov * 519a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 520a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 521a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 522a7f0765eSOleg Nesterov * do_wait(). 52339c626aeSOleg Nesterov */ 52439c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 52539c626aeSOleg Nesterov { 5269843a1e9SOleg Nesterov bool dead; 5279843a1e9SOleg Nesterov 52839c626aeSOleg Nesterov __ptrace_unlink(p); 52939c626aeSOleg Nesterov 5309843a1e9SOleg Nesterov if (p->exit_state != EXIT_ZOMBIE) 5319843a1e9SOleg Nesterov return false; 5329843a1e9SOleg Nesterov 5339843a1e9SOleg Nesterov dead = !thread_group_leader(p); 5349843a1e9SOleg Nesterov 5359843a1e9SOleg Nesterov if (!dead && thread_group_empty(p)) { 53639c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 5379843a1e9SOleg Nesterov dead = do_notify_parent(p, p->exit_signal); 538a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 539a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 5409843a1e9SOleg Nesterov dead = true; 54139c626aeSOleg Nesterov } 542a7f0765eSOleg Nesterov } 54339c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 5449843a1e9SOleg Nesterov if (dead) 54539c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 5469843a1e9SOleg Nesterov return dead; 54739c626aeSOleg Nesterov } 54839c626aeSOleg Nesterov 549e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 5501da177e4SLinus Torvalds { 5517ed20e1aSJesper Juhl if (!valid_signal(data)) 5521da177e4SLinus Torvalds return -EIO; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 5551da177e4SLinus Torvalds ptrace_disable(child); 5561da177e4SLinus Torvalds 55795c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 55839c626aeSOleg Nesterov /* 55964a4096cSOleg Nesterov * We rely on ptrace_freeze_traced(). It can't be killed and 56064a4096cSOleg Nesterov * untraced by another thread, it can't be a zombie. 56139c626aeSOleg Nesterov */ 56264a4096cSOleg Nesterov WARN_ON(!child->ptrace || child->exit_state); 56364a4096cSOleg Nesterov /* 56464a4096cSOleg Nesterov * tasklist_lock avoids the race with wait_task_stopped(), see 56564a4096cSOleg Nesterov * the comment in ptrace_resume(). 56664a4096cSOleg Nesterov */ 56795c3eb76SOleg Nesterov child->exit_code = data; 56864a4096cSOleg Nesterov __ptrace_detach(current, child); 5691da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 5701da177e4SLinus Torvalds 571f701e5b7SVladimir Zapolskiy proc_ptrace_connector(child, PTRACE_DETACH); 5724576145cSOleg Nesterov 5731da177e4SLinus Torvalds return 0; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 57639c626aeSOleg Nesterov /* 577c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 5787c8bd232SOleg Nesterov * for writing. 57939c626aeSOleg Nesterov */ 5807c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead) 58139c626aeSOleg Nesterov { 58239c626aeSOleg Nesterov struct task_struct *p, *n; 583c7e49c14SOleg Nesterov 58439c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 585992fb6e1SOleg Nesterov if (unlikely(p->ptrace & PT_EXITKILL)) 586079b22dcSEric W. Biederman send_sig_info(SIGKILL, SEND_SIG_PRIV, p); 587992fb6e1SOleg Nesterov 58839c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 5897c8bd232SOleg Nesterov list_add(&p->ptrace_entry, dead); 59039c626aeSOleg Nesterov } 59139c626aeSOleg Nesterov } 59239c626aeSOleg Nesterov 5931da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds int copied = 0; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds while (len > 0) { 5981da177e4SLinus Torvalds char buf[128]; 5991da177e4SLinus Torvalds int this_len, retval; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 60284d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE); 60384d77d3fSEric W. Biederman 6041da177e4SLinus Torvalds if (!retval) { 6051da177e4SLinus Torvalds if (copied) 6061da177e4SLinus Torvalds break; 6071da177e4SLinus Torvalds return -EIO; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 6101da177e4SLinus Torvalds return -EFAULT; 6111da177e4SLinus Torvalds copied += retval; 6121da177e4SLinus Torvalds src += retval; 6131da177e4SLinus Torvalds dst += retval; 6141da177e4SLinus Torvalds len -= retval; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds return copied; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds int copied = 0; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds while (len > 0) { 6241da177e4SLinus Torvalds char buf[128]; 6251da177e4SLinus Torvalds int this_len, retval; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 6281da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 6291da177e4SLinus Torvalds return -EFAULT; 63084d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, dst, buf, this_len, 631f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 6321da177e4SLinus Torvalds if (!retval) { 6331da177e4SLinus Torvalds if (copied) 6341da177e4SLinus Torvalds break; 6351da177e4SLinus Torvalds return -EIO; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds copied += retval; 6381da177e4SLinus Torvalds src += retval; 6391da177e4SLinus Torvalds dst += retval; 6401da177e4SLinus Torvalds len -= retval; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds return copied; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6454abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 6461da177e4SLinus Torvalds { 64786b6c1f3SDenys Vlasenko unsigned flags; 64886b6c1f3SDenys Vlasenko 6498c5cf9e5SDenys Vlasenko if (data & ~(unsigned long)PTRACE_O_MASK) 6508c5cf9e5SDenys Vlasenko return -EINVAL; 6518c5cf9e5SDenys Vlasenko 65213c4a901STycho Andersen if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { 65397f2645fSMasahiro Yamada if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) || 65497f2645fSMasahiro Yamada !IS_ENABLED(CONFIG_SECCOMP)) 65513c4a901STycho Andersen return -EINVAL; 65613c4a901STycho Andersen 65713c4a901STycho Andersen if (!capable(CAP_SYS_ADMIN)) 65813c4a901STycho Andersen return -EPERM; 65913c4a901STycho Andersen 66013c4a901STycho Andersen if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || 66113c4a901STycho Andersen current->ptrace & PT_SUSPEND_SECCOMP) 66213c4a901STycho Andersen return -EPERM; 66313c4a901STycho Andersen } 66413c4a901STycho Andersen 66586b6c1f3SDenys Vlasenko /* Avoid intermediate state when all opts are cleared */ 66686b6c1f3SDenys Vlasenko flags = child->ptrace; 66786b6c1f3SDenys Vlasenko flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 66886b6c1f3SDenys Vlasenko flags |= (data << PT_OPT_FLAG_SHIFT); 66986b6c1f3SDenys Vlasenko child->ptrace = flags; 6701da177e4SLinus Torvalds 6718c5cf9e5SDenys Vlasenko return 0; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 674ae7795bcSEric W. Biederman static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info) 6751da177e4SLinus Torvalds { 676e4961254SOleg Nesterov unsigned long flags; 6771da177e4SLinus Torvalds int error = -ESRCH; 6781da177e4SLinus Torvalds 679e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6801da177e4SLinus Torvalds error = -EINVAL; 6811da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 6820752d7bfSEric W. Biederman copy_siginfo(info, child->last_siginfo); 6831da177e4SLinus Torvalds error = 0; 6841da177e4SLinus Torvalds } 685e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds return error; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 690ae7795bcSEric W. Biederman static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info) 6911da177e4SLinus Torvalds { 692e4961254SOleg Nesterov unsigned long flags; 6931da177e4SLinus Torvalds int error = -ESRCH; 6941da177e4SLinus Torvalds 695e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6961da177e4SLinus Torvalds error = -EINVAL; 6971da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 6980752d7bfSEric W. Biederman copy_siginfo(child->last_siginfo, info); 6991da177e4SLinus Torvalds error = 0; 7001da177e4SLinus Torvalds } 701e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds return error; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 70684c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child, 70784c751bdSAndrey Vagin unsigned long addr, 70884c751bdSAndrey Vagin unsigned long data) 70984c751bdSAndrey Vagin { 71084c751bdSAndrey Vagin struct ptrace_peeksiginfo_args arg; 71184c751bdSAndrey Vagin struct sigpending *pending; 71284c751bdSAndrey Vagin struct sigqueue *q; 71384c751bdSAndrey Vagin int ret, i; 71484c751bdSAndrey Vagin 71584c751bdSAndrey Vagin ret = copy_from_user(&arg, (void __user *) addr, 71684c751bdSAndrey Vagin sizeof(struct ptrace_peeksiginfo_args)); 71784c751bdSAndrey Vagin if (ret) 71884c751bdSAndrey Vagin return -EFAULT; 71984c751bdSAndrey Vagin 72084c751bdSAndrey Vagin if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 72184c751bdSAndrey Vagin return -EINVAL; /* unknown flags */ 72284c751bdSAndrey Vagin 72384c751bdSAndrey Vagin if (arg.nr < 0) 72484c751bdSAndrey Vagin return -EINVAL; 72584c751bdSAndrey Vagin 726f6e2aa91SEric W. Biederman /* Ensure arg.off fits in an unsigned long */ 727f6e2aa91SEric W. Biederman if (arg.off > ULONG_MAX) 728f6e2aa91SEric W. Biederman return 0; 729f6e2aa91SEric W. Biederman 73084c751bdSAndrey Vagin if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 73184c751bdSAndrey Vagin pending = &child->signal->shared_pending; 73284c751bdSAndrey Vagin else 73384c751bdSAndrey Vagin pending = &child->pending; 73484c751bdSAndrey Vagin 73584c751bdSAndrey Vagin for (i = 0; i < arg.nr; ) { 736ae7795bcSEric W. Biederman kernel_siginfo_t info; 737f6e2aa91SEric W. Biederman unsigned long off = arg.off + i; 738f6e2aa91SEric W. Biederman bool found = false; 73984c751bdSAndrey Vagin 74084c751bdSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 74184c751bdSAndrey Vagin list_for_each_entry(q, &pending->list, list) { 74284c751bdSAndrey Vagin if (!off--) { 743f6e2aa91SEric W. Biederman found = true; 74484c751bdSAndrey Vagin copy_siginfo(&info, &q->info); 74584c751bdSAndrey Vagin break; 74684c751bdSAndrey Vagin } 74784c751bdSAndrey Vagin } 74884c751bdSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 74984c751bdSAndrey Vagin 750f6e2aa91SEric W. Biederman if (!found) /* beyond the end of the list */ 75184c751bdSAndrey Vagin break; 75284c751bdSAndrey Vagin 75384c751bdSAndrey Vagin #ifdef CONFIG_COMPAT 7545c465217SAndy Lutomirski if (unlikely(in_compat_syscall())) { 75584c751bdSAndrey Vagin compat_siginfo_t __user *uinfo = compat_ptr(data); 75684c751bdSAndrey Vagin 757cc731525SEric W. Biederman if (copy_siginfo_to_user32(uinfo, &info)) { 758706b23bdSMathieu Desnoyers ret = -EFAULT; 759706b23bdSMathieu Desnoyers break; 760706b23bdSMathieu Desnoyers } 761706b23bdSMathieu Desnoyers 76284c751bdSAndrey Vagin } else 76384c751bdSAndrey Vagin #endif 76484c751bdSAndrey Vagin { 76584c751bdSAndrey Vagin siginfo_t __user *uinfo = (siginfo_t __user *) data; 76684c751bdSAndrey Vagin 767cc731525SEric W. Biederman if (copy_siginfo_to_user(uinfo, &info)) { 76884c751bdSAndrey Vagin ret = -EFAULT; 76984c751bdSAndrey Vagin break; 77084c751bdSAndrey Vagin } 771706b23bdSMathieu Desnoyers } 77284c751bdSAndrey Vagin 77384c751bdSAndrey Vagin data += sizeof(siginfo_t); 77484c751bdSAndrey Vagin i++; 77584c751bdSAndrey Vagin 77684c751bdSAndrey Vagin if (signal_pending(current)) 77784c751bdSAndrey Vagin break; 77884c751bdSAndrey Vagin 77984c751bdSAndrey Vagin cond_resched(); 78084c751bdSAndrey Vagin } 78184c751bdSAndrey Vagin 78284c751bdSAndrey Vagin if (i > 0) 78384c751bdSAndrey Vagin return i; 78484c751bdSAndrey Vagin 78584c751bdSAndrey Vagin return ret; 78684c751bdSAndrey Vagin } 78736df29d7SRoland McGrath 78836df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 78936df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 79036df29d7SRoland McGrath #else 79136df29d7SRoland McGrath #define is_singlestep(request) 0 79236df29d7SRoland McGrath #endif 79336df29d7SRoland McGrath 7945b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 7955b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 7965b88abbfSRoland McGrath #else 7975b88abbfSRoland McGrath #define is_singleblock(request) 0 7985b88abbfSRoland McGrath #endif 7995b88abbfSRoland McGrath 80036df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 80136df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 80236df29d7SRoland McGrath #else 80336df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 80436df29d7SRoland McGrath #endif 80536df29d7SRoland McGrath 8064abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 8074abf9869SNamhyung Kim unsigned long data) 80836df29d7SRoland McGrath { 809b72c1869SOleg Nesterov bool need_siglock; 810b72c1869SOleg Nesterov 81136df29d7SRoland McGrath if (!valid_signal(data)) 81236df29d7SRoland McGrath return -EIO; 81336df29d7SRoland McGrath 81436df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 81564c19ba2SGabriel Krisman Bertazi set_task_syscall_work(child, SYSCALL_TRACE); 81636df29d7SRoland McGrath else 81764c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_TRACE); 81836df29d7SRoland McGrath 819*64eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 82036df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 821*64eb35f7SGabriel Krisman Bertazi set_task_syscall_work(child, SYSCALL_EMU); 82236df29d7SRoland McGrath else 823*64eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_EMU); 82436df29d7SRoland McGrath #endif 82536df29d7SRoland McGrath 8265b88abbfSRoland McGrath if (is_singleblock(request)) { 8275b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 8285b88abbfSRoland McGrath return -EIO; 8295b88abbfSRoland McGrath user_enable_block_step(child); 8305b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 83136df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 83236df29d7SRoland McGrath return -EIO; 83336df29d7SRoland McGrath user_enable_single_step(child); 8343a709703SRoland McGrath } else { 83536df29d7SRoland McGrath user_disable_single_step(child); 8363a709703SRoland McGrath } 83736df29d7SRoland McGrath 838b72c1869SOleg Nesterov /* 839b72c1869SOleg Nesterov * Change ->exit_code and ->state under siglock to avoid the race 840b72c1869SOleg Nesterov * with wait_task_stopped() in between; a non-zero ->exit_code will 841b72c1869SOleg Nesterov * wrongly look like another report from tracee. 842b72c1869SOleg Nesterov * 843b72c1869SOleg Nesterov * Note that we need siglock even if ->exit_code == data and/or this 844b72c1869SOleg Nesterov * status was not reported yet, the new status must not be cleared by 845b72c1869SOleg Nesterov * wait_task_stopped() after resume. 846b72c1869SOleg Nesterov * 847b72c1869SOleg Nesterov * If data == 0 we do not care if wait_task_stopped() reports the old 848b72c1869SOleg Nesterov * status and clears the code too; this can't race with the tracee, it 849b72c1869SOleg Nesterov * takes siglock after resume. 850b72c1869SOleg Nesterov */ 851b72c1869SOleg Nesterov need_siglock = data && !thread_group_empty(current); 852b72c1869SOleg Nesterov if (need_siglock) 853b72c1869SOleg Nesterov spin_lock_irq(&child->sighand->siglock); 85436df29d7SRoland McGrath child->exit_code = data; 8550666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 856b72c1869SOleg Nesterov if (need_siglock) 857b72c1869SOleg Nesterov spin_unlock_irq(&child->sighand->siglock); 85836df29d7SRoland McGrath 85936df29d7SRoland McGrath return 0; 86036df29d7SRoland McGrath } 86136df29d7SRoland McGrath 8622225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 8632225a122SSuresh Siddha 8642225a122SSuresh Siddha static const struct user_regset * 8652225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 8662225a122SSuresh Siddha { 8672225a122SSuresh Siddha const struct user_regset *regset; 8682225a122SSuresh Siddha int n; 8692225a122SSuresh Siddha 8702225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 8712225a122SSuresh Siddha regset = view->regsets + n; 8722225a122SSuresh Siddha if (regset->core_note_type == type) 8732225a122SSuresh Siddha return regset; 8742225a122SSuresh Siddha } 8752225a122SSuresh Siddha 8762225a122SSuresh Siddha return NULL; 8772225a122SSuresh Siddha } 8782225a122SSuresh Siddha 8792225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 8802225a122SSuresh Siddha struct iovec *kiov) 8812225a122SSuresh Siddha { 8822225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 8832225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 8842225a122SSuresh Siddha int regset_no; 8852225a122SSuresh Siddha 8862225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 887c6a0dd7eSSuresh Siddha return -EINVAL; 8882225a122SSuresh Siddha 8892225a122SSuresh Siddha regset_no = regset - view->regsets; 8902225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 8912225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 8922225a122SSuresh Siddha 8932225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 8942225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 8952225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8962225a122SSuresh Siddha else 8972225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 8982225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8992225a122SSuresh Siddha } 9002225a122SSuresh Siddha 901e8440c14SJosh Stone /* 902e8440c14SJosh Stone * This is declared in linux/regset.h and defined in machine-dependent 903e8440c14SJosh Stone * code. We put the export here, near the primary machine-neutral use, 904e8440c14SJosh Stone * to ensure no machine forgets it. 905e8440c14SJosh Stone */ 906e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view); 907201766a2SElvira Khabirova 908201766a2SElvira Khabirova static unsigned long 909201766a2SElvira Khabirova ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs, 910201766a2SElvira Khabirova struct ptrace_syscall_info *info) 911201766a2SElvira Khabirova { 912201766a2SElvira Khabirova unsigned long args[ARRAY_SIZE(info->entry.args)]; 913201766a2SElvira Khabirova int i; 914201766a2SElvira Khabirova 915201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_ENTRY; 916201766a2SElvira Khabirova info->entry.nr = syscall_get_nr(child, regs); 917201766a2SElvira Khabirova syscall_get_arguments(child, regs, args); 918201766a2SElvira Khabirova for (i = 0; i < ARRAY_SIZE(args); i++) 919201766a2SElvira Khabirova info->entry.args[i] = args[i]; 920201766a2SElvira Khabirova 921201766a2SElvira Khabirova /* args is the last field in struct ptrace_syscall_info.entry */ 922201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, entry.args); 923201766a2SElvira Khabirova } 924201766a2SElvira Khabirova 925201766a2SElvira Khabirova static unsigned long 926201766a2SElvira Khabirova ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs, 927201766a2SElvira Khabirova struct ptrace_syscall_info *info) 928201766a2SElvira Khabirova { 929201766a2SElvira Khabirova /* 930201766a2SElvira Khabirova * As struct ptrace_syscall_info.entry is currently a subset 931201766a2SElvira Khabirova * of struct ptrace_syscall_info.seccomp, it makes sense to 932201766a2SElvira Khabirova * initialize that subset using ptrace_get_syscall_info_entry(). 933201766a2SElvira Khabirova * This can be reconsidered in the future if these structures 934201766a2SElvira Khabirova * diverge significantly enough. 935201766a2SElvira Khabirova */ 936201766a2SElvira Khabirova ptrace_get_syscall_info_entry(child, regs, info); 937201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_SECCOMP; 938201766a2SElvira Khabirova info->seccomp.ret_data = child->ptrace_message; 939201766a2SElvira Khabirova 940201766a2SElvira Khabirova /* ret_data is the last field in struct ptrace_syscall_info.seccomp */ 941201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, seccomp.ret_data); 942201766a2SElvira Khabirova } 943201766a2SElvira Khabirova 944201766a2SElvira Khabirova static unsigned long 945201766a2SElvira Khabirova ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs, 946201766a2SElvira Khabirova struct ptrace_syscall_info *info) 947201766a2SElvira Khabirova { 948201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_EXIT; 949201766a2SElvira Khabirova info->exit.rval = syscall_get_error(child, regs); 950201766a2SElvira Khabirova info->exit.is_error = !!info->exit.rval; 951201766a2SElvira Khabirova if (!info->exit.is_error) 952201766a2SElvira Khabirova info->exit.rval = syscall_get_return_value(child, regs); 953201766a2SElvira Khabirova 954201766a2SElvira Khabirova /* is_error is the last field in struct ptrace_syscall_info.exit */ 955201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, exit.is_error); 956201766a2SElvira Khabirova } 957201766a2SElvira Khabirova 958201766a2SElvira Khabirova static int 959201766a2SElvira Khabirova ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size, 960201766a2SElvira Khabirova void __user *datavp) 961201766a2SElvira Khabirova { 962201766a2SElvira Khabirova struct pt_regs *regs = task_pt_regs(child); 963201766a2SElvira Khabirova struct ptrace_syscall_info info = { 964201766a2SElvira Khabirova .op = PTRACE_SYSCALL_INFO_NONE, 965201766a2SElvira Khabirova .arch = syscall_get_arch(child), 966201766a2SElvira Khabirova .instruction_pointer = instruction_pointer(regs), 967201766a2SElvira Khabirova .stack_pointer = user_stack_pointer(regs), 968201766a2SElvira Khabirova }; 969201766a2SElvira Khabirova unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry); 970201766a2SElvira Khabirova unsigned long write_size; 971201766a2SElvira Khabirova 972201766a2SElvira Khabirova /* 973201766a2SElvira Khabirova * This does not need lock_task_sighand() to access 974201766a2SElvira Khabirova * child->last_siginfo because ptrace_freeze_traced() 975201766a2SElvira Khabirova * called earlier by ptrace_check_attach() ensures that 976201766a2SElvira Khabirova * the tracee cannot go away and clear its last_siginfo. 977201766a2SElvira Khabirova */ 978201766a2SElvira Khabirova switch (child->last_siginfo ? child->last_siginfo->si_code : 0) { 979201766a2SElvira Khabirova case SIGTRAP | 0x80: 980201766a2SElvira Khabirova switch (child->ptrace_message) { 981201766a2SElvira Khabirova case PTRACE_EVENTMSG_SYSCALL_ENTRY: 982201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_entry(child, regs, 983201766a2SElvira Khabirova &info); 984201766a2SElvira Khabirova break; 985201766a2SElvira Khabirova case PTRACE_EVENTMSG_SYSCALL_EXIT: 986201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_exit(child, regs, 987201766a2SElvira Khabirova &info); 988201766a2SElvira Khabirova break; 989201766a2SElvira Khabirova } 990201766a2SElvira Khabirova break; 991201766a2SElvira Khabirova case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8): 992201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_seccomp(child, regs, 993201766a2SElvira Khabirova &info); 994201766a2SElvira Khabirova break; 995201766a2SElvira Khabirova } 996201766a2SElvira Khabirova 997201766a2SElvira Khabirova write_size = min(actual_size, user_size); 998201766a2SElvira Khabirova return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size; 999201766a2SElvira Khabirova } 1000201766a2SElvira Khabirova #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 10012225a122SSuresh Siddha 10021da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 10034abf9869SNamhyung Kim unsigned long addr, unsigned long data) 10041da177e4SLinus Torvalds { 1005fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 10061da177e4SLinus Torvalds int ret = -EIO; 1007ae7795bcSEric W. Biederman kernel_siginfo_t siginfo, *si; 10089fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 10099fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 1010fca26f26STejun Heo unsigned long flags; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds switch (request) { 101316c3e389SRoland McGrath case PTRACE_PEEKTEXT: 101416c3e389SRoland McGrath case PTRACE_PEEKDATA: 101516c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 101616c3e389SRoland McGrath case PTRACE_POKETEXT: 101716c3e389SRoland McGrath case PTRACE_POKEDATA: 101816c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 101916c3e389SRoland McGrath 10201da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 10211da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 10221da177e4SLinus Torvalds #endif 10231da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 10241da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 10251da177e4SLinus Torvalds break; 10261da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 10279fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 10281da177e4SLinus Torvalds break; 1029e16b2781SRoland McGrath 103084c751bdSAndrey Vagin case PTRACE_PEEKSIGINFO: 103184c751bdSAndrey Vagin ret = ptrace_peek_siginfo(child, addr, data); 103284c751bdSAndrey Vagin break; 103384c751bdSAndrey Vagin 10341da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 1035e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1036e16b2781SRoland McGrath if (!ret) 10379fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 10381da177e4SLinus Torvalds break; 1039e16b2781SRoland McGrath 10401da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 10414cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user(&siginfo, datavp); 10424cd2e0e7SEric W. Biederman if (!ret) 1043e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 10441da177e4SLinus Torvalds break; 1045e16b2781SRoland McGrath 1046fcfc2aa0SAndrei Vagin case PTRACE_GETSIGMASK: { 1047fcfc2aa0SAndrei Vagin sigset_t *mask; 1048fcfc2aa0SAndrei Vagin 104929000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 105029000caeSAndrey Vagin ret = -EINVAL; 105129000caeSAndrey Vagin break; 105229000caeSAndrey Vagin } 105329000caeSAndrey Vagin 1054fcfc2aa0SAndrei Vagin if (test_tsk_restore_sigmask(child)) 1055fcfc2aa0SAndrei Vagin mask = &child->saved_sigmask; 1056fcfc2aa0SAndrei Vagin else 1057fcfc2aa0SAndrei Vagin mask = &child->blocked; 1058fcfc2aa0SAndrei Vagin 1059fcfc2aa0SAndrei Vagin if (copy_to_user(datavp, mask, sizeof(sigset_t))) 106029000caeSAndrey Vagin ret = -EFAULT; 106129000caeSAndrey Vagin else 106229000caeSAndrey Vagin ret = 0; 106329000caeSAndrey Vagin 106429000caeSAndrey Vagin break; 1065fcfc2aa0SAndrei Vagin } 106629000caeSAndrey Vagin 106729000caeSAndrey Vagin case PTRACE_SETSIGMASK: { 106829000caeSAndrey Vagin sigset_t new_set; 106929000caeSAndrey Vagin 107029000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 107129000caeSAndrey Vagin ret = -EINVAL; 107229000caeSAndrey Vagin break; 107329000caeSAndrey Vagin } 107429000caeSAndrey Vagin 107529000caeSAndrey Vagin if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 107629000caeSAndrey Vagin ret = -EFAULT; 107729000caeSAndrey Vagin break; 107829000caeSAndrey Vagin } 107929000caeSAndrey Vagin 108029000caeSAndrey Vagin sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 108129000caeSAndrey Vagin 108229000caeSAndrey Vagin /* 108329000caeSAndrey Vagin * Every thread does recalc_sigpending() after resume, so 108429000caeSAndrey Vagin * retarget_shared_pending() and recalc_sigpending() are not 108529000caeSAndrey Vagin * called here. 108629000caeSAndrey Vagin */ 108729000caeSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 108829000caeSAndrey Vagin child->blocked = new_set; 108929000caeSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 109029000caeSAndrey Vagin 1091fcfc2aa0SAndrei Vagin clear_tsk_restore_sigmask(child); 1092fcfc2aa0SAndrei Vagin 109329000caeSAndrey Vagin ret = 0; 109429000caeSAndrey Vagin break; 109529000caeSAndrey Vagin } 109629000caeSAndrey Vagin 1097fca26f26STejun Heo case PTRACE_INTERRUPT: 1098fca26f26STejun Heo /* 1099fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 1100fca26f26STejun Heo * control. At least one trap is guaranteed to happen 1101fca26f26STejun Heo * after this request. If @child is already trapped, the 1102fca26f26STejun Heo * current trap is not disturbed and another trap will 1103fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 1104fca26f26STejun Heo * 1105fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 1106fca26f26STejun Heo * the pending condition is cleared regardless. 1107fca26f26STejun Heo */ 1108fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1109fca26f26STejun Heo break; 1110fca26f26STejun Heo 1111544b2c91STejun Heo /* 1112544b2c91STejun Heo * INTERRUPT doesn't disturb existing trap sans one 1113544b2c91STejun Heo * exception. If ptracer issued LISTEN for the current 1114544b2c91STejun Heo * STOP, this INTERRUPT should clear LISTEN and re-trap 1115544b2c91STejun Heo * tracee into STOP. 1116544b2c91STejun Heo */ 1117fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 1118910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 1119544b2c91STejun Heo 1120544b2c91STejun Heo unlock_task_sighand(child, &flags); 1121544b2c91STejun Heo ret = 0; 1122544b2c91STejun Heo break; 1123544b2c91STejun Heo 1124544b2c91STejun Heo case PTRACE_LISTEN: 1125544b2c91STejun Heo /* 1126544b2c91STejun Heo * Listen for events. Tracee must be in STOP. It's not 1127544b2c91STejun Heo * resumed per-se but is not considered to be in TRACED by 1128544b2c91STejun Heo * wait(2) or ptrace(2). If an async event (e.g. group 1129544b2c91STejun Heo * stop state change) happens, tracee will enter STOP trap 1130544b2c91STejun Heo * again. Alternatively, ptracer can issue INTERRUPT to 1131544b2c91STejun Heo * finish listening and re-trap tracee into STOP. 1132544b2c91STejun Heo */ 1133544b2c91STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1134544b2c91STejun Heo break; 1135544b2c91STejun Heo 1136544b2c91STejun Heo si = child->last_siginfo; 1137f9d81f61SOleg Nesterov if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 1138544b2c91STejun Heo child->jobctl |= JOBCTL_LISTENING; 1139544b2c91STejun Heo /* 1140f9d81f61SOleg Nesterov * If NOTIFY is set, it means event happened between 1141f9d81f61SOleg Nesterov * start of this trap and now. Trigger re-trap. 1142544b2c91STejun Heo */ 1143544b2c91STejun Heo if (child->jobctl & JOBCTL_TRAP_NOTIFY) 1144910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1145fca26f26STejun Heo ret = 0; 1146f9d81f61SOleg Nesterov } 1147f9d81f61SOleg Nesterov unlock_task_sighand(child, &flags); 1148fca26f26STejun Heo break; 1149fca26f26STejun Heo 11501bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 11511bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 11521bcf5482SAlexey Dobriyan break; 115336df29d7SRoland McGrath 11549c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 11559c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 1156e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 11579c1a1259SMike Frysinger unsigned long tmp = 0; 11589c1a1259SMike Frysinger 1159e0129ef9SOleg Nesterov ret = -ESRCH; 1160e0129ef9SOleg Nesterov if (!mm) 1161e0129ef9SOleg Nesterov break; 1162e0129ef9SOleg Nesterov 11639c1a1259SMike Frysinger switch (addr) { 11649c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 1165e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 11669c1a1259SMike Frysinger break; 11679c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 1168e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 11699c1a1259SMike Frysinger break; 11709c1a1259SMike Frysinger default: 11719c1a1259SMike Frysinger break; 11729c1a1259SMike Frysinger } 1173e0129ef9SOleg Nesterov mmput(mm); 11749c1a1259SMike Frysinger 11759fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 11769c1a1259SMike Frysinger break; 11779c1a1259SMike Frysinger } 11789c1a1259SMike Frysinger #endif 11799c1a1259SMike Frysinger 118036df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 118136df29d7SRoland McGrath case PTRACE_SINGLESTEP: 118236df29d7SRoland McGrath #endif 11835b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 11845b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 11855b88abbfSRoland McGrath #endif 118636df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 118736df29d7SRoland McGrath case PTRACE_SYSEMU: 118836df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 118936df29d7SRoland McGrath #endif 119036df29d7SRoland McGrath case PTRACE_SYSCALL: 119136df29d7SRoland McGrath case PTRACE_CONT: 119236df29d7SRoland McGrath return ptrace_resume(child, request, data); 119336df29d7SRoland McGrath 119436df29d7SRoland McGrath case PTRACE_KILL: 119536df29d7SRoland McGrath if (child->exit_state) /* already dead */ 119636df29d7SRoland McGrath return 0; 119736df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 119836df29d7SRoland McGrath 11992225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 12002225a122SSuresh Siddha case PTRACE_GETREGSET: 120129000caeSAndrey Vagin case PTRACE_SETREGSET: { 12022225a122SSuresh Siddha struct iovec kiov; 12039fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 12042225a122SSuresh Siddha 120596d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 12062225a122SSuresh Siddha return -EFAULT; 12072225a122SSuresh Siddha 12082225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 12092225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 12102225a122SSuresh Siddha return -EFAULT; 12112225a122SSuresh Siddha 12122225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 12132225a122SSuresh Siddha if (!ret) 12142225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 12152225a122SSuresh Siddha break; 12162225a122SSuresh Siddha } 1217201766a2SElvira Khabirova 1218201766a2SElvira Khabirova case PTRACE_GET_SYSCALL_INFO: 1219201766a2SElvira Khabirova ret = ptrace_get_syscall_info(child, addr, datavp); 1220201766a2SElvira Khabirova break; 12212225a122SSuresh Siddha #endif 1222f8e529edSTycho Andersen 1223f8e529edSTycho Andersen case PTRACE_SECCOMP_GET_FILTER: 1224f8e529edSTycho Andersen ret = seccomp_get_filter(child, addr, datavp); 1225f8e529edSTycho Andersen break; 1226f8e529edSTycho Andersen 122726500475STycho Andersen case PTRACE_SECCOMP_GET_METADATA: 122826500475STycho Andersen ret = seccomp_get_metadata(child, addr, datavp); 122926500475STycho Andersen break; 123026500475STycho Andersen 12311da177e4SLinus Torvalds default: 12321da177e4SLinus Torvalds break; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds return ret; 12361da177e4SLinus Torvalds } 1237481bed45SChristoph Hellwig 12380ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 12390ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 12400ac15559SChristoph Hellwig #endif 12410ac15559SChristoph Hellwig 12424abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 12434abf9869SNamhyung Kim unsigned long, data) 1244481bed45SChristoph Hellwig { 1245481bed45SChristoph Hellwig struct task_struct *child; 1246481bed45SChristoph Hellwig long ret; 1247481bed45SChristoph Hellwig 12486b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 12496b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 12506ea6dd93SHaavard Skinnemoen if (!ret) 12516ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 1252481bed45SChristoph Hellwig goto out; 12536b9c7ed8SChristoph Hellwig } 12546b9c7ed8SChristoph Hellwig 12552ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 12562ee08260SMike Rapoport if (!child) { 12572ee08260SMike Rapoport ret = -ESRCH; 12586b9c7ed8SChristoph Hellwig goto out; 12596b9c7ed8SChristoph Hellwig } 1260481bed45SChristoph Hellwig 12613544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1262aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 12630ac15559SChristoph Hellwig /* 12640ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 12650ac15559SChristoph Hellwig * a ptrace attach. 12660ac15559SChristoph Hellwig */ 12670ac15559SChristoph Hellwig if (!ret) 12680ac15559SChristoph Hellwig arch_ptrace_attach(child); 1269005f18dfSChristoph Hellwig goto out_put_task_struct; 1270481bed45SChristoph Hellwig } 1271481bed45SChristoph Hellwig 1272fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1273fca26f26STejun Heo request == PTRACE_INTERRUPT); 1274481bed45SChristoph Hellwig if (ret < 0) 1275481bed45SChristoph Hellwig goto out_put_task_struct; 1276481bed45SChristoph Hellwig 1277481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 12789899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 12799899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 1280481bed45SChristoph Hellwig 1281481bed45SChristoph Hellwig out_put_task_struct: 1282481bed45SChristoph Hellwig put_task_struct(child); 1283481bed45SChristoph Hellwig out: 1284481bed45SChristoph Hellwig return ret; 1285481bed45SChristoph Hellwig } 128676647323SAlexey Dobriyan 12874abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 12884abf9869SNamhyung Kim unsigned long data) 128976647323SAlexey Dobriyan { 129076647323SAlexey Dobriyan unsigned long tmp; 129176647323SAlexey Dobriyan int copied; 129276647323SAlexey Dobriyan 129384d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE); 129476647323SAlexey Dobriyan if (copied != sizeof(tmp)) 129576647323SAlexey Dobriyan return -EIO; 129676647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 129776647323SAlexey Dobriyan } 1298f284ce72SAlexey Dobriyan 12994abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 13004abf9869SNamhyung Kim unsigned long data) 1301f284ce72SAlexey Dobriyan { 1302f284ce72SAlexey Dobriyan int copied; 1303f284ce72SAlexey Dobriyan 130484d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &data, sizeof(data), 1305f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1306f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 1307f284ce72SAlexey Dobriyan } 1308032d82d9SRoland McGrath 130996b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 1310032d82d9SRoland McGrath 1311032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1312032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 1313032d82d9SRoland McGrath { 1314032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 1315032d82d9SRoland McGrath compat_ulong_t word; 1316ae7795bcSEric W. Biederman kernel_siginfo_t siginfo; 1317032d82d9SRoland McGrath int ret; 1318032d82d9SRoland McGrath 1319032d82d9SRoland McGrath switch (request) { 1320032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 1321032d82d9SRoland McGrath case PTRACE_PEEKDATA: 132284d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &word, sizeof(word), 1323f307ab6dSLorenzo Stoakes FOLL_FORCE); 1324032d82d9SRoland McGrath if (ret != sizeof(word)) 1325032d82d9SRoland McGrath ret = -EIO; 1326032d82d9SRoland McGrath else 1327032d82d9SRoland McGrath ret = put_user(word, datap); 1328032d82d9SRoland McGrath break; 1329032d82d9SRoland McGrath 1330032d82d9SRoland McGrath case PTRACE_POKETEXT: 1331032d82d9SRoland McGrath case PTRACE_POKEDATA: 133284d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &data, sizeof(data), 1333f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1334032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 1335032d82d9SRoland McGrath break; 1336032d82d9SRoland McGrath 1337032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 1338032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1339032d82d9SRoland McGrath break; 1340032d82d9SRoland McGrath 1341e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 1342e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1343e16b2781SRoland McGrath if (!ret) 1344e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 1345e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 1346e16b2781SRoland McGrath &siginfo); 1347e16b2781SRoland McGrath break; 1348e16b2781SRoland McGrath 1349e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 13504cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user32( 13514cd2e0e7SEric W. Biederman &siginfo, (struct compat_siginfo __user *) datap); 13524cd2e0e7SEric W. Biederman if (!ret) 1353e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 1354e16b2781SRoland McGrath break; 13552225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 13562225a122SSuresh Siddha case PTRACE_GETREGSET: 13572225a122SSuresh Siddha case PTRACE_SETREGSET: 13582225a122SSuresh Siddha { 13592225a122SSuresh Siddha struct iovec kiov; 13602225a122SSuresh Siddha struct compat_iovec __user *uiov = 13612225a122SSuresh Siddha (struct compat_iovec __user *) datap; 13622225a122SSuresh Siddha compat_uptr_t ptr; 13632225a122SSuresh Siddha compat_size_t len; 13642225a122SSuresh Siddha 136596d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 13662225a122SSuresh Siddha return -EFAULT; 13672225a122SSuresh Siddha 13682225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 13692225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 13702225a122SSuresh Siddha return -EFAULT; 13712225a122SSuresh Siddha 13722225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 13732225a122SSuresh Siddha kiov.iov_len = len; 13742225a122SSuresh Siddha 13752225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 13762225a122SSuresh Siddha if (!ret) 13772225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 13782225a122SSuresh Siddha break; 13792225a122SSuresh Siddha } 13802225a122SSuresh Siddha #endif 1381e16b2781SRoland McGrath 1382032d82d9SRoland McGrath default: 1383032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 1384032d82d9SRoland McGrath } 1385032d82d9SRoland McGrath 1386032d82d9SRoland McGrath return ret; 1387032d82d9SRoland McGrath } 1388c269f196SRoland McGrath 138962a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, 139062a6fa97SHeiko Carstens compat_long_t, addr, compat_long_t, data) 1391c269f196SRoland McGrath { 1392c269f196SRoland McGrath struct task_struct *child; 1393c269f196SRoland McGrath long ret; 1394c269f196SRoland McGrath 1395c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 1396c269f196SRoland McGrath ret = ptrace_traceme(); 1397c269f196SRoland McGrath goto out; 1398c269f196SRoland McGrath } 1399c269f196SRoland McGrath 14002ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 14012ee08260SMike Rapoport if (!child) { 14022ee08260SMike Rapoport ret = -ESRCH; 1403c269f196SRoland McGrath goto out; 1404c269f196SRoland McGrath } 1405c269f196SRoland McGrath 14063544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1407aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1408c269f196SRoland McGrath /* 1409c269f196SRoland McGrath * Some architectures need to do book-keeping after 1410c269f196SRoland McGrath * a ptrace attach. 1411c269f196SRoland McGrath */ 1412c269f196SRoland McGrath if (!ret) 1413c269f196SRoland McGrath arch_ptrace_attach(child); 1414c269f196SRoland McGrath goto out_put_task_struct; 1415c269f196SRoland McGrath } 1416c269f196SRoland McGrath 1417fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1418fca26f26STejun Heo request == PTRACE_INTERRUPT); 14199899d11fSOleg Nesterov if (!ret) { 1420c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 14219899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 14229899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 14239899d11fSOleg Nesterov } 1424c269f196SRoland McGrath 1425c269f196SRoland McGrath out_put_task_struct: 1426c269f196SRoland McGrath put_task_struct(child); 1427c269f196SRoland McGrath out: 1428c269f196SRoland McGrath return ret; 1429c269f196SRoland McGrath } 143096b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 1431