xref: /linux/security/landlock/syscalls.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Landlock - 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  * Copyright © 2021-2025 Microsoft Corporation
8  */
9 
10 #include <asm/current.h>
11 #include <linux/anon_inodes.h>
12 #include <linux/bitops.h>
13 #include <linux/build_bug.h>
14 #include <linux/capability.h>
15 #include <linux/cleanup.h>
16 #include <linux/compiler_types.h>
17 #include <linux/dcache.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/fs.h>
21 #include <linux/limits.h>
22 #include <linux/mount.h>
23 #include <linux/path.h>
24 #include <linux/sched.h>
25 #include <linux/security.h>
26 #include <linux/stddef.h>
27 #include <linux/syscalls.h>
28 #include <linux/types.h>
29 #include <linux/uaccess.h>
30 #include <uapi/linux/landlock.h>
31 
32 #include "cred.h"
33 #include "domain.h"
34 #include "fs.h"
35 #include "limits.h"
36 #include "net.h"
37 #include "ruleset.h"
38 #include "setup.h"
39 #include "tsync.h"
40 
41 static bool is_initialized(void)
42 {
43 	if (likely(landlock_initialized))
44 		return true;
45 
46 	pr_warn_once(
47 		"Disabled but requested by user space. "
48 		"You should enable Landlock at boot time: "
49 		"https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");
50 	return false;
51 }
52 
53 /**
54  * copy_min_struct_from_user - Safe future-proof argument copying
55  *
56  * Extend copy_struct_from_user() to check for consistent user buffer.
57  *
58  * @dst: Kernel space pointer or NULL.
59  * @ksize: Actual size of the data pointed to by @dst.
60  * @ksize_min: Minimal required size to be copied.
61  * @src: User space pointer or NULL.
62  * @usize: (Alleged) size of the data pointed to by @src.
63  *
64  * Return: 0 on success, -errno on failure.
65  */
66 static __always_inline int
67 copy_min_struct_from_user(void *const dst, const size_t ksize,
68 			  const size_t ksize_min, const void __user *const src,
69 			  const size_t usize)
70 {
71 	/* Checks buffer inconsistencies. */
72 	BUILD_BUG_ON(!dst);
73 	if (!src)
74 		return -EFAULT;
75 
76 	/* Checks size ranges. */
77 	BUILD_BUG_ON(ksize <= 0);
78 	BUILD_BUG_ON(ksize < ksize_min);
79 	if (usize < ksize_min)
80 		return -EINVAL;
81 	if (usize > PAGE_SIZE)
82 		return -E2BIG;
83 
84 	/* Copies user buffer and fills with zeros. */
85 	return copy_struct_from_user(dst, ksize, src, usize);
86 }
87 
88 /*
89  * This function only contains arithmetic operations with constants, leading to
90  * BUILD_BUG_ON().  The related code is evaluated and checked at build time,
91  * but it is then ignored thanks to compiler optimizations.
92  */
93 static void build_check_abi(void)
94 {
95 	struct landlock_ruleset_attr ruleset_attr;
96 	struct landlock_path_beneath_attr path_beneath_attr;
97 	struct landlock_net_port_attr net_port_attr;
98 	size_t ruleset_size, path_beneath_size, net_port_size;
99 
100 	/*
101 	 * For each user space ABI structures, first checks that there is no
102 	 * hole in them, then checks that all architectures have the same
103 	 * struct size.
104 	 */
105 	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
106 	ruleset_size += sizeof(ruleset_attr.handled_access_net);
107 	ruleset_size += sizeof(ruleset_attr.scoped);
108 	ruleset_size += sizeof(ruleset_attr.quiet_access_fs);
109 	ruleset_size += sizeof(ruleset_attr.quiet_access_net);
110 	ruleset_size += sizeof(ruleset_attr.quiet_scoped);
111 	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
112 	BUILD_BUG_ON(sizeof(ruleset_attr) != 48);
113 
114 	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
115 	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
116 	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
117 	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
118 
119 	net_port_size = sizeof(net_port_attr.allowed_access);
120 	net_port_size += sizeof(net_port_attr.port);
121 	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
122 	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
123 }
124 
125 /* Ruleset handling */
126 
127 static int fop_ruleset_release(struct inode *const inode,
128 			       struct file *const filp)
129 {
130 	struct landlock_ruleset *ruleset = filp->private_data;
131 
132 	landlock_put_ruleset(ruleset);
133 	return 0;
134 }
135 
136 static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,
137 			      const size_t size, loff_t *const ppos)
138 {
139 	/* Dummy handler to enable FMODE_CAN_READ. */
140 	return -EINVAL;
141 }
142 
143 static ssize_t fop_dummy_write(struct file *const filp,
144 			       const char __user *const buf, const size_t size,
145 			       loff_t *const ppos)
146 {
147 	/* Dummy handler to enable FMODE_CAN_WRITE. */
148 	return -EINVAL;
149 }
150 
151 /*
152  * A ruleset file descriptor enables to build a ruleset by adding (i.e.
153  * writing) rule after rule, without relying on the task's context.  This
154  * reentrant design is also used in a read way to enforce the ruleset on the
155  * current task.
156  */
157 static const struct file_operations ruleset_fops = {
158 	.release = fop_ruleset_release,
159 	.read = fop_dummy_read,
160 	.write = fop_dummy_write,
161 };
162 
163 /*
164  * The Landlock ABI version should be incremented for each new Landlock-related
165  * user space visible change (e.g. Landlock syscalls).  This version should
166  * only be incremented once per Linux release.  When incrementing, the date in
167  * Documentation/userspace-api/landlock.rst should be updated to reflect the
168  * UAPI change.
169  * If the change involves a fix that requires userspace awareness, also update
170  * the errata documentation in Documentation/userspace-api/landlock.rst .
171  */
172 const int landlock_abi_version = 10;
173 
174 /**
175  * sys_landlock_create_ruleset - Create a new ruleset
176  *
177  * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
178  *        the new ruleset.
179  * @size: Size of the pointed &struct landlock_ruleset_attr (needed for
180  *        backward and forward compatibility).
181  * @flags: Supported values:
182  *
183  *         - %LANDLOCK_CREATE_RULESET_VERSION
184  *         - %LANDLOCK_CREATE_RULESET_ERRATA
185  *
186  * This system call enables to create a new Landlock ruleset.
187  *
188  * If %LANDLOCK_CREATE_RULESET_VERSION or %LANDLOCK_CREATE_RULESET_ERRATA is
189  * set, then @attr must be NULL and @size must be 0.
190  *
191  * Return: The ruleset file descriptor on success, the Landlock ABI version if
192  * %LANDLOCK_CREATE_RULESET_VERSION is set, the errata value if
193  * %LANDLOCK_CREATE_RULESET_ERRATA is set, or -errno on failure.  Possible
194  * returned errors are:
195  *
196  * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
197  * - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small
198  *   @size;
199  * - %EINVAL: quiet_access_fs, quiet_access_net, or quiet_scoped is not a
200  *   subset of the corresponding handled_access_fs, handled_access_net, or
201  *   scoped;
202  * - %E2BIG: @attr or @size inconsistencies;
203  * - %EFAULT: @attr or @size inconsistencies;
204  * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
205  *
206  * .. kernel-doc:: include/uapi/linux/landlock.h
207  *     :identifiers: landlock_create_ruleset_flags
208  */
209 SYSCALL_DEFINE3(landlock_create_ruleset,
210 		const struct landlock_ruleset_attr __user *const, attr,
211 		const size_t, size, const __u32, flags)
212 {
213 	struct landlock_ruleset_attr ruleset_attr;
214 	struct landlock_ruleset *ruleset;
215 	int err, ruleset_fd;
216 
217 	/* Build-time checks. */
218 	build_check_abi();
219 
220 	if (!is_initialized())
221 		return -EOPNOTSUPP;
222 
223 	if (flags) {
224 		if (attr || size)
225 			return -EINVAL;
226 
227 		if (flags == LANDLOCK_CREATE_RULESET_VERSION)
228 			return landlock_abi_version;
229 
230 		if (flags == LANDLOCK_CREATE_RULESET_ERRATA)
231 			return landlock_errata;
232 
233 		return -EINVAL;
234 	}
235 
236 	/* Copies raw user space buffer. */
237 	err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
238 					offsetofend(typeof(ruleset_attr),
239 						    handled_access_fs),
240 					attr, size);
241 	if (err)
242 		return err;
243 
244 	/* Checks content (and 32-bits cast). */
245 	if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
246 	    LANDLOCK_MASK_ACCESS_FS)
247 		return -EINVAL;
248 
249 	/* Checks network content (and 32-bits cast). */
250 	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
251 	    LANDLOCK_MASK_ACCESS_NET)
252 		return -EINVAL;
253 
254 	/* Checks IPC scoping content (and 32-bits cast). */
255 	if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE)
256 		return -EINVAL;
257 
258 	/*
259 	 * Check that quiet masks are subsets of the respective handled masks.
260 	 * Because of the checks above this is sufficient to also ensure that
261 	 * the quiet masks are valid access masks.
262 	 */
263 	if ((ruleset_attr.quiet_access_fs | ruleset_attr.handled_access_fs) !=
264 	    ruleset_attr.handled_access_fs)
265 		return -EINVAL;
266 	if ((ruleset_attr.quiet_access_net | ruleset_attr.handled_access_net) !=
267 	    ruleset_attr.handled_access_net)
268 		return -EINVAL;
269 	if ((ruleset_attr.quiet_scoped | ruleset_attr.scoped) !=
270 	    ruleset_attr.scoped)
271 		return -EINVAL;
272 
273 	/* Checks arguments and transforms to kernel struct. */
274 	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
275 					  ruleset_attr.handled_access_net,
276 					  ruleset_attr.scoped);
277 	if (IS_ERR(ruleset))
278 		return PTR_ERR(ruleset);
279 
280 	ruleset->quiet_masks.fs = ruleset_attr.quiet_access_fs;
281 	ruleset->quiet_masks.net = ruleset_attr.quiet_access_net;
282 	ruleset->quiet_masks.scope = ruleset_attr.quiet_scoped;
283 
284 	/* Creates anonymous FD referring to the ruleset. */
285 	ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
286 				      ruleset, O_RDWR | O_CLOEXEC);
287 	if (ruleset_fd < 0)
288 		landlock_put_ruleset(ruleset);
289 	return ruleset_fd;
290 }
291 
292 /*
293  * Returns an owned ruleset from a FD. It is thus needed to call
294  * landlock_put_ruleset() on the return value.
295  */
296 static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
297 						    const fmode_t mode)
298 {
299 	CLASS(fd, ruleset_f)(fd);
300 	struct landlock_ruleset *ruleset;
301 
302 	if (fd_empty(ruleset_f))
303 		return ERR_PTR(-EBADF);
304 
305 	/* Checks FD type and access right. */
306 	if (fd_file(ruleset_f)->f_op != &ruleset_fops)
307 		return ERR_PTR(-EBADFD);
308 	if (!(fd_file(ruleset_f)->f_mode & mode))
309 		return ERR_PTR(-EPERM);
310 	ruleset = fd_file(ruleset_f)->private_data;
311 	if (WARN_ON_ONCE(ruleset->num_layers != 1))
312 		return ERR_PTR(-EINVAL);
313 	landlock_get_ruleset(ruleset);
314 	return ruleset;
315 }
316 
317 /* Path handling */
318 
319 /*
320  * @path: Must call put_path(@path) after the call if it succeeded.
321  */
322 static int get_path_from_fd(const s32 fd, struct path *const path)
323 {
324 	CLASS(fd_raw, f)(fd);
325 
326 	BUILD_BUG_ON(!__same_type(
327 		fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
328 
329 	if (fd_empty(f))
330 		return -EBADF;
331 	/*
332 	 * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
333 	 * pseudo filesystems that will never be mountable (e.g. sockfs,
334 	 * pipefs).
335 	 */
336 	if ((fd_file(f)->f_op == &ruleset_fops) ||
337 	    (fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
338 	    (fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
339 	    IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry)))
340 		return -EBADFD;
341 
342 	*path = fd_file(f)->f_path;
343 	path_get(path);
344 	return 0;
345 }
346 
347 static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
348 				 const void __user *const rule_attr, u32 flags)
349 {
350 	struct landlock_path_beneath_attr path_beneath_attr;
351 	struct path path;
352 	int res, err;
353 	access_mask_t mask;
354 
355 	/* Copies raw user space buffer. */
356 	res = copy_from_user(&path_beneath_attr, rule_attr,
357 			     sizeof(path_beneath_attr));
358 	if (res)
359 		return -EFAULT;
360 
361 	/*
362 	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
363 	 * are ignored in path walks.  However, the rule is not useless if it is
364 	 * there to hold a quiet flag.
365 	 */
366 	if (!flags && !path_beneath_attr.allowed_access)
367 		return -ENOMSG;
368 
369 	/* Checks that allowed_access matches the @ruleset constraints. */
370 	mask = ruleset->access_masks[0].fs;
371 	if ((path_beneath_attr.allowed_access | mask) != mask)
372 		return -EINVAL;
373 
374 	/* Checks for useless quiet flag. */
375 	if (flags & LANDLOCK_ADD_RULE_QUIET && !ruleset->quiet_masks.fs)
376 		return -EINVAL;
377 
378 	/* Gets and checks the new rule. */
379 	err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
380 	if (err)
381 		return err;
382 
383 	/* Imports the new rule. */
384 	err = landlock_append_fs_rule(ruleset, &path,
385 				      path_beneath_attr.allowed_access, flags);
386 	path_put(&path);
387 	return err;
388 }
389 
390 static int add_rule_net_port(struct landlock_ruleset *ruleset,
391 			     const void __user *const rule_attr, u32 flags)
392 {
393 	struct landlock_net_port_attr net_port_attr;
394 	int res;
395 	access_mask_t mask;
396 
397 	/* Copies raw user space buffer. */
398 	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
399 	if (res)
400 		return -EFAULT;
401 
402 	/*
403 	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
404 	 * are ignored by network actions.  However, the rule is not useless if
405 	 * it is there to hold a quiet flag.
406 	 */
407 	if (!flags && !net_port_attr.allowed_access)
408 		return -ENOMSG;
409 
410 	/* Checks that allowed_access matches the @ruleset constraints. */
411 	mask = landlock_get_net_access_mask(ruleset, 0);
412 	if ((net_port_attr.allowed_access | mask) != mask)
413 		return -EINVAL;
414 
415 	/* Checks for useless quiet flag. */
416 	if (flags & LANDLOCK_ADD_RULE_QUIET && !ruleset->quiet_masks.net)
417 		return -EINVAL;
418 
419 	/* Denies inserting a rule with port greater than 65535. */
420 	if (net_port_attr.port > U16_MAX)
421 		return -EINVAL;
422 
423 	/* Imports the new rule. */
424 	return landlock_append_net_rule(ruleset, net_port_attr.port,
425 					net_port_attr.allowed_access, flags);
426 }
427 
428 /**
429  * sys_landlock_add_rule - Add a new rule to a ruleset
430  *
431  * @ruleset_fd: File descriptor tied to the ruleset that should be extended
432  *		with the new rule.
433  * @rule_type: Identify the structure type pointed to by @rule_attr:
434  *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
435  * @rule_attr: Pointer to a rule (matching the @rule_type).
436  * @flags: Must be 0 or %LANDLOCK_ADD_RULE_QUIET.
437  *
438  * This system call enables to define a new rule and add it to an existing
439  * ruleset.
440  *
441  * Return: 0 on success, or -errno on failure.  Possible returned errors are:
442  *
443  * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
444  * - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
445  *   supported by the running kernel;
446  * - %EINVAL: @flags is not valid;
447  * - %EINVAL: The rule accesses are inconsistent (i.e.
448  *   &landlock_path_beneath_attr.allowed_access or
449  *   &landlock_net_port_attr.allowed_access is not a subset of the ruleset
450  *   handled accesses)
451  * - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
452  * - %EINVAL: LANDLOCK_ADD_RULE_QUIET is passed but the ruleset has no
453  *   quiet access bits set for the corresponding rule type.
454  * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
455  *   0) and no flags;
456  * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
457  *   member of @rule_attr is not a file descriptor as expected;
458  * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
459  *   @rule_attr is not the expected file descriptor type;
460  * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
461  * - %EFAULT: @rule_attr was not a valid address.
462  *
463  * .. kernel-doc:: include/uapi/linux/landlock.h
464  *     :identifiers: landlock_add_rule_flags
465  */
466 SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
467 		const enum landlock_rule_type, rule_type,
468 		const void __user *const, rule_attr, const __u32, flags)
469 {
470 	struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL;
471 
472 	if (!is_initialized())
473 		return -EOPNOTSUPP;
474 
475 	if (flags && flags != LANDLOCK_ADD_RULE_QUIET)
476 		return -EINVAL;
477 
478 	/* Gets and checks the ruleset. */
479 	ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
480 	if (IS_ERR(ruleset))
481 		return PTR_ERR(ruleset);
482 
483 	switch (rule_type) {
484 	case LANDLOCK_RULE_PATH_BENEATH:
485 		return add_rule_path_beneath(ruleset, rule_attr, flags);
486 	case LANDLOCK_RULE_NET_PORT:
487 		return add_rule_net_port(ruleset, rule_attr, flags);
488 	default:
489 		return -EINVAL;
490 	}
491 }
492 
493 /* Enforcement */
494 
495 /**
496  * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
497  *
498  * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
499  * @flags: Supported values:
500  *
501  *         - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
502  *         - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
503  *         - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
504  *         - %LANDLOCK_RESTRICT_SELF_TSYNC
505  *
506  * This system call enforces a Landlock ruleset on the current thread.
507  * Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
508  * namespace or is running with no_new_privs.  This avoids scenarios where
509  * unprivileged tasks can affect the behavior of privileged children.
510  *
511  * Return: 0 on success, or -errno on failure.  Possible returned errors are:
512  *
513  * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
514  * - %EINVAL: @flags contains an unknown bit.
515  * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
516  * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
517  * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
518  *   current thread is not running with no_new_privs, or it doesn't have
519  *   %CAP_SYS_ADMIN in its namespace.
520  * - %E2BIG: The maximum number of stacked rulesets is reached for the current
521  *   thread.
522  *
523  * .. kernel-doc:: include/uapi/linux/landlock.h
524  *     :identifiers: landlock_restrict_self_flags
525  */
526 SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
527 		flags)
528 {
529 	struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL;
530 	struct cred *new_cred;
531 	struct landlock_cred_security *new_llcred;
532 	bool __maybe_unused log_same_exec, log_new_exec, log_subdomains,
533 		prev_log_subdomains;
534 
535 	if (!is_initialized())
536 		return -EOPNOTSUPP;
537 
538 	/*
539 	 * Similar checks as for seccomp(2), except that an -EPERM may be
540 	 * returned.
541 	 */
542 	if (!task_no_new_privs(current) &&
543 	    !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
544 		return -EPERM;
545 
546 	if ((flags | LANDLOCK_MASK_RESTRICT_SELF) !=
547 	    LANDLOCK_MASK_RESTRICT_SELF)
548 		return -EINVAL;
549 
550 	/* Translates "off" flag to boolean. */
551 	log_same_exec = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF);
552 	/* Translates "on" flag to boolean. */
553 	log_new_exec = !!(flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON);
554 	/* Translates "off" flag to boolean. */
555 	log_subdomains = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
556 
557 	/*
558 	 * It is allowed to set LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF with
559 	 * -1 as ruleset_fd, optionally combined with
560 	 * LANDLOCK_RESTRICT_SELF_TSYNC to propagate this configuration to all
561 	 * threads.  No other flag must be set.
562 	 */
563 	if (!(ruleset_fd == -1 &&
564 	      (flags & ~LANDLOCK_RESTRICT_SELF_TSYNC) ==
565 		      LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) {
566 		/* Gets and checks the ruleset. */
567 		ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
568 		if (IS_ERR(ruleset))
569 			return PTR_ERR(ruleset);
570 	}
571 
572 	/* Prepares new credentials. */
573 	new_cred = prepare_creds();
574 	if (!new_cred)
575 		return -ENOMEM;
576 
577 	new_llcred = landlock_cred(new_cred);
578 
579 #ifdef CONFIG_AUDIT
580 	prev_log_subdomains = !new_llcred->log_subdomains_off;
581 	new_llcred->log_subdomains_off = !prev_log_subdomains ||
582 					 !log_subdomains;
583 #endif /* CONFIG_AUDIT */
584 
585 	/*
586 	 * The only case when a ruleset may not be set is if
587 	 * LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF is set (optionally with
588 	 * LANDLOCK_RESTRICT_SELF_TSYNC) and ruleset_fd is -1.  We could
589 	 * optimize this case by not calling commit_creds() if this flag was
590 	 * already set, but it is not worth the complexity.
591 	 */
592 	if (ruleset) {
593 		/*
594 		 * There is no possible race condition while copying and
595 		 * manipulating the current credentials because they are
596 		 * dedicated per thread.
597 		 */
598 		struct landlock_ruleset *const new_dom =
599 			landlock_merge_ruleset(new_llcred->domain, ruleset);
600 		if (IS_ERR(new_dom)) {
601 			abort_creds(new_cred);
602 			return PTR_ERR(new_dom);
603 		}
604 
605 #ifdef CONFIG_AUDIT
606 		new_dom->hierarchy->log_same_exec = log_same_exec;
607 		new_dom->hierarchy->log_new_exec = log_new_exec;
608 		if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains)
609 			new_dom->hierarchy->log_status = LANDLOCK_LOG_DISABLED;
610 #endif /* CONFIG_AUDIT */
611 
612 		/* Replaces the old (prepared) domain. */
613 		landlock_put_ruleset(new_llcred->domain);
614 		new_llcred->domain = new_dom;
615 
616 #ifdef CONFIG_AUDIT
617 		new_llcred->domain_exec |= BIT(new_dom->num_layers - 1);
618 #endif /* CONFIG_AUDIT */
619 	}
620 
621 	if (flags & LANDLOCK_RESTRICT_SELF_TSYNC) {
622 		const int err = landlock_restrict_sibling_threads(
623 			current_cred(), new_cred);
624 		if (err) {
625 			abort_creds(new_cred);
626 			return err;
627 		}
628 	}
629 
630 	return commit_creds(new_cred);
631 }
632