When a Landlock-sandboxed task holds a pre-opened nsexec file descriptor, sharing namespaces via NSEXEC_SHARE_NSFD or spawning a child via clone3(CLONE_INTO_NSEXEC) switches into existing namespaces without passing through security_namespace_install().
Wire nsexec_share and nsexec_clone in Landlock's namespace hooks so LANDLOCK_PERMISSION_NAMESPACE_USE mediates sharing a namespace into a nsexec and entering any nsexec namespace that differs from the caller's current namespaces. Signed-off-by: Jarkko Sakkinen <[email protected]> --- security/landlock/ns.c | 63 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/security/landlock/ns.c b/security/landlock/ns.c index 3d443599b16f..b5f0aa48a1d0 100644 --- a/security/landlock/ns.c +++ b/security/landlock/ns.c @@ -13,6 +13,17 @@ #include <linux/ns_common.h> #include <linux/nsproxy.h> #include <uapi/linux/landlock.h> +#ifdef CONFIG_NSEXEC +#include <linux/cgroup_namespace.h> +#include <linux/ipc_namespace.h> +#include <linux/mnt_namespace.h> +#include <linux/nsexec.h> +#include <linux/pid_namespace.h> +#include <linux/time_namespace.h> +#include <linux/user_namespace.h> +#include <linux/uts_namespace.h> +#include <net/net_namespace.h> +#endif #include "cred.h" #include "domain.h" @@ -93,10 +104,10 @@ static const struct access_masks ns_permission = { * @request_type: Namespace operation reported on denial. * * Shared check for namespace_init (creation via clone(2), unshare(2), - * open_tree(2), or fsmount(2)) and namespace_install (use via setns(2)): denies - * when the namespace type is not in the domain's allowed set. At allocation - * time @ns->ns_id is already set even though the namespace is not reachable - * from user space yet. + * open_tree(2), fsmount(2), or nsexec_open(2)), namespace_install (use via + * setns(2)), and nsexec_share / nsexec_clone: denies when the namespace + * type is not in the domain's allowed set. At allocation time @ns->ns_id is + * already set even though the namespace is not reachable from user space yet. * * Return: 0 if allowed, -EPERM if denied. */ @@ -141,9 +152,53 @@ static int hook_namespace_install(const struct nsset *const nsset, return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_ENTER); } +#ifdef CONFIG_NSEXEC +static int check_nsexec_ns(struct ns_common *const ns) +{ + if (is_current_namespace(ns)) + return 0; + return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_ENTER); +} + +static int hook_nsexec_share(struct nsexec *const nsexec, + struct ns_common *const ns) +{ + return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_ENTER); +} + +static int hook_nsexec_clone(struct nsexec *const nsexec) +{ + struct ns_common *const namespaces[] = { + to_ns_common(nsexec->user_ns), + from_mnt_ns(nsexec->ns->mnt_ns), + to_ns_common(nsexec->ns->pid_ns_for_children), + to_ns_common(nsexec->ns->cgroup_ns), + to_ns_common(nsexec->ns->ipc_ns), + to_ns_common(nsexec->ns->uts_ns), + to_ns_common(nsexec->ns->net_ns), +#ifdef CONFIG_TIME_NS + to_ns_common(nsexec->ns->time_ns), +#endif + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(namespaces); i++) { + int err = check_nsexec_ns(namespaces[i]); + + if (err) + return err; + } + return 0; +} +#endif /* CONFIG_NSEXEC */ + static struct security_hook_list landlock_hooks[] __ro_after_init = { LSM_HOOK_INIT(namespace_init, hook_namespace_init), LSM_HOOK_INIT(namespace_install, hook_namespace_install), +#ifdef CONFIG_NSEXEC + LSM_HOOK_INIT(nsexec_share, hook_nsexec_share), + LSM_HOOK_INIT(nsexec_clone, hook_nsexec_clone), +#endif }; __init void landlock_add_ns_hooks(void) -- 2.47.3
