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