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