sandbox.c 3.1 KB

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