xref: /openbmc/linux/kernel/ptrace.c (revision f8e529ed)
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->ptrace = 0;
771da177e4SLinus Torvalds 	child->parent = child->real_parent;
78f470021aSRoland McGrath 	list_del_init(&child->ptrace_entry);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	spin_lock(&child->sighand->siglock);
810e9f0a4aSTejun Heo 
821da177e4SLinus Torvalds 	/*
8373ddff2bSTejun Heo 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
8473ddff2bSTejun Heo 	 * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
8573ddff2bSTejun Heo 	 */
8673ddff2bSTejun Heo 	task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
8773ddff2bSTejun Heo 	task_clear_jobctl_trapping(child);
8873ddff2bSTejun Heo 
8973ddff2bSTejun Heo 	/*
90a8f072c1STejun Heo 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
910e9f0a4aSTejun Heo 	 * @child isn't dead.
921da177e4SLinus Torvalds 	 */
930e9f0a4aSTejun Heo 	if (!(child->flags & PF_EXITING) &&
940e9f0a4aSTejun Heo 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
958a88951bSOleg Nesterov 	     child->signal->group_stop_count)) {
96a8f072c1STejun Heo 		child->jobctl |= JOBCTL_STOP_PENDING;
970e9f0a4aSTejun Heo 
980e9f0a4aSTejun Heo 		/*
998a88951bSOleg Nesterov 		 * This is only possible if this thread was cloned by the
1008a88951bSOleg Nesterov 		 * traced task running in the stopped group, set the signal
1018a88951bSOleg Nesterov 		 * for the future reports.
1028a88951bSOleg Nesterov 		 * FIXME: we should change ptrace_init_task() to handle this
1038a88951bSOleg Nesterov 		 * case.
1048a88951bSOleg Nesterov 		 */
1058a88951bSOleg Nesterov 		if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
1068a88951bSOleg Nesterov 			child->jobctl |= SIGSTOP;
1078a88951bSOleg Nesterov 	}
1088a88951bSOleg Nesterov 
1098a88951bSOleg Nesterov 	/*
1100e9f0a4aSTejun Heo 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
1110e9f0a4aSTejun Heo 	 * @child in the butt.  Note that @resume should be used iff @child
1120e9f0a4aSTejun Heo 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
1130e9f0a4aSTejun Heo 	 * TASK_KILLABLE sleeps.
1140e9f0a4aSTejun Heo 	 */
115a8f072c1STejun Heo 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
116910ffdb1SOleg Nesterov 		ptrace_signal_wake_up(child, true);
1170e9f0a4aSTejun Heo 
1181da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1219899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */
1229899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task)
1239899d11fSOleg Nesterov {
1249899d11fSOleg Nesterov 	bool ret = false;
1259899d11fSOleg Nesterov 
1269899d11fSOleg Nesterov 	/* Lockless, nobody but us can set this flag */
1279899d11fSOleg Nesterov 	if (task->jobctl & JOBCTL_LISTENING)
1289899d11fSOleg Nesterov 		return ret;
1299899d11fSOleg Nesterov 
1309899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1319899d11fSOleg Nesterov 	if (task_is_traced(task) && !__fatal_signal_pending(task)) {
1329899d11fSOleg Nesterov 		task->state = __TASK_TRACED;
1339899d11fSOleg Nesterov 		ret = true;
1349899d11fSOleg Nesterov 	}
1359899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1369899d11fSOleg Nesterov 
1379899d11fSOleg Nesterov 	return ret;
1389899d11fSOleg Nesterov }
1399899d11fSOleg Nesterov 
1409899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task)
1419899d11fSOleg Nesterov {
1429899d11fSOleg Nesterov 	if (task->state != __TASK_TRACED)
1439899d11fSOleg Nesterov 		return;
1449899d11fSOleg Nesterov 
1459899d11fSOleg Nesterov 	WARN_ON(!task->ptrace || task->parent != current);
1469899d11fSOleg Nesterov 
1479899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1489899d11fSOleg Nesterov 	if (__fatal_signal_pending(task))
1499899d11fSOleg Nesterov 		wake_up_state(task, __TASK_TRACED);
1509899d11fSOleg Nesterov 	else
1519899d11fSOleg Nesterov 		task->state = TASK_TRACED;
1529899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1539899d11fSOleg Nesterov }
1549899d11fSOleg Nesterov 
155755e276bSTejun Heo /**
156755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
157755e276bSTejun Heo  * @child: ptracee to check for
158755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
159755e276bSTejun Heo  *
160755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
161755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
162755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
163755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
164755e276bSTejun Heo  * state.
165755e276bSTejun Heo  *
166755e276bSTejun Heo  * CONTEXT:
167755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
168755e276bSTejun Heo  *
169755e276bSTejun Heo  * RETURNS:
170755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1711da177e4SLinus Torvalds  */
172edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	int ret = -ESRCH;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/*
1771da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1781da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1791da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1801da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1811da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1821da177e4SLinus Torvalds 	 */
1831da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
1849899d11fSOleg Nesterov 	if (child->ptrace && child->parent == current) {
1859899d11fSOleg Nesterov 		WARN_ON(child->state == __TASK_TRACED);
186c0c0b649SOleg Nesterov 		/*
187c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
188c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
189c0c0b649SOleg Nesterov 		 */
1909899d11fSOleg Nesterov 		if (ignore_state || ptrace_freeze_traced(child))
191321fb561SOleg Nesterov 			ret = 0;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1941da177e4SLinus Torvalds 
1959899d11fSOleg Nesterov 	if (!ret && !ignore_state) {
1969899d11fSOleg Nesterov 		if (!wait_task_inactive(child, __TASK_TRACED)) {
1979899d11fSOleg Nesterov 			/*
1989899d11fSOleg Nesterov 			 * This can only happen if may_ptrace_stop() fails and
1999899d11fSOleg Nesterov 			 * ptrace_stop() changes ->state back to TASK_RUNNING,
2009899d11fSOleg Nesterov 			 * so we should not worry about leaking __TASK_TRACED.
2019899d11fSOleg Nesterov 			 */
2029899d11fSOleg Nesterov 			WARN_ON(child->state == __TASK_TRACED);
2039899d11fSOleg Nesterov 			ret = -ESRCH;
2049899d11fSOleg Nesterov 		}
2059899d11fSOleg Nesterov 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	return ret;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
21069f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
21169f594a3SEric Paris {
21269f594a3SEric Paris 	if (mode & PTRACE_MODE_NOAUDIT)
21369f594a3SEric Paris 		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
21469f594a3SEric Paris 	else
21569f594a3SEric Paris 		return has_ns_capability(current, ns, CAP_SYS_PTRACE);
21669f594a3SEric Paris }
21769f594a3SEric Paris 
2189f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */
2199f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
220ab8d11beSMiklos Szeredi {
221c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
222b6dff3ecSDavid Howells 
223df26c40eSEric W. Biederman 	/* May we inspect the given task?
224df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
225df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
226df26c40eSEric W. Biederman 	 *
227df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
228df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
229df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
230df26c40eSEric W. Biederman 	 */
231df26c40eSEric W. Biederman 	int dumpable = 0;
232df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
23373af963fSMark Grondona 	if (same_thread_group(task, current))
234df26c40eSEric W. Biederman 		return 0;
235c69e8d9cSDavid Howells 	rcu_read_lock();
236c69e8d9cSDavid Howells 	tcred = __task_cred(task);
2375af66203SEric W. Biederman 	if (uid_eq(cred->uid, tcred->euid) &&
2385af66203SEric W. Biederman 	    uid_eq(cred->uid, tcred->suid) &&
2395af66203SEric W. Biederman 	    uid_eq(cred->uid, tcred->uid)  &&
2405af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->egid) &&
2415af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->sgid) &&
2425af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->gid))
2438409cca7SSerge E. Hallyn 		goto ok;
244c4a4d603SEric W. Biederman 	if (ptrace_has_cap(tcred->user_ns, mode))
2458409cca7SSerge E. Hallyn 		goto ok;
246c69e8d9cSDavid Howells 	rcu_read_unlock();
247ab8d11beSMiklos Szeredi 	return -EPERM;
2488409cca7SSerge E. Hallyn ok:
249c69e8d9cSDavid Howells 	rcu_read_unlock();
250ab8d11beSMiklos Szeredi 	smp_rmb();
251df26c40eSEric W. Biederman 	if (task->mm)
2526c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
2534c44aaafSEric W. Biederman 	rcu_read_lock();
254d049f74fSKees Cook 	if (dumpable != SUID_DUMP_USER &&
255d049f74fSKees Cook 	    !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
2564c44aaafSEric W. Biederman 		rcu_read_unlock();
257ab8d11beSMiklos Szeredi 		return -EPERM;
2584c44aaafSEric W. Biederman 	}
2594c44aaafSEric W. Biederman 	rcu_read_unlock();
260ab8d11beSMiklos Szeredi 
2619e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
262ab8d11beSMiklos Szeredi }
263ab8d11beSMiklos Szeredi 
264006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
265ab8d11beSMiklos Szeredi {
266ab8d11beSMiklos Szeredi 	int err;
267ab8d11beSMiklos Szeredi 	task_lock(task);
268006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
269ab8d11beSMiklos Szeredi 	task_unlock(task);
2703a709703SRoland McGrath 	return !err;
271ab8d11beSMiklos Szeredi }
272ab8d11beSMiklos Szeredi 
2733544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request,
274aa9147c9SDenys Vlasenko 			 unsigned long addr,
2753544d72aSTejun Heo 			 unsigned long flags)
2761da177e4SLinus Torvalds {
2773544d72aSTejun Heo 	bool seize = (request == PTRACE_SEIZE);
2781da177e4SLinus Torvalds 	int retval;
279f5b40e36SLinus Torvalds 
2803544d72aSTejun Heo 	retval = -EIO;
281aa9147c9SDenys Vlasenko 	if (seize) {
282aa9147c9SDenys Vlasenko 		if (addr != 0)
2833544d72aSTejun Heo 			goto out;
284aa9147c9SDenys Vlasenko 		if (flags & ~(unsigned long)PTRACE_O_MASK)
285aa9147c9SDenys Vlasenko 			goto out;
286aa9147c9SDenys Vlasenko 		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
287aa9147c9SDenys Vlasenko 	} else {
288aa9147c9SDenys Vlasenko 		flags = PT_PTRACED;
289aa9147c9SDenys Vlasenko 	}
2903544d72aSTejun Heo 
291a5cb013dSAl Viro 	audit_ptrace(task);
292a5cb013dSAl Viro 
2931da177e4SLinus Torvalds 	retval = -EPERM;
294b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
295b79b7ba9SOleg Nesterov 		goto out;
296bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
297f5b40e36SLinus Torvalds 		goto out;
298f5b40e36SLinus Torvalds 
299f2f0b00aSOleg Nesterov 	/*
300f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
30186b6c1f3SDenys Vlasenko 	 * SUID, SGID and LSM creds get determined differently
3025e751e99SDavid Howells 	 * under ptrace.
303d84f4f99SDavid Howells 	 */
304793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
3059b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
306d84f4f99SDavid Howells 		goto out;
3074b105cbbSOleg Nesterov 
308f5b40e36SLinus Torvalds 	task_lock(task);
309006ebb40SStephen Smalley 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
3104b105cbbSOleg Nesterov 	task_unlock(task);
3111da177e4SLinus Torvalds 	if (retval)
3124b105cbbSOleg Nesterov 		goto unlock_creds;
3131da177e4SLinus Torvalds 
3144b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
315b79b7ba9SOleg Nesterov 	retval = -EPERM;
316b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
3174b105cbbSOleg Nesterov 		goto unlock_tasklist;
318f2f0b00aSOleg Nesterov 	if (task->ptrace)
3194b105cbbSOleg Nesterov 		goto unlock_tasklist;
320b79b7ba9SOleg Nesterov 
3213544d72aSTejun Heo 	if (seize)
322aa9147c9SDenys Vlasenko 		flags |= PT_SEIZED;
3234c44aaafSEric W. Biederman 	rcu_read_lock();
3244c44aaafSEric W. Biederman 	if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE))
325aa9147c9SDenys Vlasenko 		flags |= PT_PTRACE_CAP;
3264c44aaafSEric W. Biederman 	rcu_read_unlock();
327aa9147c9SDenys Vlasenko 	task->ptrace = flags;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	__ptrace_link(task, current);
3303544d72aSTejun Heo 
3313544d72aSTejun Heo 	/* SEIZE doesn't trap tracee on attach */
3323544d72aSTejun Heo 	if (!seize)
33333e9fc7dSOleg Nesterov 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
334b79b7ba9SOleg Nesterov 
335d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
336d79fdd6dSTejun Heo 
337d79fdd6dSTejun Heo 	/*
33873ddff2bSTejun Heo 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
339d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
340d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
341d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
342d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
343d79fdd6dSTejun Heo 	 * function.
344d79fdd6dSTejun Heo 	 *
345d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
346d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
347d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
348d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
349d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
350d79fdd6dSTejun Heo 	 *
351d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
352d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
353d79fdd6dSTejun Heo 	 */
3547dd3db54STejun Heo 	if (task_is_stopped(task) &&
35573ddff2bSTejun Heo 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
356910ffdb1SOleg Nesterov 		signal_wake_up_state(task, __TASK_STOPPED);
357d79fdd6dSTejun Heo 
358d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
359d79fdd6dSTejun Heo 
360b79b7ba9SOleg Nesterov 	retval = 0;
3614b105cbbSOleg Nesterov unlock_tasklist:
3624b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3634b105cbbSOleg Nesterov unlock_creds:
3649b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
365f5b40e36SLinus Torvalds out:
366f701e5b7SVladimir Zapolskiy 	if (!retval) {
36762c124ffSTejun Heo 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
36874316201SNeilBrown 			    TASK_UNINTERRUPTIBLE);
369f701e5b7SVladimir Zapolskiy 		proc_ptrace_connector(task, PTRACE_ATTACH);
370f701e5b7SVladimir Zapolskiy 	}
371f701e5b7SVladimir Zapolskiy 
3721da177e4SLinus Torvalds 	return retval;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
375f2f0b00aSOleg Nesterov /**
376f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
377f2f0b00aSOleg Nesterov  *
378f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
379f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
380f2f0b00aSOleg Nesterov  */
381e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
382f2f0b00aSOleg Nesterov {
383f2f0b00aSOleg Nesterov 	int ret = -EPERM;
384f2f0b00aSOleg Nesterov 
3854b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
3864b105cbbSOleg Nesterov 	/* Are we already being traced? */
387f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
388f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
389f2f0b00aSOleg Nesterov 		/*
390f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
391f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
392f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
393f2f0b00aSOleg Nesterov 		 */
394f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
395f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
396f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
397f2f0b00aSOleg Nesterov 		}
398f2f0b00aSOleg Nesterov 	}
3994b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
4004b105cbbSOleg Nesterov 
401f2f0b00aSOleg Nesterov 	return ret;
402f2f0b00aSOleg Nesterov }
403f2f0b00aSOleg Nesterov 
40439c626aeSOleg Nesterov /*
40539c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
40639c626aeSOleg Nesterov  */
40739c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
40839c626aeSOleg Nesterov {
40939c626aeSOleg Nesterov 	int ret;
41039c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
41139c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
41239c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
41339c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
41439c626aeSOleg Nesterov 	return ret;
41539c626aeSOleg Nesterov }
41639c626aeSOleg Nesterov 
41739c626aeSOleg Nesterov /*
41839c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
41939c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
42039c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
42139c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
42239c626aeSOleg Nesterov  *
42339c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
42439c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
42539c626aeSOleg Nesterov  * If it should reap itself, return true.
42639c626aeSOleg Nesterov  *
427a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
428a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
429a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
430a7f0765eSOleg Nesterov  * do_wait().
43139c626aeSOleg Nesterov  */
43239c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
43339c626aeSOleg Nesterov {
4349843a1e9SOleg Nesterov 	bool dead;
4359843a1e9SOleg Nesterov 
43639c626aeSOleg Nesterov 	__ptrace_unlink(p);
43739c626aeSOleg Nesterov 
4389843a1e9SOleg Nesterov 	if (p->exit_state != EXIT_ZOMBIE)
4399843a1e9SOleg Nesterov 		return false;
4409843a1e9SOleg Nesterov 
4419843a1e9SOleg Nesterov 	dead = !thread_group_leader(p);
4429843a1e9SOleg Nesterov 
4439843a1e9SOleg Nesterov 	if (!dead && thread_group_empty(p)) {
44439c626aeSOleg Nesterov 		if (!same_thread_group(p->real_parent, tracer))
4459843a1e9SOleg Nesterov 			dead = do_notify_parent(p, p->exit_signal);
446a7f0765eSOleg Nesterov 		else if (ignoring_children(tracer->sighand)) {
447a7f0765eSOleg Nesterov 			__wake_up_parent(p, tracer);
4489843a1e9SOleg Nesterov 			dead = true;
44939c626aeSOleg Nesterov 		}
450a7f0765eSOleg Nesterov 	}
45139c626aeSOleg Nesterov 	/* Mark it as in the process of being reaped. */
4529843a1e9SOleg Nesterov 	if (dead)
45339c626aeSOleg Nesterov 		p->exit_state = EXIT_DEAD;
4549843a1e9SOleg Nesterov 	return dead;
45539c626aeSOleg Nesterov }
45639c626aeSOleg Nesterov 
457e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
4581da177e4SLinus Torvalds {
4597ed20e1aSJesper Juhl 	if (!valid_signal(data))
4601da177e4SLinus Torvalds 		return -EIO;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
4631da177e4SLinus Torvalds 	ptrace_disable(child);
4647d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
4651da177e4SLinus Torvalds 
46695c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
46739c626aeSOleg Nesterov 	/*
46864a4096cSOleg Nesterov 	 * We rely on ptrace_freeze_traced(). It can't be killed and
46964a4096cSOleg Nesterov 	 * untraced by another thread, it can't be a zombie.
47039c626aeSOleg Nesterov 	 */
47164a4096cSOleg Nesterov 	WARN_ON(!child->ptrace || child->exit_state);
47264a4096cSOleg Nesterov 	/*
47364a4096cSOleg Nesterov 	 * tasklist_lock avoids the race with wait_task_stopped(), see
47464a4096cSOleg Nesterov 	 * the comment in ptrace_resume().
47564a4096cSOleg Nesterov 	 */
47695c3eb76SOleg Nesterov 	child->exit_code = data;
47764a4096cSOleg Nesterov 	__ptrace_detach(current, child);
4781da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
4791da177e4SLinus Torvalds 
480f701e5b7SVladimir Zapolskiy 	proc_ptrace_connector(child, PTRACE_DETACH);
4814576145cSOleg Nesterov 
4821da177e4SLinus Torvalds 	return 0;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
48539c626aeSOleg Nesterov /*
486c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
4877c8bd232SOleg Nesterov  * for writing.
48839c626aeSOleg Nesterov  */
4897c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
49039c626aeSOleg Nesterov {
49139c626aeSOleg Nesterov 	struct task_struct *p, *n;
492c7e49c14SOleg Nesterov 
49339c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
494992fb6e1SOleg Nesterov 		if (unlikely(p->ptrace & PT_EXITKILL))
495992fb6e1SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
496992fb6e1SOleg Nesterov 
49739c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
4987c8bd232SOleg Nesterov 			list_add(&p->ptrace_entry, dead);
49939c626aeSOleg Nesterov 	}
50039c626aeSOleg Nesterov }
50139c626aeSOleg Nesterov 
5021da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	int copied = 0;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	while (len > 0) {
5071da177e4SLinus Torvalds 		char buf[128];
5081da177e4SLinus Torvalds 		int this_len, retval;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5111da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
5121da177e4SLinus Torvalds 		if (!retval) {
5131da177e4SLinus Torvalds 			if (copied)
5141da177e4SLinus Torvalds 				break;
5151da177e4SLinus Torvalds 			return -EIO;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
5181da177e4SLinus Torvalds 			return -EFAULT;
5191da177e4SLinus Torvalds 		copied += retval;
5201da177e4SLinus Torvalds 		src += retval;
5211da177e4SLinus Torvalds 		dst += retval;
5221da177e4SLinus Torvalds 		len -= retval;
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 	return copied;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	int copied = 0;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	while (len > 0) {
5321da177e4SLinus Torvalds 		char buf[128];
5331da177e4SLinus Torvalds 		int this_len, retval;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5361da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
5371da177e4SLinus Torvalds 			return -EFAULT;
5381da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
5391da177e4SLinus Torvalds 		if (!retval) {
5401da177e4SLinus Torvalds 			if (copied)
5411da177e4SLinus Torvalds 				break;
5421da177e4SLinus Torvalds 			return -EIO;
5431da177e4SLinus Torvalds 		}
5441da177e4SLinus Torvalds 		copied += retval;
5451da177e4SLinus Torvalds 		src += retval;
5461da177e4SLinus Torvalds 		dst += retval;
5471da177e4SLinus Torvalds 		len -= retval;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	return copied;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5524abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
5531da177e4SLinus Torvalds {
55486b6c1f3SDenys Vlasenko 	unsigned flags;
55586b6c1f3SDenys Vlasenko 
5568c5cf9e5SDenys Vlasenko 	if (data & ~(unsigned long)PTRACE_O_MASK)
5578c5cf9e5SDenys Vlasenko 		return -EINVAL;
5588c5cf9e5SDenys Vlasenko 
55913c4a901STycho Andersen 	if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
56013c4a901STycho Andersen 		if (!config_enabled(CONFIG_CHECKPOINT_RESTORE) ||
56113c4a901STycho Andersen 		    !config_enabled(CONFIG_SECCOMP))
56213c4a901STycho Andersen 			return -EINVAL;
56313c4a901STycho Andersen 
56413c4a901STycho Andersen 		if (!capable(CAP_SYS_ADMIN))
56513c4a901STycho Andersen 			return -EPERM;
56613c4a901STycho Andersen 
56713c4a901STycho Andersen 		if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
56813c4a901STycho Andersen 		    current->ptrace & PT_SUSPEND_SECCOMP)
56913c4a901STycho Andersen 			return -EPERM;
57013c4a901STycho Andersen 	}
57113c4a901STycho Andersen 
57286b6c1f3SDenys Vlasenko 	/* Avoid intermediate state when all opts are cleared */
57386b6c1f3SDenys Vlasenko 	flags = child->ptrace;
57486b6c1f3SDenys Vlasenko 	flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
57586b6c1f3SDenys Vlasenko 	flags |= (data << PT_OPT_FLAG_SHIFT);
57686b6c1f3SDenys Vlasenko 	child->ptrace = flags;
5771da177e4SLinus Torvalds 
5788c5cf9e5SDenys Vlasenko 	return 0;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
581e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
5821da177e4SLinus Torvalds {
583e4961254SOleg Nesterov 	unsigned long flags;
5841da177e4SLinus Torvalds 	int error = -ESRCH;
5851da177e4SLinus Torvalds 
586e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5871da177e4SLinus Torvalds 		error = -EINVAL;
5881da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
589e16b2781SRoland McGrath 			*info = *child->last_siginfo;
5901da177e4SLinus Torvalds 			error = 0;
5911da177e4SLinus Torvalds 		}
592e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	return error;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
597e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
5981da177e4SLinus Torvalds {
599e4961254SOleg Nesterov 	unsigned long flags;
6001da177e4SLinus Torvalds 	int error = -ESRCH;
6011da177e4SLinus Torvalds 
602e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
6031da177e4SLinus Torvalds 		error = -EINVAL;
6041da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
605e16b2781SRoland McGrath 			*child->last_siginfo = *info;
6061da177e4SLinus Torvalds 			error = 0;
6071da177e4SLinus Torvalds 		}
608e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	return error;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
61384c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child,
61484c751bdSAndrey Vagin 				unsigned long addr,
61584c751bdSAndrey Vagin 				unsigned long data)
61684c751bdSAndrey Vagin {
61784c751bdSAndrey Vagin 	struct ptrace_peeksiginfo_args arg;
61884c751bdSAndrey Vagin 	struct sigpending *pending;
61984c751bdSAndrey Vagin 	struct sigqueue *q;
62084c751bdSAndrey Vagin 	int ret, i;
62184c751bdSAndrey Vagin 
62284c751bdSAndrey Vagin 	ret = copy_from_user(&arg, (void __user *) addr,
62384c751bdSAndrey Vagin 				sizeof(struct ptrace_peeksiginfo_args));
62484c751bdSAndrey Vagin 	if (ret)
62584c751bdSAndrey Vagin 		return -EFAULT;
62684c751bdSAndrey Vagin 
62784c751bdSAndrey Vagin 	if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
62884c751bdSAndrey Vagin 		return -EINVAL; /* unknown flags */
62984c751bdSAndrey Vagin 
63084c751bdSAndrey Vagin 	if (arg.nr < 0)
63184c751bdSAndrey Vagin 		return -EINVAL;
63284c751bdSAndrey Vagin 
63384c751bdSAndrey Vagin 	if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
63484c751bdSAndrey Vagin 		pending = &child->signal->shared_pending;
63584c751bdSAndrey Vagin 	else
63684c751bdSAndrey Vagin 		pending = &child->pending;
63784c751bdSAndrey Vagin 
63884c751bdSAndrey Vagin 	for (i = 0; i < arg.nr; ) {
63984c751bdSAndrey Vagin 		siginfo_t info;
64084c751bdSAndrey Vagin 		s32 off = arg.off + i;
64184c751bdSAndrey Vagin 
64284c751bdSAndrey Vagin 		spin_lock_irq(&child->sighand->siglock);
64384c751bdSAndrey Vagin 		list_for_each_entry(q, &pending->list, list) {
64484c751bdSAndrey Vagin 			if (!off--) {
64584c751bdSAndrey Vagin 				copy_siginfo(&info, &q->info);
64684c751bdSAndrey Vagin 				break;
64784c751bdSAndrey Vagin 			}
64884c751bdSAndrey Vagin 		}
64984c751bdSAndrey Vagin 		spin_unlock_irq(&child->sighand->siglock);
65084c751bdSAndrey Vagin 
65184c751bdSAndrey Vagin 		if (off >= 0) /* beyond the end of the list */
65284c751bdSAndrey Vagin 			break;
65384c751bdSAndrey Vagin 
65484c751bdSAndrey Vagin #ifdef CONFIG_COMPAT
65584c751bdSAndrey Vagin 		if (unlikely(is_compat_task())) {
65684c751bdSAndrey Vagin 			compat_siginfo_t __user *uinfo = compat_ptr(data);
65784c751bdSAndrey Vagin 
658706b23bdSMathieu Desnoyers 			if (copy_siginfo_to_user32(uinfo, &info) ||
659706b23bdSMathieu Desnoyers 			    __put_user(info.si_code, &uinfo->si_code)) {
660706b23bdSMathieu Desnoyers 				ret = -EFAULT;
661706b23bdSMathieu Desnoyers 				break;
662706b23bdSMathieu Desnoyers 			}
663706b23bdSMathieu Desnoyers 
66484c751bdSAndrey Vagin 		} else
66584c751bdSAndrey Vagin #endif
66684c751bdSAndrey Vagin 		{
66784c751bdSAndrey Vagin 			siginfo_t __user *uinfo = (siginfo_t __user *) data;
66884c751bdSAndrey Vagin 
669706b23bdSMathieu Desnoyers 			if (copy_siginfo_to_user(uinfo, &info) ||
670706b23bdSMathieu Desnoyers 			    __put_user(info.si_code, &uinfo->si_code)) {
67184c751bdSAndrey Vagin 				ret = -EFAULT;
67284c751bdSAndrey Vagin 				break;
67384c751bdSAndrey Vagin 			}
674706b23bdSMathieu Desnoyers 		}
67584c751bdSAndrey Vagin 
67684c751bdSAndrey Vagin 		data += sizeof(siginfo_t);
67784c751bdSAndrey Vagin 		i++;
67884c751bdSAndrey Vagin 
67984c751bdSAndrey Vagin 		if (signal_pending(current))
68084c751bdSAndrey Vagin 			break;
68184c751bdSAndrey Vagin 
68284c751bdSAndrey Vagin 		cond_resched();
68384c751bdSAndrey Vagin 	}
68484c751bdSAndrey Vagin 
68584c751bdSAndrey Vagin 	if (i > 0)
68684c751bdSAndrey Vagin 		return i;
68784c751bdSAndrey Vagin 
68884c751bdSAndrey Vagin 	return ret;
68984c751bdSAndrey Vagin }
69036df29d7SRoland McGrath 
69136df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
69236df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
69336df29d7SRoland McGrath #else
69436df29d7SRoland McGrath #define is_singlestep(request)		0
69536df29d7SRoland McGrath #endif
69636df29d7SRoland McGrath 
6975b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
6985b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
6995b88abbfSRoland McGrath #else
7005b88abbfSRoland McGrath #define is_singleblock(request)		0
7015b88abbfSRoland McGrath #endif
7025b88abbfSRoland McGrath 
70336df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
70436df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
70536df29d7SRoland McGrath #else
70636df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
70736df29d7SRoland McGrath #endif
70836df29d7SRoland McGrath 
7094abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
7104abf9869SNamhyung Kim 			 unsigned long data)
71136df29d7SRoland McGrath {
712b72c1869SOleg Nesterov 	bool need_siglock;
713b72c1869SOleg Nesterov 
71436df29d7SRoland McGrath 	if (!valid_signal(data))
71536df29d7SRoland McGrath 		return -EIO;
71636df29d7SRoland McGrath 
71736df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
71836df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
71936df29d7SRoland McGrath 	else
72036df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
72136df29d7SRoland McGrath 
72236df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
72336df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
72436df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
72536df29d7SRoland McGrath 	else
72636df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
72736df29d7SRoland McGrath #endif
72836df29d7SRoland McGrath 
7295b88abbfSRoland McGrath 	if (is_singleblock(request)) {
7305b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
7315b88abbfSRoland McGrath 			return -EIO;
7325b88abbfSRoland McGrath 		user_enable_block_step(child);
7335b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
73436df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
73536df29d7SRoland McGrath 			return -EIO;
73636df29d7SRoland McGrath 		user_enable_single_step(child);
7373a709703SRoland McGrath 	} else {
73836df29d7SRoland McGrath 		user_disable_single_step(child);
7393a709703SRoland McGrath 	}
74036df29d7SRoland McGrath 
741b72c1869SOleg Nesterov 	/*
742b72c1869SOleg Nesterov 	 * Change ->exit_code and ->state under siglock to avoid the race
743b72c1869SOleg Nesterov 	 * with wait_task_stopped() in between; a non-zero ->exit_code will
744b72c1869SOleg Nesterov 	 * wrongly look like another report from tracee.
745b72c1869SOleg Nesterov 	 *
746b72c1869SOleg Nesterov 	 * Note that we need siglock even if ->exit_code == data and/or this
747b72c1869SOleg Nesterov 	 * status was not reported yet, the new status must not be cleared by
748b72c1869SOleg Nesterov 	 * wait_task_stopped() after resume.
749b72c1869SOleg Nesterov 	 *
750b72c1869SOleg Nesterov 	 * If data == 0 we do not care if wait_task_stopped() reports the old
751b72c1869SOleg Nesterov 	 * status and clears the code too; this can't race with the tracee, it
752b72c1869SOleg Nesterov 	 * takes siglock after resume.
753b72c1869SOleg Nesterov 	 */
754b72c1869SOleg Nesterov 	need_siglock = data && !thread_group_empty(current);
755b72c1869SOleg Nesterov 	if (need_siglock)
756b72c1869SOleg Nesterov 		spin_lock_irq(&child->sighand->siglock);
75736df29d7SRoland McGrath 	child->exit_code = data;
7580666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
759b72c1869SOleg Nesterov 	if (need_siglock)
760b72c1869SOleg Nesterov 		spin_unlock_irq(&child->sighand->siglock);
76136df29d7SRoland McGrath 
76236df29d7SRoland McGrath 	return 0;
76336df29d7SRoland McGrath }
76436df29d7SRoland McGrath 
7652225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
7662225a122SSuresh Siddha 
7672225a122SSuresh Siddha static const struct user_regset *
7682225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
7692225a122SSuresh Siddha {
7702225a122SSuresh Siddha 	const struct user_regset *regset;
7712225a122SSuresh Siddha 	int n;
7722225a122SSuresh Siddha 
7732225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
7742225a122SSuresh Siddha 		regset = view->regsets + n;
7752225a122SSuresh Siddha 		if (regset->core_note_type == type)
7762225a122SSuresh Siddha 			return regset;
7772225a122SSuresh Siddha 	}
7782225a122SSuresh Siddha 
7792225a122SSuresh Siddha 	return NULL;
7802225a122SSuresh Siddha }
7812225a122SSuresh Siddha 
7822225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
7832225a122SSuresh Siddha 			 struct iovec *kiov)
7842225a122SSuresh Siddha {
7852225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
7862225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
7872225a122SSuresh Siddha 	int regset_no;
7882225a122SSuresh Siddha 
7892225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
790c6a0dd7eSSuresh Siddha 		return -EINVAL;
7912225a122SSuresh Siddha 
7922225a122SSuresh Siddha 	regset_no = regset - view->regsets;
7932225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
7942225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
7952225a122SSuresh Siddha 
7962225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
7972225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
7982225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
7992225a122SSuresh Siddha 	else
8002225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
8012225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
8022225a122SSuresh Siddha }
8032225a122SSuresh Siddha 
804e8440c14SJosh Stone /*
805e8440c14SJosh Stone  * This is declared in linux/regset.h and defined in machine-dependent
806e8440c14SJosh Stone  * code.  We put the export here, near the primary machine-neutral use,
807e8440c14SJosh Stone  * to ensure no machine forgets it.
808e8440c14SJosh Stone  */
809e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view);
8102225a122SSuresh Siddha #endif
8112225a122SSuresh Siddha 
8121da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
8134abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
8141da177e4SLinus Torvalds {
815fca26f26STejun Heo 	bool seized = child->ptrace & PT_SEIZED;
8161da177e4SLinus Torvalds 	int ret = -EIO;
817544b2c91STejun Heo 	siginfo_t siginfo, *si;
8189fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
8199fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
820fca26f26STejun Heo 	unsigned long flags;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	switch (request) {
82316c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
82416c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
82516c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
82616c3e389SRoland McGrath 	case PTRACE_POKETEXT:
82716c3e389SRoland McGrath 	case PTRACE_POKEDATA:
82816c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
82916c3e389SRoland McGrath 
8301da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
8311da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
8321da177e4SLinus Torvalds #endif
8331da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
8341da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
8351da177e4SLinus Torvalds 		break;
8361da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
8379fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
8381da177e4SLinus Torvalds 		break;
839e16b2781SRoland McGrath 
84084c751bdSAndrey Vagin 	case PTRACE_PEEKSIGINFO:
84184c751bdSAndrey Vagin 		ret = ptrace_peek_siginfo(child, addr, data);
84284c751bdSAndrey Vagin 		break;
84384c751bdSAndrey Vagin 
8441da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
845e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
846e16b2781SRoland McGrath 		if (!ret)
8479fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
8481da177e4SLinus Torvalds 		break;
849e16b2781SRoland McGrath 
8501da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
8519fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
852e16b2781SRoland McGrath 			ret = -EFAULT;
853e16b2781SRoland McGrath 		else
854e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
8551da177e4SLinus Torvalds 		break;
856e16b2781SRoland McGrath 
85729000caeSAndrey Vagin 	case PTRACE_GETSIGMASK:
85829000caeSAndrey Vagin 		if (addr != sizeof(sigset_t)) {
85929000caeSAndrey Vagin 			ret = -EINVAL;
86029000caeSAndrey Vagin 			break;
86129000caeSAndrey Vagin 		}
86229000caeSAndrey Vagin 
86329000caeSAndrey Vagin 		if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
86429000caeSAndrey Vagin 			ret = -EFAULT;
86529000caeSAndrey Vagin 		else
86629000caeSAndrey Vagin 			ret = 0;
86729000caeSAndrey Vagin 
86829000caeSAndrey Vagin 		break;
86929000caeSAndrey Vagin 
87029000caeSAndrey Vagin 	case PTRACE_SETSIGMASK: {
87129000caeSAndrey Vagin 		sigset_t new_set;
87229000caeSAndrey Vagin 
87329000caeSAndrey Vagin 		if (addr != sizeof(sigset_t)) {
87429000caeSAndrey Vagin 			ret = -EINVAL;
87529000caeSAndrey Vagin 			break;
87629000caeSAndrey Vagin 		}
87729000caeSAndrey Vagin 
87829000caeSAndrey Vagin 		if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
87929000caeSAndrey Vagin 			ret = -EFAULT;
88029000caeSAndrey Vagin 			break;
88129000caeSAndrey Vagin 		}
88229000caeSAndrey Vagin 
88329000caeSAndrey Vagin 		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
88429000caeSAndrey Vagin 
88529000caeSAndrey Vagin 		/*
88629000caeSAndrey Vagin 		 * Every thread does recalc_sigpending() after resume, so
88729000caeSAndrey Vagin 		 * retarget_shared_pending() and recalc_sigpending() are not
88829000caeSAndrey Vagin 		 * called here.
88929000caeSAndrey Vagin 		 */
89029000caeSAndrey Vagin 		spin_lock_irq(&child->sighand->siglock);
89129000caeSAndrey Vagin 		child->blocked = new_set;
89229000caeSAndrey Vagin 		spin_unlock_irq(&child->sighand->siglock);
89329000caeSAndrey Vagin 
89429000caeSAndrey Vagin 		ret = 0;
89529000caeSAndrey Vagin 		break;
89629000caeSAndrey Vagin 	}
89729000caeSAndrey Vagin 
898fca26f26STejun Heo 	case PTRACE_INTERRUPT:
899fca26f26STejun Heo 		/*
900fca26f26STejun Heo 		 * Stop tracee without any side-effect on signal or job
901fca26f26STejun Heo 		 * control.  At least one trap is guaranteed to happen
902fca26f26STejun Heo 		 * after this request.  If @child is already trapped, the
903fca26f26STejun Heo 		 * current trap is not disturbed and another trap will
904fca26f26STejun Heo 		 * happen after the current trap is ended with PTRACE_CONT.
905fca26f26STejun Heo 		 *
906fca26f26STejun Heo 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
907fca26f26STejun Heo 		 * the pending condition is cleared regardless.
908fca26f26STejun Heo 		 */
909fca26f26STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
910fca26f26STejun Heo 			break;
911fca26f26STejun Heo 
912544b2c91STejun Heo 		/*
913544b2c91STejun Heo 		 * INTERRUPT doesn't disturb existing trap sans one
914544b2c91STejun Heo 		 * exception.  If ptracer issued LISTEN for the current
915544b2c91STejun Heo 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
916544b2c91STejun Heo 		 * tracee into STOP.
917544b2c91STejun Heo 		 */
918fca26f26STejun Heo 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
919910ffdb1SOleg Nesterov 			ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
920544b2c91STejun Heo 
921544b2c91STejun Heo 		unlock_task_sighand(child, &flags);
922544b2c91STejun Heo 		ret = 0;
923544b2c91STejun Heo 		break;
924544b2c91STejun Heo 
925544b2c91STejun Heo 	case PTRACE_LISTEN:
926544b2c91STejun Heo 		/*
927544b2c91STejun Heo 		 * Listen for events.  Tracee must be in STOP.  It's not
928544b2c91STejun Heo 		 * resumed per-se but is not considered to be in TRACED by
929544b2c91STejun Heo 		 * wait(2) or ptrace(2).  If an async event (e.g. group
930544b2c91STejun Heo 		 * stop state change) happens, tracee will enter STOP trap
931544b2c91STejun Heo 		 * again.  Alternatively, ptracer can issue INTERRUPT to
932544b2c91STejun Heo 		 * finish listening and re-trap tracee into STOP.
933544b2c91STejun Heo 		 */
934544b2c91STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
935544b2c91STejun Heo 			break;
936544b2c91STejun Heo 
937544b2c91STejun Heo 		si = child->last_siginfo;
938f9d81f61SOleg Nesterov 		if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
939544b2c91STejun Heo 			child->jobctl |= JOBCTL_LISTENING;
940544b2c91STejun Heo 			/*
941f9d81f61SOleg Nesterov 			 * If NOTIFY is set, it means event happened between
942f9d81f61SOleg Nesterov 			 * start of this trap and now.  Trigger re-trap.
943544b2c91STejun Heo 			 */
944544b2c91STejun Heo 			if (child->jobctl & JOBCTL_TRAP_NOTIFY)
945910ffdb1SOleg Nesterov 				ptrace_signal_wake_up(child, true);
946fca26f26STejun Heo 			ret = 0;
947f9d81f61SOleg Nesterov 		}
948f9d81f61SOleg Nesterov 		unlock_task_sighand(child, &flags);
949fca26f26STejun Heo 		break;
950fca26f26STejun Heo 
9511bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
9521bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
9531bcf5482SAlexey Dobriyan 		break;
95436df29d7SRoland McGrath 
9559c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
9569c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
957e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
9589c1a1259SMike Frysinger 		unsigned long tmp = 0;
9599c1a1259SMike Frysinger 
960e0129ef9SOleg Nesterov 		ret = -ESRCH;
961e0129ef9SOleg Nesterov 		if (!mm)
962e0129ef9SOleg Nesterov 			break;
963e0129ef9SOleg Nesterov 
9649c1a1259SMike Frysinger 		switch (addr) {
9659c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
966e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
9679c1a1259SMike Frysinger 			break;
9689c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
969e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
9709c1a1259SMike Frysinger 			break;
9719c1a1259SMike Frysinger 		default:
9729c1a1259SMike Frysinger 			break;
9739c1a1259SMike Frysinger 		}
974e0129ef9SOleg Nesterov 		mmput(mm);
9759c1a1259SMike Frysinger 
9769fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
9779c1a1259SMike Frysinger 		break;
9789c1a1259SMike Frysinger 	}
9799c1a1259SMike Frysinger #endif
9809c1a1259SMike Frysinger 
98136df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
98236df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
98336df29d7SRoland McGrath #endif
9845b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
9855b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
9865b88abbfSRoland McGrath #endif
98736df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
98836df29d7SRoland McGrath 	case PTRACE_SYSEMU:
98936df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
99036df29d7SRoland McGrath #endif
99136df29d7SRoland McGrath 	case PTRACE_SYSCALL:
99236df29d7SRoland McGrath 	case PTRACE_CONT:
99336df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
99436df29d7SRoland McGrath 
99536df29d7SRoland McGrath 	case PTRACE_KILL:
99636df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
99736df29d7SRoland McGrath 			return 0;
99836df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
99936df29d7SRoland McGrath 
10002225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
10012225a122SSuresh Siddha 	case PTRACE_GETREGSET:
100229000caeSAndrey Vagin 	case PTRACE_SETREGSET: {
10032225a122SSuresh Siddha 		struct iovec kiov;
10049fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
10052225a122SSuresh Siddha 
10062225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
10072225a122SSuresh Siddha 			return -EFAULT;
10082225a122SSuresh Siddha 
10092225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
10102225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
10112225a122SSuresh Siddha 			return -EFAULT;
10122225a122SSuresh Siddha 
10132225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
10142225a122SSuresh Siddha 		if (!ret)
10152225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
10162225a122SSuresh Siddha 		break;
10172225a122SSuresh Siddha 	}
10182225a122SSuresh Siddha #endif
1019f8e529edSTycho Andersen 
1020f8e529edSTycho Andersen 	case PTRACE_SECCOMP_GET_FILTER:
1021f8e529edSTycho Andersen 		ret = seccomp_get_filter(child, addr, datavp);
1022f8e529edSTycho Andersen 		break;
1023f8e529edSTycho Andersen 
10241da177e4SLinus Torvalds 	default:
10251da177e4SLinus Torvalds 		break;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	return ret;
10291da177e4SLinus Torvalds }
1030481bed45SChristoph Hellwig 
10318053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
10326b9c7ed8SChristoph Hellwig {
10336b9c7ed8SChristoph Hellwig 	struct task_struct *child;
10346b9c7ed8SChristoph Hellwig 
10358053bdd5SOleg Nesterov 	rcu_read_lock();
1036228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
1037481bed45SChristoph Hellwig 	if (child)
1038481bed45SChristoph Hellwig 		get_task_struct(child);
10398053bdd5SOleg Nesterov 	rcu_read_unlock();
1040f400e198SSukadev Bhattiprolu 
1041481bed45SChristoph Hellwig 	if (!child)
10426b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
10436b9c7ed8SChristoph Hellwig 	return child;
1044481bed45SChristoph Hellwig }
1045481bed45SChristoph Hellwig 
10460ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
10470ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
10480ac15559SChristoph Hellwig #endif
10490ac15559SChristoph Hellwig 
10504abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
10514abf9869SNamhyung Kim 		unsigned long, data)
1052481bed45SChristoph Hellwig {
1053481bed45SChristoph Hellwig 	struct task_struct *child;
1054481bed45SChristoph Hellwig 	long ret;
1055481bed45SChristoph Hellwig 
10566b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
10576b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
10586ea6dd93SHaavard Skinnemoen 		if (!ret)
10596ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
1060481bed45SChristoph Hellwig 		goto out;
10616b9c7ed8SChristoph Hellwig 	}
10626b9c7ed8SChristoph Hellwig 
10636b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
10646b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
10656b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
10666b9c7ed8SChristoph Hellwig 		goto out;
10676b9c7ed8SChristoph Hellwig 	}
1068481bed45SChristoph Hellwig 
10693544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1070aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
10710ac15559SChristoph Hellwig 		/*
10720ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
10730ac15559SChristoph Hellwig 		 * a ptrace attach.
10740ac15559SChristoph Hellwig 		 */
10750ac15559SChristoph Hellwig 		if (!ret)
10760ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
1077005f18dfSChristoph Hellwig 		goto out_put_task_struct;
1078481bed45SChristoph Hellwig 	}
1079481bed45SChristoph Hellwig 
1080fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1081fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
1082481bed45SChristoph Hellwig 	if (ret < 0)
1083481bed45SChristoph Hellwig 		goto out_put_task_struct;
1084481bed45SChristoph Hellwig 
1085481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
10869899d11fSOleg Nesterov 	if (ret || request != PTRACE_DETACH)
10879899d11fSOleg Nesterov 		ptrace_unfreeze_traced(child);
1088481bed45SChristoph Hellwig 
1089481bed45SChristoph Hellwig  out_put_task_struct:
1090481bed45SChristoph Hellwig 	put_task_struct(child);
1091481bed45SChristoph Hellwig  out:
1092481bed45SChristoph Hellwig 	return ret;
1093481bed45SChristoph Hellwig }
109476647323SAlexey Dobriyan 
10954abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
10964abf9869SNamhyung Kim 			    unsigned long data)
109776647323SAlexey Dobriyan {
109876647323SAlexey Dobriyan 	unsigned long tmp;
109976647323SAlexey Dobriyan 	int copied;
110076647323SAlexey Dobriyan 
110176647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
110276647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
110376647323SAlexey Dobriyan 		return -EIO;
110476647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
110576647323SAlexey Dobriyan }
1106f284ce72SAlexey Dobriyan 
11074abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
11084abf9869SNamhyung Kim 			    unsigned long data)
1109f284ce72SAlexey Dobriyan {
1110f284ce72SAlexey Dobriyan 	int copied;
1111f284ce72SAlexey Dobriyan 
1112f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
1113f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
1114f284ce72SAlexey Dobriyan }
1115032d82d9SRoland McGrath 
111696b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
1117032d82d9SRoland McGrath 
1118032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1119032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
1120032d82d9SRoland McGrath {
1121032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
1122032d82d9SRoland McGrath 	compat_ulong_t word;
1123e16b2781SRoland McGrath 	siginfo_t siginfo;
1124032d82d9SRoland McGrath 	int ret;
1125032d82d9SRoland McGrath 
1126032d82d9SRoland McGrath 	switch (request) {
1127032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
1128032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
1129032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
1130032d82d9SRoland McGrath 		if (ret != sizeof(word))
1131032d82d9SRoland McGrath 			ret = -EIO;
1132032d82d9SRoland McGrath 		else
1133032d82d9SRoland McGrath 			ret = put_user(word, datap);
1134032d82d9SRoland McGrath 		break;
1135032d82d9SRoland McGrath 
1136032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
1137032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
1138032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
1139032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
1140032d82d9SRoland McGrath 		break;
1141032d82d9SRoland McGrath 
1142032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
1143032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1144032d82d9SRoland McGrath 		break;
1145032d82d9SRoland McGrath 
1146e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
1147e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
1148e16b2781SRoland McGrath 		if (!ret)
1149e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
1150e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
1151e16b2781SRoland McGrath 				&siginfo);
1152e16b2781SRoland McGrath 		break;
1153e16b2781SRoland McGrath 
1154e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
1155e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
1156e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
1157e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
1158e16b2781SRoland McGrath 			ret = -EFAULT;
1159e16b2781SRoland McGrath 		else
1160e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
1161e16b2781SRoland McGrath 		break;
11622225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
11632225a122SSuresh Siddha 	case PTRACE_GETREGSET:
11642225a122SSuresh Siddha 	case PTRACE_SETREGSET:
11652225a122SSuresh Siddha 	{
11662225a122SSuresh Siddha 		struct iovec kiov;
11672225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
11682225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
11692225a122SSuresh Siddha 		compat_uptr_t ptr;
11702225a122SSuresh Siddha 		compat_size_t len;
11712225a122SSuresh Siddha 
11722225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
11732225a122SSuresh Siddha 			return -EFAULT;
11742225a122SSuresh Siddha 
11752225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
11762225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
11772225a122SSuresh Siddha 			return -EFAULT;
11782225a122SSuresh Siddha 
11792225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
11802225a122SSuresh Siddha 		kiov.iov_len = len;
11812225a122SSuresh Siddha 
11822225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
11832225a122SSuresh Siddha 		if (!ret)
11842225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
11852225a122SSuresh Siddha 		break;
11862225a122SSuresh Siddha 	}
11872225a122SSuresh Siddha #endif
1188e16b2781SRoland McGrath 
1189032d82d9SRoland McGrath 	default:
1190032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
1191032d82d9SRoland McGrath 	}
1192032d82d9SRoland McGrath 
1193032d82d9SRoland McGrath 	return ret;
1194032d82d9SRoland McGrath }
1195c269f196SRoland McGrath 
119662a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
119762a6fa97SHeiko Carstens 		       compat_long_t, addr, compat_long_t, data)
1198c269f196SRoland McGrath {
1199c269f196SRoland McGrath 	struct task_struct *child;
1200c269f196SRoland McGrath 	long ret;
1201c269f196SRoland McGrath 
1202c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
1203c269f196SRoland McGrath 		ret = ptrace_traceme();
1204c269f196SRoland McGrath 		goto out;
1205c269f196SRoland McGrath 	}
1206c269f196SRoland McGrath 
1207c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
1208c269f196SRoland McGrath 	if (IS_ERR(child)) {
1209c269f196SRoland McGrath 		ret = PTR_ERR(child);
1210c269f196SRoland McGrath 		goto out;
1211c269f196SRoland McGrath 	}
1212c269f196SRoland McGrath 
12133544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1214aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
1215c269f196SRoland McGrath 		/*
1216c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
1217c269f196SRoland McGrath 		 * a ptrace attach.
1218c269f196SRoland McGrath 		 */
1219c269f196SRoland McGrath 		if (!ret)
1220c269f196SRoland McGrath 			arch_ptrace_attach(child);
1221c269f196SRoland McGrath 		goto out_put_task_struct;
1222c269f196SRoland McGrath 	}
1223c269f196SRoland McGrath 
1224fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1225fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
12269899d11fSOleg Nesterov 	if (!ret) {
1227c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
12289899d11fSOleg Nesterov 		if (ret || request != PTRACE_DETACH)
12299899d11fSOleg Nesterov 			ptrace_unfreeze_traced(child);
12309899d11fSOleg Nesterov 	}
1231c269f196SRoland McGrath 
1232c269f196SRoland McGrath  out_put_task_struct:
1233c269f196SRoland McGrath 	put_task_struct(child);
1234c269f196SRoland McGrath  out:
1235c269f196SRoland McGrath 	return ret;
1236c269f196SRoland McGrath }
123796b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
1238