sandbox.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 value. */
  54. intptr_t value;
  55. /** parameter value, second argument. */
  56. intptr_t value2;
  57. /** parameter flag (0 = not protected, 1 = protected). */
  58. int prot;
  59. } smp_param_t;
  60. /**
  61. * Structure used to manage a sandbox configuration.
  62. *
  63. * It is implemented as a linked list of parameters. Currently only controls
  64. * parameters for open, openat, execve, stat64.
  65. */
  66. struct sandbox_cfg_elem {
  67. /** Sandbox implementation which dictates the parameter type. */
  68. SB_IMPL implem;
  69. /** Configuration parameter. */
  70. smp_param_t *param;
  71. /** Next element of the configuration*/
  72. struct sandbox_cfg_elem *next;
  73. };
  74. /** Function pointer defining the prototype of a filter function.*/
  75. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  76. sandbox_cfg_t *filter);
  77. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  78. typedef struct {
  79. /** function pointers associated with the filter */
  80. sandbox_filter_func_t *filter_func;
  81. /** filter function pointer parameters */
  82. sandbox_cfg_t *filter_dynamic;
  83. } sandbox_t;
  84. #endif // USE_LIBSECCOMP
  85. #ifdef USE_LIBSECCOMP
  86. /** Pre-calls getaddrinfo in order to pre-record result. */
  87. int sandbox_add_addrinfo(const char *addr);
  88. struct addrinfo;
  89. /** Replacement for getaddrinfo(), using pre-recorded results. */
  90. int sandbox_getaddrinfo(const char *name, const char *servname,
  91. const struct addrinfo *hints,
  92. struct addrinfo **res);
  93. #define sandbox_freeaddrinfo(addrinfo) ((void)0)
  94. void sandbox_free_getaddrinfo_cache(void);
  95. #else
  96. #define sandbox_getaddrinfo(name, servname, hints, res) \
  97. getaddrinfo((name),(servname), (hints),(res))
  98. #define sandbox_add_addrinfo(name) \
  99. ((void)(name))
  100. #define sandbox_freeaddrinfo(addrinfo) \
  101. freeaddrinfo((addrinfo))
  102. #define sandbox_free_getaddrinfo_cache()
  103. #endif
  104. #ifdef USE_LIBSECCOMP
  105. /** Returns a registered protected string used with the sandbox, given that
  106. * it matches the parameter.
  107. */
  108. const char* sandbox_intern_string(const char *param);
  109. #else
  110. #define sandbox_intern_string(s) (s)
  111. #endif
  112. /** Creates an empty sandbox configuration file.*/
  113. sandbox_cfg_t * sandbox_cfg_new(void);
  114. /**
  115. * Function used to add a open allowed filename to a supplied configuration.
  116. * The (char*) specifies the path to the allowed file; we take ownership
  117. * of the pointer.
  118. */
  119. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file);
  120. /**DOCDOC*/
  121. int sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2);
  122. /** Function used to add a series of open allowed filenames to a supplied
  123. * configuration.
  124. * @param cfg sandbox configuration.
  125. * @param ... a list of stealable pointers to permitted files. The last
  126. * one must be NULL.
  127. */
  128. int sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...);
  129. /**
  130. * Function used to add a openat allowed filename to a supplied configuration.
  131. * The (char*) specifies the path to the allowed file; we steal the pointer to
  132. * that file.
  133. */
  134. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file);
  135. /** Function used to add a series of openat allowed filenames to a supplied
  136. * configuration.
  137. * @param cfg sandbox configuration.
  138. * @param ... a list of stealable pointers to permitted files. The last
  139. * one must be NULL.
  140. */
  141. int sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...);
  142. #if 0
  143. /**
  144. * Function used to add a execve allowed filename to a supplied configuration.
  145. * The (char*) specifies the path to the allowed file; that pointer is stolen.
  146. */
  147. int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com);
  148. /** Function used to add a series of execve allowed filenames to a supplied
  149. * configuration.
  150. * @param cfg sandbox configuration.
  151. * @param ... an array of stealable pointers to permitted files. The last
  152. * one must be NULL.
  153. */
  154. int sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...);
  155. #endif
  156. /**
  157. * Function used to add a stat/stat64 allowed filename to a configuration.
  158. * The (char*) specifies the path to the allowed file; that pointer is stolen.
  159. */
  160. int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file);
  161. /** Function used to add a series of stat64 allowed filenames to a supplied
  162. * configuration.
  163. * @param cfg sandbox configuration.
  164. * @param ... an array of stealable pointers to permitted files. The last
  165. * one must be NULL.
  166. */
  167. int sandbox_cfg_allow_stat_filename_array(sandbox_cfg_t **cfg, ...);
  168. /** Function used to initialise a sandbox configuration.*/
  169. int sandbox_init(sandbox_cfg_t* cfg);
  170. /** Return true iff the sandbox is turned on. */
  171. int sandbox_is_active(void);
  172. #endif /* SANDBOX_H_ */