syscalls.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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:
  144. * - %LANDLOCK_CREATE_RULESET_VERSION
  145. * - %LANDLOCK_CREATE_RULESET_ERRATA
  146. *
  147. * This system call enables to create a new Landlock ruleset, and returns the
  148. * related file descriptor on success.
  149. *
  150. * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
  151. * 0, then the returned value is the highest supported Landlock ABI version
  152. * (starting at 1).
  153. *
  154. * If @flags is %LANDLOCK_CREATE_RULESET_ERRATA and @attr is NULL and @size is
  155. * 0, then the returned value is a bitmask of fixed issues for the current
  156. * Landlock ABI version.
  157. *
  158. * Possible returned errors are:
  159. *
  160. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  161. * - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small @size;
  162. * - %E2BIG: @attr or @size inconsistencies;
  163. * - %EFAULT: @attr or @size inconsistencies;
  164. * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
  165. */
  166. SYSCALL_DEFINE3(landlock_create_ruleset,
  167. const struct landlock_ruleset_attr __user *const, attr,
  168. const size_t, size, const __u32, flags)
  169. {
  170. struct landlock_ruleset_attr ruleset_attr;
  171. struct landlock_ruleset *ruleset;
  172. int err, ruleset_fd;
  173. /* Build-time checks. */
  174. build_check_abi();
  175. if (!is_initialized())
  176. return -EOPNOTSUPP;
  177. if (flags) {
  178. if (attr || size)
  179. return -EINVAL;
  180. if (flags == LANDLOCK_CREATE_RULESET_VERSION)
  181. return landlock_abi_version;
  182. if (flags == LANDLOCK_CREATE_RULESET_ERRATA)
  183. return landlock_errata;
  184. return -EINVAL;
  185. }
  186. /* Copies raw user space buffer. */
  187. err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
  188. offsetofend(typeof(ruleset_attr),
  189. handled_access_fs),
  190. attr, size);
  191. if (err)
  192. return err;
  193. /* Checks content (and 32-bits cast). */
  194. if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
  195. LANDLOCK_MASK_ACCESS_FS)
  196. return -EINVAL;
  197. /* Checks network content (and 32-bits cast). */
  198. if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
  199. LANDLOCK_MASK_ACCESS_NET)
  200. return -EINVAL;
  201. /* Checks IPC scoping content (and 32-bits cast). */
  202. if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE)
  203. return -EINVAL;
  204. /* Checks arguments and transforms to kernel struct. */
  205. ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
  206. ruleset_attr.handled_access_net,
  207. ruleset_attr.scoped);
  208. if (IS_ERR(ruleset))
  209. return PTR_ERR(ruleset);
  210. /* Creates anonymous FD referring to the ruleset. */
  211. ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
  212. ruleset, O_RDWR | O_CLOEXEC);
  213. if (ruleset_fd < 0)
  214. landlock_put_ruleset(ruleset);
  215. return ruleset_fd;
  216. }
  217. const int landlock_abi_version = LANDLOCK_ABI_VERSION;
  218. /*
  219. * Returns an owned ruleset from a FD. It is thus needed to call
  220. * landlock_put_ruleset() on the return value.
  221. */
  222. static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
  223. const fmode_t mode)
  224. {
  225. struct fd ruleset_f;
  226. struct landlock_ruleset *ruleset;
  227. ruleset_f = fdget(fd);
  228. if (!fd_file(ruleset_f))
  229. return ERR_PTR(-EBADF);
  230. /* Checks FD type and access right. */
  231. if (fd_file(ruleset_f)->f_op != &ruleset_fops) {
  232. ruleset = ERR_PTR(-EBADFD);
  233. goto out_fdput;
  234. }
  235. if (!(fd_file(ruleset_f)->f_mode & mode)) {
  236. ruleset = ERR_PTR(-EPERM);
  237. goto out_fdput;
  238. }
  239. ruleset = fd_file(ruleset_f)->private_data;
  240. if (WARN_ON_ONCE(ruleset->num_layers != 1)) {
  241. ruleset = ERR_PTR(-EINVAL);
  242. goto out_fdput;
  243. }
  244. landlock_get_ruleset(ruleset);
  245. out_fdput:
  246. fdput(ruleset_f);
  247. return ruleset;
  248. }
  249. /* Path handling */
  250. /*
  251. * @path: Must call put_path(@path) after the call if it succeeded.
  252. */
  253. static int get_path_from_fd(const s32 fd, struct path *const path)
  254. {
  255. struct fd f;
  256. int err = 0;
  257. BUILD_BUG_ON(!__same_type(
  258. fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
  259. /* Handles O_PATH. */
  260. f = fdget_raw(fd);
  261. if (!fd_file(f))
  262. return -EBADF;
  263. /*
  264. * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
  265. * pseudo filesystems that will never be mountable (e.g. sockfs,
  266. * pipefs).
  267. */
  268. if ((fd_file(f)->f_op == &ruleset_fops) ||
  269. (fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
  270. (fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
  271. d_is_negative(fd_file(f)->f_path.dentry) ||
  272. IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry))) {
  273. err = -EBADFD;
  274. goto out_fdput;
  275. }
  276. *path = fd_file(f)->f_path;
  277. path_get(path);
  278. out_fdput:
  279. fdput(f);
  280. return err;
  281. }
  282. static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
  283. const void __user *const rule_attr)
  284. {
  285. struct landlock_path_beneath_attr path_beneath_attr;
  286. struct path path;
  287. int res, err;
  288. access_mask_t mask;
  289. /* Copies raw user space buffer. */
  290. res = copy_from_user(&path_beneath_attr, rule_attr,
  291. sizeof(path_beneath_attr));
  292. if (res)
  293. return -EFAULT;
  294. /*
  295. * Informs about useless rule: empty allowed_access (i.e. deny rules)
  296. * are ignored in path walks.
  297. */
  298. if (!path_beneath_attr.allowed_access)
  299. return -ENOMSG;
  300. /* Checks that allowed_access matches the @ruleset constraints. */
  301. mask = ruleset->access_masks[0].fs;
  302. if ((path_beneath_attr.allowed_access | mask) != mask)
  303. return -EINVAL;
  304. /* Gets and checks the new rule. */
  305. err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
  306. if (err)
  307. return err;
  308. /* Imports the new rule. */
  309. err = landlock_append_fs_rule(ruleset, &path,
  310. path_beneath_attr.allowed_access);
  311. path_put(&path);
  312. return err;
  313. }
  314. static int add_rule_net_port(struct landlock_ruleset *ruleset,
  315. const void __user *const rule_attr)
  316. {
  317. struct landlock_net_port_attr net_port_attr;
  318. int res;
  319. access_mask_t mask;
  320. /* Copies raw user space buffer. */
  321. res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
  322. if (res)
  323. return -EFAULT;
  324. /*
  325. * Informs about useless rule: empty allowed_access (i.e. deny rules)
  326. * are ignored by network actions.
  327. */
  328. if (!net_port_attr.allowed_access)
  329. return -ENOMSG;
  330. /* Checks that allowed_access matches the @ruleset constraints. */
  331. mask = landlock_get_net_access_mask(ruleset, 0);
  332. if ((net_port_attr.allowed_access | mask) != mask)
  333. return -EINVAL;
  334. /* Denies inserting a rule with port greater than 65535. */
  335. if (net_port_attr.port > U16_MAX)
  336. return -EINVAL;
  337. /* Imports the new rule. */
  338. return landlock_append_net_rule(ruleset, net_port_attr.port,
  339. net_port_attr.allowed_access);
  340. }
  341. /**
  342. * sys_landlock_add_rule - Add a new rule to a ruleset
  343. *
  344. * @ruleset_fd: File descriptor tied to the ruleset that should be extended
  345. * with the new rule.
  346. * @rule_type: Identify the structure type pointed to by @rule_attr:
  347. * %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
  348. * @rule_attr: Pointer to a rule (matching the @rule_type).
  349. * @flags: Must be 0.
  350. *
  351. * This system call enables to define a new rule and add it to an existing
  352. * ruleset.
  353. *
  354. * Possible returned errors are:
  355. *
  356. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  357. * - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
  358. * supported by the running kernel;
  359. * - %EINVAL: @flags is not 0;
  360. * - %EINVAL: The rule accesses are inconsistent (i.e.
  361. * &landlock_path_beneath_attr.allowed_access or
  362. * &landlock_net_port_attr.allowed_access is not a subset of the ruleset
  363. * handled accesses)
  364. * - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
  365. * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
  366. * 0);
  367. * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
  368. * member of @rule_attr is not a file descriptor as expected;
  369. * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
  370. * @rule_attr is not the expected file descriptor type;
  371. * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
  372. * - %EFAULT: @rule_attr was not a valid address.
  373. */
  374. SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
  375. const enum landlock_rule_type, rule_type,
  376. const void __user *const, rule_attr, const __u32, flags)
  377. {
  378. struct landlock_ruleset *ruleset;
  379. int err;
  380. if (!is_initialized())
  381. return -EOPNOTSUPP;
  382. /* No flag for now. */
  383. if (flags)
  384. return -EINVAL;
  385. /* Gets and checks the ruleset. */
  386. ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
  387. if (IS_ERR(ruleset))
  388. return PTR_ERR(ruleset);
  389. switch (rule_type) {
  390. case LANDLOCK_RULE_PATH_BENEATH:
  391. err = add_rule_path_beneath(ruleset, rule_attr);
  392. break;
  393. case LANDLOCK_RULE_NET_PORT:
  394. err = add_rule_net_port(ruleset, rule_attr);
  395. break;
  396. default:
  397. err = -EINVAL;
  398. break;
  399. }
  400. landlock_put_ruleset(ruleset);
  401. return err;
  402. }
  403. /* Enforcement */
  404. /**
  405. * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
  406. *
  407. * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
  408. * @flags: Must be 0.
  409. *
  410. * This system call enables to enforce a Landlock ruleset on the current
  411. * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
  412. * namespace or is running with no_new_privs. This avoids scenarios where
  413. * unprivileged tasks can affect the behavior of privileged children.
  414. *
  415. * Possible returned errors are:
  416. *
  417. * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
  418. * - %EINVAL: @flags is not 0.
  419. * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
  420. * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
  421. * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
  422. * current thread is not running with no_new_privs, or it doesn't have
  423. * %CAP_SYS_ADMIN in its namespace.
  424. * - %E2BIG: The maximum number of stacked rulesets is reached for the current
  425. * thread.
  426. */
  427. SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
  428. flags)
  429. {
  430. struct landlock_ruleset *new_dom, *ruleset;
  431. struct cred *new_cred;
  432. struct landlock_cred_security *new_llcred;
  433. int err;
  434. if (!is_initialized())
  435. return -EOPNOTSUPP;
  436. /*
  437. * Similar checks as for seccomp(2), except that an -EPERM may be
  438. * returned.
  439. */
  440. if (!task_no_new_privs(current) &&
  441. !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
  442. return -EPERM;
  443. /* No flag for now. */
  444. if (flags)
  445. return -EINVAL;
  446. /* Gets and checks the ruleset. */
  447. ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
  448. if (IS_ERR(ruleset))
  449. return PTR_ERR(ruleset);
  450. /* Prepares new credentials. */
  451. new_cred = prepare_creds();
  452. if (!new_cred) {
  453. err = -ENOMEM;
  454. goto out_put_ruleset;
  455. }
  456. new_llcred = landlock_cred(new_cred);
  457. /*
  458. * There is no possible race condition while copying and manipulating
  459. * the current credentials because they are dedicated per thread.
  460. */
  461. new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);
  462. if (IS_ERR(new_dom)) {
  463. err = PTR_ERR(new_dom);
  464. goto out_put_creds;
  465. }
  466. /* Replaces the old (prepared) domain. */
  467. landlock_put_ruleset(new_llcred->domain);
  468. new_llcred->domain = new_dom;
  469. landlock_put_ruleset(ruleset);
  470. return commit_creds(new_cred);
  471. out_put_creds:
  472. abort_creds(new_cred);
  473. out_put_ruleset:
  474. landlock_put_ruleset(ruleset);
  475. return err;
  476. }