ptrace.h 15 KB

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