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