sandbox.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. int syscall; // syscall associated with parameter
  40. char pindex; // parameter index
  41. intptr_t param; // parameter value
  42. char prot; // parameter flag (0 = not protected, 1 = protected)
  43. struct pfd_elem *next;
  44. };
  45. /** Typedef to structure used to manage a sandbox configuration. */
  46. typedef struct pfd_elem sandbox_cfg_t;
  47. /** Function pointer defining the prototype of a filter function.*/
  48. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  49. sandbox_cfg_t *filter);
  50. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  51. typedef struct {
  52. // function pointers associated with the filter
  53. sandbox_filter_func_t *filter_func;
  54. // filter function pointer parameters
  55. sandbox_cfg_t *filter_dynamic;
  56. } sandbox_t;
  57. /**
  58. * Linux 32 bit definitions
  59. */
  60. #if defined(__i386__)
  61. #define REG_SYSCALL REG_EAX
  62. /**
  63. * Linux 64 bit definitions
  64. */
  65. #elif defined(__x86_64__)
  66. #define REG_SYSCALL REG_RAX
  67. #endif
  68. #endif // __linux__
  69. /** Replacement for getaddrinfo(), using pre-recorded results. */
  70. int sandbox_getaddrinfo(const char *name, struct addrinfo **res);
  71. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  72. void sandbox_set_debugging_fd(int fd);
  73. /** Returns a registered protected string used with the sandbox, given that
  74. * it matches the parameter.
  75. */
  76. const char* sandbox_intern_string(const char *param);
  77. /** Creates an empty sandbox configuration file.*/
  78. sandbox_cfg_t * sandbox_cfg_new();
  79. /**
  80. * Function used to add a open allowed filename to a supplied configuration.
  81. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  82. * function that the char* needs to be free-ed, 0 means the pointer does not
  83. * need to be free-ed.
  84. */
  85. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file,
  86. char fr);
  87. /** Function used to add a series of open allowed filenames to a supplied
  88. * configuration.
  89. * @param cfg sandbox configuration.
  90. * @param num number of files.
  91. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  92. * the char* specifies the path to the allowed file, 1 tells the function
  93. * that the char* needs to be free-ed, 0 means the pointer does not need to
  94. * be free-ed.
  95. */
  96. int sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, int num, ...);
  97. /**
  98. * Function used to add a openat allowed filename to a supplied configuration.
  99. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  100. * function that the char* needs to be free-ed, 0 means the pointer does not
  101. * need to be free-ed.
  102. */
  103. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file,
  104. char fr);
  105. /** Function used to add a series of openat allowed filenames to a supplied
  106. * configuration.
  107. * @param cfg sandbox configuration.
  108. * @param num number of files.
  109. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  110. * the char* specifies the path to the allowed file, 1 tells the function
  111. * that the char* needs to be free-ed, 0 means the pointer does not need to
  112. * be free-ed.
  113. */
  114. int sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, int num, ...);
  115. /**
  116. * Function used to add a execve allowed filename to a supplied configuration.
  117. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  118. * function that the char* needs to be free-ed, 0 means the pointer does not
  119. * need to be free-ed.
  120. */
  121. int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, char *com);
  122. /** Function used to add a series of execve allowed filenames to a supplied
  123. * configuration.
  124. * @param cfg sandbox configuration.
  125. * @param num number of files.
  126. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  127. * the char* specifies the path to the allowed file, 1 tells the function
  128. * that the char* needs to be free-ed, 0 means the pointer does not need to
  129. * be free-ed.
  130. */
  131. int sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, int num, ...);
  132. /**
  133. * Function used to add a stat64 allowed filename to a supplied configuration.
  134. * The (char*) specifies the path to the allowed file, fr = 1 tells the
  135. * function that the char* needs to be free-ed, 0 means the pointer does not
  136. * need to be free-ed.
  137. */
  138. int sandbox_cfg_allow_stat64_filename(sandbox_cfg_t **cfg, char *file,
  139. char fr);
  140. /** Function used to add a series of stat64 allowed filenames to a supplied
  141. * configuration.
  142. * @param cfg sandbox configuration.
  143. * @param num number of files.
  144. * @param ... all future parameters are specified as pairs of <(char*), 1 / 0>
  145. * the char* specifies the path to the allowed file, 1 tells the function
  146. * that the char* needs to be free-ed, 0 means the pointer does not need to
  147. * be free-ed.
  148. */
  149. int sandbox_cfg_allow_stat64_filename_array(sandbox_cfg_t **cfg,
  150. int num, ...);
  151. /** Function used to initialise a sandbox configuration.*/
  152. int sandbox_init(sandbox_cfg_t* cfg);
  153. #endif /* SANDBOX_H_ */