sandbox.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "xs.h"
  2. #include "snac.h"
  3. #if defined(__OpenBSD__)
  4. void sbox_enter(const char *basedir)
  5. {
  6. const char *address = xs_dict_get(srv_config, "address");
  7. if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
  8. srv_log(xs_dup("OpenBSD security disabled by admin"));
  9. return;
  10. }
  11. srv_debug(1, xs_fmt("Calling unveil()"));
  12. unveil(basedir, "rwc");
  13. unveil("/tmp", "rwc");
  14. unveil("/etc/resolv.conf", "r");
  15. unveil("/etc/hosts", "r");
  16. unveil("/etc/ssl/openssl.cnf", "r");
  17. unveil("/etc/ssl/cert.pem", "r");
  18. unveil("/usr/share/zoneinfo", "r");
  19. if (*address == '/')
  20. unveil(address, "rwc");
  21. unveil(NULL, NULL);
  22. srv_debug(1, xs_fmt("Calling pledge()"));
  23. xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
  24. if (*address == '/')
  25. p = xs_str_cat(p, " unix");
  26. pledge(p, NULL);
  27. }
  28. #elif defined(__linux__)
  29. #if defined(WITH_LINUX_SANDBOX)
  30. #include <unistd.h>
  31. #define LL_PRINTERR(fmt, ...) srv_debug(0, xs_fmt(fmt, __VA_ARGS__))
  32. #include "landloc.h"
  33. static
  34. LL_BEGIN(sbox_enter_linux_, const char* basedir, const char *address, int smtp_port) {
  35. const unsigned long long
  36. rd = LANDLOCK_ACCESS_FS_READ_DIR,
  37. rf = LANDLOCK_ACCESS_FS_READ_FILE,
  38. w = LANDLOCK_ACCESS_FS_WRITE_FILE |
  39. LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT,
  40. c = LANDLOCK_ACCESS_FS_MAKE_DIR |
  41. LANDLOCK_ACCESS_FS_MAKE_REG |
  42. LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT |
  43. LANDLOCK_ACCESS_FS_MAKE_SYM |
  44. LANDLOCK_ACCESS_FS_REMOVE_DIR |
  45. LANDLOCK_ACCESS_FS_REMOVE_FILE |
  46. LANDLOCK_ACCESS_FS_REFER_COMPAT,
  47. s = LANDLOCK_ACCESS_FS_MAKE_SOCK,
  48. x = LANDLOCK_ACCESS_FS_EXECUTE;
  49. char *resolved_path = NULL;
  50. LL_PATH(basedir, rf|rd|w|c);
  51. LL_PATH("/tmp", rf|rd|w|c);
  52. #ifndef WITHOUT_SHM
  53. LL_PATH("/dev/shm", rf|w|c );
  54. #endif
  55. LL_PATH("/dev/urandom", rf );
  56. LL_PATH("/etc/resolv.conf", rf );
  57. LL_PATH("/etc/hosts", rf );
  58. LL_PATH("/etc/ssl", rf|rd );
  59. if ((resolved_path = realpath("/etc/ssl/cert.pem", NULL))) {
  60. /* some distros like cert.pem to be a symlink */
  61. LL_PATH(resolved_path, rf );
  62. free(resolved_path);
  63. }
  64. LL_PATH("/usr/share/zoneinfo", rf );
  65. if (mtime("/etc/pki") > 0)
  66. LL_PATH("/etc/pki", rf );
  67. if (*address == '/') {
  68. /* the directory holding the socket must be allowed */
  69. xs *l = xs_split(address, "/");
  70. l = xs_list_del(l, -1);
  71. xs *sdir = xs_join(l, "/");
  72. LL_PATH(sdir, s);
  73. }
  74. if (*address != '/') {
  75. unsigned short listen_port = xs_number_get(xs_dict_get(srv_config, "port"));
  76. LL_PORT(listen_port, LANDLOCK_ACCESS_NET_BIND_TCP_COMPAT);
  77. }
  78. LL_PORT(80, LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT);
  79. LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT);
  80. if (smtp_port > 0)
  81. LL_PORT((unsigned short)smtp_port, LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT);
  82. } LL_END
  83. void sbox_enter(const char *basedir)
  84. {
  85. const char *errstr;
  86. const char *address = xs_dict_get(srv_config, "address");
  87. const char *smtp_url = xs_dict_get(srv_config, "smtp_url");
  88. int smtp_port = -1;
  89. if (xs_is_true(xs_dict_get(srv_config, "disable_sandbox"))) {
  90. srv_debug(1, xs_dup("Linux sandbox disabled by admin"));
  91. return;
  92. }
  93. if (xs_is_string(smtp_url) && *smtp_url != '\0') {
  94. smtp_port = parse_port(smtp_url, &errstr);
  95. if (errstr)
  96. srv_debug(0, xs_fmt("Couldn't determine port from '%s': %s", smtp_url, errstr));
  97. }
  98. if (sbox_enter_linux_(basedir, address, smtp_port) == 0)
  99. srv_debug(1, xs_dup("Linux sandbox enabled"));
  100. else
  101. srv_debug(0, xs_dup("Linux sandbox failed"));
  102. }
  103. #else /* defined(WITH_LINUX_SANDBOX) */
  104. void sbox_enter(const char *basedir)
  105. {
  106. (void)basedir;
  107. srv_debug(1, xs_fmt("Linux sandbox not compiled in"));
  108. }
  109. #endif
  110. #else
  111. /* other OSs: dummy sbox_enter() */
  112. void sbox_enter(const char *basedir)
  113. {
  114. (void)basedir;
  115. }
  116. #endif /* __OpenBSD__ */