sandbox.c 3.2 KB

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