sandbox.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file sandbox.h
  8. * \brief Header file for sandbox.c.
  9. **/
  10. #ifndef SANDBOX_H_
  11. #define SANDBOX_H_
  12. #include "orconfig.h"
  13. #include "torint.h"
  14. #ifndef SYS_SECCOMP
  15. /**
  16. * Used by SIGSYS signal handler to check if the signal was issued due to a
  17. * seccomp2 filter violation.
  18. */
  19. #define SYS_SECCOMP 1
  20. #endif
  21. #if defined(HAVE_SECCOMP_H) && defined(__linux__)
  22. #define USE_LIBSECCOMP
  23. #endif
  24. /**
  25. * Linux definitions
  26. */
  27. #ifdef USE_LIBSECCOMP
  28. #ifndef __USE_GNU
  29. #define __USE_GNU
  30. #endif
  31. #include <sys/ucontext.h>
  32. #include <seccomp.h>
  33. #include <netdb.h>
  34. #define PARAM_PTR 0
  35. #define PARAM_NUM 1
  36. /**
  37. * Enum used to manage the type of the implementation for general purpose.
  38. */
  39. typedef enum {
  40. /** Libseccomp implementation based on seccomp2*/
  41. LIBSECCOMP2 = 0
  42. } SB_IMPL;
  43. /**
  44. * Configuration parameter structure associated with the LIBSECCOMP2
  45. * implementation.
  46. */
  47. typedef struct smp_param {
  48. /** syscall associated with parameter. */
  49. int syscall;
  50. /** parameter index. */
  51. int pindex;
  52. /** parameter value. */
  53. intptr_t value;
  54. /** parameter flag (0 = not protected, 1 = protected). */
  55. int prot;
  56. } smp_param_t;
  57. /**
  58. * Structure used to manage a sandbox configuration.
  59. *
  60. * It is implemented as a linked list of parameters. Currently only controls
  61. * parameters for open, openat, execve, stat64.
  62. */
  63. struct sandbox_cfg_elem {
  64. /** Sandbox implementation which dictates the parameter type. */
  65. SB_IMPL implem;
  66. /** Configuration parameter. */
  67. void *param;
  68. /** Next element of the configuration*/
  69. struct sandbox_cfg_elem *next;
  70. };
  71. /** Typedef to structure used to manage a sandbox configuration. */
  72. typedef struct sandbox_cfg_elem sandbox_cfg_t;
  73. /**
  74. * Structure used for keeping a linked list of getaddrinfo pre-recorded
  75. * results.
  76. */
  77. struct sb_addr_info_el {
  78. /** Name of the address info result. */
  79. char *name;
  80. /** Pre-recorded getaddrinfo result. */
  81. struct addrinfo *info;
  82. /** Next element in the list. */
  83. struct sb_addr_info_el *next;
  84. };
  85. /** Typedef to structure used to manage an addrinfo list. */
  86. typedef struct sb_addr_info_el sb_addr_info_t;
  87. /** Function pointer defining the prototype of a filter function.*/
  88. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  89. sandbox_cfg_t *filter);
  90. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  91. typedef struct {
  92. /** function pointers associated with the filter */
  93. sandbox_filter_func_t *filter_func;
  94. /** filter function pointer parameters */
  95. sandbox_cfg_t *filter_dynamic;
  96. } sandbox_t;
  97. /**
  98. * Linux 32 bit definitions
  99. */
  100. #if defined(__i386__)
  101. #define REG_SYSCALL REG_EAX
  102. /**
  103. * Linux 64 bit definitions
  104. */
  105. #elif defined(__x86_64__)
  106. #define REG_SYSCALL REG_RAX
  107. #endif
  108. #endif // USE_LIBSECCOMP
  109. #ifdef USE_LIBSECCOMP
  110. /** Pre-calls getaddrinfo in order to pre-record result. */
  111. int sandbox_add_addrinfo(const char *addr);
  112. struct addrinfo;
  113. /** Replacement for getaddrinfo(), using pre-recorded results. */
  114. int sandbox_getaddrinfo(const char *name, const char *servname,
  115. const struct addrinfo *hints,
  116. struct addrinfo **res);
  117. #else
  118. #define sandbox_getaddrinfo(name, servname, hints, res) \
  119. getaddrinfo((name),(servname), (hints),(res))
  120. #define sandbox_add_addrinfo(name) \
  121. ((void)(name))
  122. #endif
  123. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  124. void sandbox_set_debugging_fd(int fd);
  125. #ifdef USE_LIBSECCOMP
  126. /** Returns a registered protected string used with the sandbox, given that
  127. * it matches the parameter.
  128. */
  129. const char* sandbox_intern_string(const char *param);
  130. #else
  131. #define sandbox_intern_string(s) (s)
  132. #endif
  133. /** Creates an empty sandbox configuration file.*/
  134. sandbox_cfg_t * sandbox_cfg_new(void);
  135. /**
  136. * Function used to add a open allowed filename to a supplied configuration.
  137. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  138. * function that the char* needs to be free-ed, 0 means the pointer does not
  139. * need to be free-ed.
  140. */
  141. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file,
  142. int fr);
  143. /** Function used to add a series of open allowed filenames to a supplied
  144. * configuration.
  145. * @param cfg sandbox configuration.
  146. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  147. * the char* specifies the path to the allowed file, 1 tells the function
  148. * that the char* needs to be free-ed, 0 means the pointer does not need to
  149. * be free-ed; the final parameter needs to be <NULL, 0>.
  150. */
  151. int sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...);
  152. /**
  153. * Function used to add a openat allowed filename to a supplied configuration.
  154. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  155. * function that the char* needs to be free-ed, 0 means the pointer does not
  156. * need to be free-ed.
  157. */
  158. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file,
  159. int fr);
  160. /** Function used to add a series of openat allowed filenames to a supplied
  161. * configuration.
  162. * @param cfg sandbox configuration.
  163. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  164. * the char* specifies the path to the allowed file, 1 tells the function
  165. * that the char* needs to be free-ed, 0 means the pointer does not need to
  166. * be free-ed; the final parameter needs to be <NULL, 0>.
  167. */
  168. int sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...);
  169. /**
  170. * Function used to add a execve allowed filename to a supplied configuration.
  171. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  172. * function that the char* needs to be free-ed, 0 means the pointer does not
  173. * need to be free-ed.
  174. */
  175. int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com);
  176. /** Function used to add a series of execve allowed filenames to a supplied
  177. * configuration.
  178. * @param cfg sandbox configuration.
  179. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  180. * the char* specifies the path to the allowed file, 1 tells the function
  181. * that the char* needs to be free-ed, 0 means the pointer does not need to
  182. * be free-ed; the final parameter needs to be <NULL, 0>.
  183. */
  184. int sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...);
  185. /**
  186. * Function used to add a stat/stat64 allowed filename to a configuration.
  187. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  188. * function that the char* needs to be free-ed, 0 means the pointer does not
  189. * need to be free-ed.
  190. */
  191. int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file,
  192. int fr);
  193. /** Function used to add a series of stat64 allowed filenames to a supplied
  194. * configuration.
  195. * @param cfg sandbox configuration.
  196. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  197. * the char* specifies the path to the allowed file, 1 tells the function
  198. * that the char* needs to be free-ed, 0 means the pointer does not need to
  199. * be free-ed; the final parameter needs to be <NULL, 0>.
  200. */
  201. int sandbox_cfg_allow_stat_filename_array(sandbox_cfg_t **cfg, ...);
  202. /** Function used to initialise a sandbox configuration.*/
  203. int sandbox_init(sandbox_cfg_t* cfg);
  204. #endif /* SANDBOX_H_ */