landloc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <linux/version.h>
  57. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 13, 0)
  58. #include <unistd.h>
  59. #include <linux/landlock.h>
  60. #include <sys/syscall.h>
  61. #include <sys/prctl.h>
  62. #include <fcntl.h>
  63. #ifndef O_PATH
  64. # define O_PATH 010000000
  65. #endif
  66. #ifndef LL_PRINTERR
  67. # define LL_PRINTERR(fmt, ...) (void)fmt;
  68. #else
  69. # include <string.h>
  70. # include <errno.h>
  71. #endif
  72. #ifdef LANDLOCK_ACCESS_FS_REFER
  73. # define __LL_FS_REFER_COMPAT LANDLOCK_ACCESS_FS_REFER
  74. # define __LL_SWITCH_FS_REFER __rattr.handled_access_fs &= ~__LL_FS_REFER_COMPAT
  75. #else
  76. # define __LL_FS_REFER_COMPAT 0
  77. # define __LL_SWITCH_FS_REFER (void)0
  78. #endif
  79. #ifdef LANDLOCK_ACCESS_FS_TRUNCATE
  80. # define __LL_FS_TRUNCATE_COMPAT LANDLOCK_ACCESS_FS_TRUNCATE
  81. # define __LL_SWITCH_FS_TRUNCATE __rattr.handled_access_fs &= ~__LL_FS_TRUNCATE_COMPAT
  82. #else
  83. # define __LL_FS_TRUNCATE_COMPAT 0
  84. # define __LL_SWITCH_FS_TRUNCATE (void)0
  85. #endif
  86. #ifdef LANDLOCK_ACCESS_FS_IOCTL_DEV
  87. # define __LL_FS_IOCTL_DEV_COMPAT LANDLOCK_ACCESS_FS_IOCTL_DEV
  88. # define __LL_SWITCH_FS_IOCTL_DEV __rattr.handled_access_fs &= ~__LL_FS_IOCTL_DEV_COMPAT
  89. #else
  90. # define __LL_FS_IOCTL_DEV_COMPAT 0
  91. # define __LL_SWITCH_FS_IOCTL_DEV (void)0
  92. #endif
  93. #define LL_FS_ALL (\
  94. LANDLOCK_ACCESS_FS_EXECUTE |\
  95. LANDLOCK_ACCESS_FS_WRITE_FILE |\
  96. LANDLOCK_ACCESS_FS_READ_FILE |\
  97. LANDLOCK_ACCESS_FS_READ_DIR |\
  98. LANDLOCK_ACCESS_FS_REMOVE_DIR |\
  99. LANDLOCK_ACCESS_FS_REMOVE_FILE |\
  100. LANDLOCK_ACCESS_FS_MAKE_CHAR |\
  101. LANDLOCK_ACCESS_FS_MAKE_DIR |\
  102. LANDLOCK_ACCESS_FS_MAKE_REG |\
  103. LANDLOCK_ACCESS_FS_MAKE_SOCK |\
  104. LANDLOCK_ACCESS_FS_MAKE_FIFO |\
  105. LANDLOCK_ACCESS_FS_MAKE_BLOCK |\
  106. LANDLOCK_ACCESS_FS_MAKE_SYM |\
  107. __LL_FS_REFER_COMPAT |\
  108. __LL_FS_TRUNCATE_COMPAT |\
  109. __LL_FS_IOCTL_DEV_COMPAT )
  110. #if defined(LANDLOCK_ACCESS_NET_BIND_TCP) && defined(LANDLOCK_ACCESS_NET_CONNECT_TCP)
  111. # define __LL_HAVE_NET
  112. #endif
  113. #ifdef __LL_HAVE_NET
  114. # define LL_NET_ALL (LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP)
  115. # define __LL_DECLARE_NET struct landlock_net_port_attr __nattr = {0}
  116. # define __LL_INIT_NET __rattr.handled_access_net = LL_NET_ALL
  117. # define __LL_SWITCH_NET do { __rattr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP); } while (0)
  118. #else
  119. # define LL_NET_ALL 0
  120. # define __LL_DECLARE_NET (void)0
  121. # define __LL_INIT_NET (void)0
  122. # define __LL_SWITCH_NET (void)0
  123. #endif
  124. #define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
  125. int ll_rule_fd, ll_abi;\
  126. struct landlock_ruleset_attr __rattr = {0};\
  127. struct landlock_path_beneath_attr __pattr = {0};\
  128. __LL_DECLARE_NET;\
  129. int __err = 0;\
  130. __rattr.handled_access_fs = LL_FS_ALL;\
  131. __LL_INIT_NET;\
  132. ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
  133. switch (ll_abi) {\
  134. case -1: return -1;\
  135. case 1: __LL_SWITCH_FS_REFER; __attribute__((fallthrough));\
  136. case 2: __LL_SWITCH_FS_TRUNCATE; __attribute__((fallthrough));\
  137. case 3: __LL_SWITCH_NET; __attribute__((fallthrough));\
  138. case 4: __LL_SWITCH_FS_IOCTL_DEV;\
  139. default: break;\
  140. }\
  141. ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
  142. if (-1 == ll_rule_fd) {\
  143. LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
  144. return -1;\
  145. }
  146. #define LL_END \
  147. __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
  148. if (-1 == __err) {\
  149. LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
  150. goto __close;\
  151. }\
  152. __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
  153. if (__err)\
  154. LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
  155. __close: close(ll_rule_fd);\
  156. return __err; }
  157. #define LL_PATH(p, rules) do {\
  158. const char *__path = (p);\
  159. __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
  160. __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
  161. if (-1 == __pattr.parent_fd) {\
  162. LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
  163. __err = -1;\
  164. goto __close;\
  165. }\
  166. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
  167. if (__err) {\
  168. LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
  169. goto __close;\
  170. }\
  171. close(__pattr.parent_fd);\
  172. } while (0)
  173. #ifdef __LL_HAVE_NET
  174. #define LL_PORT(p, rules) do {\
  175. unsigned short __port = (p);\
  176. __nattr.allowed_access = (rules);\
  177. if (ll_abi > 3) {\
  178. __nattr.port = __port;\
  179. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
  180. if (__err) {\
  181. LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
  182. goto __close;\
  183. }\
  184. }\
  185. } while (0)
  186. #else
  187. #define LL_PORT(p, rules) do { (void)p; (void)rules; } while (0)
  188. #endif /* __LL_HAVE_NET */
  189. #endif /* KERNEL_VERSION(5, 13, 0) */
  190. #endif /* __LANDLOC_H__ */