syscalls.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Landlock LSM - System call implementations and user space interfaces
  4. *
  5. * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2018-2020 ANSSI
  7. */
  8. #include <asm/current.h>
  9. #include <linux/anon_inodes.h>
  10. #include <linux/build_bug.h>
  11. #include <linux/capability.h>
  12. #include <linux/compiler_types.h>
  13. #include <linux/dcache.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/fs.h>
  17. #include <linux/limits.h>
  18. #include <linux/mount.h>
  19. #include <linux/path.h>
  20. #include <linux/sched.h>
  21. #include <linux/security.h>
  22. #include <linux/stddef.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/types.h>
  25. #include <linux/uaccess.h>
  26. #include <uapi/linux/landlock.h>
  27. #include "cred.h"
  28. #include "fs.h"
  29. #include "limits.h"
  30. #include "net.h"
  31. #include "ruleset.h"
  32. #include "setup.h"
  33. static bool is_initialized(void)
  34. {
  35. if (likely(landlock_initialized))
  36. return true;
  37. pr_warn_once(
  38. "Disabled but requested by user space. "
  39. "You should enable Landlock at boot time: "
  40. "https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");
  41. return false;
  42. }
  43. /**
  44. * copy_min_struct_from_user - Safe future-proof argument copying
  45. *
  46. * Extend copy_struct_from_user() to check for consistent user buffer.
  47. *
  48. * @dst: Kernel space pointer or NULL.
  49. * @ksize: Actual size of the data pointed to by @dst.
  50. * @ksize_min: Minimal required size to be copied.
  51. * @src: User space pointer or NULL.
  52. * @usize: (Alleged) size of the data pointed to by @src.
  53. */
  54. static __always_inline int
  55. copy_min_struct_from_user(void *const dst, const size_t ksize,
  56. const size_t ksize_min, const void __user *const src,
  57. const size_t usize)
  58. {
  59. /* Checks buffer inconsistencies. */
  60. BUILD_BUG_ON(!dst);
  61. if (!src)
  62. return -EFAULT;
  63. /* Checks size ranges. */
  64. BUILD_BUG_ON(ksize <= 0);
  65. BUILD_BUG_ON(ksize < ksize_min);
  66. if (usize < ksize_min)
  67. return -EINVAL;
  68. if (usize > PAGE_SIZE)
  69. return -E2BIG;
  70. /* Copies user buffer and fills with zeros. */
  71. return copy_struct_from_user(dst, ksize, src, usize);
  72. }
  73. /*
  74. * This function only contains arithmetic operations with constants, leading to
  75. * BUILD_BUG_ON(). The related code is evaluated and checked at build time,
  76. * but it is then ignored thanks to compiler optimizations.
  77. */
  78. static void build_check_abi(void)
  79. {
  80. struct landlock_ruleset_attr ruleset_attr;
  81. struct landlock_path_beneath_attr path_beneath_attr;
  82. struct landlock_net_port_attr net_port_attr;
  83. size_t ruleset_size, path_beneath_size, net_port_size;
  84. /*
  85. * For each user space ABI structures, first checks that there is no
  86. * hole in them, then checks that all architectures have the same
  87. * struct size.
  88. */
  89. ruleset_size = sizeof(ruleset_attr.handled_access_fs);
  90. ruleset_size += sizeof(ruleset_attr.handled_access_net);
  91. ruleset_size += sizeof(ruleset_attr.scoped);
  92. BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
  93. BUILD_BUG_ON(sizeof(ruleset_attr) != 24);
  94. path_beneath_size = sizeof(path_beneath_attr.allowed_access);
  95. path_beneath_size += sizeof(path_beneath_attr.parent_fd);
  96. BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
  97. BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
  98. net_port_size = sizeof(net_port_attr.allowed_access);
  99. net_port_size += sizeof(net_port_attr.port);
  100. BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
  101. BUILD_BUG_ON(sizeof(net_port_attr) != 16);
  102. }
  103. /* Ruleset handling */
  104. static int fop_ruleset_release(struct inode *const inode,
  105. struct file *const filp)
  106. {
  107. struct landlock_ruleset *ruleset = filp->private_data;
  108. landlock_put_ruleset(ruleset);
  109. return 0;
  110. }
  111. static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,
  112. const size_t size, loff_t *const ppos)
  113. {
  114. /* Dummy handler to enable FMODE_CAN_READ. */
  115. return -EINVAL;
  116. }
  117. static ssize_t fop_dummy_write(struct file *const filp,
  118. const char __user *const buf, const size_t size,
  119. loff_t *const ppos)
  120. {
  121. /* Dummy handler to enable FMODE_CAN_WRITE. */
  122. return -EINVAL;
  123. }
  124. /*
  125. * A ruleset file descriptor enables to build a ruleset by adding (i.e.
  126. * writing) rule after rule, without relying on the task's context. This
  127. * reentrant design is also used in a read way to enforce the ruleset on the
  128. * current task.
  129. */
  130. static const struct file_operations ruleset_fops = {
  131. .release = fop_ruleset_release,
  132. .read = fop_dummy_read,
  133. .write = fop_dummy_write,
  134. };
  135. #define LANDLOCK_ABI_VERSION 6
  136. /**
  137. * sys_landlock_create_ruleset - Create a new ruleset
  138. *
  139. * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
  140. * the new ruleset.
  141. * @size: Size of the pointed &struct landlock_ruleset_attr (needed for
  142. * backward and forward compatibility).
  143. * @flags: Supported value: %LANDLOCK_CREATE_RULESET_VERSION.
  144. *
  145. * This system call enables to create a new Landlock ruleset, and returns the
  146. * related file descriptor on success.
  147. *
  148. * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
  149. * 0, then the returned value is the highest supported Landlock ABI version
  150. * (starting at 1).
  151. *
  152. * Possible returned errors are:
  153. *
  154. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  155. * - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small @size;
  156. * - %E2BIG: @attr or @size inconsistencies;
  157. * - %EFAULT: @attr or @size inconsistencies;
  158. * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
  159. */
  160. SYSCALL_DEFINE3(landlock_create_ruleset,
  161. const struct landlock_ruleset_attr __user *const, attr,
  162. const size_t, size, const __u32, flags)
  163. {
  164. struct landlock_ruleset_attr ruleset_attr;
  165. struct landlock_ruleset *ruleset;
  166. int err, ruleset_fd;
  167. /* Build-time checks. */
  168. build_check_abi();
  169. if (!is_initialized())
  170. return -EOPNOTSUPP;
  171. if (flags) {
  172. if ((flags == LANDLOCK_CREATE_RULESET_VERSION) && !attr &&
  173. !size)
  174. return LANDLOCK_ABI_VERSION;
  175. return -EINVAL;
  176. }
  177. /* Copies raw user space buffer. */
  178. err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
  179. offsetofend(typeof(ruleset_attr),
  180. handled_access_fs),
  181. attr, size);
  182. if (err)
  183. return err;
  184. /* Checks content (and 32-bits cast). */
  185. if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
  186. LANDLOCK_MASK_ACCESS_FS)
  187. return -EINVAL;
  188. /* Checks network content (and 32-bits cast). */
  189. if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
  190. LANDLOCK_MASK_ACCESS_NET)
  191. return -EINVAL;
  192. /* Checks IPC scoping content (and 32-bits cast). */
  193. if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE)
  194. return -EINVAL;
  195. /* Checks arguments and transforms to kernel struct. */
  196. ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
  197. ruleset_attr.handled_access_net,
  198. ruleset_attr.scoped);
  199. if (IS_ERR(ruleset))
  200. return PTR_ERR(ruleset);
  201. /* Creates anonymous FD referring to the ruleset. */
  202. ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
  203. ruleset, O_RDWR | O_CLOEXEC);
  204. if (ruleset_fd < 0)
  205. landlock_put_ruleset(ruleset);
  206. return ruleset_fd;
  207. }
  208. /*
  209. * Returns an owned ruleset from a FD. It is thus needed to call
  210. * landlock_put_ruleset() on the return value.
  211. */
  212. static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
  213. const fmode_t mode)
  214. {
  215. struct fd ruleset_f;
  216. struct landlock_ruleset *ruleset;
  217. ruleset_f = fdget(fd);
  218. if (!fd_file(ruleset_f))
  219. return ERR_PTR(-EBADF);
  220. /* Checks FD type and access right. */
  221. if (fd_file(ruleset_f)->f_op != &ruleset_fops) {
  222. ruleset = ERR_PTR(-EBADFD);
  223. goto out_fdput;
  224. }
  225. if (!(fd_file(ruleset_f)->f_mode & mode)) {
  226. ruleset = ERR_PTR(-EPERM);
  227. goto out_fdput;
  228. }
  229. ruleset = fd_file(ruleset_f)->private_data;
  230. if (WARN_ON_ONCE(ruleset->num_layers != 1)) {
  231. ruleset = ERR_PTR(-EINVAL);
  232. goto out_fdput;
  233. }
  234. landlock_get_ruleset(ruleset);
  235. out_fdput:
  236. fdput(ruleset_f);
  237. return ruleset;
  238. }
  239. /* Path handling */
  240. /*
  241. * @path: Must call put_path(@path) after the call if it succeeded.
  242. */
  243. static int get_path_from_fd(const s32 fd, struct path *const path)
  244. {
  245. struct fd f;
  246. int err = 0;
  247. BUILD_BUG_ON(!__same_type(
  248. fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
  249. /* Handles O_PATH. */
  250. f = fdget_raw(fd);
  251. if (!fd_file(f))
  252. return -EBADF;
  253. /*
  254. * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
  255. * pseudo filesystems that will never be mountable (e.g. sockfs,
  256. * pipefs).
  257. */
  258. if ((fd_file(f)->f_op == &ruleset_fops) ||
  259. (fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
  260. (fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
  261. d_is_negative(fd_file(f)->f_path.dentry) ||
  262. IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry))) {
  263. err = -EBADFD;
  264. goto out_fdput;
  265. }
  266. *path = fd_file(f)->f_path;
  267. path_get(path);
  268. out_fdput:
  269. fdput(f);
  270. return err;
  271. }
  272. static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
  273. const void __user *const rule_attr)
  274. {
  275. struct landlock_path_beneath_attr path_beneath_attr;
  276. struct path path;
  277. int res, err;
  278. access_mask_t mask;
  279. /* Copies raw user space buffer. */
  280. res = copy_from_user(&path_beneath_attr, rule_attr,
  281. sizeof(path_beneath_attr));
  282. if (res)
  283. return -EFAULT;
  284. /*
  285. * Informs about useless rule: empty allowed_access (i.e. deny rules)
  286. * are ignored in path walks.
  287. */
  288. if (!path_beneath_attr.allowed_access)
  289. return -ENOMSG;
  290. /* Checks that allowed_access matches the @ruleset constraints. */
  291. mask = ruleset->access_masks[0].fs;
  292. if ((path_beneath_attr.allowed_access | mask) != mask)
  293. return -EINVAL;
  294. /* Gets and checks the new rule. */
  295. err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
  296. if (err)
  297. return err;
  298. /* Imports the new rule. */
  299. err = landlock_append_fs_rule(ruleset, &path,
  300. path_beneath_attr.allowed_access);
  301. path_put(&path);
  302. return err;
  303. }
  304. static int add_rule_net_port(struct landlock_ruleset *ruleset,
  305. const void __user *const rule_attr)
  306. {
  307. struct landlock_net_port_attr net_port_attr;
  308. int res;
  309. access_mask_t mask;
  310. /* Copies raw user space buffer. */
  311. res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
  312. if (res)
  313. return -EFAULT;
  314. /*
  315. * Informs about useless rule: empty allowed_access (i.e. deny rules)
  316. * are ignored by network actions.
  317. */
  318. if (!net_port_attr.allowed_access)
  319. return -ENOMSG;
  320. /* Checks that allowed_access matches the @ruleset constraints. */
  321. mask = landlock_get_net_access_mask(ruleset, 0);
  322. if ((net_port_attr.allowed_access | mask) != mask)
  323. return -EINVAL;
  324. /* Denies inserting a rule with port greater than 65535. */
  325. if (net_port_attr.port > U16_MAX)
  326. return -EINVAL;
  327. /* Imports the new rule. */
  328. return landlock_append_net_rule(ruleset, net_port_attr.port,
  329. net_port_attr.allowed_access);
  330. }
  331. /**
  332. * sys_landlock_add_rule - Add a new rule to a ruleset
  333. *
  334. * @ruleset_fd: File descriptor tied to the ruleset that should be extended
  335. * with the new rule.
  336. * @rule_type: Identify the structure type pointed to by @rule_attr:
  337. * %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
  338. * @rule_attr: Pointer to a rule (matching the @rule_type).
  339. * @flags: Must be 0.
  340. *
  341. * This system call enables to define a new rule and add it to an existing
  342. * ruleset.
  343. *
  344. * Possible returned errors are:
  345. *
  346. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  347. * - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
  348. * supported by the running kernel;
  349. * - %EINVAL: @flags is not 0;
  350. * - %EINVAL: The rule accesses are inconsistent (i.e.
  351. * &landlock_path_beneath_attr.allowed_access or
  352. * &landlock_net_port_attr.allowed_access is not a subset of the ruleset
  353. * handled accesses)
  354. * - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
  355. * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
  356. * 0);
  357. * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
  358. * member of @rule_attr is not a file descriptor as expected;
  359. * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
  360. * @rule_attr is not the expected file descriptor type;
  361. * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
  362. * - %EFAULT: @rule_attr was not a valid address.
  363. */
  364. SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
  365. const enum landlock_rule_type, rule_type,
  366. const void __user *const, rule_attr, const __u32, flags)
  367. {
  368. struct landlock_ruleset *ruleset;
  369. int err;
  370. if (!is_initialized())
  371. return -EOPNOTSUPP;
  372. /* No flag for now. */
  373. if (flags)
  374. return -EINVAL;
  375. /* Gets and checks the ruleset. */
  376. ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
  377. if (IS_ERR(ruleset))
  378. return PTR_ERR(ruleset);
  379. switch (rule_type) {
  380. case LANDLOCK_RULE_PATH_BENEATH:
  381. err = add_rule_path_beneath(ruleset, rule_attr);
  382. break;
  383. case LANDLOCK_RULE_NET_PORT:
  384. err = add_rule_net_port(ruleset, rule_attr);
  385. break;
  386. default:
  387. err = -EINVAL;
  388. break;
  389. }
  390. landlock_put_ruleset(ruleset);
  391. return err;
  392. }
  393. /* Enforcement */
  394. /**
  395. * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
  396. *
  397. * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
  398. * @flags: Must be 0.
  399. *
  400. * This system call enables to enforce a Landlock ruleset on the current
  401. * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
  402. * namespace or is running with no_new_privs. This avoids scenarios where
  403. * unprivileged tasks can affect the behavior of privileged children.
  404. *
  405. * Possible returned errors are:
  406. *
  407. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  408. * - %EINVAL: @flags is not 0.
  409. * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
  410. * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
  411. * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
  412. * current thread is not running with no_new_privs, or it doesn't have
  413. * %CAP_SYS_ADMIN in its namespace.
  414. * - %E2BIG: The maximum number of stacked rulesets is reached for the current
  415. * thread.
  416. */
  417. SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
  418. flags)
  419. {
  420. struct landlock_ruleset *new_dom, *ruleset;
  421. struct cred *new_cred;
  422. struct landlock_cred_security *new_llcred;
  423. int err;
  424. if (!is_initialized())
  425. return -EOPNOTSUPP;
  426. /*
  427. * Similar checks as for seccomp(2), except that an -EPERM may be
  428. * returned.
  429. */
  430. if (!task_no_new_privs(current) &&
  431. !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
  432. return -EPERM;
  433. /* No flag for now. */
  434. if (flags)
  435. return -EINVAL;
  436. /* Gets and checks the ruleset. */
  437. ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
  438. if (IS_ERR(ruleset))
  439. return PTR_ERR(ruleset);
  440. /* Prepares new credentials. */
  441. new_cred = prepare_creds();
  442. if (!new_cred) {
  443. err = -ENOMEM;
  444. goto out_put_ruleset;
  445. }
  446. new_llcred = landlock_cred(new_cred);
  447. /*
  448. * There is no possible race condition while copying and manipulating
  449. * the current credentials because they are dedicated per thread.
  450. */
  451. new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);
  452. if (IS_ERR(new_dom)) {
  453. err = PTR_ERR(new_dom);
  454. goto out_put_creds;
  455. }
  456. /* Replaces the old (prepared) domain. */
  457. landlock_put_ruleset(new_llcred->domain);
  458. new_llcred->domain = new_dom;
  459. landlock_put_ruleset(ruleset);
  460. return commit_creds(new_cred);
  461. out_put_creds:
  462. abort_creds(new_cred);
  463. out_put_ruleset:
  464. landlock_put_ruleset(ruleset);
  465. return err;
  466. }