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