// SPDX-License-Identifier: GPL-2.0-only1/*2* Landlock - System call implementations and user space interfaces3*4* Copyright © 2016-2020 Mickaël Salaün <[email protected]>5* Copyright © 2018-2020 ANSSI6* Copyright © 2021-2025 Microsoft Corporation7*/89#include <asm/current.h>10#include <linux/anon_inodes.h>11#include <linux/bitops.h>12#include <linux/build_bug.h>13#include <linux/capability.h>14#include <linux/cleanup.h>15#include <linux/compiler_types.h>16#include <linux/dcache.h>17#include <linux/err.h>18#include <linux/errno.h>19#include <linux/fs.h>20#include <linux/limits.h>21#include <linux/mount.h>22#include <linux/path.h>23#include <linux/sched.h>24#include <linux/security.h>25#include <linux/stddef.h>26#include <linux/syscalls.h>27#include <linux/types.h>28#include <linux/uaccess.h>29#include <uapi/linux/landlock.h>3031#include "cred.h"32#include "domain.h"33#include "fs.h"34#include "limits.h"35#include "net.h"36#include "ruleset.h"37#include "setup.h"3839static bool is_initialized(void)40{41if (likely(landlock_initialized))42return true;4344pr_warn_once(45"Disabled but requested by user space. "46"You should enable Landlock at boot time: "47"https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");48return false;49}5051/**52* copy_min_struct_from_user - Safe future-proof argument copying53*54* Extend copy_struct_from_user() to check for consistent user buffer.55*56* @dst: Kernel space pointer or NULL.57* @ksize: Actual size of the data pointed to by @dst.58* @ksize_min: Minimal required size to be copied.59* @src: User space pointer or NULL.60* @usize: (Alleged) size of the data pointed to by @src.61*/62static __always_inline int63copy_min_struct_from_user(void *const dst, const size_t ksize,64const size_t ksize_min, const void __user *const src,65const size_t usize)66{67/* Checks buffer inconsistencies. */68BUILD_BUG_ON(!dst);69if (!src)70return -EFAULT;7172/* Checks size ranges. */73BUILD_BUG_ON(ksize <= 0);74BUILD_BUG_ON(ksize < ksize_min);75if (usize < ksize_min)76return -EINVAL;77if (usize > PAGE_SIZE)78return -E2BIG;7980/* Copies user buffer and fills with zeros. */81return copy_struct_from_user(dst, ksize, src, usize);82}8384/*85* This function only contains arithmetic operations with constants, leading to86* BUILD_BUG_ON(). The related code is evaluated and checked at build time,87* but it is then ignored thanks to compiler optimizations.88*/89static void build_check_abi(void)90{91struct landlock_ruleset_attr ruleset_attr;92struct landlock_path_beneath_attr path_beneath_attr;93struct landlock_net_port_attr net_port_attr;94size_t ruleset_size, path_beneath_size, net_port_size;9596/*97* For each user space ABI structures, first checks that there is no98* hole in them, then checks that all architectures have the same99* struct size.100*/101ruleset_size = sizeof(ruleset_attr.handled_access_fs);102ruleset_size += sizeof(ruleset_attr.handled_access_net);103ruleset_size += sizeof(ruleset_attr.scoped);104BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);105BUILD_BUG_ON(sizeof(ruleset_attr) != 24);106107path_beneath_size = sizeof(path_beneath_attr.allowed_access);108path_beneath_size += sizeof(path_beneath_attr.parent_fd);109BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);110BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);111112net_port_size = sizeof(net_port_attr.allowed_access);113net_port_size += sizeof(net_port_attr.port);114BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);115BUILD_BUG_ON(sizeof(net_port_attr) != 16);116}117118/* Ruleset handling */119120static int fop_ruleset_release(struct inode *const inode,121struct file *const filp)122{123struct landlock_ruleset *ruleset = filp->private_data;124125landlock_put_ruleset(ruleset);126return 0;127}128129static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,130const size_t size, loff_t *const ppos)131{132/* Dummy handler to enable FMODE_CAN_READ. */133return -EINVAL;134}135136static ssize_t fop_dummy_write(struct file *const filp,137const char __user *const buf, const size_t size,138loff_t *const ppos)139{140/* Dummy handler to enable FMODE_CAN_WRITE. */141return -EINVAL;142}143144/*145* A ruleset file descriptor enables to build a ruleset by adding (i.e.146* writing) rule after rule, without relying on the task's context. This147* reentrant design is also used in a read way to enforce the ruleset on the148* current task.149*/150static const struct file_operations ruleset_fops = {151.release = fop_ruleset_release,152.read = fop_dummy_read,153.write = fop_dummy_write,154};155156/*157* The Landlock ABI version should be incremented for each new Landlock-related158* user space visible change (e.g. Landlock syscalls). This version should159* only be incremented once per Linux release, and the date in160* Documentation/userspace-api/landlock.rst should be updated to reflect the161* UAPI change.162*/163const int landlock_abi_version = 7;164165/**166* sys_landlock_create_ruleset - Create a new ruleset167*168* @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of169* the new ruleset.170* @size: Size of the pointed &struct landlock_ruleset_attr (needed for171* backward and forward compatibility).172* @flags: Supported values:173*174* - %LANDLOCK_CREATE_RULESET_VERSION175* - %LANDLOCK_CREATE_RULESET_ERRATA176*177* This system call enables to create a new Landlock ruleset, and returns the178* related file descriptor on success.179*180* If %LANDLOCK_CREATE_RULESET_VERSION or %LANDLOCK_CREATE_RULESET_ERRATA is181* set, then @attr must be NULL and @size must be 0.182*183* Possible returned errors are:184*185* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;186* - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small @size;187* - %E2BIG: @attr or @size inconsistencies;188* - %EFAULT: @attr or @size inconsistencies;189* - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.190*191* .. kernel-doc:: include/uapi/linux/landlock.h192* :identifiers: landlock_create_ruleset_flags193*/194SYSCALL_DEFINE3(landlock_create_ruleset,195const struct landlock_ruleset_attr __user *const, attr,196const size_t, size, const __u32, flags)197{198struct landlock_ruleset_attr ruleset_attr;199struct landlock_ruleset *ruleset;200int err, ruleset_fd;201202/* Build-time checks. */203build_check_abi();204205if (!is_initialized())206return -EOPNOTSUPP;207208if (flags) {209if (attr || size)210return -EINVAL;211212if (flags == LANDLOCK_CREATE_RULESET_VERSION)213return landlock_abi_version;214215if (flags == LANDLOCK_CREATE_RULESET_ERRATA)216return landlock_errata;217218return -EINVAL;219}220221/* Copies raw user space buffer. */222err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),223offsetofend(typeof(ruleset_attr),224handled_access_fs),225attr, size);226if (err)227return err;228229/* Checks content (and 32-bits cast). */230if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=231LANDLOCK_MASK_ACCESS_FS)232return -EINVAL;233234/* Checks network content (and 32-bits cast). */235if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=236LANDLOCK_MASK_ACCESS_NET)237return -EINVAL;238239/* Checks IPC scoping content (and 32-bits cast). */240if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE)241return -EINVAL;242243/* Checks arguments and transforms to kernel struct. */244ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,245ruleset_attr.handled_access_net,246ruleset_attr.scoped);247if (IS_ERR(ruleset))248return PTR_ERR(ruleset);249250/* Creates anonymous FD referring to the ruleset. */251ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,252ruleset, O_RDWR | O_CLOEXEC);253if (ruleset_fd < 0)254landlock_put_ruleset(ruleset);255return ruleset_fd;256}257258/*259* Returns an owned ruleset from a FD. It is thus needed to call260* landlock_put_ruleset() on the return value.261*/262static struct landlock_ruleset *get_ruleset_from_fd(const int fd,263const fmode_t mode)264{265CLASS(fd, ruleset_f)(fd);266struct landlock_ruleset *ruleset;267268if (fd_empty(ruleset_f))269return ERR_PTR(-EBADF);270271/* Checks FD type and access right. */272if (fd_file(ruleset_f)->f_op != &ruleset_fops)273return ERR_PTR(-EBADFD);274if (!(fd_file(ruleset_f)->f_mode & mode))275return ERR_PTR(-EPERM);276ruleset = fd_file(ruleset_f)->private_data;277if (WARN_ON_ONCE(ruleset->num_layers != 1))278return ERR_PTR(-EINVAL);279landlock_get_ruleset(ruleset);280return ruleset;281}282283/* Path handling */284285/*286* @path: Must call put_path(@path) after the call if it succeeded.287*/288static int get_path_from_fd(const s32 fd, struct path *const path)289{290CLASS(fd_raw, f)(fd);291292BUILD_BUG_ON(!__same_type(293fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));294295if (fd_empty(f))296return -EBADF;297/*298* Forbids ruleset FDs, internal filesystems (e.g. nsfs), including299* pseudo filesystems that will never be mountable (e.g. sockfs,300* pipefs).301*/302if ((fd_file(f)->f_op == &ruleset_fops) ||303(fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||304(fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||305IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry)))306return -EBADFD;307308*path = fd_file(f)->f_path;309path_get(path);310return 0;311}312313static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,314const void __user *const rule_attr)315{316struct landlock_path_beneath_attr path_beneath_attr;317struct path path;318int res, err;319access_mask_t mask;320321/* Copies raw user space buffer. */322res = copy_from_user(&path_beneath_attr, rule_attr,323sizeof(path_beneath_attr));324if (res)325return -EFAULT;326327/*328* Informs about useless rule: empty allowed_access (i.e. deny rules)329* are ignored in path walks.330*/331if (!path_beneath_attr.allowed_access)332return -ENOMSG;333334/* Checks that allowed_access matches the @ruleset constraints. */335mask = ruleset->access_masks[0].fs;336if ((path_beneath_attr.allowed_access | mask) != mask)337return -EINVAL;338339/* Gets and checks the new rule. */340err = get_path_from_fd(path_beneath_attr.parent_fd, &path);341if (err)342return err;343344/* Imports the new rule. */345err = landlock_append_fs_rule(ruleset, &path,346path_beneath_attr.allowed_access);347path_put(&path);348return err;349}350351static int add_rule_net_port(struct landlock_ruleset *ruleset,352const void __user *const rule_attr)353{354struct landlock_net_port_attr net_port_attr;355int res;356access_mask_t mask;357358/* Copies raw user space buffer. */359res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));360if (res)361return -EFAULT;362363/*364* Informs about useless rule: empty allowed_access (i.e. deny rules)365* are ignored by network actions.366*/367if (!net_port_attr.allowed_access)368return -ENOMSG;369370/* Checks that allowed_access matches the @ruleset constraints. */371mask = landlock_get_net_access_mask(ruleset, 0);372if ((net_port_attr.allowed_access | mask) != mask)373return -EINVAL;374375/* Denies inserting a rule with port greater than 65535. */376if (net_port_attr.port > U16_MAX)377return -EINVAL;378379/* Imports the new rule. */380return landlock_append_net_rule(ruleset, net_port_attr.port,381net_port_attr.allowed_access);382}383384/**385* sys_landlock_add_rule - Add a new rule to a ruleset386*387* @ruleset_fd: File descriptor tied to the ruleset that should be extended388* with the new rule.389* @rule_type: Identify the structure type pointed to by @rule_attr:390* %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.391* @rule_attr: Pointer to a rule (matching the @rule_type).392* @flags: Must be 0.393*394* This system call enables to define a new rule and add it to an existing395* ruleset.396*397* Possible returned errors are:398*399* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;400* - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not401* supported by the running kernel;402* - %EINVAL: @flags is not 0;403* - %EINVAL: The rule accesses are inconsistent (i.e.404* &landlock_path_beneath_attr.allowed_access or405* &landlock_net_port_attr.allowed_access is not a subset of the ruleset406* handled accesses)407* - %EINVAL: &landlock_net_port_attr.port is greater than 65535;408* - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is409* 0);410* - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a411* member of @rule_attr is not a file descriptor as expected;412* - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of413* @rule_attr is not the expected file descriptor type;414* - %EPERM: @ruleset_fd has no write access to the underlying ruleset;415* - %EFAULT: @rule_attr was not a valid address.416*/417SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,418const enum landlock_rule_type, rule_type,419const void __user *const, rule_attr, const __u32, flags)420{421struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL;422423if (!is_initialized())424return -EOPNOTSUPP;425426/* No flag for now. */427if (flags)428return -EINVAL;429430/* Gets and checks the ruleset. */431ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);432if (IS_ERR(ruleset))433return PTR_ERR(ruleset);434435switch (rule_type) {436case LANDLOCK_RULE_PATH_BENEATH:437return add_rule_path_beneath(ruleset, rule_attr);438case LANDLOCK_RULE_NET_PORT:439return add_rule_net_port(ruleset, rule_attr);440default:441return -EINVAL;442}443}444445/* Enforcement */446447/**448* sys_landlock_restrict_self - Enforce a ruleset on the calling thread449*450* @ruleset_fd: File descriptor tied to the ruleset to merge with the target.451* @flags: Supported values:452*453* - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF454* - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON455* - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF456*457* This system call enables to enforce a Landlock ruleset on the current458* thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its459* namespace or is running with no_new_privs. This avoids scenarios where460* unprivileged tasks can affect the behavior of privileged children.461*462* Possible returned errors are:463*464* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;465* - %EINVAL: @flags contains an unknown bit.466* - %EBADF: @ruleset_fd is not a file descriptor for the current thread;467* - %EBADFD: @ruleset_fd is not a ruleset file descriptor;468* - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the469* current thread is not running with no_new_privs, or it doesn't have470* %CAP_SYS_ADMIN in its namespace.471* - %E2BIG: The maximum number of stacked rulesets is reached for the current472* thread.473*474* .. kernel-doc:: include/uapi/linux/landlock.h475* :identifiers: landlock_restrict_self_flags476*/477SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,478flags)479{480struct landlock_ruleset *new_dom,481*ruleset __free(landlock_put_ruleset) = NULL;482struct cred *new_cred;483struct landlock_cred_security *new_llcred;484bool __maybe_unused log_same_exec, log_new_exec, log_subdomains,485prev_log_subdomains;486487if (!is_initialized())488return -EOPNOTSUPP;489490/*491* Similar checks as for seccomp(2), except that an -EPERM may be492* returned.493*/494if (!task_no_new_privs(current) &&495!ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))496return -EPERM;497498if ((flags | LANDLOCK_MASK_RESTRICT_SELF) !=499LANDLOCK_MASK_RESTRICT_SELF)500return -EINVAL;501502/* Translates "off" flag to boolean. */503log_same_exec = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF);504/* Translates "on" flag to boolean. */505log_new_exec = !!(flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON);506/* Translates "off" flag to boolean. */507log_subdomains = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);508509/*510* It is allowed to set LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF with511* -1 as ruleset_fd, but no other flag must be set.512*/513if (!(ruleset_fd == -1 &&514flags == LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) {515/* Gets and checks the ruleset. */516ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);517if (IS_ERR(ruleset))518return PTR_ERR(ruleset);519}520521/* Prepares new credentials. */522new_cred = prepare_creds();523if (!new_cred)524return -ENOMEM;525526new_llcred = landlock_cred(new_cred);527528#ifdef CONFIG_AUDIT529prev_log_subdomains = !new_llcred->log_subdomains_off;530new_llcred->log_subdomains_off = !prev_log_subdomains ||531!log_subdomains;532#endif /* CONFIG_AUDIT */533534/*535* The only case when a ruleset may not be set is if536* LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF is set and ruleset_fd is -1.537* We could optimize this case by not calling commit_creds() if this flag538* was already set, but it is not worth the complexity.539*/540if (!ruleset)541return commit_creds(new_cred);542543/*544* There is no possible race condition while copying and manipulating545* the current credentials because they are dedicated per thread.546*/547new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);548if (IS_ERR(new_dom)) {549abort_creds(new_cred);550return PTR_ERR(new_dom);551}552553#ifdef CONFIG_AUDIT554new_dom->hierarchy->log_same_exec = log_same_exec;555new_dom->hierarchy->log_new_exec = log_new_exec;556if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains)557new_dom->hierarchy->log_status = LANDLOCK_LOG_DISABLED;558#endif /* CONFIG_AUDIT */559560/* Replaces the old (prepared) domain. */561landlock_put_ruleset(new_llcred->domain);562new_llcred->domain = new_dom;563564#ifdef CONFIG_AUDIT565new_llcred->domain_exec |= BIT(new_dom->num_layers - 1);566#endif /* CONFIG_AUDIT */567568return commit_creds(new_cred);569}570571572