sandboxer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Simple Landlock sandbox manager able to execute a process restricted by
  4. * user-defined file system and network access control policies.
  5. *
  6. * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  7. * Copyright © 2020 ANSSI
  8. */
  9. #define _GNU_SOURCE
  10. #define __SANE_USERSPACE_TYPES__
  11. #include <arpa/inet.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <linux/landlock.h>
  15. #include <linux/prctl.h>
  16. #include <linux/socket.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/prctl.h>
  22. #include <sys/stat.h>
  23. #include <sys/syscall.h>
  24. #include <unistd.h>
  25. #include <stdbool.h>
  26. #ifndef landlock_create_ruleset
  27. static inline int
  28. landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
  29. const size_t size, const __u32 flags)
  30. {
  31. return syscall(__NR_landlock_create_ruleset, attr, size, flags);
  32. }
  33. #endif
  34. #ifndef landlock_add_rule
  35. static inline int landlock_add_rule(const int ruleset_fd,
  36. const enum landlock_rule_type rule_type,
  37. const void *const rule_attr,
  38. const __u32 flags)
  39. {
  40. return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
  41. flags);
  42. }
  43. #endif
  44. #ifndef landlock_restrict_self
  45. static inline int landlock_restrict_self(const int ruleset_fd,
  46. const __u32 flags)
  47. {
  48. return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
  49. }
  50. #endif
  51. #define ENV_FS_RO_NAME "LL_FS_RO"
  52. #define ENV_FS_RW_NAME "LL_FS_RW"
  53. #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
  54. #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
  55. #define ENV_SCOPED_NAME "LL_SCOPED"
  56. #define ENV_DELIMITER ":"
  57. static int str2num(const char *numstr, __u64 *num_dst)
  58. {
  59. char *endptr = NULL;
  60. int err = 0;
  61. __u64 num;
  62. errno = 0;
  63. num = strtoull(numstr, &endptr, 10);
  64. if (errno != 0)
  65. err = errno;
  66. /* Was the string empty, or not entirely parsed successfully? */
  67. else if ((*numstr == '\0') || (*endptr != '\0'))
  68. err = EINVAL;
  69. else
  70. *num_dst = num;
  71. return err;
  72. }
  73. static int parse_path(char *env_path, const char ***const path_list)
  74. {
  75. int i, num_paths = 0;
  76. if (env_path) {
  77. num_paths++;
  78. for (i = 0; env_path[i]; i++) {
  79. if (env_path[i] == ENV_DELIMITER[0])
  80. num_paths++;
  81. }
  82. }
  83. *path_list = malloc(num_paths * sizeof(**path_list));
  84. if (!*path_list)
  85. return -1;
  86. for (i = 0; i < num_paths; i++)
  87. (*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
  88. return num_paths;
  89. }
  90. /* clang-format off */
  91. #define ACCESS_FILE ( \
  92. LANDLOCK_ACCESS_FS_EXECUTE | \
  93. LANDLOCK_ACCESS_FS_WRITE_FILE | \
  94. LANDLOCK_ACCESS_FS_READ_FILE | \
  95. LANDLOCK_ACCESS_FS_TRUNCATE | \
  96. LANDLOCK_ACCESS_FS_IOCTL_DEV)
  97. /* clang-format on */
  98. static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
  99. const __u64 allowed_access)
  100. {
  101. int num_paths, i, ret = 1;
  102. char *env_path_name;
  103. const char **path_list = NULL;
  104. struct landlock_path_beneath_attr path_beneath = {
  105. .parent_fd = -1,
  106. };
  107. env_path_name = getenv(env_var);
  108. if (!env_path_name) {
  109. /* Prevents users to forget a setting. */
  110. fprintf(stderr, "Missing environment variable %s\n", env_var);
  111. return 1;
  112. }
  113. env_path_name = strdup(env_path_name);
  114. unsetenv(env_var);
  115. num_paths = parse_path(env_path_name, &path_list);
  116. if (num_paths < 0) {
  117. fprintf(stderr, "Failed to allocate memory\n");
  118. goto out_free_name;
  119. }
  120. if (num_paths == 1 && path_list[0][0] == '\0') {
  121. /*
  122. * Allows to not use all possible restrictions (e.g. use
  123. * LL_FS_RO without LL_FS_RW).
  124. */
  125. ret = 0;
  126. goto out_free_name;
  127. }
  128. for (i = 0; i < num_paths; i++) {
  129. struct stat statbuf;
  130. path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
  131. if (path_beneath.parent_fd < 0) {
  132. fprintf(stderr, "Failed to open \"%s\": %s\n",
  133. path_list[i], strerror(errno));
  134. continue;
  135. }
  136. if (fstat(path_beneath.parent_fd, &statbuf)) {
  137. fprintf(stderr, "Failed to stat \"%s\": %s\n",
  138. path_list[i], strerror(errno));
  139. close(path_beneath.parent_fd);
  140. goto out_free_name;
  141. }
  142. path_beneath.allowed_access = allowed_access;
  143. if (!S_ISDIR(statbuf.st_mode))
  144. path_beneath.allowed_access &= ACCESS_FILE;
  145. if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  146. &path_beneath, 0)) {
  147. fprintf(stderr,
  148. "Failed to update the ruleset with \"%s\": %s\n",
  149. path_list[i], strerror(errno));
  150. close(path_beneath.parent_fd);
  151. goto out_free_name;
  152. }
  153. close(path_beneath.parent_fd);
  154. }
  155. ret = 0;
  156. out_free_name:
  157. free(path_list);
  158. free(env_path_name);
  159. return ret;
  160. }
  161. static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
  162. const __u64 allowed_access)
  163. {
  164. int ret = 1;
  165. char *env_port_name, *env_port_name_next, *strport;
  166. struct landlock_net_port_attr net_port = {
  167. .allowed_access = allowed_access,
  168. };
  169. env_port_name = getenv(env_var);
  170. if (!env_port_name)
  171. return 0;
  172. env_port_name = strdup(env_port_name);
  173. unsetenv(env_var);
  174. env_port_name_next = env_port_name;
  175. while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
  176. __u64 port;
  177. if (strcmp(strport, "") == 0)
  178. continue;
  179. if (str2num(strport, &port)) {
  180. fprintf(stderr, "Failed to parse port at \"%s\"\n",
  181. strport);
  182. goto out_free_name;
  183. }
  184. net_port.port = port;
  185. if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
  186. &net_port, 0)) {
  187. fprintf(stderr,
  188. "Failed to update the ruleset with port \"%llu\": %s\n",
  189. net_port.port, strerror(errno));
  190. goto out_free_name;
  191. }
  192. }
  193. ret = 0;
  194. out_free_name:
  195. free(env_port_name);
  196. return ret;
  197. }
  198. /* Returns true on error, false otherwise. */
  199. static bool check_ruleset_scope(const char *const env_var,
  200. struct landlock_ruleset_attr *ruleset_attr)
  201. {
  202. char *env_type_scope, *env_type_scope_next, *ipc_scoping_name;
  203. bool error = false;
  204. bool abstract_scoping = false;
  205. bool signal_scoping = false;
  206. /* Scoping is not supported by Landlock ABI */
  207. if (!(ruleset_attr->scoped &
  208. (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
  209. goto out_unset;
  210. env_type_scope = getenv(env_var);
  211. /* Scoping is not supported by the user */
  212. if (!env_type_scope || strcmp("", env_type_scope) == 0)
  213. goto out_unset;
  214. env_type_scope = strdup(env_type_scope);
  215. env_type_scope_next = env_type_scope;
  216. while ((ipc_scoping_name =
  217. strsep(&env_type_scope_next, ENV_DELIMITER))) {
  218. if (strcmp("a", ipc_scoping_name) == 0 && !abstract_scoping) {
  219. abstract_scoping = true;
  220. } else if (strcmp("s", ipc_scoping_name) == 0 &&
  221. !signal_scoping) {
  222. signal_scoping = true;
  223. } else {
  224. fprintf(stderr, "Unknown or duplicate scope \"%s\"\n",
  225. ipc_scoping_name);
  226. error = true;
  227. goto out_free_name;
  228. }
  229. }
  230. out_free_name:
  231. free(env_type_scope);
  232. out_unset:
  233. if (!abstract_scoping)
  234. ruleset_attr->scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
  235. if (!signal_scoping)
  236. ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL;
  237. unsetenv(env_var);
  238. return error;
  239. }
  240. /* clang-format off */
  241. #define ACCESS_FS_ROUGHLY_READ ( \
  242. LANDLOCK_ACCESS_FS_EXECUTE | \
  243. LANDLOCK_ACCESS_FS_READ_FILE | \
  244. LANDLOCK_ACCESS_FS_READ_DIR)
  245. #define ACCESS_FS_ROUGHLY_WRITE ( \
  246. LANDLOCK_ACCESS_FS_WRITE_FILE | \
  247. LANDLOCK_ACCESS_FS_REMOVE_DIR | \
  248. LANDLOCK_ACCESS_FS_REMOVE_FILE | \
  249. LANDLOCK_ACCESS_FS_MAKE_CHAR | \
  250. LANDLOCK_ACCESS_FS_MAKE_DIR | \
  251. LANDLOCK_ACCESS_FS_MAKE_REG | \
  252. LANDLOCK_ACCESS_FS_MAKE_SOCK | \
  253. LANDLOCK_ACCESS_FS_MAKE_FIFO | \
  254. LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
  255. LANDLOCK_ACCESS_FS_MAKE_SYM | \
  256. LANDLOCK_ACCESS_FS_REFER | \
  257. LANDLOCK_ACCESS_FS_TRUNCATE | \
  258. LANDLOCK_ACCESS_FS_IOCTL_DEV)
  259. /* clang-format on */
  260. #define LANDLOCK_ABI_LAST 6
  261. #define XSTR(s) #s
  262. #define STR(s) XSTR(s)
  263. /* clang-format off */
  264. static const char help[] =
  265. "usage: " ENV_FS_RO_NAME "=\"...\" " ENV_FS_RW_NAME "=\"...\" "
  266. "[other environment variables] %1$s <cmd> [args]...\n"
  267. "\n"
  268. "Execute the given command in a restricted environment.\n"
  269. "Multi-valued settings (lists of ports, paths, scopes) are colon-delimited.\n"
  270. "\n"
  271. "Mandatory settings:\n"
  272. "* " ENV_FS_RO_NAME ": paths allowed to be used in a read-only way\n"
  273. "* " ENV_FS_RW_NAME ": paths allowed to be used in a read-write way\n"
  274. "\n"
  275. "Optional settings (when not set, their associated access check "
  276. "is always allowed, which is different from an empty string which "
  277. "means an empty list):\n"
  278. "* " ENV_TCP_BIND_NAME ": ports allowed to bind (server)\n"
  279. "* " ENV_TCP_CONNECT_NAME ": ports allowed to connect (client)\n"
  280. "* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n"
  281. " - \"a\" to restrict opening abstract unix sockets\n"
  282. " - \"s\" to restrict sending signals\n"
  283. "\n"
  284. "Example:\n"
  285. ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
  286. ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
  287. ENV_TCP_BIND_NAME "=\"9418\" "
  288. ENV_TCP_CONNECT_NAME "=\"80:443\" "
  289. ENV_SCOPED_NAME "=\"a:s\" "
  290. "%1$s bash -i\n"
  291. "\n"
  292. "This sandboxer can use Landlock features up to ABI version "
  293. STR(LANDLOCK_ABI_LAST) ".\n";
  294. /* clang-format on */
  295. int main(const int argc, char *const argv[], char *const *const envp)
  296. {
  297. const char *cmd_path;
  298. char *const *cmd_argv;
  299. int ruleset_fd, abi;
  300. char *env_port_name;
  301. __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
  302. access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
  303. struct landlock_ruleset_attr ruleset_attr = {
  304. .handled_access_fs = access_fs_rw,
  305. .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
  306. LANDLOCK_ACCESS_NET_CONNECT_TCP,
  307. .scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
  308. LANDLOCK_SCOPE_SIGNAL,
  309. };
  310. if (argc < 2) {
  311. fprintf(stderr, help, argv[0]);
  312. return 1;
  313. }
  314. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  315. if (abi < 0) {
  316. const int err = errno;
  317. perror("Failed to check Landlock compatibility");
  318. switch (err) {
  319. case ENOSYS:
  320. fprintf(stderr,
  321. "Hint: Landlock is not supported by the current kernel. "
  322. "To support it, build the kernel with "
  323. "CONFIG_SECURITY_LANDLOCK=y and prepend "
  324. "\"landlock,\" to the content of CONFIG_LSM.\n");
  325. break;
  326. case EOPNOTSUPP:
  327. fprintf(stderr,
  328. "Hint: Landlock is currently disabled. "
  329. "It can be enabled in the kernel configuration by "
  330. "prepending \"landlock,\" to the content of CONFIG_LSM, "
  331. "or at boot time by setting the same content to the "
  332. "\"lsm\" kernel parameter.\n");
  333. break;
  334. }
  335. return 1;
  336. }
  337. /* Best-effort security. */
  338. switch (abi) {
  339. case 1:
  340. /*
  341. * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
  342. *
  343. * Note: The "refer" operations (file renaming and linking
  344. * across different directories) are always forbidden when using
  345. * Landlock with ABI 1.
  346. *
  347. * If only ABI 1 is available, this sandboxer knowingly forbids
  348. * refer operations.
  349. *
  350. * If a program *needs* to do refer operations after enabling
  351. * Landlock, it can not use Landlock at ABI level 1. To be
  352. * compatible with different kernel versions, such programs
  353. * should then fall back to not restrict themselves at all if
  354. * the running kernel only supports ABI 1.
  355. */
  356. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
  357. __attribute__((fallthrough));
  358. case 2:
  359. /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
  360. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
  361. __attribute__((fallthrough));
  362. case 3:
  363. /* Removes network support for ABI < 4 */
  364. ruleset_attr.handled_access_net &=
  365. ~(LANDLOCK_ACCESS_NET_BIND_TCP |
  366. LANDLOCK_ACCESS_NET_CONNECT_TCP);
  367. __attribute__((fallthrough));
  368. case 4:
  369. /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
  370. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
  371. __attribute__((fallthrough));
  372. case 5:
  373. /* Removes LANDLOCK_SCOPE_* for ABI < 6 */
  374. ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
  375. LANDLOCK_SCOPE_SIGNAL);
  376. fprintf(stderr,
  377. "Hint: You should update the running kernel "
  378. "to leverage Landlock features "
  379. "provided by ABI version %d (instead of %d).\n",
  380. LANDLOCK_ABI_LAST, abi);
  381. __attribute__((fallthrough));
  382. case LANDLOCK_ABI_LAST:
  383. break;
  384. default:
  385. fprintf(stderr,
  386. "Hint: You should update this sandboxer "
  387. "to leverage Landlock features "
  388. "provided by ABI version %d (instead of %d).\n",
  389. abi, LANDLOCK_ABI_LAST);
  390. }
  391. access_fs_ro &= ruleset_attr.handled_access_fs;
  392. access_fs_rw &= ruleset_attr.handled_access_fs;
  393. /* Removes bind access attribute if not supported by a user. */
  394. env_port_name = getenv(ENV_TCP_BIND_NAME);
  395. if (!env_port_name) {
  396. ruleset_attr.handled_access_net &=
  397. ~LANDLOCK_ACCESS_NET_BIND_TCP;
  398. }
  399. /* Removes connect access attribute if not supported by a user. */
  400. env_port_name = getenv(ENV_TCP_CONNECT_NAME);
  401. if (!env_port_name) {
  402. ruleset_attr.handled_access_net &=
  403. ~LANDLOCK_ACCESS_NET_CONNECT_TCP;
  404. }
  405. if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
  406. return 1;
  407. ruleset_fd =
  408. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  409. if (ruleset_fd < 0) {
  410. perror("Failed to create a ruleset");
  411. return 1;
  412. }
  413. if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
  414. goto err_close_ruleset;
  415. }
  416. if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
  417. goto err_close_ruleset;
  418. }
  419. if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
  420. LANDLOCK_ACCESS_NET_BIND_TCP)) {
  421. goto err_close_ruleset;
  422. }
  423. if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
  424. LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
  425. goto err_close_ruleset;
  426. }
  427. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  428. perror("Failed to restrict privileges");
  429. goto err_close_ruleset;
  430. }
  431. if (landlock_restrict_self(ruleset_fd, 0)) {
  432. perror("Failed to enforce ruleset");
  433. goto err_close_ruleset;
  434. }
  435. close(ruleset_fd);
  436. cmd_path = argv[1];
  437. cmd_argv = argv + 1;
  438. fprintf(stderr, "Executing the sandboxed command...\n");
  439. execvpe(cmd_path, cmd_argv, envp);
  440. fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
  441. strerror(errno));
  442. fprintf(stderr, "Hint: access to the binary, the interpreter or "
  443. "shared libraries may be denied.\n");
  444. return 1;
  445. err_close_ruleset:
  446. close(ruleset_fd);
  447. return 1;
  448. }