xref: /openbmc/linux/kernel/ptrace.c (revision f701e5b7)
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>
111da177e4SLinus Torvalds #include <linux/module.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>
20a5cb013dSAl Viro #include <linux/audit.h>
21b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
22f17d30a8SAdrian Bunk #include <linux/syscalls.h>
233a709703SRoland McGrath #include <linux/uaccess.h>
242225a122SSuresh Siddha #include <linux/regset.h>
25bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h>
26f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h>
271da177e4SLinus Torvalds 
28bf53de90SMarkus Metzger 
2962c124ffSTejun Heo static int ptrace_trapping_sleep_fn(void *flags)
3062c124ffSTejun Heo {
3162c124ffSTejun Heo 	schedule();
3262c124ffSTejun Heo 	return 0;
3362c124ffSTejun Heo }
3462c124ffSTejun Heo 
35bf53de90SMarkus Metzger /*
361da177e4SLinus Torvalds  * ptrace a task: make the debugger its new parent and
371da177e4SLinus Torvalds  * move it to the ptrace list.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * Must be called with the tasklist lock write-held.
401da177e4SLinus Torvalds  */
4136c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
421da177e4SLinus Torvalds {
43f470021aSRoland McGrath 	BUG_ON(!list_empty(&child->ptrace_entry));
44f470021aSRoland McGrath 	list_add(&child->ptrace_entry, &new_parent->ptraced);
451da177e4SLinus Torvalds 	child->parent = new_parent;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
48e3bd058fSTejun Heo /**
49e3bd058fSTejun Heo  * __ptrace_unlink - unlink ptracee and restore its execution state
50e3bd058fSTejun Heo  * @child: ptracee to be unlinked
511da177e4SLinus Torvalds  *
520e9f0a4aSTejun Heo  * Remove @child from the ptrace list, move it back to the original parent,
530e9f0a4aSTejun Heo  * and restore the execution state so that it conforms to the group stop
540e9f0a4aSTejun Heo  * state.
550e9f0a4aSTejun Heo  *
560e9f0a4aSTejun Heo  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
570e9f0a4aSTejun Heo  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
580e9f0a4aSTejun Heo  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
590e9f0a4aSTejun Heo  * If the ptracer is exiting, the ptracee can be in any state.
600e9f0a4aSTejun Heo  *
610e9f0a4aSTejun Heo  * After detach, the ptracee should be in a state which conforms to the
620e9f0a4aSTejun Heo  * group stop.  If the group is stopped or in the process of stopping, the
630e9f0a4aSTejun Heo  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
640e9f0a4aSTejun Heo  * up from TASK_TRACED.
650e9f0a4aSTejun Heo  *
660e9f0a4aSTejun Heo  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
670e9f0a4aSTejun Heo  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
680e9f0a4aSTejun Heo  * to but in the opposite direction of what happens while attaching to a
690e9f0a4aSTejun Heo  * stopped task.  However, in this direction, the intermediate RUNNING
700e9f0a4aSTejun Heo  * state is not hidden even from the current ptracer and if it immediately
710e9f0a4aSTejun Heo  * re-attaches and performs a WNOHANG wait(2), it may fail.
72e3bd058fSTejun Heo  *
73e3bd058fSTejun Heo  * CONTEXT:
74e3bd058fSTejun Heo  * write_lock_irq(tasklist_lock)
751da177e4SLinus Torvalds  */
7636c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child)
771da177e4SLinus Torvalds {
785ecfbae0SOleg Nesterov 	BUG_ON(!child->ptrace);
795ecfbae0SOleg Nesterov 
801da177e4SLinus Torvalds 	child->ptrace = 0;
811da177e4SLinus Torvalds 	child->parent = child->real_parent;
82f470021aSRoland McGrath 	list_del_init(&child->ptrace_entry);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	spin_lock(&child->sighand->siglock);
850e9f0a4aSTejun Heo 
861da177e4SLinus Torvalds 	/*
8773ddff2bSTejun Heo 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
8873ddff2bSTejun Heo 	 * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
8973ddff2bSTejun Heo 	 */
9073ddff2bSTejun Heo 	task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
9173ddff2bSTejun Heo 	task_clear_jobctl_trapping(child);
9273ddff2bSTejun Heo 
9373ddff2bSTejun Heo 	/*
94a8f072c1STejun Heo 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
950e9f0a4aSTejun Heo 	 * @child isn't dead.
961da177e4SLinus Torvalds 	 */
970e9f0a4aSTejun Heo 	if (!(child->flags & PF_EXITING) &&
980e9f0a4aSTejun Heo 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
990e9f0a4aSTejun Heo 	     child->signal->group_stop_count))
100a8f072c1STejun Heo 		child->jobctl |= JOBCTL_STOP_PENDING;
1010e9f0a4aSTejun Heo 
1020e9f0a4aSTejun Heo 	/*
1030e9f0a4aSTejun Heo 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
1040e9f0a4aSTejun Heo 	 * @child in the butt.  Note that @resume should be used iff @child
1050e9f0a4aSTejun Heo 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
1060e9f0a4aSTejun Heo 	 * TASK_KILLABLE sleeps.
1070e9f0a4aSTejun Heo 	 */
108a8f072c1STejun Heo 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
1090e9f0a4aSTejun Heo 		signal_wake_up(child, task_is_traced(child));
1100e9f0a4aSTejun Heo 
1111da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114755e276bSTejun Heo /**
115755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
116755e276bSTejun Heo  * @child: ptracee to check for
117755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
118755e276bSTejun Heo  *
119755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
120755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
121755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
122755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
123755e276bSTejun Heo  * state.
124755e276bSTejun Heo  *
125755e276bSTejun Heo  * CONTEXT:
126755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
127755e276bSTejun Heo  *
128755e276bSTejun Heo  * RETURNS:
129755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1301da177e4SLinus Torvalds  */
131755e276bSTejun Heo int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	int ret = -ESRCH;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/*
1361da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1371da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1381da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1391da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1401da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1411da177e4SLinus Torvalds 	 */
1421da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
143c0c0b649SOleg Nesterov 	if ((child->ptrace & PT_PTRACED) && child->parent == current) {
144c0c0b649SOleg Nesterov 		/*
145c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
146c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
147c0c0b649SOleg Nesterov 		 */
1481da177e4SLinus Torvalds 		spin_lock_irq(&child->sighand->siglock);
149321fb561SOleg Nesterov 		WARN_ON_ONCE(task_is_stopped(child));
150544b2c91STejun Heo 		if (ignore_state || (task_is_traced(child) &&
151544b2c91STejun Heo 				     !(child->jobctl & JOBCTL_LISTENING)))
152321fb561SOleg Nesterov 			ret = 0;
1531da177e4SLinus Torvalds 		spin_unlock_irq(&child->sighand->siglock);
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1561da177e4SLinus Torvalds 
157755e276bSTejun Heo 	if (!ret && !ignore_state)
15885ba2d86SRoland McGrath 		ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* All systems go.. */
1611da177e4SLinus Torvalds 	return ret;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
164006ebb40SStephen Smalley int __ptrace_may_access(struct task_struct *task, unsigned int mode)
165ab8d11beSMiklos Szeredi {
166c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
167b6dff3ecSDavid Howells 
168df26c40eSEric W. Biederman 	/* May we inspect the given task?
169df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
170df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
171df26c40eSEric W. Biederman 	 *
172df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
173df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
174df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
175df26c40eSEric W. Biederman 	 */
176df26c40eSEric W. Biederman 	int dumpable = 0;
177df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
178df26c40eSEric W. Biederman 	if (task == current)
179df26c40eSEric W. Biederman 		return 0;
180c69e8d9cSDavid Howells 	rcu_read_lock();
181c69e8d9cSDavid Howells 	tcred = __task_cred(task);
1828409cca7SSerge E. Hallyn 	if (cred->user->user_ns == tcred->user->user_ns &&
1838409cca7SSerge E. Hallyn 	    (cred->uid == tcred->euid &&
1848409cca7SSerge E. Hallyn 	     cred->uid == tcred->suid &&
1858409cca7SSerge E. Hallyn 	     cred->uid == tcred->uid  &&
1868409cca7SSerge E. Hallyn 	     cred->gid == tcred->egid &&
1878409cca7SSerge E. Hallyn 	     cred->gid == tcred->sgid &&
1888409cca7SSerge E. Hallyn 	     cred->gid == tcred->gid))
1898409cca7SSerge E. Hallyn 		goto ok;
1908409cca7SSerge E. Hallyn 	if (ns_capable(tcred->user->user_ns, CAP_SYS_PTRACE))
1918409cca7SSerge E. Hallyn 		goto ok;
192c69e8d9cSDavid Howells 	rcu_read_unlock();
193ab8d11beSMiklos Szeredi 	return -EPERM;
1948409cca7SSerge E. Hallyn ok:
195c69e8d9cSDavid Howells 	rcu_read_unlock();
196ab8d11beSMiklos Szeredi 	smp_rmb();
197df26c40eSEric W. Biederman 	if (task->mm)
1986c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
1998409cca7SSerge E. Hallyn 	if (!dumpable && !task_ns_capable(task, CAP_SYS_PTRACE))
200ab8d11beSMiklos Szeredi 		return -EPERM;
201ab8d11beSMiklos Szeredi 
2029e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
203ab8d11beSMiklos Szeredi }
204ab8d11beSMiklos Szeredi 
205006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
206ab8d11beSMiklos Szeredi {
207ab8d11beSMiklos Szeredi 	int err;
208ab8d11beSMiklos Szeredi 	task_lock(task);
209006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
210ab8d11beSMiklos Szeredi 	task_unlock(task);
2113a709703SRoland McGrath 	return !err;
212ab8d11beSMiklos Szeredi }
213ab8d11beSMiklos Szeredi 
2143544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request,
2153544d72aSTejun Heo 			 unsigned long flags)
2161da177e4SLinus Torvalds {
2173544d72aSTejun Heo 	bool seize = (request == PTRACE_SEIZE);
2181da177e4SLinus Torvalds 	int retval;
219f5b40e36SLinus Torvalds 
2203544d72aSTejun Heo 	/*
2213544d72aSTejun Heo 	 * SEIZE will enable new ptrace behaviors which will be implemented
2223544d72aSTejun Heo 	 * gradually.  SEIZE_DEVEL is used to prevent applications
2233544d72aSTejun Heo 	 * expecting full SEIZE behaviors trapping on kernel commits which
2243544d72aSTejun Heo 	 * are still in the process of implementing them.
2253544d72aSTejun Heo 	 *
2263544d72aSTejun Heo 	 * Only test programs for new ptrace behaviors being implemented
2273544d72aSTejun Heo 	 * should set SEIZE_DEVEL.  If unset, SEIZE will fail with -EIO.
2283544d72aSTejun Heo 	 *
2293544d72aSTejun Heo 	 * Once SEIZE behaviors are completely implemented, this flag and
2303544d72aSTejun Heo 	 * the following test will be removed.
2313544d72aSTejun Heo 	 */
2323544d72aSTejun Heo 	retval = -EIO;
2333544d72aSTejun Heo 	if (seize && !(flags & PTRACE_SEIZE_DEVEL))
2343544d72aSTejun Heo 		goto out;
2353544d72aSTejun Heo 
236a5cb013dSAl Viro 	audit_ptrace(task);
237a5cb013dSAl Viro 
2381da177e4SLinus Torvalds 	retval = -EPERM;
239b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
240b79b7ba9SOleg Nesterov 		goto out;
241bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
242f5b40e36SLinus Torvalds 		goto out;
243f5b40e36SLinus Torvalds 
244f2f0b00aSOleg Nesterov 	/*
245f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
2465e751e99SDavid Howells 	 * interference; SUID, SGID and LSM creds get determined differently
2475e751e99SDavid Howells 	 * under ptrace.
248d84f4f99SDavid Howells 	 */
249793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
2509b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
251d84f4f99SDavid Howells 		goto out;
2524b105cbbSOleg Nesterov 
253f5b40e36SLinus Torvalds 	task_lock(task);
254006ebb40SStephen Smalley 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
2554b105cbbSOleg Nesterov 	task_unlock(task);
2561da177e4SLinus Torvalds 	if (retval)
2574b105cbbSOleg Nesterov 		goto unlock_creds;
2581da177e4SLinus Torvalds 
2594b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
260b79b7ba9SOleg Nesterov 	retval = -EPERM;
261b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
2624b105cbbSOleg Nesterov 		goto unlock_tasklist;
263f2f0b00aSOleg Nesterov 	if (task->ptrace)
2644b105cbbSOleg Nesterov 		goto unlock_tasklist;
265b79b7ba9SOleg Nesterov 
266f2f0b00aSOleg Nesterov 	task->ptrace = PT_PTRACED;
2673544d72aSTejun Heo 	if (seize)
2683544d72aSTejun Heo 		task->ptrace |= PT_SEIZED;
2698409cca7SSerge E. Hallyn 	if (task_ns_capable(task, CAP_SYS_PTRACE))
2701da177e4SLinus Torvalds 		task->ptrace |= PT_PTRACE_CAP;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	__ptrace_link(task, current);
2733544d72aSTejun Heo 
2743544d72aSTejun Heo 	/* SEIZE doesn't trap tracee on attach */
2753544d72aSTejun Heo 	if (!seize)
27633e9fc7dSOleg Nesterov 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
277b79b7ba9SOleg Nesterov 
278d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
279d79fdd6dSTejun Heo 
280d79fdd6dSTejun Heo 	/*
28173ddff2bSTejun Heo 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
282d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
283d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
284d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
285d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
286d79fdd6dSTejun Heo 	 * function.
287d79fdd6dSTejun Heo 	 *
288d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
289d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
290d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
291d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
292d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
293d79fdd6dSTejun Heo 	 *
294d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
295d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
296d79fdd6dSTejun Heo 	 */
2977dd3db54STejun Heo 	if (task_is_stopped(task) &&
29873ddff2bSTejun Heo 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
299d79fdd6dSTejun Heo 		signal_wake_up(task, 1);
300d79fdd6dSTejun Heo 
301d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
302d79fdd6dSTejun Heo 
303b79b7ba9SOleg Nesterov 	retval = 0;
3044b105cbbSOleg Nesterov unlock_tasklist:
3054b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3064b105cbbSOleg Nesterov unlock_creds:
3079b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
308f5b40e36SLinus Torvalds out:
309f701e5b7SVladimir Zapolskiy 	if (!retval) {
31062c124ffSTejun Heo 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
31162c124ffSTejun Heo 			    ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
312f701e5b7SVladimir Zapolskiy 		proc_ptrace_connector(task, PTRACE_ATTACH);
313f701e5b7SVladimir Zapolskiy 	}
314f701e5b7SVladimir Zapolskiy 
3151da177e4SLinus Torvalds 	return retval;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318f2f0b00aSOleg Nesterov /**
319f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
320f2f0b00aSOleg Nesterov  *
321f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
322f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
323f2f0b00aSOleg Nesterov  */
324e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
325f2f0b00aSOleg Nesterov {
326f2f0b00aSOleg Nesterov 	int ret = -EPERM;
327f2f0b00aSOleg Nesterov 
3284b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
3294b105cbbSOleg Nesterov 	/* Are we already being traced? */
330f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
331f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
332f2f0b00aSOleg Nesterov 		/*
333f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
334f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
335f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
336f2f0b00aSOleg Nesterov 		 */
337f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
338f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
339f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
340f2f0b00aSOleg Nesterov 		}
341f2f0b00aSOleg Nesterov 	}
3424b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3434b105cbbSOleg Nesterov 
344f2f0b00aSOleg Nesterov 	return ret;
345f2f0b00aSOleg Nesterov }
346f2f0b00aSOleg Nesterov 
34739c626aeSOleg Nesterov /*
34839c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
34939c626aeSOleg Nesterov  */
35039c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
35139c626aeSOleg Nesterov {
35239c626aeSOleg Nesterov 	int ret;
35339c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
35439c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
35539c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
35639c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
35739c626aeSOleg Nesterov 	return ret;
35839c626aeSOleg Nesterov }
35939c626aeSOleg Nesterov 
36039c626aeSOleg Nesterov /*
36139c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
36239c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
36339c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
36439c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
36539c626aeSOleg Nesterov  *
36639c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
36739c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
36839c626aeSOleg Nesterov  * If it should reap itself, return true.
36939c626aeSOleg Nesterov  *
370a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
371a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
372a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
373a7f0765eSOleg Nesterov  * do_wait().
37439c626aeSOleg Nesterov  */
37539c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
37639c626aeSOleg Nesterov {
3779843a1e9SOleg Nesterov 	bool dead;
3789843a1e9SOleg Nesterov 
37939c626aeSOleg Nesterov 	__ptrace_unlink(p);
38039c626aeSOleg Nesterov 
3819843a1e9SOleg Nesterov 	if (p->exit_state != EXIT_ZOMBIE)
3829843a1e9SOleg Nesterov 		return false;
3839843a1e9SOleg Nesterov 
3849843a1e9SOleg Nesterov 	dead = !thread_group_leader(p);
3859843a1e9SOleg Nesterov 
3869843a1e9SOleg Nesterov 	if (!dead && thread_group_empty(p)) {
38739c626aeSOleg Nesterov 		if (!same_thread_group(p->real_parent, tracer))
3889843a1e9SOleg Nesterov 			dead = do_notify_parent(p, p->exit_signal);
389a7f0765eSOleg Nesterov 		else if (ignoring_children(tracer->sighand)) {
390a7f0765eSOleg Nesterov 			__wake_up_parent(p, tracer);
3919843a1e9SOleg Nesterov 			dead = true;
39239c626aeSOleg Nesterov 		}
393a7f0765eSOleg Nesterov 	}
39439c626aeSOleg Nesterov 	/* Mark it as in the process of being reaped. */
3959843a1e9SOleg Nesterov 	if (dead)
39639c626aeSOleg Nesterov 		p->exit_state = EXIT_DEAD;
3979843a1e9SOleg Nesterov 	return dead;
39839c626aeSOleg Nesterov }
39939c626aeSOleg Nesterov 
400e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
4011da177e4SLinus Torvalds {
40239c626aeSOleg Nesterov 	bool dead = false;
4034576145cSOleg Nesterov 
4047ed20e1aSJesper Juhl 	if (!valid_signal(data))
4051da177e4SLinus Torvalds 		return -EIO;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
4081da177e4SLinus Torvalds 	ptrace_disable(child);
4097d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
4101da177e4SLinus Torvalds 
41195c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
41239c626aeSOleg Nesterov 	/*
41339c626aeSOleg Nesterov 	 * This child can be already killed. Make sure de_thread() or
41439c626aeSOleg Nesterov 	 * our sub-thread doing do_wait() didn't do release_task() yet.
41539c626aeSOleg Nesterov 	 */
41695c3eb76SOleg Nesterov 	if (child->ptrace) {
41795c3eb76SOleg Nesterov 		child->exit_code = data;
4184576145cSOleg Nesterov 		dead = __ptrace_detach(current, child);
41995c3eb76SOleg Nesterov 	}
4201da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
4211da177e4SLinus Torvalds 
422f701e5b7SVladimir Zapolskiy 	proc_ptrace_connector(child, PTRACE_DETACH);
4234576145cSOleg Nesterov 	if (unlikely(dead))
4244576145cSOleg Nesterov 		release_task(child);
4254576145cSOleg Nesterov 
4261da177e4SLinus Torvalds 	return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
42939c626aeSOleg Nesterov /*
430c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
431c7e49c14SOleg Nesterov  * for writing, and returns with it held too. But note it can release
432c7e49c14SOleg Nesterov  * and reacquire the lock.
43339c626aeSOleg Nesterov  */
43439c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer)
435c4b5ed25SNamhyung Kim 	__releases(&tasklist_lock)
436c4b5ed25SNamhyung Kim 	__acquires(&tasklist_lock)
43739c626aeSOleg Nesterov {
43839c626aeSOleg Nesterov 	struct task_struct *p, *n;
43939c626aeSOleg Nesterov 	LIST_HEAD(ptrace_dead);
44039c626aeSOleg Nesterov 
441c7e49c14SOleg Nesterov 	if (likely(list_empty(&tracer->ptraced)))
442c7e49c14SOleg Nesterov 		return;
443c7e49c14SOleg Nesterov 
44439c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
44539c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
44639c626aeSOleg Nesterov 			list_add(&p->ptrace_entry, &ptrace_dead);
44739c626aeSOleg Nesterov 	}
44839c626aeSOleg Nesterov 
449c7e49c14SOleg Nesterov 	write_unlock_irq(&tasklist_lock);
45039c626aeSOleg Nesterov 	BUG_ON(!list_empty(&tracer->ptraced));
45139c626aeSOleg Nesterov 
45239c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
45339c626aeSOleg Nesterov 		list_del_init(&p->ptrace_entry);
45439c626aeSOleg Nesterov 		release_task(p);
45539c626aeSOleg Nesterov 	}
456c7e49c14SOleg Nesterov 
457c7e49c14SOleg Nesterov 	write_lock_irq(&tasklist_lock);
45839c626aeSOleg Nesterov }
45939c626aeSOleg Nesterov 
4601da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	int copied = 0;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	while (len > 0) {
4651da177e4SLinus Torvalds 		char buf[128];
4661da177e4SLinus Torvalds 		int this_len, retval;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
4691da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
4701da177e4SLinus Torvalds 		if (!retval) {
4711da177e4SLinus Torvalds 			if (copied)
4721da177e4SLinus Torvalds 				break;
4731da177e4SLinus Torvalds 			return -EIO;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
4761da177e4SLinus Torvalds 			return -EFAULT;
4771da177e4SLinus Torvalds 		copied += retval;
4781da177e4SLinus Torvalds 		src += retval;
4791da177e4SLinus Torvalds 		dst += retval;
4801da177e4SLinus Torvalds 		len -= retval;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	return copied;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	int copied = 0;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	while (len > 0) {
4901da177e4SLinus Torvalds 		char buf[128];
4911da177e4SLinus Torvalds 		int this_len, retval;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
4941da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
4951da177e4SLinus Torvalds 			return -EFAULT;
4961da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
4971da177e4SLinus Torvalds 		if (!retval) {
4981da177e4SLinus Torvalds 			if (copied)
4991da177e4SLinus Torvalds 				break;
5001da177e4SLinus Torvalds 			return -EIO;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 		copied += retval;
5031da177e4SLinus Torvalds 		src += retval;
5041da177e4SLinus Torvalds 		dst += retval;
5051da177e4SLinus Torvalds 		len -= retval;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 	return copied;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5104abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	child->ptrace &= ~PT_TRACE_MASK;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACESYSGOOD)
5151da177e4SLinus Torvalds 		child->ptrace |= PT_TRACESYSGOOD;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEFORK)
5181da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_FORK;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORK)
5211da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACECLONE)
5241da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_CLONE;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXEC)
5271da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXEC;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORKDONE)
5301da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK_DONE;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXIT)
5331da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXIT;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
538e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
5391da177e4SLinus Torvalds {
540e4961254SOleg Nesterov 	unsigned long flags;
5411da177e4SLinus Torvalds 	int error = -ESRCH;
5421da177e4SLinus Torvalds 
543e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5441da177e4SLinus Torvalds 		error = -EINVAL;
5451da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
546e16b2781SRoland McGrath 			*info = *child->last_siginfo;
5471da177e4SLinus Torvalds 			error = 0;
5481da177e4SLinus Torvalds 		}
549e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 	return error;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
554e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
5551da177e4SLinus Torvalds {
556e4961254SOleg Nesterov 	unsigned long flags;
5571da177e4SLinus Torvalds 	int error = -ESRCH;
5581da177e4SLinus Torvalds 
559e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5601da177e4SLinus Torvalds 		error = -EINVAL;
5611da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
562e16b2781SRoland McGrath 			*child->last_siginfo = *info;
5631da177e4SLinus Torvalds 			error = 0;
5641da177e4SLinus Torvalds 		}
565e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	return error;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57036df29d7SRoland McGrath 
57136df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
57236df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
57336df29d7SRoland McGrath #else
57436df29d7SRoland McGrath #define is_singlestep(request)		0
57536df29d7SRoland McGrath #endif
57636df29d7SRoland McGrath 
5775b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
5785b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
5795b88abbfSRoland McGrath #else
5805b88abbfSRoland McGrath #define is_singleblock(request)		0
5815b88abbfSRoland McGrath #endif
5825b88abbfSRoland McGrath 
58336df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
58436df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
58536df29d7SRoland McGrath #else
58636df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
58736df29d7SRoland McGrath #endif
58836df29d7SRoland McGrath 
5894abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
5904abf9869SNamhyung Kim 			 unsigned long data)
59136df29d7SRoland McGrath {
59236df29d7SRoland McGrath 	if (!valid_signal(data))
59336df29d7SRoland McGrath 		return -EIO;
59436df29d7SRoland McGrath 
59536df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
59636df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
59736df29d7SRoland McGrath 	else
59836df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
59936df29d7SRoland McGrath 
60036df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
60136df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
60236df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
60336df29d7SRoland McGrath 	else
60436df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
60536df29d7SRoland McGrath #endif
60636df29d7SRoland McGrath 
6075b88abbfSRoland McGrath 	if (is_singleblock(request)) {
6085b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
6095b88abbfSRoland McGrath 			return -EIO;
6105b88abbfSRoland McGrath 		user_enable_block_step(child);
6115b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
61236df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
61336df29d7SRoland McGrath 			return -EIO;
61436df29d7SRoland McGrath 		user_enable_single_step(child);
6153a709703SRoland McGrath 	} else {
61636df29d7SRoland McGrath 		user_disable_single_step(child);
6173a709703SRoland McGrath 	}
61836df29d7SRoland McGrath 
61936df29d7SRoland McGrath 	child->exit_code = data;
6200666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
62136df29d7SRoland McGrath 
62236df29d7SRoland McGrath 	return 0;
62336df29d7SRoland McGrath }
62436df29d7SRoland McGrath 
6252225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
6262225a122SSuresh Siddha 
6272225a122SSuresh Siddha static const struct user_regset *
6282225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
6292225a122SSuresh Siddha {
6302225a122SSuresh Siddha 	const struct user_regset *regset;
6312225a122SSuresh Siddha 	int n;
6322225a122SSuresh Siddha 
6332225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
6342225a122SSuresh Siddha 		regset = view->regsets + n;
6352225a122SSuresh Siddha 		if (regset->core_note_type == type)
6362225a122SSuresh Siddha 			return regset;
6372225a122SSuresh Siddha 	}
6382225a122SSuresh Siddha 
6392225a122SSuresh Siddha 	return NULL;
6402225a122SSuresh Siddha }
6412225a122SSuresh Siddha 
6422225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
6432225a122SSuresh Siddha 			 struct iovec *kiov)
6442225a122SSuresh Siddha {
6452225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
6462225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
6472225a122SSuresh Siddha 	int regset_no;
6482225a122SSuresh Siddha 
6492225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
650c6a0dd7eSSuresh Siddha 		return -EINVAL;
6512225a122SSuresh Siddha 
6522225a122SSuresh Siddha 	regset_no = regset - view->regsets;
6532225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
6542225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
6552225a122SSuresh Siddha 
6562225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
6572225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
6582225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
6592225a122SSuresh Siddha 	else
6602225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
6612225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
6622225a122SSuresh Siddha }
6632225a122SSuresh Siddha 
6642225a122SSuresh Siddha #endif
6652225a122SSuresh Siddha 
6661da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
6674abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
6681da177e4SLinus Torvalds {
669fca26f26STejun Heo 	bool seized = child->ptrace & PT_SEIZED;
6701da177e4SLinus Torvalds 	int ret = -EIO;
671544b2c91STejun Heo 	siginfo_t siginfo, *si;
6729fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
6739fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
674fca26f26STejun Heo 	unsigned long flags;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	switch (request) {
67716c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
67816c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
67916c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
68016c3e389SRoland McGrath 	case PTRACE_POKETEXT:
68116c3e389SRoland McGrath 	case PTRACE_POKEDATA:
68216c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
68316c3e389SRoland McGrath 
6841da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
6851da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
6861da177e4SLinus Torvalds #endif
6871da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
6881da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
6891da177e4SLinus Torvalds 		break;
6901da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
6919fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
6921da177e4SLinus Torvalds 		break;
693e16b2781SRoland McGrath 
6941da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
695e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
696e16b2781SRoland McGrath 		if (!ret)
6979fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
6981da177e4SLinus Torvalds 		break;
699e16b2781SRoland McGrath 
7001da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
7019fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
702e16b2781SRoland McGrath 			ret = -EFAULT;
703e16b2781SRoland McGrath 		else
704e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
7051da177e4SLinus Torvalds 		break;
706e16b2781SRoland McGrath 
707fca26f26STejun Heo 	case PTRACE_INTERRUPT:
708fca26f26STejun Heo 		/*
709fca26f26STejun Heo 		 * Stop tracee without any side-effect on signal or job
710fca26f26STejun Heo 		 * control.  At least one trap is guaranteed to happen
711fca26f26STejun Heo 		 * after this request.  If @child is already trapped, the
712fca26f26STejun Heo 		 * current trap is not disturbed and another trap will
713fca26f26STejun Heo 		 * happen after the current trap is ended with PTRACE_CONT.
714fca26f26STejun Heo 		 *
715fca26f26STejun Heo 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
716fca26f26STejun Heo 		 * the pending condition is cleared regardless.
717fca26f26STejun Heo 		 */
718fca26f26STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
719fca26f26STejun Heo 			break;
720fca26f26STejun Heo 
721544b2c91STejun Heo 		/*
722544b2c91STejun Heo 		 * INTERRUPT doesn't disturb existing trap sans one
723544b2c91STejun Heo 		 * exception.  If ptracer issued LISTEN for the current
724544b2c91STejun Heo 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
725544b2c91STejun Heo 		 * tracee into STOP.
726544b2c91STejun Heo 		 */
727fca26f26STejun Heo 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
728544b2c91STejun Heo 			signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
729544b2c91STejun Heo 
730544b2c91STejun Heo 		unlock_task_sighand(child, &flags);
731544b2c91STejun Heo 		ret = 0;
732544b2c91STejun Heo 		break;
733544b2c91STejun Heo 
734544b2c91STejun Heo 	case PTRACE_LISTEN:
735544b2c91STejun Heo 		/*
736544b2c91STejun Heo 		 * Listen for events.  Tracee must be in STOP.  It's not
737544b2c91STejun Heo 		 * resumed per-se but is not considered to be in TRACED by
738544b2c91STejun Heo 		 * wait(2) or ptrace(2).  If an async event (e.g. group
739544b2c91STejun Heo 		 * stop state change) happens, tracee will enter STOP trap
740544b2c91STejun Heo 		 * again.  Alternatively, ptracer can issue INTERRUPT to
741544b2c91STejun Heo 		 * finish listening and re-trap tracee into STOP.
742544b2c91STejun Heo 		 */
743544b2c91STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
744544b2c91STejun Heo 			break;
745544b2c91STejun Heo 
746544b2c91STejun Heo 		si = child->last_siginfo;
747544b2c91STejun Heo 		if (unlikely(!si || si->si_code >> 8 != PTRACE_EVENT_STOP))
748544b2c91STejun Heo 			break;
749544b2c91STejun Heo 
750544b2c91STejun Heo 		child->jobctl |= JOBCTL_LISTENING;
751544b2c91STejun Heo 
752544b2c91STejun Heo 		/*
753544b2c91STejun Heo 		 * If NOTIFY is set, it means event happened between start
754544b2c91STejun Heo 		 * of this trap and now.  Trigger re-trap immediately.
755544b2c91STejun Heo 		 */
756544b2c91STejun Heo 		if (child->jobctl & JOBCTL_TRAP_NOTIFY)
757544b2c91STejun Heo 			signal_wake_up(child, true);
758fca26f26STejun Heo 
759fca26f26STejun Heo 		unlock_task_sighand(child, &flags);
760fca26f26STejun Heo 		ret = 0;
761fca26f26STejun Heo 		break;
762fca26f26STejun Heo 
7631bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
7641bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
7651bcf5482SAlexey Dobriyan 		break;
76636df29d7SRoland McGrath 
7679c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
7689c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
769e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
7709c1a1259SMike Frysinger 		unsigned long tmp = 0;
7719c1a1259SMike Frysinger 
772e0129ef9SOleg Nesterov 		ret = -ESRCH;
773e0129ef9SOleg Nesterov 		if (!mm)
774e0129ef9SOleg Nesterov 			break;
775e0129ef9SOleg Nesterov 
7769c1a1259SMike Frysinger 		switch (addr) {
7779c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
778e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
7799c1a1259SMike Frysinger 			break;
7809c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
781e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
7829c1a1259SMike Frysinger 			break;
7839c1a1259SMike Frysinger 		default:
7849c1a1259SMike Frysinger 			break;
7859c1a1259SMike Frysinger 		}
786e0129ef9SOleg Nesterov 		mmput(mm);
7879c1a1259SMike Frysinger 
7889fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
7899c1a1259SMike Frysinger 		break;
7909c1a1259SMike Frysinger 	}
7919c1a1259SMike Frysinger #endif
7929c1a1259SMike Frysinger 
79336df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
79436df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
79536df29d7SRoland McGrath #endif
7965b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
7975b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
7985b88abbfSRoland McGrath #endif
79936df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
80036df29d7SRoland McGrath 	case PTRACE_SYSEMU:
80136df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
80236df29d7SRoland McGrath #endif
80336df29d7SRoland McGrath 	case PTRACE_SYSCALL:
80436df29d7SRoland McGrath 	case PTRACE_CONT:
80536df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
80636df29d7SRoland McGrath 
80736df29d7SRoland McGrath 	case PTRACE_KILL:
80836df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
80936df29d7SRoland McGrath 			return 0;
81036df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
81136df29d7SRoland McGrath 
8122225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
8132225a122SSuresh Siddha 	case PTRACE_GETREGSET:
8142225a122SSuresh Siddha 	case PTRACE_SETREGSET:
8152225a122SSuresh Siddha 	{
8162225a122SSuresh Siddha 		struct iovec kiov;
8179fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
8182225a122SSuresh Siddha 
8192225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
8202225a122SSuresh Siddha 			return -EFAULT;
8212225a122SSuresh Siddha 
8222225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
8232225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
8242225a122SSuresh Siddha 			return -EFAULT;
8252225a122SSuresh Siddha 
8262225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
8272225a122SSuresh Siddha 		if (!ret)
8282225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
8292225a122SSuresh Siddha 		break;
8302225a122SSuresh Siddha 	}
8312225a122SSuresh Siddha #endif
8321da177e4SLinus Torvalds 	default:
8331da177e4SLinus Torvalds 		break;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	return ret;
8371da177e4SLinus Torvalds }
838481bed45SChristoph Hellwig 
8398053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
8406b9c7ed8SChristoph Hellwig {
8416b9c7ed8SChristoph Hellwig 	struct task_struct *child;
8426b9c7ed8SChristoph Hellwig 
8438053bdd5SOleg Nesterov 	rcu_read_lock();
844228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
845481bed45SChristoph Hellwig 	if (child)
846481bed45SChristoph Hellwig 		get_task_struct(child);
8478053bdd5SOleg Nesterov 	rcu_read_unlock();
848f400e198SSukadev Bhattiprolu 
849481bed45SChristoph Hellwig 	if (!child)
8506b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
8516b9c7ed8SChristoph Hellwig 	return child;
852481bed45SChristoph Hellwig }
853481bed45SChristoph Hellwig 
8540ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
8550ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
8560ac15559SChristoph Hellwig #endif
8570ac15559SChristoph Hellwig 
8584abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
8594abf9869SNamhyung Kim 		unsigned long, data)
860481bed45SChristoph Hellwig {
861481bed45SChristoph Hellwig 	struct task_struct *child;
862481bed45SChristoph Hellwig 	long ret;
863481bed45SChristoph Hellwig 
8646b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
8656b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
8666ea6dd93SHaavard Skinnemoen 		if (!ret)
8676ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
868481bed45SChristoph Hellwig 		goto out;
8696b9c7ed8SChristoph Hellwig 	}
8706b9c7ed8SChristoph Hellwig 
8716b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
8726b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
8736b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
8746b9c7ed8SChristoph Hellwig 		goto out;
8756b9c7ed8SChristoph Hellwig 	}
876481bed45SChristoph Hellwig 
8773544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
8783544d72aSTejun Heo 		ret = ptrace_attach(child, request, data);
8790ac15559SChristoph Hellwig 		/*
8800ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
8810ac15559SChristoph Hellwig 		 * a ptrace attach.
8820ac15559SChristoph Hellwig 		 */
8830ac15559SChristoph Hellwig 		if (!ret)
8840ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
885005f18dfSChristoph Hellwig 		goto out_put_task_struct;
886481bed45SChristoph Hellwig 	}
887481bed45SChristoph Hellwig 
888fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
889fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
890481bed45SChristoph Hellwig 	if (ret < 0)
891481bed45SChristoph Hellwig 		goto out_put_task_struct;
892481bed45SChristoph Hellwig 
893481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
894481bed45SChristoph Hellwig 
895481bed45SChristoph Hellwig  out_put_task_struct:
896481bed45SChristoph Hellwig 	put_task_struct(child);
897481bed45SChristoph Hellwig  out:
898481bed45SChristoph Hellwig 	return ret;
899481bed45SChristoph Hellwig }
90076647323SAlexey Dobriyan 
9014abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
9024abf9869SNamhyung Kim 			    unsigned long data)
90376647323SAlexey Dobriyan {
90476647323SAlexey Dobriyan 	unsigned long tmp;
90576647323SAlexey Dobriyan 	int copied;
90676647323SAlexey Dobriyan 
90776647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
90876647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
90976647323SAlexey Dobriyan 		return -EIO;
91076647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
91176647323SAlexey Dobriyan }
912f284ce72SAlexey Dobriyan 
9134abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
9144abf9869SNamhyung Kim 			    unsigned long data)
915f284ce72SAlexey Dobriyan {
916f284ce72SAlexey Dobriyan 	int copied;
917f284ce72SAlexey Dobriyan 
918f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
919f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
920f284ce72SAlexey Dobriyan }
921032d82d9SRoland McGrath 
92296b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
923032d82d9SRoland McGrath #include <linux/compat.h>
924032d82d9SRoland McGrath 
925032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
926032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
927032d82d9SRoland McGrath {
928032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
929032d82d9SRoland McGrath 	compat_ulong_t word;
930e16b2781SRoland McGrath 	siginfo_t siginfo;
931032d82d9SRoland McGrath 	int ret;
932032d82d9SRoland McGrath 
933032d82d9SRoland McGrath 	switch (request) {
934032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
935032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
936032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
937032d82d9SRoland McGrath 		if (ret != sizeof(word))
938032d82d9SRoland McGrath 			ret = -EIO;
939032d82d9SRoland McGrath 		else
940032d82d9SRoland McGrath 			ret = put_user(word, datap);
941032d82d9SRoland McGrath 		break;
942032d82d9SRoland McGrath 
943032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
944032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
945032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
946032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
947032d82d9SRoland McGrath 		break;
948032d82d9SRoland McGrath 
949032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
950032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
951032d82d9SRoland McGrath 		break;
952032d82d9SRoland McGrath 
953e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
954e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
955e16b2781SRoland McGrath 		if (!ret)
956e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
957e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
958e16b2781SRoland McGrath 				&siginfo);
959e16b2781SRoland McGrath 		break;
960e16b2781SRoland McGrath 
961e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
962e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
963e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
964e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
965e16b2781SRoland McGrath 			ret = -EFAULT;
966e16b2781SRoland McGrath 		else
967e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
968e16b2781SRoland McGrath 		break;
9692225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
9702225a122SSuresh Siddha 	case PTRACE_GETREGSET:
9712225a122SSuresh Siddha 	case PTRACE_SETREGSET:
9722225a122SSuresh Siddha 	{
9732225a122SSuresh Siddha 		struct iovec kiov;
9742225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
9752225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
9762225a122SSuresh Siddha 		compat_uptr_t ptr;
9772225a122SSuresh Siddha 		compat_size_t len;
9782225a122SSuresh Siddha 
9792225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
9802225a122SSuresh Siddha 			return -EFAULT;
9812225a122SSuresh Siddha 
9822225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
9832225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
9842225a122SSuresh Siddha 			return -EFAULT;
9852225a122SSuresh Siddha 
9862225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
9872225a122SSuresh Siddha 		kiov.iov_len = len;
9882225a122SSuresh Siddha 
9892225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
9902225a122SSuresh Siddha 		if (!ret)
9912225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
9922225a122SSuresh Siddha 		break;
9932225a122SSuresh Siddha 	}
9942225a122SSuresh Siddha #endif
995e16b2781SRoland McGrath 
996032d82d9SRoland McGrath 	default:
997032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
998032d82d9SRoland McGrath 	}
999032d82d9SRoland McGrath 
1000032d82d9SRoland McGrath 	return ret;
1001032d82d9SRoland McGrath }
1002c269f196SRoland McGrath 
1003c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
1004c269f196SRoland McGrath 				  compat_long_t addr, compat_long_t data)
1005c269f196SRoland McGrath {
1006c269f196SRoland McGrath 	struct task_struct *child;
1007c269f196SRoland McGrath 	long ret;
1008c269f196SRoland McGrath 
1009c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
1010c269f196SRoland McGrath 		ret = ptrace_traceme();
1011c269f196SRoland McGrath 		goto out;
1012c269f196SRoland McGrath 	}
1013c269f196SRoland McGrath 
1014c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
1015c269f196SRoland McGrath 	if (IS_ERR(child)) {
1016c269f196SRoland McGrath 		ret = PTR_ERR(child);
1017c269f196SRoland McGrath 		goto out;
1018c269f196SRoland McGrath 	}
1019c269f196SRoland McGrath 
10203544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
10213544d72aSTejun Heo 		ret = ptrace_attach(child, request, data);
1022c269f196SRoland McGrath 		/*
1023c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
1024c269f196SRoland McGrath 		 * a ptrace attach.
1025c269f196SRoland McGrath 		 */
1026c269f196SRoland McGrath 		if (!ret)
1027c269f196SRoland McGrath 			arch_ptrace_attach(child);
1028c269f196SRoland McGrath 		goto out_put_task_struct;
1029c269f196SRoland McGrath 	}
1030c269f196SRoland McGrath 
1031fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1032fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
1033c269f196SRoland McGrath 	if (!ret)
1034c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
1035c269f196SRoland McGrath 
1036c269f196SRoland McGrath  out_put_task_struct:
1037c269f196SRoland McGrath 	put_task_struct(child);
1038c269f196SRoland McGrath  out:
1039c269f196SRoland McGrath 	return ret;
1040c269f196SRoland McGrath }
104196b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
1042bf26c018SFrederic Weisbecker 
1043bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT
1044bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk)
1045bf26c018SFrederic Weisbecker {
1046bf26c018SFrederic Weisbecker 	if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
1047bf26c018SFrederic Weisbecker 		return 0;
1048bf26c018SFrederic Weisbecker 
1049bf26c018SFrederic Weisbecker 	return -1;
1050bf26c018SFrederic Weisbecker }
1051bf26c018SFrederic Weisbecker 
1052bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk)
1053bf26c018SFrederic Weisbecker {
1054bf26c018SFrederic Weisbecker 	if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
1055bf26c018SFrederic Weisbecker 		flush_ptrace_hw_breakpoint(tsk);
1056bf26c018SFrederic Weisbecker }
1057bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1058