ptrace.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #ifndef _LINUX_PTRACE_H
  2. #define _LINUX_PTRACE_H
  3. #include <linux/compiler.h> /* For unlikely. */
  4. #include <linux/sched.h> /* For struct task_struct. */
  5. #include <linux/err.h> /* for IS_ERR_VALUE */
  6. #include <linux/bug.h> /* For BUG_ON. */
  7. #include <linux/pid_namespace.h> /* For task_active_pid_ns. */
  8. #include <uapi/linux/ptrace.h>
  9. /*
  10. * Ptrace flags
  11. *
  12. * The owner ship rules for task->ptrace which holds the ptrace
  13. * flags is simple. When a task is running it owns it's task->ptrace
  14. * flags. When the a task is stopped the ptracer owns task->ptrace.
  15. */
  16. #define PT_SEIZED 0x00010000 /* SEIZE used, enable new behavior */
  17. #define PT_PTRACED 0x00000001
  18. #define PT_DTRACE 0x00000002 /* delayed trace (used on m68k, i386) */
  19. #define PT_PTRACE_CAP 0x00000004 /* ptracer can follow suid-exec */
  20. #define PT_OPT_FLAG_SHIFT 3
  21. /* PT_TRACE_* event enable flags */
  22. #define PT_EVENT_FLAG(event) (1 << (PT_OPT_FLAG_SHIFT + (event)))
  23. #define PT_TRACESYSGOOD PT_EVENT_FLAG(0)
  24. #define PT_TRACE_FORK PT_EVENT_FLAG(PTRACE_EVENT_FORK)
  25. #define PT_TRACE_VFORK PT_EVENT_FLAG(PTRACE_EVENT_VFORK)
  26. #define PT_TRACE_CLONE PT_EVENT_FLAG(PTRACE_EVENT_CLONE)
  27. #define PT_TRACE_EXEC PT_EVENT_FLAG(PTRACE_EVENT_EXEC)
  28. #define PT_TRACE_VFORK_DONE PT_EVENT_FLAG(PTRACE_EVENT_VFORK_DONE)
  29. #define PT_TRACE_EXIT PT_EVENT_FLAG(PTRACE_EVENT_EXIT)
  30. #define PT_TRACE_SECCOMP PT_EVENT_FLAG(PTRACE_EVENT_SECCOMP)
  31. #define PT_EXITKILL (PTRACE_O_EXITKILL << PT_OPT_FLAG_SHIFT)
  32. /* single stepping state bits (used on ARM and PA-RISC) */
  33. #define PT_SINGLESTEP_BIT 31
  34. #define PT_SINGLESTEP (1<<PT_SINGLESTEP_BIT)
  35. #define PT_BLOCKSTEP_BIT 30
  36. #define PT_BLOCKSTEP (1<<PT_BLOCKSTEP_BIT)
  37. extern long arch_ptrace(struct task_struct *child, long request,
  38. unsigned long addr, unsigned long data);
  39. extern int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len);
  40. extern int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len);
  41. extern void ptrace_disable(struct task_struct *);
  42. extern int ptrace_request(struct task_struct *child, long request,
  43. unsigned long addr, unsigned long data);
  44. extern void ptrace_notify(int exit_code);
  45. extern void __ptrace_link(struct task_struct *child,
  46. struct task_struct *new_parent);
  47. extern void __ptrace_unlink(struct task_struct *child);
  48. extern void exit_ptrace(struct task_struct *tracer);
  49. #define PTRACE_MODE_READ 0x01
  50. #define PTRACE_MODE_ATTACH 0x02
  51. #define PTRACE_MODE_NOAUDIT 0x04
  52. /* Returns true on success, false on denial. */
  53. extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
  54. static inline int ptrace_reparented(struct task_struct *child)
  55. {
  56. return !same_thread_group(child->real_parent, child->parent);
  57. }
  58. static inline void ptrace_unlink(struct task_struct *child)
  59. {
  60. if (unlikely(child->ptrace))
  61. __ptrace_unlink(child);
  62. }
  63. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  64. unsigned long data);
  65. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  66. unsigned long data);
  67. /**
  68. * ptrace_parent - return the task that is tracing the given task
  69. * @task: task to consider
  70. *
  71. * Returns %NULL if no one is tracing @task, or the &struct task_struct
  72. * pointer to its tracer.
  73. *
  74. * Must called under rcu_read_lock(). The pointer returned might be kept
  75. * live only by RCU. During exec, this may be called with task_lock() held
  76. * on @task, still held from when check_unsafe_exec() was called.
  77. */
  78. static inline struct task_struct *ptrace_parent(struct task_struct *task)
  79. {
  80. if (unlikely(task->ptrace))
  81. return rcu_dereference(task->parent);
  82. return NULL;
  83. }
  84. /**
  85. * ptrace_event_enabled - test whether a ptrace event is enabled
  86. * @task: ptracee of interest
  87. * @event: %PTRACE_EVENT_* to test
  88. *
  89. * Test whether @event is enabled for ptracee @task.
  90. *
  91. * Returns %true if @event is enabled, %false otherwise.
  92. */
  93. static inline bool ptrace_event_enabled(struct task_struct *task, int event)
  94. {
  95. return task->ptrace & PT_EVENT_FLAG(event);
  96. }
  97. /**
  98. * ptrace_event - possibly stop for a ptrace event notification
  99. * @event: %PTRACE_EVENT_* value to report
  100. * @message: value for %PTRACE_GETEVENTMSG to return
  101. *
  102. * Check whether @event is enabled and, if so, report @event and @message
  103. * to the ptrace parent.
  104. *
  105. * Called without locks.
  106. */
  107. static inline void ptrace_event(int event, unsigned long message)
  108. {
  109. if (unlikely(ptrace_event_enabled(current, event))) {
  110. current->ptrace_message = message;
  111. ptrace_notify((event << 8) | SIGTRAP);
  112. } else if (event == PTRACE_EVENT_EXEC) {
  113. /* legacy EXEC report via SIGTRAP */
  114. if ((current->ptrace & (PT_PTRACED|PT_SEIZED)) == PT_PTRACED)
  115. send_sig(SIGTRAP, current, 0);
  116. }
  117. }
  118. /**
  119. * ptrace_event_pid - possibly stop for a ptrace event notification
  120. * @event: %PTRACE_EVENT_* value to report
  121. * @pid: process identifier for %PTRACE_GETEVENTMSG to return
  122. *
  123. * Check whether @event is enabled and, if so, report @event and @pid
  124. * to the ptrace parent. @pid is reported as the pid_t seen from the
  125. * the ptrace parent's pid namespace.
  126. *
  127. * Called without locks.
  128. */
  129. static inline void ptrace_event_pid(int event, struct pid *pid)
  130. {
  131. /*
  132. * FIXME: There's a potential race if a ptracer in a different pid
  133. * namespace than parent attaches between computing message below and
  134. * when we acquire tasklist_lock in ptrace_stop(). If this happens,
  135. * the ptracer will get a bogus pid from PTRACE_GETEVENTMSG.
  136. */
  137. unsigned long message = 0;
  138. struct pid_namespace *ns;
  139. rcu_read_lock();
  140. ns = task_active_pid_ns(rcu_dereference(current->parent));
  141. if (ns)
  142. message = pid_nr_ns(pid, ns);
  143. rcu_read_unlock();
  144. ptrace_event(event, message);
  145. }
  146. /**
  147. * ptrace_init_task - initialize ptrace state for a new child
  148. * @child: new child task
  149. * @ptrace: true if child should be ptrace'd by parent's tracer
  150. *
  151. * This is called immediately after adding @child to its parent's children
  152. * list. @ptrace is false in the normal case, and true to ptrace @child.
  153. *
  154. * Called with current's siglock and write_lock_irq(&tasklist_lock) held.
  155. */
  156. static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
  157. {
  158. INIT_LIST_HEAD(&child->ptrace_entry);
  159. INIT_LIST_HEAD(&child->ptraced);
  160. child->jobctl = 0;
  161. child->ptrace = 0;
  162. child->parent = child->real_parent;
  163. if (unlikely(ptrace) && current->ptrace) {
  164. child->ptrace = current->ptrace;
  165. __ptrace_link(child, current->parent);
  166. if (child->ptrace & PT_SEIZED)
  167. task_set_jobctl_pending(child, JOBCTL_TRAP_STOP);
  168. else
  169. sigaddset(&child->pending.signal, SIGSTOP);
  170. set_tsk_thread_flag(child, TIF_SIGPENDING);
  171. }
  172. }
  173. /**
  174. * ptrace_release_task - final ptrace-related cleanup of a zombie being reaped
  175. * @task: task in %EXIT_DEAD state
  176. *
  177. * Called with write_lock(&tasklist_lock) held.
  178. */
  179. static inline void ptrace_release_task(struct task_struct *task)
  180. {
  181. BUG_ON(!list_empty(&task->ptraced));
  182. ptrace_unlink(task);
  183. BUG_ON(!list_empty(&task->ptrace_entry));
  184. }
  185. #ifndef force_successful_syscall_return
  186. /*
  187. * System call handlers that, upon successful completion, need to return a
  188. * negative value should call force_successful_syscall_return() right before
  189. * returning. On architectures where the syscall convention provides for a
  190. * separate error flag (e.g., alpha, ia64, ppc{,64}, sparc{,64}, possibly
  191. * others), this macro can be used to ensure that the error flag will not get
  192. * set. On architectures which do not support a separate error flag, the macro
  193. * is a no-op and the spurious error condition needs to be filtered out by some
  194. * other means (e.g., in user-level, by passing an extra argument to the
  195. * syscall handler, or something along those lines).
  196. */
  197. #define force_successful_syscall_return() do { } while (0)
  198. #endif
  199. #ifndef is_syscall_success
  200. /*
  201. * On most systems we can tell if a syscall is a success based on if the retval
  202. * is an error value. On some systems like ia64 and powerpc they have different
  203. * indicators of success/failure and must define their own.
  204. */
  205. #define is_syscall_success(regs) (!IS_ERR_VALUE((unsigned long)(regs_return_value(regs))))
  206. #endif
  207. /*
  208. * <asm/ptrace.h> should define the following things inside #ifdef __KERNEL__.
  209. *
  210. * These do-nothing inlines are used when the arch does not
  211. * implement single-step. The kerneldoc comments are here
  212. * to document the interface for all arch definitions.
  213. */
  214. #ifndef arch_has_single_step
  215. /**
  216. * arch_has_single_step - does this CPU support user-mode single-step?
  217. *
  218. * If this is defined, then there must be function declarations or
  219. * inlines for user_enable_single_step() and user_disable_single_step().
  220. * arch_has_single_step() should evaluate to nonzero iff the machine
  221. * supports instruction single-step for user mode.
  222. * It can be a constant or it can test a CPU feature bit.
  223. */
  224. #define arch_has_single_step() (0)
  225. /**
  226. * user_enable_single_step - single-step in user-mode task
  227. * @task: either current or a task stopped in %TASK_TRACED
  228. *
  229. * This can only be called when arch_has_single_step() has returned nonzero.
  230. * Set @task so that when it returns to user mode, it will trap after the
  231. * next single instruction executes. If arch_has_block_step() is defined,
  232. * this must clear the effects of user_enable_block_step() too.
  233. */
  234. static inline void user_enable_single_step(struct task_struct *task)
  235. {
  236. BUG(); /* This can never be called. */
  237. }
  238. /**
  239. * user_disable_single_step - cancel user-mode single-step
  240. * @task: either current or a task stopped in %TASK_TRACED
  241. *
  242. * Clear @task of the effects of user_enable_single_step() and
  243. * user_enable_block_step(). This can be called whether or not either
  244. * of those was ever called on @task, and even if arch_has_single_step()
  245. * returned zero.
  246. */
  247. static inline void user_disable_single_step(struct task_struct *task)
  248. {
  249. }
  250. #else
  251. extern void user_enable_single_step(struct task_struct *);
  252. extern void user_disable_single_step(struct task_struct *);
  253. #endif /* arch_has_single_step */
  254. #ifndef arch_has_block_step
  255. /**
  256. * arch_has_block_step - does this CPU support user-mode block-step?
  257. *
  258. * If this is defined, then there must be a function declaration or inline
  259. * for user_enable_block_step(), and arch_has_single_step() must be defined
  260. * too. arch_has_block_step() should evaluate to nonzero iff the machine
  261. * supports step-until-branch for user mode. It can be a constant or it
  262. * can test a CPU feature bit.
  263. */
  264. #define arch_has_block_step() (0)
  265. /**
  266. * user_enable_block_step - step until branch in user-mode task
  267. * @task: either current or a task stopped in %TASK_TRACED
  268. *
  269. * This can only be called when arch_has_block_step() has returned nonzero,
  270. * and will never be called when single-instruction stepping is being used.
  271. * Set @task so that when it returns to user mode, it will trap after the
  272. * next branch or trap taken.
  273. */
  274. static inline void user_enable_block_step(struct task_struct *task)
  275. {
  276. BUG(); /* This can never be called. */
  277. }
  278. #else
  279. extern void user_enable_block_step(struct task_struct *);
  280. #endif /* arch_has_block_step */
  281. #ifdef ARCH_HAS_USER_SINGLE_STEP_INFO
  282. extern void user_single_step_siginfo(struct task_struct *tsk,
  283. struct pt_regs *regs, siginfo_t *info);
  284. #else
  285. static inline void user_single_step_siginfo(struct task_struct *tsk,
  286. struct pt_regs *regs, siginfo_t *info)
  287. {
  288. memset(info, 0, sizeof(*info));
  289. info->si_signo = SIGTRAP;
  290. }
  291. #endif
  292. #ifndef arch_ptrace_stop_needed
  293. /**
  294. * arch_ptrace_stop_needed - Decide whether arch_ptrace_stop() should be called
  295. * @code: current->exit_code value ptrace will stop with
  296. * @info: siginfo_t pointer (or %NULL) for signal ptrace will stop with
  297. *
  298. * This is called with the siglock held, to decide whether or not it's
  299. * necessary to release the siglock and call arch_ptrace_stop() with the
  300. * same @code and @info arguments. It can be defined to a constant if
  301. * arch_ptrace_stop() is never required, or always is. On machines where
  302. * this makes sense, it should be defined to a quick test to optimize out
  303. * calling arch_ptrace_stop() when it would be superfluous. For example,
  304. * if the thread has not been back to user mode since the last stop, the
  305. * thread state might indicate that nothing needs to be done.
  306. *
  307. * This is guaranteed to be invoked once before a task stops for ptrace and
  308. * may include arch-specific operations necessary prior to a ptrace stop.
  309. */
  310. #define arch_ptrace_stop_needed(code, info) (0)
  311. #endif
  312. #ifndef arch_ptrace_stop
  313. /**
  314. * arch_ptrace_stop - Do machine-specific work before stopping for ptrace
  315. * @code: current->exit_code value ptrace will stop with
  316. * @info: siginfo_t pointer (or %NULL) for signal ptrace will stop with
  317. *
  318. * This is called with no locks held when arch_ptrace_stop_needed() has
  319. * just returned nonzero. It is allowed to block, e.g. for user memory
  320. * access. The arch can have machine-specific work to be done before
  321. * ptrace stops. On ia64, register backing store gets written back to user
  322. * memory here. Since this can be costly (requires dropping the siglock),
  323. * we only do it when the arch requires it for this particular stop, as
  324. * indicated by arch_ptrace_stop_needed().
  325. */
  326. #define arch_ptrace_stop(code, info) do { } while (0)
  327. #endif
  328. #ifndef current_pt_regs
  329. #define current_pt_regs() task_pt_regs(current)
  330. #endif
  331. #ifndef ptrace_signal_deliver
  332. #define ptrace_signal_deliver() ((void)0)
  333. #endif
  334. /*
  335. * unlike current_pt_regs(), this one is equal to task_pt_regs(current)
  336. * on *all* architectures; the only reason to have a per-arch definition
  337. * is optimisation.
  338. */
  339. #ifndef signal_pt_regs
  340. #define signal_pt_regs() task_pt_regs(current)
  341. #endif
  342. #ifndef current_user_stack_pointer
  343. #define current_user_stack_pointer() user_stack_pointer(current_pt_regs())
  344. #endif
  345. extern int task_current_syscall(struct task_struct *target, long *callno,
  346. unsigned long args[6], unsigned int maxargs,
  347. unsigned long *sp, unsigned long *pc);
  348. #endif