sandbox.h 7.4 KB

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