landloc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * Repository: https://git.sr.ht/~shtrophic/landloc.h
  21. */
  22. /**
  23. * Usage:
  24. *
  25. * Define a sandboxing function using the LL_BEGIN(...) and LL_END macros.
  26. * the arguments of LL_BEGIN are the function's signature.
  27. * Between those macros, implement your sandbox using LL_PATH() and LL_PORT() macros.
  28. * Calling LL_PATH() and LL_PORT() anywhere else will not work.
  29. * You may prepend `static` before LL_BEGIN to make the function static.
  30. * You need (should) wrap your sandboxing code in another set of braces:
  31. *
  32. LL_BEGIN(my_sandbox_function, const char *rw_path) {
  33. LL_PATH(rw_path, LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_EXECUTE);
  34. LL_PORT(443, LANDLOCK_ACCESS_NET_CONNECT_TCP);
  35. } LL_END
  36. *
  37. * Then, call it in your application's code.
  38. *
  39. int main(void) {
  40. int status = my_sandbox_function("some/path");
  41. if (status != 0) {
  42. // error
  43. }
  44. }
  45. *
  46. * You may define LL_PRINTERR(fmt, ...) before including this header to enable debug output:
  47. *
  48. #define LL_PRINTERR(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
  49. #include "landloc.h"
  50. */
  51. #ifndef __LANDLOC_H__
  52. #define __LANDLOC_H__
  53. #ifndef __linux__
  54. #error "no landlock without linux"
  55. #endif
  56. #include <unistd.h>
  57. #include <linux/landlock.h>
  58. #include <sys/syscall.h>
  59. #include <sys/prctl.h>
  60. #include <fcntl.h>
  61. #ifndef O_PATH
  62. #define O_PATH 010000000
  63. #endif
  64. #ifndef LL_PRINTERR
  65. #define LL_PRINTERR(fmt, ...) (void)fmt;
  66. #else
  67. #include <string.h>
  68. #include <errno.h>
  69. #endif
  70. #define LL_FS_ALL (\
  71. LANDLOCK_ACCESS_FS_EXECUTE |\
  72. LANDLOCK_ACCESS_FS_WRITE_FILE |\
  73. LANDLOCK_ACCESS_FS_READ_FILE |\
  74. LANDLOCK_ACCESS_FS_READ_DIR |\
  75. LANDLOCK_ACCESS_FS_REMOVE_DIR |\
  76. LANDLOCK_ACCESS_FS_REMOVE_FILE |\
  77. LANDLOCK_ACCESS_FS_MAKE_CHAR |\
  78. LANDLOCK_ACCESS_FS_MAKE_DIR |\
  79. LANDLOCK_ACCESS_FS_MAKE_REG |\
  80. LANDLOCK_ACCESS_FS_MAKE_SOCK |\
  81. LANDLOCK_ACCESS_FS_MAKE_FIFO |\
  82. LANDLOCK_ACCESS_FS_MAKE_BLOCK |\
  83. LANDLOCK_ACCESS_FS_MAKE_SYM |\
  84. LANDLOCK_ACCESS_FS_REFER |\
  85. LANDLOCK_ACCESS_FS_TRUNCATE |\
  86. LANDLOCK_ACCESS_FS_IOCTL_DEV )
  87. #define LL_NET_ALL (\
  88. LANDLOCK_ACCESS_NET_BIND_TCP |\
  89. LANDLOCK_ACCESS_NET_CONNECT_TCP )
  90. #define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
  91. int ll_rule_fd, ll_abi;\
  92. struct landlock_ruleset_attr __rattr = {0};\
  93. struct landlock_path_beneath_attr __pattr = {0};\
  94. struct landlock_net_port_attr __nattr = {0};\
  95. int __err = 0;\
  96. __rattr.handled_access_fs = LL_FS_ALL;\
  97. __rattr.handled_access_net = LL_NET_ALL;\
  98. ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
  99. switch (ll_abi) {\
  100. case -1: return -1;\
  101. case 1: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; __attribute__((fallthrough));\
  102. case 2: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; __attribute__((fallthrough));\
  103. case 3: __rattr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP); __attribute__((fallthrough));\
  104. case 4: __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;\
  105. default: break;\
  106. }\
  107. ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
  108. if (-1 == ll_rule_fd) {\
  109. LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
  110. return -1;\
  111. }
  112. #define LL_END \
  113. __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
  114. if (-1 == __err) {\
  115. LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
  116. goto __close;\
  117. }\
  118. __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
  119. if (__err)\
  120. LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
  121. __close: close(ll_rule_fd);\
  122. return __err; }
  123. #define LL_PATH(p, rules) do {\
  124. const char *__path = (p);\
  125. __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
  126. __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
  127. if (-1 == __pattr.parent_fd) {\
  128. LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
  129. __err = -1;\
  130. goto __close;\
  131. }\
  132. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
  133. if (__err) {\
  134. LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
  135. goto __close;\
  136. }\
  137. close(__pattr.parent_fd);\
  138. } while (0)
  139. #define LL_PORT(p, rules) do {\
  140. unsigned short __port = (p);\
  141. __nattr.allowed_access = (rules);\
  142. if (ll_abi > 3) {\
  143. __nattr.port = __port;\
  144. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
  145. if (__err) {\
  146. LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
  147. goto __close;\
  148. }\
  149. }\
  150. } while (0)
  151. #endif /* __LANDLOC_H__ */