ptrace.h 15 KB

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