sandbox.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-2016, 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. #ifndef _GNU_SOURCE
  35. #define _GNU_SOURCE
  36. #endif
  37. #include <sys/ucontext.h>
  38. #include <seccomp.h>
  39. #include <netdb.h>
  40. #define PARAM_PTR 0
  41. #define PARAM_NUM 1
  42. /**
  43. * Enum used to manage the type of the implementation for general purpose.
  44. */
  45. typedef enum {
  46. /** Libseccomp implementation based on seccomp2*/
  47. LIBSECCOMP2 = 0
  48. } SB_IMPL;
  49. /**
  50. * Configuration parameter structure associated with the LIBSECCOMP2
  51. * implementation.
  52. */
  53. typedef struct smp_param {
  54. /** syscall associated with parameter. */
  55. int syscall;
  56. /** parameter value. */
  57. char *value;
  58. /** parameter value, second argument. */
  59. char *value2;
  60. /** parameter flag (0 = not protected, 1 = protected). */
  61. int prot;
  62. } smp_param_t;
  63. /**
  64. * Structure used to manage a sandbox configuration.
  65. *
  66. * It is implemented as a linked list of parameters. Currently only controls
  67. * parameters for open, openat, execve, stat64.
  68. */
  69. struct sandbox_cfg_elem {
  70. /** Sandbox implementation which dictates the parameter type. */
  71. SB_IMPL implem;
  72. /** Configuration parameter. */
  73. smp_param_t *param;
  74. /** Next element of the configuration*/
  75. struct sandbox_cfg_elem *next;
  76. };
  77. /** Function pointer defining the prototype of a filter function.*/
  78. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  79. sandbox_cfg_t *filter);
  80. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  81. typedef struct {
  82. /** function pointers associated with the filter */
  83. sandbox_filter_func_t *filter_func;
  84. /** filter function pointer parameters */
  85. sandbox_cfg_t *filter_dynamic;
  86. } sandbox_t;
  87. #endif // USE_LIBSECCOMP
  88. #ifdef USE_LIBSECCOMP
  89. /** Pre-calls getaddrinfo in order to pre-record result. */
  90. int sandbox_add_addrinfo(const char *addr);
  91. struct addrinfo;
  92. /** Replacement for getaddrinfo(), using pre-recorded results. */
  93. int sandbox_getaddrinfo(const char *name, const char *servname,
  94. const struct addrinfo *hints,
  95. struct addrinfo **res);
  96. void sandbox_freeaddrinfo(struct addrinfo *addrinfo);
  97. void sandbox_free_getaddrinfo_cache(void);
  98. #else
  99. #define sandbox_getaddrinfo(name, servname, hints, res) \
  100. getaddrinfo((name),(servname), (hints),(res))
  101. #define sandbox_add_addrinfo(name) \
  102. ((void)(name))
  103. #define sandbox_freeaddrinfo(addrinfo) \
  104. freeaddrinfo((addrinfo))
  105. #define sandbox_free_getaddrinfo_cache()
  106. #endif
  107. #ifdef USE_LIBSECCOMP
  108. /** Returns a registered protected string used with the sandbox, given that
  109. * it matches the parameter.
  110. */
  111. const char* sandbox_intern_string(const char *param);
  112. #else
  113. #define sandbox_intern_string(s) (s)
  114. #endif
  115. /** Creates an empty sandbox configuration file.*/
  116. sandbox_cfg_t * sandbox_cfg_new(void);
  117. /**
  118. * Function used to add a open allowed filename to a supplied configuration.
  119. * The (char*) specifies the path to the allowed file; we take ownership
  120. * of the pointer.
  121. */
  122. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file);
  123. /**DOCDOC*/
  124. int sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2);
  125. /**
  126. * Function used to add a openat allowed filename to a supplied configuration.
  127. * The (char*) specifies the path to the allowed file; we steal the pointer to
  128. * that file.
  129. */
  130. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file);
  131. #if 0
  132. /**
  133. * Function used to add a execve allowed filename to a supplied configuration.
  134. * The (char*) specifies the path to the allowed file; that pointer is stolen.
  135. */
  136. int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com);
  137. #endif
  138. /**
  139. * Function used to add a stat/stat64 allowed filename to a configuration.
  140. * The (char*) specifies the path to the allowed file; that pointer is stolen.
  141. */
  142. int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file);
  143. /** Function used to initialise a sandbox configuration.*/
  144. int sandbox_init(sandbox_cfg_t* cfg);
  145. /** Return true iff the sandbox is turned on. */
  146. int sandbox_is_active(void);
  147. void sandbox_disable_getaddrinfo_cache(void);
  148. #endif /* SANDBOX_H_ */