xref: /openbmc/linux/kernel/ptrace.c (revision 97f2645f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/kernel/ptrace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Common interfaces for "ptrace()" which we do not want
71da177e4SLinus Torvalds  * to continually duplicate across every architecture.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
10c59ede7bSRandy.Dunlap #include <linux/capability.h>
119984de1aSPaul Gortmaker #include <linux/export.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/highmem.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
171da177e4SLinus Torvalds #include <linux/ptrace.h>
181da177e4SLinus Torvalds #include <linux/security.h>
197ed20e1aSJesper Juhl #include <linux/signal.h>
20a27bb332SKent Overstreet #include <linux/uio.h>
21a5cb013dSAl Viro #include <linux/audit.h>
22b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
23f17d30a8SAdrian Bunk #include <linux/syscalls.h>
243a709703SRoland McGrath #include <linux/uaccess.h>
252225a122SSuresh Siddha #include <linux/regset.h>
26bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h>
27f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h>
2884c751bdSAndrey Vagin #include <linux/compat.h>
291da177e4SLinus Torvalds 
30bf53de90SMarkus Metzger 
31bf53de90SMarkus Metzger /*
321da177e4SLinus Torvalds  * ptrace a task: make the debugger its new parent and
331da177e4SLinus Torvalds  * move it to the ptrace list.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * Must be called with the tasklist lock write-held.
361da177e4SLinus Torvalds  */
3736c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
381da177e4SLinus Torvalds {
39f470021aSRoland McGrath 	BUG_ON(!list_empty(&child->ptrace_entry));
40f470021aSRoland McGrath 	list_add(&child->ptrace_entry, &new_parent->ptraced);
411da177e4SLinus Torvalds 	child->parent = new_parent;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
44e3bd058fSTejun Heo /**
45e3bd058fSTejun Heo  * __ptrace_unlink - unlink ptracee and restore its execution state
46e3bd058fSTejun Heo  * @child: ptracee to be unlinked
471da177e4SLinus Torvalds  *
480e9f0a4aSTejun Heo  * Remove @child from the ptrace list, move it back to the original parent,
490e9f0a4aSTejun Heo  * and restore the execution state so that it conforms to the group stop
500e9f0a4aSTejun Heo  * state.
510e9f0a4aSTejun Heo  *
520e9f0a4aSTejun Heo  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
530e9f0a4aSTejun Heo  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
540e9f0a4aSTejun Heo  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
550e9f0a4aSTejun Heo  * If the ptracer is exiting, the ptracee can be in any state.
560e9f0a4aSTejun Heo  *
570e9f0a4aSTejun Heo  * After detach, the ptracee should be in a state which conforms to the
580e9f0a4aSTejun Heo  * group stop.  If the group is stopped or in the process of stopping, the
590e9f0a4aSTejun Heo  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
600e9f0a4aSTejun Heo  * up from TASK_TRACED.
610e9f0a4aSTejun Heo  *
620e9f0a4aSTejun Heo  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
630e9f0a4aSTejun Heo  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
640e9f0a4aSTejun Heo  * to but in the opposite direction of what happens while attaching to a
650e9f0a4aSTejun Heo  * stopped task.  However, in this direction, the intermediate RUNNING
660e9f0a4aSTejun Heo  * state is not hidden even from the current ptracer and if it immediately
670e9f0a4aSTejun Heo  * re-attaches and performs a WNOHANG wait(2), it may fail.
68e3bd058fSTejun Heo  *
69e3bd058fSTejun Heo  * CONTEXT:
70e3bd058fSTejun Heo  * write_lock_irq(tasklist_lock)
711da177e4SLinus Torvalds  */
7236c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child)
731da177e4SLinus Torvalds {
745ecfbae0SOleg Nesterov 	BUG_ON(!child->ptrace);
755ecfbae0SOleg Nesterov 
761da177e4SLinus Torvalds 	child->parent = child->real_parent;
77f470021aSRoland McGrath 	list_del_init(&child->ptrace_entry);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	spin_lock(&child->sighand->siglock);
801333ab03SOleg Nesterov 	child->ptrace = 0;
811da177e4SLinus Torvalds 	/*
8273ddff2bSTejun Heo 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
8373ddff2bSTejun Heo 	 * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
8473ddff2bSTejun Heo 	 */
8573ddff2bSTejun Heo 	task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
8673ddff2bSTejun Heo 	task_clear_jobctl_trapping(child);
8773ddff2bSTejun Heo 
8873ddff2bSTejun Heo 	/*
89a8f072c1STejun Heo 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
900e9f0a4aSTejun Heo 	 * @child isn't dead.
911da177e4SLinus Torvalds 	 */
920e9f0a4aSTejun Heo 	if (!(child->flags & PF_EXITING) &&
930e9f0a4aSTejun Heo 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
948a88951bSOleg Nesterov 	     child->signal->group_stop_count)) {
95a8f072c1STejun Heo 		child->jobctl |= JOBCTL_STOP_PENDING;
960e9f0a4aSTejun Heo 
970e9f0a4aSTejun Heo 		/*
988a88951bSOleg Nesterov 		 * This is only possible if this thread was cloned by the
998a88951bSOleg Nesterov 		 * traced task running in the stopped group, set the signal
1008a88951bSOleg Nesterov 		 * for the future reports.
1018a88951bSOleg Nesterov 		 * FIXME: we should change ptrace_init_task() to handle this
1028a88951bSOleg Nesterov 		 * case.
1038a88951bSOleg Nesterov 		 */
1048a88951bSOleg Nesterov 		if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
1058a88951bSOleg Nesterov 			child->jobctl |= SIGSTOP;
1068a88951bSOleg Nesterov 	}
1078a88951bSOleg Nesterov 
1088a88951bSOleg Nesterov 	/*
1090e9f0a4aSTejun Heo 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
1100e9f0a4aSTejun Heo 	 * @child in the butt.  Note that @resume should be used iff @child
1110e9f0a4aSTejun Heo 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
1120e9f0a4aSTejun Heo 	 * TASK_KILLABLE sleeps.
1130e9f0a4aSTejun Heo 	 */
114a8f072c1STejun Heo 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
115910ffdb1SOleg Nesterov 		ptrace_signal_wake_up(child, true);
1160e9f0a4aSTejun Heo 
1171da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1209899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */
1219899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task)
1229899d11fSOleg Nesterov {
1239899d11fSOleg Nesterov 	bool ret = false;
1249899d11fSOleg Nesterov 
1259899d11fSOleg Nesterov 	/* Lockless, nobody but us can set this flag */
1269899d11fSOleg Nesterov 	if (task->jobctl & JOBCTL_LISTENING)
1279899d11fSOleg Nesterov 		return ret;
1289899d11fSOleg Nesterov 
1299899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1309899d11fSOleg Nesterov 	if (task_is_traced(task) && !__fatal_signal_pending(task)) {
1319899d11fSOleg Nesterov 		task->state = __TASK_TRACED;
1329899d11fSOleg Nesterov 		ret = true;
1339899d11fSOleg Nesterov 	}
1349899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1359899d11fSOleg Nesterov 
1369899d11fSOleg Nesterov 	return ret;
1379899d11fSOleg Nesterov }
1389899d11fSOleg Nesterov 
1399899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task)
1409899d11fSOleg Nesterov {
1419899d11fSOleg Nesterov 	if (task->state != __TASK_TRACED)
1429899d11fSOleg Nesterov 		return;
1439899d11fSOleg Nesterov 
1449899d11fSOleg Nesterov 	WARN_ON(!task->ptrace || task->parent != current);
1459899d11fSOleg Nesterov 
1469899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1479899d11fSOleg Nesterov 	if (__fatal_signal_pending(task))
1489899d11fSOleg Nesterov 		wake_up_state(task, __TASK_TRACED);
1499899d11fSOleg Nesterov 	else
1509899d11fSOleg Nesterov 		task->state = TASK_TRACED;
1519899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1529899d11fSOleg Nesterov }
1539899d11fSOleg Nesterov 
154755e276bSTejun Heo /**
155755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
156755e276bSTejun Heo  * @child: ptracee to check for
157755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
158755e276bSTejun Heo  *
159755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
160755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
161755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
162755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
163755e276bSTejun Heo  * state.
164755e276bSTejun Heo  *
165755e276bSTejun Heo  * CONTEXT:
166755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
167755e276bSTejun Heo  *
168755e276bSTejun Heo  * RETURNS:
169755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1701da177e4SLinus Torvalds  */
171edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	int ret = -ESRCH;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/*
1761da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1771da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1781da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1791da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1801da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
1839899d11fSOleg Nesterov 	if (child->ptrace && child->parent == current) {
1849899d11fSOleg Nesterov 		WARN_ON(child->state == __TASK_TRACED);
185c0c0b649SOleg Nesterov 		/*
186c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
187c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
188c0c0b649SOleg Nesterov 		 */
1899899d11fSOleg Nesterov 		if (ignore_state || ptrace_freeze_traced(child))
190321fb561SOleg Nesterov 			ret = 0;
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1931da177e4SLinus Torvalds 
1949899d11fSOleg Nesterov 	if (!ret && !ignore_state) {
1959899d11fSOleg Nesterov 		if (!wait_task_inactive(child, __TASK_TRACED)) {
1969899d11fSOleg Nesterov 			/*
1979899d11fSOleg Nesterov 			 * This can only happen if may_ptrace_stop() fails and
1989899d11fSOleg Nesterov 			 * ptrace_stop() changes ->state back to TASK_RUNNING,
1999899d11fSOleg Nesterov 			 * so we should not worry about leaking __TASK_TRACED.
2009899d11fSOleg Nesterov 			 */
2019899d11fSOleg Nesterov 			WARN_ON(child->state == __TASK_TRACED);
2029899d11fSOleg Nesterov 			ret = -ESRCH;
2039899d11fSOleg Nesterov 		}
2049899d11fSOleg Nesterov 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	return ret;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
20969f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
21069f594a3SEric Paris {
21169f594a3SEric Paris 	if (mode & PTRACE_MODE_NOAUDIT)
21269f594a3SEric Paris 		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
21369f594a3SEric Paris 	else
21469f594a3SEric Paris 		return has_ns_capability(current, ns, CAP_SYS_PTRACE);
21569f594a3SEric Paris }
21669f594a3SEric Paris 
2179f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */
2189f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
219ab8d11beSMiklos Szeredi {
220c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
221caaee623SJann Horn 	int dumpable = 0;
222caaee623SJann Horn 	kuid_t caller_uid;
223caaee623SJann Horn 	kgid_t caller_gid;
224caaee623SJann Horn 
225caaee623SJann Horn 	if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
226caaee623SJann Horn 		WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
227caaee623SJann Horn 		return -EPERM;
228caaee623SJann Horn 	}
229b6dff3ecSDavid Howells 
230df26c40eSEric W. Biederman 	/* May we inspect the given task?
231df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
232df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
233df26c40eSEric W. Biederman 	 *
234df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
235df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
236df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
237df26c40eSEric W. Biederman 	 */
238caaee623SJann Horn 
239df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
24073af963fSMark Grondona 	if (same_thread_group(task, current))
241df26c40eSEric W. Biederman 		return 0;
242c69e8d9cSDavid Howells 	rcu_read_lock();
243caaee623SJann Horn 	if (mode & PTRACE_MODE_FSCREDS) {
244caaee623SJann Horn 		caller_uid = cred->fsuid;
245caaee623SJann Horn 		caller_gid = cred->fsgid;
246caaee623SJann Horn 	} else {
247caaee623SJann Horn 		/*
248caaee623SJann Horn 		 * Using the euid would make more sense here, but something
249caaee623SJann Horn 		 * in userland might rely on the old behavior, and this
250caaee623SJann Horn 		 * shouldn't be a security problem since
251caaee623SJann Horn 		 * PTRACE_MODE_REALCREDS implies that the caller explicitly
252caaee623SJann Horn 		 * used a syscall that requests access to another process
253caaee623SJann Horn 		 * (and not a filesystem syscall to procfs).
254caaee623SJann Horn 		 */
255caaee623SJann Horn 		caller_uid = cred->uid;
256caaee623SJann Horn 		caller_gid = cred->gid;
257caaee623SJann Horn 	}
258c69e8d9cSDavid Howells 	tcred = __task_cred(task);
259caaee623SJann Horn 	if (uid_eq(caller_uid, tcred->euid) &&
260caaee623SJann Horn 	    uid_eq(caller_uid, tcred->suid) &&
261caaee623SJann Horn 	    uid_eq(caller_uid, tcred->uid)  &&
262caaee623SJann Horn 	    gid_eq(caller_gid, tcred->egid) &&
263caaee623SJann Horn 	    gid_eq(caller_gid, tcred->sgid) &&
264caaee623SJann Horn 	    gid_eq(caller_gid, tcred->gid))
2658409cca7SSerge E. Hallyn 		goto ok;
266c4a4d603SEric W. Biederman 	if (ptrace_has_cap(tcred->user_ns, mode))
2678409cca7SSerge E. Hallyn 		goto ok;
268c69e8d9cSDavid Howells 	rcu_read_unlock();
269ab8d11beSMiklos Szeredi 	return -EPERM;
2708409cca7SSerge E. Hallyn ok:
271c69e8d9cSDavid Howells 	rcu_read_unlock();
272ab8d11beSMiklos Szeredi 	smp_rmb();
273df26c40eSEric W. Biederman 	if (task->mm)
2746c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
2754c44aaafSEric W. Biederman 	rcu_read_lock();
276d049f74fSKees Cook 	if (dumpable != SUID_DUMP_USER &&
277d049f74fSKees Cook 	    !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
2784c44aaafSEric W. Biederman 		rcu_read_unlock();
279ab8d11beSMiklos Szeredi 		return -EPERM;
2804c44aaafSEric W. Biederman 	}
2814c44aaafSEric W. Biederman 	rcu_read_unlock();
282ab8d11beSMiklos Szeredi 
2839e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
284ab8d11beSMiklos Szeredi }
285ab8d11beSMiklos Szeredi 
286006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
287ab8d11beSMiklos Szeredi {
288ab8d11beSMiklos Szeredi 	int err;
289ab8d11beSMiklos Szeredi 	task_lock(task);
290006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
291ab8d11beSMiklos Szeredi 	task_unlock(task);
2923a709703SRoland McGrath 	return !err;
293ab8d11beSMiklos Szeredi }
294ab8d11beSMiklos Szeredi 
2953544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request,
296aa9147c9SDenys Vlasenko 			 unsigned long addr,
2973544d72aSTejun Heo 			 unsigned long flags)
2981da177e4SLinus Torvalds {
2993544d72aSTejun Heo 	bool seize = (request == PTRACE_SEIZE);
3001da177e4SLinus Torvalds 	int retval;
301f5b40e36SLinus Torvalds 
3023544d72aSTejun Heo 	retval = -EIO;
303aa9147c9SDenys Vlasenko 	if (seize) {
304aa9147c9SDenys Vlasenko 		if (addr != 0)
3053544d72aSTejun Heo 			goto out;
306aa9147c9SDenys Vlasenko 		if (flags & ~(unsigned long)PTRACE_O_MASK)
307aa9147c9SDenys Vlasenko 			goto out;
308aa9147c9SDenys Vlasenko 		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
309aa9147c9SDenys Vlasenko 	} else {
310aa9147c9SDenys Vlasenko 		flags = PT_PTRACED;
311aa9147c9SDenys Vlasenko 	}
3123544d72aSTejun Heo 
313a5cb013dSAl Viro 	audit_ptrace(task);
314a5cb013dSAl Viro 
3151da177e4SLinus Torvalds 	retval = -EPERM;
316b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
317b79b7ba9SOleg Nesterov 		goto out;
318bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
319f5b40e36SLinus Torvalds 		goto out;
320f5b40e36SLinus Torvalds 
321f2f0b00aSOleg Nesterov 	/*
322f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
32386b6c1f3SDenys Vlasenko 	 * SUID, SGID and LSM creds get determined differently
3245e751e99SDavid Howells 	 * under ptrace.
325d84f4f99SDavid Howells 	 */
326793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
3279b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
328d84f4f99SDavid Howells 		goto out;
3294b105cbbSOleg Nesterov 
330f5b40e36SLinus Torvalds 	task_lock(task);
331caaee623SJann Horn 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
3324b105cbbSOleg Nesterov 	task_unlock(task);
3331da177e4SLinus Torvalds 	if (retval)
3344b105cbbSOleg Nesterov 		goto unlock_creds;
3351da177e4SLinus Torvalds 
3364b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
337b79b7ba9SOleg Nesterov 	retval = -EPERM;
338b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
3394b105cbbSOleg Nesterov 		goto unlock_tasklist;
340f2f0b00aSOleg Nesterov 	if (task->ptrace)
3414b105cbbSOleg Nesterov 		goto unlock_tasklist;
342b79b7ba9SOleg Nesterov 
3433544d72aSTejun Heo 	if (seize)
344aa9147c9SDenys Vlasenko 		flags |= PT_SEIZED;
3454c44aaafSEric W. Biederman 	rcu_read_lock();
3464c44aaafSEric W. Biederman 	if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE))
347aa9147c9SDenys Vlasenko 		flags |= PT_PTRACE_CAP;
3484c44aaafSEric W. Biederman 	rcu_read_unlock();
349aa9147c9SDenys Vlasenko 	task->ptrace = flags;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	__ptrace_link(task, current);
3523544d72aSTejun Heo 
3533544d72aSTejun Heo 	/* SEIZE doesn't trap tracee on attach */
3543544d72aSTejun Heo 	if (!seize)
35533e9fc7dSOleg Nesterov 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
356b79b7ba9SOleg Nesterov 
357d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
358d79fdd6dSTejun Heo 
359d79fdd6dSTejun Heo 	/*
36073ddff2bSTejun Heo 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
361d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
362d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
363d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
364d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
365d79fdd6dSTejun Heo 	 * function.
366d79fdd6dSTejun Heo 	 *
367d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
368d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
369d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
370d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
371d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
372d79fdd6dSTejun Heo 	 *
373d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
374d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
375d79fdd6dSTejun Heo 	 */
3767dd3db54STejun Heo 	if (task_is_stopped(task) &&
37773ddff2bSTejun Heo 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
378910ffdb1SOleg Nesterov 		signal_wake_up_state(task, __TASK_STOPPED);
379d79fdd6dSTejun Heo 
380d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
381d79fdd6dSTejun Heo 
382b79b7ba9SOleg Nesterov 	retval = 0;
3834b105cbbSOleg Nesterov unlock_tasklist:
3844b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3854b105cbbSOleg Nesterov unlock_creds:
3869b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
387f5b40e36SLinus Torvalds out:
388f701e5b7SVladimir Zapolskiy 	if (!retval) {
3897c3b00e0SOleg Nesterov 		/*
3907c3b00e0SOleg Nesterov 		 * We do not bother to change retval or clear JOBCTL_TRAPPING
3917c3b00e0SOleg Nesterov 		 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
3927c3b00e0SOleg Nesterov 		 * not return to user-mode, it will exit and clear this bit in
3937c3b00e0SOleg Nesterov 		 * __ptrace_unlink() if it wasn't already cleared by the tracee;
3947c3b00e0SOleg Nesterov 		 * and until then nobody can ptrace this task.
3957c3b00e0SOleg Nesterov 		 */
3967c3b00e0SOleg Nesterov 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
397f701e5b7SVladimir Zapolskiy 		proc_ptrace_connector(task, PTRACE_ATTACH);
398f701e5b7SVladimir Zapolskiy 	}
399f701e5b7SVladimir Zapolskiy 
4001da177e4SLinus Torvalds 	return retval;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
403f2f0b00aSOleg Nesterov /**
404f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
405f2f0b00aSOleg Nesterov  *
406f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
407f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
408f2f0b00aSOleg Nesterov  */
409e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
410f2f0b00aSOleg Nesterov {
411f2f0b00aSOleg Nesterov 	int ret = -EPERM;
412f2f0b00aSOleg Nesterov 
4134b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
4144b105cbbSOleg Nesterov 	/* Are we already being traced? */
415f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
416f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
417f2f0b00aSOleg Nesterov 		/*
418f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
419f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
420f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
421f2f0b00aSOleg Nesterov 		 */
422f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
423f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
424f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
425f2f0b00aSOleg Nesterov 		}
426f2f0b00aSOleg Nesterov 	}
4274b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
4284b105cbbSOleg Nesterov 
429f2f0b00aSOleg Nesterov 	return ret;
430f2f0b00aSOleg Nesterov }
431f2f0b00aSOleg Nesterov 
43239c626aeSOleg Nesterov /*
43339c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
43439c626aeSOleg Nesterov  */
43539c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
43639c626aeSOleg Nesterov {
43739c626aeSOleg Nesterov 	int ret;
43839c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
43939c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
44039c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
44139c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
44239c626aeSOleg Nesterov 	return ret;
44339c626aeSOleg Nesterov }
44439c626aeSOleg Nesterov 
44539c626aeSOleg Nesterov /*
44639c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
44739c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
44839c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
44939c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
45039c626aeSOleg Nesterov  *
45139c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
45239c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
45339c626aeSOleg Nesterov  * If it should reap itself, return true.
45439c626aeSOleg Nesterov  *
455a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
456a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
457a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
458a7f0765eSOleg Nesterov  * do_wait().
45939c626aeSOleg Nesterov  */
46039c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
46139c626aeSOleg Nesterov {
4629843a1e9SOleg Nesterov 	bool dead;
4639843a1e9SOleg Nesterov 
46439c626aeSOleg Nesterov 	__ptrace_unlink(p);
46539c626aeSOleg Nesterov 
4669843a1e9SOleg Nesterov 	if (p->exit_state != EXIT_ZOMBIE)
4679843a1e9SOleg Nesterov 		return false;
4689843a1e9SOleg Nesterov 
4699843a1e9SOleg Nesterov 	dead = !thread_group_leader(p);
4709843a1e9SOleg Nesterov 
4719843a1e9SOleg Nesterov 	if (!dead && thread_group_empty(p)) {
47239c626aeSOleg Nesterov 		if (!same_thread_group(p->real_parent, tracer))
4739843a1e9SOleg Nesterov 			dead = do_notify_parent(p, p->exit_signal);
474a7f0765eSOleg Nesterov 		else if (ignoring_children(tracer->sighand)) {
475a7f0765eSOleg Nesterov 			__wake_up_parent(p, tracer);
4769843a1e9SOleg Nesterov 			dead = true;
47739c626aeSOleg Nesterov 		}
478a7f0765eSOleg Nesterov 	}
47939c626aeSOleg Nesterov 	/* Mark it as in the process of being reaped. */
4809843a1e9SOleg Nesterov 	if (dead)
48139c626aeSOleg Nesterov 		p->exit_state = EXIT_DEAD;
4829843a1e9SOleg Nesterov 	return dead;
48339c626aeSOleg Nesterov }
48439c626aeSOleg Nesterov 
485e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
4861da177e4SLinus Torvalds {
4877ed20e1aSJesper Juhl 	if (!valid_signal(data))
4881da177e4SLinus Torvalds 		return -EIO;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
4911da177e4SLinus Torvalds 	ptrace_disable(child);
4927d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
4931da177e4SLinus Torvalds 
49495c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
49539c626aeSOleg Nesterov 	/*
49664a4096cSOleg Nesterov 	 * We rely on ptrace_freeze_traced(). It can't be killed and
49764a4096cSOleg Nesterov 	 * untraced by another thread, it can't be a zombie.
49839c626aeSOleg Nesterov 	 */
49964a4096cSOleg Nesterov 	WARN_ON(!child->ptrace || child->exit_state);
50064a4096cSOleg Nesterov 	/*
50164a4096cSOleg Nesterov 	 * tasklist_lock avoids the race with wait_task_stopped(), see
50264a4096cSOleg Nesterov 	 * the comment in ptrace_resume().
50364a4096cSOleg Nesterov 	 */
50495c3eb76SOleg Nesterov 	child->exit_code = data;
50564a4096cSOleg Nesterov 	__ptrace_detach(current, child);
5061da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
5071da177e4SLinus Torvalds 
508f701e5b7SVladimir Zapolskiy 	proc_ptrace_connector(child, PTRACE_DETACH);
5094576145cSOleg Nesterov 
5101da177e4SLinus Torvalds 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
51339c626aeSOleg Nesterov /*
514c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
5157c8bd232SOleg Nesterov  * for writing.
51639c626aeSOleg Nesterov  */
5177c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
51839c626aeSOleg Nesterov {
51939c626aeSOleg Nesterov 	struct task_struct *p, *n;
520c7e49c14SOleg Nesterov 
52139c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
522992fb6e1SOleg Nesterov 		if (unlikely(p->ptrace & PT_EXITKILL))
523992fb6e1SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
524992fb6e1SOleg Nesterov 
52539c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
5267c8bd232SOleg Nesterov 			list_add(&p->ptrace_entry, dead);
52739c626aeSOleg Nesterov 	}
52839c626aeSOleg Nesterov }
52939c626aeSOleg Nesterov 
5301da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	int copied = 0;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	while (len > 0) {
5351da177e4SLinus Torvalds 		char buf[128];
5361da177e4SLinus Torvalds 		int this_len, retval;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5391da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
5401da177e4SLinus Torvalds 		if (!retval) {
5411da177e4SLinus Torvalds 			if (copied)
5421da177e4SLinus Torvalds 				break;
5431da177e4SLinus Torvalds 			return -EIO;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
5461da177e4SLinus Torvalds 			return -EFAULT;
5471da177e4SLinus Torvalds 		copied += retval;
5481da177e4SLinus Torvalds 		src += retval;
5491da177e4SLinus Torvalds 		dst += retval;
5501da177e4SLinus Torvalds 		len -= retval;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 	return copied;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	int copied = 0;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	while (len > 0) {
5601da177e4SLinus Torvalds 		char buf[128];
5611da177e4SLinus Torvalds 		int this_len, retval;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5641da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
5651da177e4SLinus Torvalds 			return -EFAULT;
5661da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
5671da177e4SLinus Torvalds 		if (!retval) {
5681da177e4SLinus Torvalds 			if (copied)
5691da177e4SLinus Torvalds 				break;
5701da177e4SLinus Torvalds 			return -EIO;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 		copied += retval;
5731da177e4SLinus Torvalds 		src += retval;
5741da177e4SLinus Torvalds 		dst += retval;
5751da177e4SLinus Torvalds 		len -= retval;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 	return copied;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5804abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
5811da177e4SLinus Torvalds {
58286b6c1f3SDenys Vlasenko 	unsigned flags;
58386b6c1f3SDenys Vlasenko 
5848c5cf9e5SDenys Vlasenko 	if (data & ~(unsigned long)PTRACE_O_MASK)
5858c5cf9e5SDenys Vlasenko 		return -EINVAL;
5868c5cf9e5SDenys Vlasenko 
58713c4a901STycho Andersen 	if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
58897f2645fSMasahiro Yamada 		if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
58997f2645fSMasahiro Yamada 		    !IS_ENABLED(CONFIG_SECCOMP))
59013c4a901STycho Andersen 			return -EINVAL;
59113c4a901STycho Andersen 
59213c4a901STycho Andersen 		if (!capable(CAP_SYS_ADMIN))
59313c4a901STycho Andersen 			return -EPERM;
59413c4a901STycho Andersen 
59513c4a901STycho Andersen 		if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
59613c4a901STycho Andersen 		    current->ptrace & PT_SUSPEND_SECCOMP)
59713c4a901STycho Andersen 			return -EPERM;
59813c4a901STycho Andersen 	}
59913c4a901STycho Andersen 
60086b6c1f3SDenys Vlasenko 	/* Avoid intermediate state when all opts are cleared */
60186b6c1f3SDenys Vlasenko 	flags = child->ptrace;
60286b6c1f3SDenys Vlasenko 	flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
60386b6c1f3SDenys Vlasenko 	flags |= (data << PT_OPT_FLAG_SHIFT);
60486b6c1f3SDenys Vlasenko 	child->ptrace = flags;
6051da177e4SLinus Torvalds 
6068c5cf9e5SDenys Vlasenko 	return 0;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
609e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
6101da177e4SLinus Torvalds {
611e4961254SOleg Nesterov 	unsigned long flags;
6121da177e4SLinus Torvalds 	int error = -ESRCH;
6131da177e4SLinus Torvalds 
614e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
6151da177e4SLinus Torvalds 		error = -EINVAL;
6161da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
617e16b2781SRoland McGrath 			*info = *child->last_siginfo;
6181da177e4SLinus Torvalds 			error = 0;
6191da177e4SLinus Torvalds 		}
620e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 	return error;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
625e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
6261da177e4SLinus Torvalds {
627e4961254SOleg Nesterov 	unsigned long flags;
6281da177e4SLinus Torvalds 	int error = -ESRCH;
6291da177e4SLinus Torvalds 
630e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
6311da177e4SLinus Torvalds 		error = -EINVAL;
6321da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
633e16b2781SRoland McGrath 			*child->last_siginfo = *info;
6341da177e4SLinus Torvalds 			error = 0;
6351da177e4SLinus Torvalds 		}
636e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 	return error;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
64184c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child,
64284c751bdSAndrey Vagin 				unsigned long addr,
64384c751bdSAndrey Vagin 				unsigned long data)
64484c751bdSAndrey Vagin {
64584c751bdSAndrey Vagin 	struct ptrace_peeksiginfo_args arg;
64684c751bdSAndrey Vagin 	struct sigpending *pending;
64784c751bdSAndrey Vagin 	struct sigqueue *q;
64884c751bdSAndrey Vagin 	int ret, i;
64984c751bdSAndrey Vagin 
65084c751bdSAndrey Vagin 	ret = copy_from_user(&arg, (void __user *) addr,
65184c751bdSAndrey Vagin 				sizeof(struct ptrace_peeksiginfo_args));
65284c751bdSAndrey Vagin 	if (ret)
65384c751bdSAndrey Vagin 		return -EFAULT;
65484c751bdSAndrey Vagin 
65584c751bdSAndrey Vagin 	if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
65684c751bdSAndrey Vagin 		return -EINVAL; /* unknown flags */
65784c751bdSAndrey Vagin 
65884c751bdSAndrey Vagin 	if (arg.nr < 0)
65984c751bdSAndrey Vagin 		return -EINVAL;
66084c751bdSAndrey Vagin 
66184c751bdSAndrey Vagin 	if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
66284c751bdSAndrey Vagin 		pending = &child->signal->shared_pending;
66384c751bdSAndrey Vagin 	else
66484c751bdSAndrey Vagin 		pending = &child->pending;
66584c751bdSAndrey Vagin 
66684c751bdSAndrey Vagin 	for (i = 0; i < arg.nr; ) {
66784c751bdSAndrey Vagin 		siginfo_t info;
66884c751bdSAndrey Vagin 		s32 off = arg.off + i;
66984c751bdSAndrey Vagin 
67084c751bdSAndrey Vagin 		spin_lock_irq(&child->sighand->siglock);
67184c751bdSAndrey Vagin 		list_for_each_entry(q, &pending->list, list) {
67284c751bdSAndrey Vagin 			if (!off--) {
67384c751bdSAndrey Vagin 				copy_siginfo(&info, &q->info);
67484c751bdSAndrey Vagin 				break;
67584c751bdSAndrey Vagin 			}
67684c751bdSAndrey Vagin 		}
67784c751bdSAndrey Vagin 		spin_unlock_irq(&child->sighand->siglock);
67884c751bdSAndrey Vagin 
67984c751bdSAndrey Vagin 		if (off >= 0) /* beyond the end of the list */
68084c751bdSAndrey Vagin 			break;
68184c751bdSAndrey Vagin 
68284c751bdSAndrey Vagin #ifdef CONFIG_COMPAT
6835c465217SAndy Lutomirski 		if (unlikely(in_compat_syscall())) {
68484c751bdSAndrey Vagin 			compat_siginfo_t __user *uinfo = compat_ptr(data);
68584c751bdSAndrey Vagin 
686706b23bdSMathieu Desnoyers 			if (copy_siginfo_to_user32(uinfo, &info) ||
687706b23bdSMathieu Desnoyers 			    __put_user(info.si_code, &uinfo->si_code)) {
688706b23bdSMathieu Desnoyers 				ret = -EFAULT;
689706b23bdSMathieu Desnoyers 				break;
690706b23bdSMathieu Desnoyers 			}
691706b23bdSMathieu Desnoyers 
69284c751bdSAndrey Vagin 		} else
69384c751bdSAndrey Vagin #endif
69484c751bdSAndrey Vagin 		{
69584c751bdSAndrey Vagin 			siginfo_t __user *uinfo = (siginfo_t __user *) data;
69684c751bdSAndrey Vagin 
697706b23bdSMathieu Desnoyers 			if (copy_siginfo_to_user(uinfo, &info) ||
698706b23bdSMathieu Desnoyers 			    __put_user(info.si_code, &uinfo->si_code)) {
69984c751bdSAndrey Vagin 				ret = -EFAULT;
70084c751bdSAndrey Vagin 				break;
70184c751bdSAndrey Vagin 			}
702706b23bdSMathieu Desnoyers 		}
70384c751bdSAndrey Vagin 
70484c751bdSAndrey Vagin 		data += sizeof(siginfo_t);
70584c751bdSAndrey Vagin 		i++;
70684c751bdSAndrey Vagin 
70784c751bdSAndrey Vagin 		if (signal_pending(current))
70884c751bdSAndrey Vagin 			break;
70984c751bdSAndrey Vagin 
71084c751bdSAndrey Vagin 		cond_resched();
71184c751bdSAndrey Vagin 	}
71284c751bdSAndrey Vagin 
71384c751bdSAndrey Vagin 	if (i > 0)
71484c751bdSAndrey Vagin 		return i;
71584c751bdSAndrey Vagin 
71684c751bdSAndrey Vagin 	return ret;
71784c751bdSAndrey Vagin }
71836df29d7SRoland McGrath 
71936df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
72036df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
72136df29d7SRoland McGrath #else
72236df29d7SRoland McGrath #define is_singlestep(request)		0
72336df29d7SRoland McGrath #endif
72436df29d7SRoland McGrath 
7255b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
7265b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
7275b88abbfSRoland McGrath #else
7285b88abbfSRoland McGrath #define is_singleblock(request)		0
7295b88abbfSRoland McGrath #endif
7305b88abbfSRoland McGrath 
73136df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
73236df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
73336df29d7SRoland McGrath #else
73436df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
73536df29d7SRoland McGrath #endif
73636df29d7SRoland McGrath 
7374abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
7384abf9869SNamhyung Kim 			 unsigned long data)
73936df29d7SRoland McGrath {
740b72c1869SOleg Nesterov 	bool need_siglock;
741b72c1869SOleg Nesterov 
74236df29d7SRoland McGrath 	if (!valid_signal(data))
74336df29d7SRoland McGrath 		return -EIO;
74436df29d7SRoland McGrath 
74536df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
74636df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
74736df29d7SRoland McGrath 	else
74836df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
74936df29d7SRoland McGrath 
75036df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
75136df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
75236df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
75336df29d7SRoland McGrath 	else
75436df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
75536df29d7SRoland McGrath #endif
75636df29d7SRoland McGrath 
7575b88abbfSRoland McGrath 	if (is_singleblock(request)) {
7585b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
7595b88abbfSRoland McGrath 			return -EIO;
7605b88abbfSRoland McGrath 		user_enable_block_step(child);
7615b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
76236df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
76336df29d7SRoland McGrath 			return -EIO;
76436df29d7SRoland McGrath 		user_enable_single_step(child);
7653a709703SRoland McGrath 	} else {
76636df29d7SRoland McGrath 		user_disable_single_step(child);
7673a709703SRoland McGrath 	}
76836df29d7SRoland McGrath 
769b72c1869SOleg Nesterov 	/*
770b72c1869SOleg Nesterov 	 * Change ->exit_code and ->state under siglock to avoid the race
771b72c1869SOleg Nesterov 	 * with wait_task_stopped() in between; a non-zero ->exit_code will
772b72c1869SOleg Nesterov 	 * wrongly look like another report from tracee.
773b72c1869SOleg Nesterov 	 *
774b72c1869SOleg Nesterov 	 * Note that we need siglock even if ->exit_code == data and/or this
775b72c1869SOleg Nesterov 	 * status was not reported yet, the new status must not be cleared by
776b72c1869SOleg Nesterov 	 * wait_task_stopped() after resume.
777b72c1869SOleg Nesterov 	 *
778b72c1869SOleg Nesterov 	 * If data == 0 we do not care if wait_task_stopped() reports the old
779b72c1869SOleg Nesterov 	 * status and clears the code too; this can't race with the tracee, it
780b72c1869SOleg Nesterov 	 * takes siglock after resume.
781b72c1869SOleg Nesterov 	 */
782b72c1869SOleg Nesterov 	need_siglock = data && !thread_group_empty(current);
783b72c1869SOleg Nesterov 	if (need_siglock)
784b72c1869SOleg Nesterov 		spin_lock_irq(&child->sighand->siglock);
78536df29d7SRoland McGrath 	child->exit_code = data;
7860666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
787b72c1869SOleg Nesterov 	if (need_siglock)
788b72c1869SOleg Nesterov 		spin_unlock_irq(&child->sighand->siglock);
78936df29d7SRoland McGrath 
79036df29d7SRoland McGrath 	return 0;
79136df29d7SRoland McGrath }
79236df29d7SRoland McGrath 
7932225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
7942225a122SSuresh Siddha 
7952225a122SSuresh Siddha static const struct user_regset *
7962225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
7972225a122SSuresh Siddha {
7982225a122SSuresh Siddha 	const struct user_regset *regset;
7992225a122SSuresh Siddha 	int n;
8002225a122SSuresh Siddha 
8012225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
8022225a122SSuresh Siddha 		regset = view->regsets + n;
8032225a122SSuresh Siddha 		if (regset->core_note_type == type)
8042225a122SSuresh Siddha 			return regset;
8052225a122SSuresh Siddha 	}
8062225a122SSuresh Siddha 
8072225a122SSuresh Siddha 	return NULL;
8082225a122SSuresh Siddha }
8092225a122SSuresh Siddha 
8102225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
8112225a122SSuresh Siddha 			 struct iovec *kiov)
8122225a122SSuresh Siddha {
8132225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
8142225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
8152225a122SSuresh Siddha 	int regset_no;
8162225a122SSuresh Siddha 
8172225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
818c6a0dd7eSSuresh Siddha 		return -EINVAL;
8192225a122SSuresh Siddha 
8202225a122SSuresh Siddha 	regset_no = regset - view->regsets;
8212225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
8222225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
8232225a122SSuresh Siddha 
8242225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
8252225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
8262225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
8272225a122SSuresh Siddha 	else
8282225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
8292225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
8302225a122SSuresh Siddha }
8312225a122SSuresh Siddha 
832e8440c14SJosh Stone /*
833e8440c14SJosh Stone  * This is declared in linux/regset.h and defined in machine-dependent
834e8440c14SJosh Stone  * code.  We put the export here, near the primary machine-neutral use,
835e8440c14SJosh Stone  * to ensure no machine forgets it.
836e8440c14SJosh Stone  */
837e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view);
8382225a122SSuresh Siddha #endif
8392225a122SSuresh Siddha 
8401da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
8414abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
8421da177e4SLinus Torvalds {
843fca26f26STejun Heo 	bool seized = child->ptrace & PT_SEIZED;
8441da177e4SLinus Torvalds 	int ret = -EIO;
845544b2c91STejun Heo 	siginfo_t siginfo, *si;
8469fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
8479fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
848fca26f26STejun Heo 	unsigned long flags;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	switch (request) {
85116c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
85216c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
85316c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
85416c3e389SRoland McGrath 	case PTRACE_POKETEXT:
85516c3e389SRoland McGrath 	case PTRACE_POKEDATA:
85616c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
85716c3e389SRoland McGrath 
8581da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
8591da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
8601da177e4SLinus Torvalds #endif
8611da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
8621da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
8631da177e4SLinus Torvalds 		break;
8641da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
8659fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
8661da177e4SLinus Torvalds 		break;
867e16b2781SRoland McGrath 
86884c751bdSAndrey Vagin 	case PTRACE_PEEKSIGINFO:
86984c751bdSAndrey Vagin 		ret = ptrace_peek_siginfo(child, addr, data);
87084c751bdSAndrey Vagin 		break;
87184c751bdSAndrey Vagin 
8721da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
873e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
874e16b2781SRoland McGrath 		if (!ret)
8759fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
8761da177e4SLinus Torvalds 		break;
877e16b2781SRoland McGrath 
8781da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
8799fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
880e16b2781SRoland McGrath 			ret = -EFAULT;
881e16b2781SRoland McGrath 		else
882e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
8831da177e4SLinus Torvalds 		break;
884e16b2781SRoland McGrath 
88529000caeSAndrey Vagin 	case PTRACE_GETSIGMASK:
88629000caeSAndrey Vagin 		if (addr != sizeof(sigset_t)) {
88729000caeSAndrey Vagin 			ret = -EINVAL;
88829000caeSAndrey Vagin 			break;
88929000caeSAndrey Vagin 		}
89029000caeSAndrey Vagin 
89129000caeSAndrey Vagin 		if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
89229000caeSAndrey Vagin 			ret = -EFAULT;
89329000caeSAndrey Vagin 		else
89429000caeSAndrey Vagin 			ret = 0;
89529000caeSAndrey Vagin 
89629000caeSAndrey Vagin 		break;
89729000caeSAndrey Vagin 
89829000caeSAndrey Vagin 	case PTRACE_SETSIGMASK: {
89929000caeSAndrey Vagin 		sigset_t new_set;
90029000caeSAndrey Vagin 
90129000caeSAndrey Vagin 		if (addr != sizeof(sigset_t)) {
90229000caeSAndrey Vagin 			ret = -EINVAL;
90329000caeSAndrey Vagin 			break;
90429000caeSAndrey Vagin 		}
90529000caeSAndrey Vagin 
90629000caeSAndrey Vagin 		if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
90729000caeSAndrey Vagin 			ret = -EFAULT;
90829000caeSAndrey Vagin 			break;
90929000caeSAndrey Vagin 		}
91029000caeSAndrey Vagin 
91129000caeSAndrey Vagin 		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
91229000caeSAndrey Vagin 
91329000caeSAndrey Vagin 		/*
91429000caeSAndrey Vagin 		 * Every thread does recalc_sigpending() after resume, so
91529000caeSAndrey Vagin 		 * retarget_shared_pending() and recalc_sigpending() are not
91629000caeSAndrey Vagin 		 * called here.
91729000caeSAndrey Vagin 		 */
91829000caeSAndrey Vagin 		spin_lock_irq(&child->sighand->siglock);
91929000caeSAndrey Vagin 		child->blocked = new_set;
92029000caeSAndrey Vagin 		spin_unlock_irq(&child->sighand->siglock);
92129000caeSAndrey Vagin 
92229000caeSAndrey Vagin 		ret = 0;
92329000caeSAndrey Vagin 		break;
92429000caeSAndrey Vagin 	}
92529000caeSAndrey Vagin 
926fca26f26STejun Heo 	case PTRACE_INTERRUPT:
927fca26f26STejun Heo 		/*
928fca26f26STejun Heo 		 * Stop tracee without any side-effect on signal or job
929fca26f26STejun Heo 		 * control.  At least one trap is guaranteed to happen
930fca26f26STejun Heo 		 * after this request.  If @child is already trapped, the
931fca26f26STejun Heo 		 * current trap is not disturbed and another trap will
932fca26f26STejun Heo 		 * happen after the current trap is ended with PTRACE_CONT.
933fca26f26STejun Heo 		 *
934fca26f26STejun Heo 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
935fca26f26STejun Heo 		 * the pending condition is cleared regardless.
936fca26f26STejun Heo 		 */
937fca26f26STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
938fca26f26STejun Heo 			break;
939fca26f26STejun Heo 
940544b2c91STejun Heo 		/*
941544b2c91STejun Heo 		 * INTERRUPT doesn't disturb existing trap sans one
942544b2c91STejun Heo 		 * exception.  If ptracer issued LISTEN for the current
943544b2c91STejun Heo 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
944544b2c91STejun Heo 		 * tracee into STOP.
945544b2c91STejun Heo 		 */
946fca26f26STejun Heo 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
947910ffdb1SOleg Nesterov 			ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
948544b2c91STejun Heo 
949544b2c91STejun Heo 		unlock_task_sighand(child, &flags);
950544b2c91STejun Heo 		ret = 0;
951544b2c91STejun Heo 		break;
952544b2c91STejun Heo 
953544b2c91STejun Heo 	case PTRACE_LISTEN:
954544b2c91STejun Heo 		/*
955544b2c91STejun Heo 		 * Listen for events.  Tracee must be in STOP.  It's not
956544b2c91STejun Heo 		 * resumed per-se but is not considered to be in TRACED by
957544b2c91STejun Heo 		 * wait(2) or ptrace(2).  If an async event (e.g. group
958544b2c91STejun Heo 		 * stop state change) happens, tracee will enter STOP trap
959544b2c91STejun Heo 		 * again.  Alternatively, ptracer can issue INTERRUPT to
960544b2c91STejun Heo 		 * finish listening and re-trap tracee into STOP.
961544b2c91STejun Heo 		 */
962544b2c91STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
963544b2c91STejun Heo 			break;
964544b2c91STejun Heo 
965544b2c91STejun Heo 		si = child->last_siginfo;
966f9d81f61SOleg Nesterov 		if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
967544b2c91STejun Heo 			child->jobctl |= JOBCTL_LISTENING;
968544b2c91STejun Heo 			/*
969f9d81f61SOleg Nesterov 			 * If NOTIFY is set, it means event happened between
970f9d81f61SOleg Nesterov 			 * start of this trap and now.  Trigger re-trap.
971544b2c91STejun Heo 			 */
972544b2c91STejun Heo 			if (child->jobctl & JOBCTL_TRAP_NOTIFY)
973910ffdb1SOleg Nesterov 				ptrace_signal_wake_up(child, true);
974fca26f26STejun Heo 			ret = 0;
975f9d81f61SOleg Nesterov 		}
976f9d81f61SOleg Nesterov 		unlock_task_sighand(child, &flags);
977fca26f26STejun Heo 		break;
978fca26f26STejun Heo 
9791bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
9801bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
9811bcf5482SAlexey Dobriyan 		break;
98236df29d7SRoland McGrath 
9839c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
9849c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
985e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
9869c1a1259SMike Frysinger 		unsigned long tmp = 0;
9879c1a1259SMike Frysinger 
988e0129ef9SOleg Nesterov 		ret = -ESRCH;
989e0129ef9SOleg Nesterov 		if (!mm)
990e0129ef9SOleg Nesterov 			break;
991e0129ef9SOleg Nesterov 
9929c1a1259SMike Frysinger 		switch (addr) {
9939c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
994e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
9959c1a1259SMike Frysinger 			break;
9969c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
997e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
9989c1a1259SMike Frysinger 			break;
9999c1a1259SMike Frysinger 		default:
10009c1a1259SMike Frysinger 			break;
10019c1a1259SMike Frysinger 		}
1002e0129ef9SOleg Nesterov 		mmput(mm);
10039c1a1259SMike Frysinger 
10049fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
10059c1a1259SMike Frysinger 		break;
10069c1a1259SMike Frysinger 	}
10079c1a1259SMike Frysinger #endif
10089c1a1259SMike Frysinger 
100936df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
101036df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
101136df29d7SRoland McGrath #endif
10125b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
10135b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
10145b88abbfSRoland McGrath #endif
101536df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
101636df29d7SRoland McGrath 	case PTRACE_SYSEMU:
101736df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
101836df29d7SRoland McGrath #endif
101936df29d7SRoland McGrath 	case PTRACE_SYSCALL:
102036df29d7SRoland McGrath 	case PTRACE_CONT:
102136df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
102236df29d7SRoland McGrath 
102336df29d7SRoland McGrath 	case PTRACE_KILL:
102436df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
102536df29d7SRoland McGrath 			return 0;
102636df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
102736df29d7SRoland McGrath 
10282225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
10292225a122SSuresh Siddha 	case PTRACE_GETREGSET:
103029000caeSAndrey Vagin 	case PTRACE_SETREGSET: {
10312225a122SSuresh Siddha 		struct iovec kiov;
10329fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
10332225a122SSuresh Siddha 
10342225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
10352225a122SSuresh Siddha 			return -EFAULT;
10362225a122SSuresh Siddha 
10372225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
10382225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
10392225a122SSuresh Siddha 			return -EFAULT;
10402225a122SSuresh Siddha 
10412225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
10422225a122SSuresh Siddha 		if (!ret)
10432225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
10442225a122SSuresh Siddha 		break;
10452225a122SSuresh Siddha 	}
10462225a122SSuresh Siddha #endif
1047f8e529edSTycho Andersen 
1048f8e529edSTycho Andersen 	case PTRACE_SECCOMP_GET_FILTER:
1049f8e529edSTycho Andersen 		ret = seccomp_get_filter(child, addr, datavp);
1050f8e529edSTycho Andersen 		break;
1051f8e529edSTycho Andersen 
10521da177e4SLinus Torvalds 	default:
10531da177e4SLinus Torvalds 		break;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	return ret;
10571da177e4SLinus Torvalds }
1058481bed45SChristoph Hellwig 
10598053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
10606b9c7ed8SChristoph Hellwig {
10616b9c7ed8SChristoph Hellwig 	struct task_struct *child;
10626b9c7ed8SChristoph Hellwig 
10638053bdd5SOleg Nesterov 	rcu_read_lock();
1064228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
1065481bed45SChristoph Hellwig 	if (child)
1066481bed45SChristoph Hellwig 		get_task_struct(child);
10678053bdd5SOleg Nesterov 	rcu_read_unlock();
1068f400e198SSukadev Bhattiprolu 
1069481bed45SChristoph Hellwig 	if (!child)
10706b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
10716b9c7ed8SChristoph Hellwig 	return child;
1072481bed45SChristoph Hellwig }
1073481bed45SChristoph Hellwig 
10740ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
10750ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
10760ac15559SChristoph Hellwig #endif
10770ac15559SChristoph Hellwig 
10784abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
10794abf9869SNamhyung Kim 		unsigned long, data)
1080481bed45SChristoph Hellwig {
1081481bed45SChristoph Hellwig 	struct task_struct *child;
1082481bed45SChristoph Hellwig 	long ret;
1083481bed45SChristoph Hellwig 
10846b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
10856b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
10866ea6dd93SHaavard Skinnemoen 		if (!ret)
10876ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
1088481bed45SChristoph Hellwig 		goto out;
10896b9c7ed8SChristoph Hellwig 	}
10906b9c7ed8SChristoph Hellwig 
10916b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
10926b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
10936b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
10946b9c7ed8SChristoph Hellwig 		goto out;
10956b9c7ed8SChristoph Hellwig 	}
1096481bed45SChristoph Hellwig 
10973544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1098aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
10990ac15559SChristoph Hellwig 		/*
11000ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
11010ac15559SChristoph Hellwig 		 * a ptrace attach.
11020ac15559SChristoph Hellwig 		 */
11030ac15559SChristoph Hellwig 		if (!ret)
11040ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
1105005f18dfSChristoph Hellwig 		goto out_put_task_struct;
1106481bed45SChristoph Hellwig 	}
1107481bed45SChristoph Hellwig 
1108fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1109fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
1110481bed45SChristoph Hellwig 	if (ret < 0)
1111481bed45SChristoph Hellwig 		goto out_put_task_struct;
1112481bed45SChristoph Hellwig 
1113481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
11149899d11fSOleg Nesterov 	if (ret || request != PTRACE_DETACH)
11159899d11fSOleg Nesterov 		ptrace_unfreeze_traced(child);
1116481bed45SChristoph Hellwig 
1117481bed45SChristoph Hellwig  out_put_task_struct:
1118481bed45SChristoph Hellwig 	put_task_struct(child);
1119481bed45SChristoph Hellwig  out:
1120481bed45SChristoph Hellwig 	return ret;
1121481bed45SChristoph Hellwig }
112276647323SAlexey Dobriyan 
11234abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
11244abf9869SNamhyung Kim 			    unsigned long data)
112576647323SAlexey Dobriyan {
112676647323SAlexey Dobriyan 	unsigned long tmp;
112776647323SAlexey Dobriyan 	int copied;
112876647323SAlexey Dobriyan 
112976647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
113076647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
113176647323SAlexey Dobriyan 		return -EIO;
113276647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
113376647323SAlexey Dobriyan }
1134f284ce72SAlexey Dobriyan 
11354abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
11364abf9869SNamhyung Kim 			    unsigned long data)
1137f284ce72SAlexey Dobriyan {
1138f284ce72SAlexey Dobriyan 	int copied;
1139f284ce72SAlexey Dobriyan 
1140f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
1141f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
1142f284ce72SAlexey Dobriyan }
1143032d82d9SRoland McGrath 
114496b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
1145032d82d9SRoland McGrath 
1146032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1147032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
1148032d82d9SRoland McGrath {
1149032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
1150032d82d9SRoland McGrath 	compat_ulong_t word;
1151e16b2781SRoland McGrath 	siginfo_t siginfo;
1152032d82d9SRoland McGrath 	int ret;
1153032d82d9SRoland McGrath 
1154032d82d9SRoland McGrath 	switch (request) {
1155032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
1156032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
1157032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
1158032d82d9SRoland McGrath 		if (ret != sizeof(word))
1159032d82d9SRoland McGrath 			ret = -EIO;
1160032d82d9SRoland McGrath 		else
1161032d82d9SRoland McGrath 			ret = put_user(word, datap);
1162032d82d9SRoland McGrath 		break;
1163032d82d9SRoland McGrath 
1164032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
1165032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
1166032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
1167032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
1168032d82d9SRoland McGrath 		break;
1169032d82d9SRoland McGrath 
1170032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
1171032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1172032d82d9SRoland McGrath 		break;
1173032d82d9SRoland McGrath 
1174e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
1175e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
1176e16b2781SRoland McGrath 		if (!ret)
1177e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
1178e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
1179e16b2781SRoland McGrath 				&siginfo);
1180e16b2781SRoland McGrath 		break;
1181e16b2781SRoland McGrath 
1182e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
1183e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
1184e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
1185e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
1186e16b2781SRoland McGrath 			ret = -EFAULT;
1187e16b2781SRoland McGrath 		else
1188e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
1189e16b2781SRoland McGrath 		break;
11902225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
11912225a122SSuresh Siddha 	case PTRACE_GETREGSET:
11922225a122SSuresh Siddha 	case PTRACE_SETREGSET:
11932225a122SSuresh Siddha 	{
11942225a122SSuresh Siddha 		struct iovec kiov;
11952225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
11962225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
11972225a122SSuresh Siddha 		compat_uptr_t ptr;
11982225a122SSuresh Siddha 		compat_size_t len;
11992225a122SSuresh Siddha 
12002225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
12012225a122SSuresh Siddha 			return -EFAULT;
12022225a122SSuresh Siddha 
12032225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
12042225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
12052225a122SSuresh Siddha 			return -EFAULT;
12062225a122SSuresh Siddha 
12072225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
12082225a122SSuresh Siddha 		kiov.iov_len = len;
12092225a122SSuresh Siddha 
12102225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
12112225a122SSuresh Siddha 		if (!ret)
12122225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
12132225a122SSuresh Siddha 		break;
12142225a122SSuresh Siddha 	}
12152225a122SSuresh Siddha #endif
1216e16b2781SRoland McGrath 
1217032d82d9SRoland McGrath 	default:
1218032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
1219032d82d9SRoland McGrath 	}
1220032d82d9SRoland McGrath 
1221032d82d9SRoland McGrath 	return ret;
1222032d82d9SRoland McGrath }
1223c269f196SRoland McGrath 
122462a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
122562a6fa97SHeiko Carstens 		       compat_long_t, addr, compat_long_t, data)
1226c269f196SRoland McGrath {
1227c269f196SRoland McGrath 	struct task_struct *child;
1228c269f196SRoland McGrath 	long ret;
1229c269f196SRoland McGrath 
1230c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
1231c269f196SRoland McGrath 		ret = ptrace_traceme();
1232c269f196SRoland McGrath 		goto out;
1233c269f196SRoland McGrath 	}
1234c269f196SRoland McGrath 
1235c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
1236c269f196SRoland McGrath 	if (IS_ERR(child)) {
1237c269f196SRoland McGrath 		ret = PTR_ERR(child);
1238c269f196SRoland McGrath 		goto out;
1239c269f196SRoland McGrath 	}
1240c269f196SRoland McGrath 
12413544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1242aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
1243c269f196SRoland McGrath 		/*
1244c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
1245c269f196SRoland McGrath 		 * a ptrace attach.
1246c269f196SRoland McGrath 		 */
1247c269f196SRoland McGrath 		if (!ret)
1248c269f196SRoland McGrath 			arch_ptrace_attach(child);
1249c269f196SRoland McGrath 		goto out_put_task_struct;
1250c269f196SRoland McGrath 	}
1251c269f196SRoland McGrath 
1252fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1253fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
12549899d11fSOleg Nesterov 	if (!ret) {
1255c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
12569899d11fSOleg Nesterov 		if (ret || request != PTRACE_DETACH)
12579899d11fSOleg Nesterov 			ptrace_unfreeze_traced(child);
12589899d11fSOleg Nesterov 	}
1259c269f196SRoland McGrath 
1260c269f196SRoland McGrath  out_put_task_struct:
1261c269f196SRoland McGrath 	put_task_struct(child);
1262c269f196SRoland McGrath  out:
1263c269f196SRoland McGrath 	return ret;
1264c269f196SRoland McGrath }
126596b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
1266