sandbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "xs.h"
  2. #include "snac.h"
  3. #ifdef __linux__
  4. #ifndef WITHOUT_SANDBOX
  5. #include <linux/version.h>
  6. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
  7. #define WITHOUT_SANDBOX
  8. #endif
  9. #endif
  10. #endif /* __linux__ */
  11. #ifdef WITHOUT_SANDBOX
  12. void sbox_enter(const char *basedir)
  13. {
  14. /* nothing to do */
  15. (void)basedir;
  16. srv_debug(0, xs_fmt("Linux sandboxing disabled or unsupported"));
  17. }
  18. #else /* WITHOUT_SANDBOX */
  19. #include <unistd.h>
  20. #if defined (__linux__)
  21. #define LL_PRINTERR(fmt, ...) srv_debug(0, xs_fmt(fmt, __VA_ARGS__))
  22. #include "landloc.h"
  23. static
  24. LL_BEGIN(sbox_enter_linux_, const char* basedir, const char *address, int smail) {
  25. const unsigned long long
  26. rd = LANDLOCK_ACCESS_FS_READ_DIR,
  27. rf = LANDLOCK_ACCESS_FS_READ_FILE,
  28. w = LANDLOCK_ACCESS_FS_WRITE_FILE |
  29. LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT,
  30. c = LANDLOCK_ACCESS_FS_MAKE_DIR |
  31. LANDLOCK_ACCESS_FS_MAKE_REG |
  32. LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT |
  33. LANDLOCK_ACCESS_FS_MAKE_SYM |
  34. LANDLOCK_ACCESS_FS_REMOVE_DIR |
  35. LANDLOCK_ACCESS_FS_REMOVE_FILE |
  36. LANDLOCK_ACCESS_FS_REFER_COMPAT,
  37. s = LANDLOCK_ACCESS_FS_MAKE_SOCK,
  38. x = LANDLOCK_ACCESS_FS_EXECUTE;
  39. LL_PATH(basedir, rf|rd|w|c);
  40. LL_PATH("/tmp", rf|rd|w|c);
  41. #ifndef WITHOUT_SHM
  42. LL_PATH("/dev/shm", rf|w|c );
  43. #endif
  44. LL_PATH("/etc/resolv.conf", rf );
  45. LL_PATH("/etc/hosts", rf );
  46. LL_PATH("/etc/ssl", rf );
  47. LL_PATH("/usr/share/zoneinfo", rf );
  48. if (mtime("/etc/pki") > 0)
  49. LL_PATH("/etc/pki", rf );
  50. if (*address == '/')
  51. LL_PATH(address, s);
  52. if (smail)
  53. LL_PATH("/usr/sbin/sendmail", x);
  54. if (*address != '/') {
  55. unsigned short listen_port = xs_number_get(xs_dict_get(srv_config, "port"));
  56. LL_PORT(listen_port, LANDLOCK_ACCESS_NET_BIND_TCP_COMPAT);
  57. }
  58. LL_PORT(80, LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT);
  59. LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT);
  60. } LL_END
  61. #endif
  62. void sbox_enter(const char *basedir)
  63. {
  64. const char *address = xs_dict_get(srv_config, "address");
  65. int smail = !xs_is_true(xs_dict_get(srv_config, "disable_email_notifications"));
  66. #if defined (__OpenBSD__)
  67. if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
  68. srv_log(xs_dup("disable_openbsd_security is deprecated. Use disable_sandbox instead."));
  69. return;
  70. }
  71. if (xs_is_true(xs_dict_get(srv_config, "disable_sandbox"))) {
  72. srv_debug(0, xs_dup("Sandbox disabled by admin"));
  73. return;
  74. }
  75. srv_debug(1, xs_fmt("Calling unveil()"));
  76. unveil(basedir, "rwc");
  77. unveil("/tmp", "rwc");
  78. unveil("/etc/resolv.conf", "r");
  79. unveil("/etc/hosts", "r");
  80. unveil("/etc/ssl/openssl.cnf", "r");
  81. unveil("/etc/ssl/cert.pem", "r");
  82. unveil("/usr/share/zoneinfo", "r");
  83. if (smail)
  84. unveil("/usr/sbin/sendmail", "x");
  85. if (*address == '/')
  86. unveil(address, "rwc");
  87. unveil(NULL, NULL);
  88. srv_debug(1, xs_fmt("Calling pledge()"));
  89. xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
  90. if (smail)
  91. p = xs_str_cat(p, " exec");
  92. if (*address == '/')
  93. p = xs_str_cat(p, " unix");
  94. pledge(p, NULL);
  95. #elif defined (__linux__)
  96. if (xs_is_true(xs_dict_get_def(srv_config, "disable_sandbox", xs_stock(XSTYPE_TRUE)))) {
  97. srv_debug(0, xs_dup("Sandbox disabled by admin"));
  98. return;
  99. }
  100. if (sbox_enter_linux_(basedir, address, smail) == 0)
  101. srv_log(xs_dup("landlocked"));
  102. else
  103. srv_log(xs_dup("landlocking failed"));
  104. #endif
  105. }
  106. #endif /* WITHOUT_SANDBOX */