landloc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Zero-Clause BSD
  3. * ===============
  4. *
  5. * Copyright 2024 shtrophic <christoph@liebender.dev>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted.
  9. *
  10. * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  12. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
  13. * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
  14. * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  15. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  16. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. */
  19. /**
  20. * Usage example:
  21. *
  22. LL_BEGIN(my_sandbox_function, const char *rw_path) {
  23. LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
  24. LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP);
  25. } LL_END
  26. int main(void) {
  27. int status = my_sandbox_function("some/path");
  28. if (status != 0) {
  29. // error
  30. }
  31. }
  32. */
  33. #ifndef __LANDLOC_H__
  34. #define __LANDLOC_H__
  35. #ifndef __linux__
  36. #error "no landlock without linux"
  37. #endif
  38. #include <unistd.h>
  39. #include <linux/landlock.h>
  40. #include <sys/syscall.h>
  41. #include <sys/prctl.h>
  42. #include <fcntl.h>
  43. #ifndef O_PATH
  44. #define O_PATH 010000000
  45. #endif
  46. #ifndef LL_PRINTERR
  47. #define LL_PRINTERR(fmt, ...) (void)fmt;
  48. #else
  49. #include <string.h>
  50. #include <errno.h>
  51. #endif
  52. #define LL_FS_ALL (\
  53. LANDLOCK_ACCESS_FS_EXECUTE |\
  54. LANDLOCK_ACCESS_FS_WRITE_FILE |\
  55. LANDLOCK_ACCESS_FS_READ_FILE |\
  56. LANDLOCK_ACCESS_FS_READ_DIR |\
  57. LANDLOCK_ACCESS_FS_REMOVE_DIR |\
  58. LANDLOCK_ACCESS_FS_REMOVE_FILE |\
  59. LANDLOCK_ACCESS_FS_MAKE_CHAR |\
  60. LANDLOCK_ACCESS_FS_MAKE_DIR |\
  61. LANDLOCK_ACCESS_FS_MAKE_REG |\
  62. LANDLOCK_ACCESS_FS_MAKE_SOCK |\
  63. LANDLOCK_ACCESS_FS_MAKE_FIFO |\
  64. LANDLOCK_ACCESS_FS_MAKE_BLOCK |\
  65. LANDLOCK_ACCESS_FS_MAKE_SYM |\
  66. LANDLOCK_ACCESS_FS_REFER |\
  67. LANDLOCK_ACCESS_FS_TRUNCATE |\
  68. LANDLOCK_ACCESS_FS_IOCTL_DEV )
  69. #define LL_NET_ALL (\
  70. LANDLOCK_ACCESS_NET_BIND_TCP |\
  71. LANDLOCK_ACCESS_NET_CONNECT_TCP )
  72. #define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
  73. int ll_rule_fd, ll_abi;\
  74. struct landlock_ruleset_attr __rattr = {0};\
  75. struct landlock_path_beneath_attr __pattr = {0};\
  76. struct landlock_net_port_attr __nattr = {0};\
  77. int __err = 0;\
  78. __rattr.handled_access_fs = LL_FS_ALL;\
  79. __rattr.handled_access_net = LL_NET_ALL;\
  80. ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
  81. switch (ll_abi) {\
  82. case -1: return -1;\
  83. case 1: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; __attribute__((fallthrough));\
  84. case 2: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; __attribute__((fallthrough));\
  85. case 3: __rattr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP); __attribute__((fallthrough));\
  86. case 4: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;\
  87. default: break;\
  88. }\
  89. ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
  90. if (-1 == ll_rule_fd) {\
  91. LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
  92. return -1;\
  93. }
  94. #define LL_END \
  95. __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
  96. if (-1 == __err) {\
  97. LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
  98. goto __close;\
  99. }\
  100. __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
  101. if (__err)\
  102. LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
  103. __close: close(ll_rule_fd);\
  104. return __err; }
  105. #define LL_PATH(p, rules) do {\
  106. const char *__path = (p);\
  107. __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
  108. __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
  109. if (-1 == __pattr.parent_fd) {\
  110. LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
  111. __err = -1;\
  112. goto __close;\
  113. }\
  114. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
  115. if (__err) {\
  116. LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
  117. goto __close;\
  118. }\
  119. close(__pattr.parent_fd);\
  120. } while (0)
  121. #define LL_PORT(p, rules) do {\
  122. if (ll_abi > 3) {\
  123. unsigned short __port = (p);\
  124. __nattr.allowed_access = (rules);\
  125. __nattr.port = __port;\
  126. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
  127. if (__err) {\
  128. LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
  129. goto __close;\
  130. }\
  131. }\
  132. } while (0)
  133. #endif /* __LANDLOC_H__ */