When a Landlock-sandboxed task holds a pre-opened userland file descriptor, sharing namespaces via USERLAND_SHARE_NSFD or spawning a child via clone3(CLONE_INTO_USERLAND) switches into existing namespaces without passing through security_namespace_install(). Wire userland_share and userland_clone in Landlock's namespace hooks so LANDLOCK_PERMISSION_NAMESPACE_USE mediates sharing a namespace into a userland and entering any userland namespace that differs from the caller's current namespaces. Signed-off-by: Jarkko Sakkinen --- 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..9ef7c9bc8447 100644 --- a/security/landlock/ns.c +++ b/security/landlock/ns.c @@ -13,6 +13,17 @@ #include #include #include +#ifdef CONFIG_USERLAND +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 userland_open(2)), namespace_install (use via + * setns(2)), and userland_share / userland_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_USERLAND +static int check_userland_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_userland_share(struct userland *const userland, + struct ns_common *const ns) +{ + return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_ENTER); +} + +static int hook_userland_clone(struct userland *const userland) +{ + struct ns_common *const namespaces[] = { + to_ns_common(userland->user_ns), + from_mnt_ns(userland->ns->mnt_ns), + to_ns_common(userland->ns->pid_ns_for_children), + to_ns_common(userland->ns->cgroup_ns), + to_ns_common(userland->ns->ipc_ns), + to_ns_common(userland->ns->uts_ns), + to_ns_common(userland->ns->net_ns), +#ifdef CONFIG_TIME_NS + to_ns_common(userland->ns->time_ns), +#endif + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(namespaces); i++) { + int err = check_userland_ns(namespaces[i]); + + if (err) + return err; + } + return 0; +} +#endif /* CONFIG_USERLAND */ + 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_USERLAND + LSM_HOOK_INIT(userland_share, hook_userland_share), + LSM_HOOK_INIT(userland_clone, hook_userland_clone), +#endif }; __init void landlock_add_ns_hooks(void) -- 2.47.3