landloc.h 7.6 KB

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