sandbox.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2018, 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 "lib/cc/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 /* !defined(SYS_SECCOMP) */
  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. #include <sys/ucontext.h>
  32. #include <seccomp.h>
  33. #include <netdb.h>
  34. #define PARAM_PTR 0
  35. #define PARAM_NUM 1
  36. /**
  37. * Enum used to manage the type of the implementation for general purpose.
  38. */
  39. typedef enum {
  40. /** Libseccomp implementation based on seccomp2*/
  41. LIBSECCOMP2 = 0
  42. } SB_IMPL;
  43. /**
  44. * Configuration parameter structure associated with the LIBSECCOMP2
  45. * implementation.
  46. */
  47. typedef struct smp_param {
  48. /** syscall associated with parameter. */
  49. int syscall;
  50. /** parameter value. */
  51. char *value;
  52. /** parameter value, second argument. */
  53. char *value2;
  54. /** parameter flag (0 = not protected, 1 = protected). */
  55. int prot;
  56. } smp_param_t;
  57. /**
  58. * Structure used to manage a sandbox configuration.
  59. *
  60. * It is implemented as a linked list of parameters. Currently only controls
  61. * parameters for open, openat, execve, stat64.
  62. */
  63. struct sandbox_cfg_elem {
  64. /** Sandbox implementation which dictates the parameter type. */
  65. SB_IMPL implem;
  66. /** Configuration parameter. */
  67. smp_param_t *param;
  68. /** Next element of the configuration*/
  69. struct sandbox_cfg_elem *next;
  70. };
  71. /** Function pointer defining the prototype of a filter function.*/
  72. typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
  73. sandbox_cfg_t *filter);
  74. /** Type that will be used in step 3 in order to manage multiple sandboxes.*/
  75. typedef struct {
  76. /** function pointers associated with the filter */
  77. sandbox_filter_func_t *filter_func;
  78. /** filter function pointer parameters */
  79. sandbox_cfg_t *filter_dynamic;
  80. } sandbox_t;
  81. #endif /* defined(USE_LIBSECCOMP) */
  82. #ifdef USE_LIBSECCOMP
  83. /** Returns a registered protected string used with the sandbox, given that
  84. * it matches the parameter.
  85. */
  86. const char* sandbox_intern_string(const char *param);
  87. #else /* !(defined(USE_LIBSECCOMP)) */
  88. #define sandbox_intern_string(s) (s)
  89. #endif /* defined(USE_LIBSECCOMP) */
  90. /** Creates an empty sandbox configuration file.*/
  91. sandbox_cfg_t * sandbox_cfg_new(void);
  92. /**
  93. * Function used to add a open allowed filename to a supplied configuration.
  94. * The (char*) specifies the path to the allowed file; we take ownership
  95. * of the pointer.
  96. */
  97. int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file);
  98. int sandbox_cfg_allow_chmod_filename(sandbox_cfg_t **cfg, char *file);
  99. int sandbox_cfg_allow_chown_filename(sandbox_cfg_t **cfg, char *file);
  100. /* DOCDOC */
  101. int sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2);
  102. /**
  103. * Function used to add a openat allowed filename to a supplied configuration.
  104. * The (char*) specifies the path to the allowed file; we steal the pointer to
  105. * that file.
  106. */
  107. int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file);
  108. /**
  109. * Function used to add a stat/stat64 allowed filename to a configuration.
  110. * The (char*) specifies the path to the allowed file; that pointer is stolen.
  111. */
  112. int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file);
  113. /** Function used to initialise a sandbox configuration.*/
  114. int sandbox_init(sandbox_cfg_t* cfg);
  115. /** Return true iff the sandbox is turned on. */
  116. int sandbox_is_active(void);
  117. #endif /* !defined(SANDBOX_H_) */