ptracesandbox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef VSF_PTRACESANDBOX_H
  2. #define VSF_PTRACESANDBOX_H
  3. /* Forward delcarations */
  4. struct pt_sandbox;
  5. typedef int (*ptrace_sandbox_validator_t)(struct pt_sandbox*, void*);
  6. /* ptrace_sandbox_alloc()
  7. * PURPOSE
  8. * Allocates a ptrace sandbox object which is needed for the rest of the API.
  9. * RETURNS
  10. * NULL on failure, otherwise an opaque handle.
  11. * TODO
  12. * Only one per process supported at this time.
  13. */
  14. struct pt_sandbox* ptrace_sandbox_alloc();
  15. /* ptrace_sandbox_free()
  16. * PURPOSE
  17. * Frees the sandbox object.
  18. * PARAMETERS
  19. * p_sandbox - the sandbox handle to free
  20. */
  21. void ptrace_sandbox_free(struct pt_sandbox* p_sandbox);
  22. /* ptrace_sandbox_launch_process()
  23. * PURPOSE
  24. * Launches a new process and attaches the sandbox to it when it stops.
  25. * PARAMETERS
  26. * p_sandbox - the sandbox handle
  27. * p_func - the function to call at the start of the new process
  28. * p_arg - an argument to pass to the function
  29. * RETURNS
  30. * -1 on failure, otherwise an id for the created process. Not necessarily a
  31. * "pid", please treat is as opaque!
  32. * TODO
  33. * Only one call to this per sandbox object is supported at this time.
  34. */
  35. int ptrace_sandbox_launch_process(struct pt_sandbox* p_sandbox,
  36. void (*p_func)(void*),
  37. void* p_arg);
  38. /* ptrace_sandbox_run_processes()
  39. * PURPOSE
  40. * Runs sandboxed children until they exit or are killed.
  41. * PARAMETERS
  42. * p_sandbox - the sandbox handle
  43. * RETURNS
  44. * 0 on normal exit or death of processes.
  45. * -1 if any process breached the policy.
  46. */
  47. int ptrace_sandbox_run_processes(struct pt_sandbox* p_sandbox);
  48. /* ptrace_sandbox_kill_processes()
  49. * PURPOSE
  50. * Safely kills off all sandboxed processes.
  51. * PARAMETERS
  52. * p_sandbox - the sandbox handle
  53. */
  54. void ptrace_sandbox_kill_processes(struct pt_sandbox* p_sandbox);
  55. /* ptrace_sandbox_get_arg()
  56. * PURPOSE
  57. * Gets a syscall argument value for a process stopped in syscall entry.
  58. * PARAMETERS
  59. * p_sandbox - the sandbox handle
  60. * arg - the arg number to get (zero-based)
  61. * p_out - the result is written here
  62. * RETURNS
  63. * 0 on success; otherwise it's a failure.
  64. */
  65. int ptrace_sandbox_get_arg(struct pt_sandbox* p_sandbox,
  66. int arg,
  67. unsigned long* p_out);
  68. /* ptrace_sandbox_get_socketcall_arg()
  69. * PURPOSE
  70. * Gets a syscall argument value for a process stopped in syscall entry, where
  71. * the system call is a socket-related one. On some architectures (e.g. i386,
  72. * socket calls are in fact multiplexed and store the arguments in a struct
  73. * in user space, hence the need for abstraction.
  74. * PARAMETERS
  75. * p_sandbox - the sandbox handle
  76. * arg - the arg number to get (zero-based)
  77. * p_out - the result is written here
  78. * RETURNS
  79. * 0 on success; otherwise it's a failure.
  80. */
  81. int ptrace_sandbox_get_socketcall_arg(struct pt_sandbox* p_sandbox,
  82. int arg,
  83. unsigned long* p_out);
  84. /* ptrace_sandbox_get_long()
  85. * PURPOSE
  86. * Gets a long from the address space of the process stopped in syscall entry.
  87. * PARAMETERS
  88. * p_sandbox - the sandbox handle
  89. * ptr - the address to read the long from
  90. * p_out - the result is written here
  91. * RETURNS
  92. * 0 on success; otherwise it's a failure.
  93. */
  94. int ptrace_sandbox_get_long(struct pt_sandbox* p_sandbox,
  95. unsigned long ptr,
  96. unsigned long* p_out);
  97. /* ptrace_sandbox_get_buf()
  98. * PURPOSE
  99. * Gets a piece of memory from the address space of the process stopped in
  100. * syscall entry.
  101. * PARAMETERS
  102. * p_sandbox - the sandbox handle
  103. * ptr - the address to read the buffer from
  104. * len - the length of the buffer
  105. * p_buf - the result is written here
  106. * RETURNS
  107. * 0 on success; otherwise it's a failure.
  108. */
  109. int ptrace_sandbox_get_buf(struct pt_sandbox* p_sandbox,
  110. unsigned long ptr,
  111. unsigned long len,
  112. void* p_buf);
  113. /* ptrace_sandbox_attach_point()
  114. * PURPOSE
  115. * Used by the sandbox child code to stop and indicate it is ready to be
  116. * attached to.
  117. * NOTES
  118. * In the event of error trying to stop, the process is forcibly killed as a
  119. * security measure.
  120. */
  121. void ptrace_sandbox_attach_point(void);
  122. /* POLICY EDIT: permits exit() and exit_group() */
  123. void ptrace_sandbox_permit_exit(struct pt_sandbox* p_sandbox);
  124. /* POLICY EDIT: permits read() */
  125. void ptrace_sandbox_permit_read(struct pt_sandbox* p_sandbox);
  126. /* POLICY EDIT: permits write() */
  127. void ptrace_sandbox_permit_write(struct pt_sandbox* p_sandbox);
  128. /* POLICY EDIT: permits sigaction() and rt_sigaction() */
  129. void ptrace_sandbox_permit_sigaction(struct pt_sandbox* p_sandbox);
  130. /* POLICY EDIT: permits alarm() */
  131. void ptrace_sandbox_permit_alarm(struct pt_sandbox* p_sandbox);
  132. /* POLICY EDIT: permits time() and gettimeofday() */
  133. void ptrace_sandbox_permit_query_time(struct pt_sandbox* p_sandbox);
  134. /* POLICY EDIT: permits mmap2() (but not the MAP_SHARED flag) */
  135. void ptrace_sandbox_permit_mmap(struct pt_sandbox* p_sandbox);
  136. /* POLICY EDIT: permits mprotect() */
  137. void ptrace_sandbox_permit_mprotect(struct pt_sandbox* p_sandbox);
  138. /* POLICY EDIT: permits stat(), stat64(), lstat(), lstat64() */
  139. void ptrace_sandbox_permit_file_stats(struct pt_sandbox* p_sandbox);
  140. /* POLICY EDIT: permits fstat(), fstat64() */
  141. void ptrace_sandbox_permit_fd_stats(struct pt_sandbox* p_sandbox);
  142. /* POLICY EDIT: permits getcwd() */
  143. void ptrace_sandbox_permit_getcwd(struct pt_sandbox* p_sandbox);
  144. /* POLICY EDIT: permits chdir() */
  145. void ptrace_sandbox_permit_chdir(struct pt_sandbox* p_sandbox);
  146. /* POLICY EDIT: permits umask() */
  147. void ptrace_sandbox_permit_umask(struct pt_sandbox* p_sandbox);
  148. /* POLICY EDIT: permits open(), except O_ASYNC and O_DIRECT. Only O_RDONLY
  149. * allowed unless writeable is 1
  150. */
  151. void ptrace_sandbox_permit_open(struct pt_sandbox* p_sandbox, int writeable);
  152. /* POLICY EDIT: permits close() */
  153. void ptrace_sandbox_permit_close(struct pt_sandbox* p_sandbox);
  154. /* POLICY EDIT: permits getdents(), getdents64() */
  155. void ptrace_sandbox_permit_getdents(struct pt_sandbox* p_sandbox);
  156. /* POLICY EDIT: permits fcntl(), fcntl64() for file locking, safe F_SETFL flag
  157. * setting (no O_ASYNC, O_DIRECT), F_SETOWN for your own pid and F_SETFD.
  158. */
  159. void ptrace_sandbox_permit_fcntl(struct pt_sandbox* p_sandbox);
  160. /* POLICY EDIT: permits sendfile(), sendfile64() */
  161. void ptrace_sandbox_permit_sendfile(struct pt_sandbox* p_sandbox);
  162. /* POLICY EDIT: permits lseek(), llseek() */
  163. void ptrace_sandbox_permit_seek(struct pt_sandbox* p_sandbox);
  164. /* POLICY EDIT: permits select(), newselect() */
  165. void ptrace_sandbox_permit_select(struct pt_sandbox* p_sandbox);
  166. /* POLICY EDIT: permits unlink() */
  167. void ptrace_sandbox_permit_unlink(struct pt_sandbox* p_sandbox);
  168. /* POLICY EDIT: permits mkdir() */
  169. void ptrace_sandbox_permit_mkdir(struct pt_sandbox* p_sandbox);
  170. /* POLICY EDIT: permits rmdir() */
  171. void ptrace_sandbox_permit_rmdir(struct pt_sandbox* p_sandbox);
  172. /* POLICY EDIT: permits rename() */
  173. void ptrace_sandbox_permit_rename(struct pt_sandbox* p_sandbox);
  174. /* POLICY EDIT: permits utime(), utimes() */
  175. void ptrace_sandbox_permit_utime(struct pt_sandbox* p_sandbox);
  176. /* POLICY EDIT: permits sigreturn() */
  177. void ptrace_sandbox_permit_sigreturn(struct pt_sandbox* p_sandbox);
  178. /* POLICY EDIT: permits recv() */
  179. void ptrace_sandbox_permit_recv(struct pt_sandbox* p_sandbox);
  180. /* POLICY EDIT: permits readlink() */
  181. void ptrace_sandbox_permit_readlink(struct pt_sandbox* p_sandbox);
  182. /* POLICY EDIT: permits brk() */
  183. void ptrace_sandbox_permit_brk(struct pt_sandbox* p_sandbox);
  184. /* POLICY EDIT: permits nanosleep() */
  185. void ptrace_sandbox_permit_sleep(struct pt_sandbox* p_sandbox);
  186. /* POLICY EDIT: permits fchmod() */
  187. void ptrace_sandbox_permit_fchmod(struct pt_sandbox* p_sandbox);
  188. /* POLICY EDIT: permits chmod() */
  189. void ptrace_sandbox_permit_chmod(struct pt_sandbox* p_sandbox);
  190. /* POLICY EDIT: permits fchown(), fchown32() */
  191. void ptrace_sandbox_permit_fchown(struct pt_sandbox* p_sandbox);
  192. /* POLICY EDIT: permits mremap() */
  193. void ptrace_sandbox_permit_mremap(struct pt_sandbox* p_sandbox);
  194. /* POLICY EDIT: permits ftruncate(), ftruncate64() */
  195. void ptrace_sandbox_permit_ftruncate(struct pt_sandbox* p_sandbox);
  196. /* POLICY EDIT: permits socket() */
  197. void ptrace_sandbox_permit_socket(struct pt_sandbox* p_sandbox);
  198. /* POLICY EDIT: set validator for socket() */
  199. void ptrace_sandbox_set_socket_validator(struct pt_sandbox* p_sandbox,
  200. ptrace_sandbox_validator_t val,
  201. void* p_arg);
  202. /* POLICY EDIT: permits bind() */
  203. void ptrace_sandbox_permit_bind(struct pt_sandbox* p_sandbox);
  204. /* POLICY EDIT: set validator for bind() */
  205. void ptrace_sandbox_set_bind_validator(struct pt_sandbox* p_sandbox,
  206. ptrace_sandbox_validator_t val,
  207. void* p_arg);
  208. /* POLICY EDIT: permits connect() */
  209. void ptrace_sandbox_permit_connect(struct pt_sandbox* p_sandbox);
  210. /* POLICY EDIT: set validator for connect() */
  211. void ptrace_sandbox_set_connect_validator(struct pt_sandbox* p_sandbox,
  212. ptrace_sandbox_validator_t val,
  213. void* p_arg);
  214. /* POLICY EDIT: permits listen() */
  215. void ptrace_sandbox_permit_listen(struct pt_sandbox* p_sandbox);
  216. /* POLICY EDIT: permits accept() */
  217. void ptrace_sandbox_permit_accept(struct pt_sandbox* p_sandbox);
  218. /* POLICY EDIT: permits setsockopt() */
  219. void ptrace_sandbox_permit_setsockopt(struct pt_sandbox* p_sandbox);
  220. /* POLICY EDIT: set validator for setsockopt() */
  221. void ptrace_sandbox_set_setsockopt_validator(struct pt_sandbox* p_sandbox,
  222. ptrace_sandbox_validator_t val,
  223. void* p_arg);
  224. /* POLICY EDIT: permits getsockopt() */
  225. void ptrace_sandbox_permit_getsockopt(struct pt_sandbox* p_sandbox);
  226. /* POLICY EDIT: set validator for getsockopt() */
  227. void ptrace_sandbox_set_getsockopt_validator(struct pt_sandbox* p_sandbox,
  228. ptrace_sandbox_validator_t val,
  229. void* p_arg);
  230. /* POLICY EDIT: permits shutdown() */
  231. void ptrace_sandbox_permit_shutdown(struct pt_sandbox* p_sandbox);
  232. /* The traced process is unexpectedly dead; probably an external SIGKILL */
  233. #define PTRACE_SANDBOX_ERR_DEAD -1
  234. /* An unexpected error from ptrace() */
  235. #define PTRACE_SANDBOX_ERR_PTRACE -2
  236. /* An unexpected error from waitpid() */
  237. #define PTRACE_SANDBOX_ERR_WAITPID -3
  238. /* An unexpected waitpid() status was returned */
  239. #define PTRACE_SANDBOX_ERR_WAIT_STATUS -4
  240. /* A syscall not in the policy was attempted */
  241. #define PTRACE_SANDBOX_ERR_POLICY_SYSCALL -5
  242. /* A "bad" syscall was attemped: out-of-bounds, 64-bit in a 32-bit child etc. */
  243. #define PTRACE_SANDBOX_ERR_BAD_SYSCALL -6
  244. /* Bad arguments to a generally accepted syscall */
  245. #define PTRACE_SANDBOX_ERR_POLICY_ARGS -7
  246. /* Abuse of our API */
  247. #define PTRACE_SANDBOX_ERR_API_ABUSE_STOPIT -8
  248. #endif /* VSF_PTRACESANDBOX_H */