sandbox.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifndef SYS_SECCOMP
  13. /**
  14. * Used by SIGSYS signal handler to check if the signal was issued due to a
  15. * seccomp2 filter violation.
  16. */
  17. #define SYS_SECCOMP 1
  18. #endif
  19. #include "torint.h"
  20. /**
  21. * Linux definitions
  22. */
  23. #ifdef __linux__
  24. #ifndef __USE_GNU
  25. #define __USE_GNU
  26. #endif
  27. #include <sys/ucontext.h>
  28. #include <seccomp.h>
  29. #include <netdb.h>
  30. #define PARAM_PTR 0
  31. #define PARAM_NUM 1
  32. /**
  33. * Structure used to manage a sandbox configuration.
  34. *
  35. * It is implemented as a linked list of parameters. Currently only controls
  36. * parameters for open, openat, execve, stat64.
  37. */
  38. struct pfd_elem {
  39. /** syscall associated with parameter. */
  40. int syscall;
  41. /** parameter index. */
  42. int pindex;
  43. /** parameter value. */
  44. intptr_t param;
  45. /** parameter flag (0 = not protected, 1 = protected). */
  46. int prot;
  47. struct pfd_elem *next;
  48. };
  49. /** Typedef to structure used to manage a sandbox configuration. */
  50. typedef struct pfd_elem sandbox_cfg_t;
  51. /**
  52. * Structure used for keeping a linked list of getaddrinfo pre-recorded
  53. * results.
  54. */
  55. struct sb_addr_info_el {
  56. /** Name of the address info result. */
  57. char *name;
  58. /** Pre-recorded getaddrinfo result. */
  59. struct addrinfo *info;
  60. /** Next element in the list. */
  61. struct sb_addr_info_el *next;
  62. };
  63. /** Typedef to structure used to manage an addrinfo list. */
  64. typedef struct sb_addr_info_el sb_addr_info_t;
  65. /** Function pointer defining the prototype of a filter function.*/
  66. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  67. sandbox_cfg_t *filter);
  68. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  69. typedef struct {
  70. /** function pointers associated with the filter */
  71. sandbox_filter_func_t *filter_func;
  72. /** filter function pointer parameters */
  73. sandbox_cfg_t *filter_dynamic;
  74. } sandbox_t;
  75. /**
  76. * Linux 32 bit definitions
  77. */
  78. #if defined(__i386__)
  79. #define REG_SYSCALL REG_EAX
  80. /**
  81. * Linux 64 bit definitions
  82. */
  83. #elif defined(__x86_64__)
  84. #define REG_SYSCALL REG_RAX
  85. #endif
  86. #endif // __linux__
  87. /** Pre-calls getaddrinfo in order to pre-record result. */
  88. int sandbox_add_addrinfo(const char *addr);
  89. /** Replacement for getaddrinfo(), using pre-recorded results. */
  90. int sandbox_getaddrinfo(const char *name, struct addrinfo **res);
  91. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  92. void sandbox_set_debugging_fd(int fd);
  93. /** Returns a registered protected string used with the sandbox, given that
  94. * it matches the parameter.
  95. */
  96. const char* sandbox_intern_string(const char *param);
  97. /** Creates an empty sandbox configuration file.*/
  98. sandbox_cfg_t * sandbox_cfg_new(void);
  99. /**
  100. * Function used to add a open allowed filename to a supplied configuration.
  101. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  102. * function that the char* needs to be free-ed, 0 means the pointer does not
  103. * need to be free-ed.
  104. */
  105. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file,
  106. int fr);
  107. /** Function used to add a series of open allowed filenames to a supplied
  108. * configuration.
  109. * @param cfg sandbox configuration.
  110. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  111. * the char* specifies the path to the allowed file, 1 tells the function
  112. * that the char* needs to be free-ed, 0 means the pointer does not need to
  113. * be free-ed; the final parameter needs to be <NULL, 0>.
  114. */
  115. int sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...);
  116. /**
  117. * Function used to add a openat allowed filename to a supplied configuration.
  118. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  119. * function that the char* needs to be free-ed, 0 means the pointer does not
  120. * need to be free-ed.
  121. */
  122. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file,
  123. int fr);
  124. /** Function used to add a series of openat allowed filenames to a supplied
  125. * configuration.
  126. * @param cfg sandbox configuration.
  127. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  128. * the char* specifies the path to the allowed file, 1 tells the function
  129. * that the char* needs to be free-ed, 0 means the pointer does not need to
  130. * be free-ed; the final parameter needs to be <NULL, 0>.
  131. */
  132. int sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...);
  133. /**
  134. * Function used to add a execve allowed filename to a supplied configuration.
  135. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  136. * function that the char* needs to be free-ed, 0 means the pointer does not
  137. * need to be free-ed.
  138. */
  139. int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, char *com);
  140. /** Function used to add a series of execve allowed filenames to a supplied
  141. * configuration.
  142. * @param cfg sandbox configuration.
  143. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  144. * the char* specifies the path to the allowed file, 1 tells the function
  145. * that the char* needs to be free-ed, 0 means the pointer does not need to
  146. * be free-ed; the final parameter needs to be <NULL, 0>.
  147. */
  148. int sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...);
  149. /**
  150. * Function used to add a stat64 allowed filename to a supplied configuration.
  151. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  152. * function that the char* needs to be free-ed, 0 means the pointer does not
  153. * need to be free-ed.
  154. */
  155. int sandbox_cfg_allow_stat64_filename(sandbox_cfg_t **cfg, char *file,
  156. int fr);
  157. /** Function used to add a series of stat64 allowed filenames to a supplied
  158. * configuration.
  159. * @param cfg sandbox configuration.
  160. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  161. * the char* specifies the path to the allowed file, 1 tells the function
  162. * that the char* needs to be free-ed, 0 means the pointer does not need to
  163. * be free-ed; the final parameter needs to be <NULL, 0>.
  164. */
  165. int sandbox_cfg_allow_stat64_filename_array(sandbox_cfg_t **cfg, ...);
  166. /** Function used to initialise a sandbox configuration.*/
  167. int sandbox_init(sandbox_cfg_t* cfg);
  168. #endif /* SANDBOX_H_ */