landloc.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 LANDLOCK_ACCESS_FS_REFER_COMPAT LANDLOCK_ACCESS_FS_REFER
  74. # define __LL_SWITCH_FS_REFER __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER_COMPAT
  75. #else
  76. # define LANDLOCK_ACCESS_FS_REFER_COMPAT 0
  77. # define __LL_SWITCH_FS_REFER (void)0
  78. #endif
  79. #ifdef LANDLOCK_ACCESS_FS_TRUNCATE
  80. # define LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT LANDLOCK_ACCESS_FS_TRUNCATE
  81. # define __LL_SWITCH_FS_TRUNCATE __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT
  82. #else
  83. # define LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT 0
  84. # define __LL_SWITCH_FS_TRUNCATE (void)0
  85. #endif
  86. #ifdef LANDLOCK_ACCESS_FS_IOCTL_DEV
  87. # define LANDLOCK_ACCESS_FS_IOCTL_DEV_COMPAT LANDLOCK_ACCESS_FS_IOCTL_DEV
  88. # define __LL_SWITCH_FS_IOCTL_DEV __rattr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV_COMPAT
  89. #else
  90. # define LANDLOCK_ACCESS_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. LANDLOCK_ACCESS_FS_REFER_COMPAT |\
  108. LANDLOCK_ACCESS_FS_TRUNCATE_COMPAT |\
  109. LANDLOCK_ACCESS_FS_IOCTL_DEV_COMPAT )
  110. #if defined(LANDLOCK_ACCESS_NET_BIND_TCP) && defined(LANDLOCK_ACCESS_NET_CONNECT_TCP)
  111. # define LL_HAVE_NET 1
  112. # define LANDLOCK_ACCESS_NET_BIND_TCP_COMPAT LANDLOCK_ACCESS_NET_BIND_TCP
  113. # define LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT LANDLOCK_ACCESS_NET_CONNECT_TCP
  114. # define LL_NET_ALL (LANDLOCK_ACCESS_NET_BIND_TCP_COMPAT | LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT)
  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_HAVE_NET 0
  120. # define LANDLOCK_ACCESS_NET_BIND_TCP_COMPAT 0
  121. # define LANDLOCK_ACCESS_NET_CONNECT_TCP_COMPAT 0
  122. # define LL_NET_ALL 0
  123. # define __LL_DECLARE_NET (void)0
  124. # define __LL_INIT_NET (void)0
  125. # define __LL_SWITCH_NET (void)0
  126. #endif
  127. #define LL_BEGIN(function, ...) int function(__VA_ARGS__) {\
  128. int ll_rule_fd, ll_abi;\
  129. struct landlock_ruleset_attr __rattr = {0};\
  130. struct landlock_path_beneath_attr __pattr = {0};\
  131. __LL_DECLARE_NET;\
  132. int __err = 0;\
  133. __rattr.handled_access_fs = LL_FS_ALL;\
  134. __LL_INIT_NET;\
  135. ll_abi = (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);\
  136. switch (ll_abi) {\
  137. case -1: return -1;\
  138. case 1: __LL_SWITCH_FS_REFER; __attribute__((fallthrough));\
  139. case 2: __LL_SWITCH_FS_TRUNCATE; __attribute__((fallthrough));\
  140. case 3: __LL_SWITCH_NET; __attribute__((fallthrough));\
  141. case 4: __LL_SWITCH_FS_IOCTL_DEV;\
  142. default: break;\
  143. }\
  144. ll_rule_fd = (int)syscall(SYS_landlock_create_ruleset, &__rattr, sizeof(struct landlock_ruleset_attr), 0);\
  145. if (-1 == ll_rule_fd) {\
  146. LL_PRINTERR("landlock_create_ruleset: %s", strerror(errno));\
  147. return -1;\
  148. }
  149. #define LL_END \
  150. __err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);\
  151. if (-1 == __err) {\
  152. LL_PRINTERR("set_no_new_privs: %s", strerror(errno));\
  153. goto __close;\
  154. }\
  155. __err = (int)syscall(SYS_landlock_restrict_self, ll_rule_fd, 0);\
  156. if (__err)\
  157. LL_PRINTERR("landlock_restrict_self: %s", strerror(errno));\
  158. __close: close(ll_rule_fd);\
  159. return __err; }
  160. #define LL_PATH(p, rules) do {\
  161. const char *__path = (p);\
  162. __pattr.allowed_access = (rules) & __rattr.handled_access_fs;\
  163. if (__pattr.allowed_access != 0) {\
  164. __pattr.parent_fd = open(__path, O_PATH | O_CLOEXEC);\
  165. if (-1 == __pattr.parent_fd) {\
  166. LL_PRINTERR("open(%s): %s", __path, strerror(errno));\
  167. __err = -1;\
  168. goto __close;\
  169. }\
  170. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_PATH_BENEATH, &__pattr, 0);\
  171. if (__err) {\
  172. LL_PRINTERR("landlock_add_rule(%s): %s", __path, strerror(errno));\
  173. goto __close;\
  174. }\
  175. close(__pattr.parent_fd);\
  176. }\
  177. } while (0)
  178. #if LL_HAVE_NET
  179. #define LL_PORT(p, rules) do {\
  180. unsigned short __port = (p);\
  181. __nattr.allowed_access = (rules);\
  182. if (ll_abi > 3 && __nattr.allowed_access != 0) {\
  183. __nattr.port = __port;\
  184. __err = (int)syscall(SYS_landlock_add_rule, ll_rule_fd, LANDLOCK_RULE_NET_PORT, &__nattr, 0);\
  185. if (__err) {\
  186. LL_PRINTERR("landlock_add_rule(%u): %s", __port, strerror(errno));\
  187. goto __close;\
  188. }\
  189. }\
  190. } while (0)
  191. #else
  192. #define LL_PORT(p, rules) do { (void)p; (void)rules; } while (0)
  193. #endif /* LL_HAVE_NET */
  194. #endif /* KERNEL_VERSION(5, 13, 0) */
  195. #endif /* __LANDLOC_H__ */