sandbox.c 3.6 KB

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