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