New network namespaces start with the loopback interface DOWN by default, requiring CAP_NET_ADMIN over the owning user namespace to bring it up. To block external network access while preserving the localhost networking that many applications expect, sandboxes (such as bwrap, nsjail and minijail) commonly create an isolated network namespace with only the loopback interface enabled. However, requiring a sandbox to hold CAP_NET_ADMIN in a new user namespace solely to bring loopback up conflicts with reducing the CAP_NET_ADMIN attack surface. Today, an unprivileged sandbox or sandbox manager must start with CAP_NET_ADMIN in its user namespace to be able to bring up loopback, drop it, and then take precautions to block CAP_NET_ADMIN from being regained in nested user namespaces (e.g. via seccomp or max_user_namespaces limits). This adds considerable complexity just to bring up loopback. Conversely, if CAP_NET_ADMIN is restricted at user namespace creation time, tasks inside the new namespace never acquire CAP_NET_ADMIN. While this removes the CAP_NET_ADMIN attack surface down the namespace hierarchy, it also prevents userspace from bringing loopback up, breaking localhost networking for unprivileged sandboxes. Introduce the CLONE_NEWNET_LOOPBACK flag for clone3() and the corresponding UNSHARE_NEWNET_LOOPBACK flag for unshare(). When specified, each flag implies CLONE_NEWNET and automatically brings the loopback device up in copy_net_ns() before returning the new network namespace. Making this an explicit opt-in preserves default network namespace semantics while allowing unprivileged sandboxes to create an isolated network namespace with functional localhost networking without ever holding CAP_NET_ADMIN. Signed-off-by: Hallgrimur H. Gunnarsson --- include/uapi/linux/sched.h | 2 ++ kernel/fork.c | 22 ++++++++++++++++++---- kernel/nsproxy.c | 9 +++++++++ net/core/net_namespace.c | 11 +++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 33a4624285cd..efd00d9f8bd3 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -40,6 +40,7 @@ #define CLONE_NNP (1ULL << 35) /* Set no_new_privs on child. */ #define CLONE_PIDFD_AUTOKILL (1ULL << 36) /* Kill child when clone pidfd closes. */ #define CLONE_EMPTY_MNTNS (1ULL << 37) /* Create an empty mount namespace. */ +#define CLONE_NEWNET_LOOPBACK (1ULL << 38) /* Create network namespace with loopback UP. */ /* * cloning flags intersect with CSIGNAL so can be used with unshare and clone3 @@ -52,6 +53,7 @@ * unshare syscall: */ #define UNSHARE_EMPTY_MNTNS 0x00100000 /* Unshare an empty mount namespace. */ +#define UNSHARE_NEWNET_LOOPBACK 0x00200000 /* Unshare network namespace with loopback UP. */ #ifndef __ASSEMBLY__ /** diff --git a/kernel/fork.c b/kernel/fork.c index 416758c8a3d4..c0f7520e754a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2728,6 +2728,15 @@ pid_t kernel_clone(struct kernel_clone_args *args) args->flags = clone_flags; } + /* + * Set implied CLONE_NEWNET before copy_process() so that + * security_task_alloc() and other clone_flags checks see it. + */ + if (clone_flags & CLONE_NEWNET_LOOPBACK) { + clone_flags |= CLONE_NEWNET; + args->flags = clone_flags; + } + /* * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are @@ -3010,7 +3019,8 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs) if (kargs->flags & ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP | CLONE_AUTOREAP | CLONE_NNP | - CLONE_PIDFD_AUTOKILL | CLONE_EMPTY_MNTNS)) + CLONE_PIDFD_AUTOKILL | CLONE_EMPTY_MNTNS | + CLONE_NEWNET_LOOPBACK)) return false; /* @@ -3160,9 +3170,10 @@ void __init proc_caches_init(void) */ static int check_unshare_flags(unsigned long unshare_flags) { - if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_SIGHAND| - CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| - CLONE_NS_ALL | UNSHARE_EMPTY_MNTNS)) + if (unshare_flags & ~(CLONE_THREAD | CLONE_FS | CLONE_SIGHAND | + CLONE_VM | CLONE_FILES | CLONE_SYSVSEM | + CLONE_NS_ALL | UNSHARE_EMPTY_MNTNS | + UNSHARE_NEWNET_LOOPBACK)) return -EINVAL; /* * Not implemented, but pretend it works if there is nothing @@ -3266,6 +3277,9 @@ int ksys_unshare(unsigned long unshare_flags) if (unshare_flags & CLONE_NEWNS) unshare_flags |= CLONE_FS; + if (unshare_flags & UNSHARE_NEWNET_LOOPBACK) + unshare_flags |= CLONE_NEWNET; + /* No unsharing with overriden fs state */ VFS_WARN_ON_ONCE(unshare_flags & (CLONE_NEWNS | CLONE_FS) && current->fs != current->real_fs); diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c index d9d3d5973bf5..4e30c6a9f8ed 100644 --- a/kernel/nsproxy.c +++ b/kernel/nsproxy.c @@ -231,6 +231,15 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags, flags |= CLONE_EMPTY_MNTNS; } + /* + * Convert the 32-bit UNSHARE_NEWNET_LOOPBACK (which aliases + * CLONE_CHILD_CLEARTID) to the unique 64-bit CLONE_NEWNET_LOOPBACK. + */ + if (flags & UNSHARE_NEWNET_LOOPBACK) { + flags &= ~(u64)UNSHARE_NEWNET_LOOPBACK; + flags |= CLONE_NEWNET_LOOPBACK; + } + *new_nsp = create_new_namespaces(flags, current, user_ns, new_fs ? new_fs : current->fs); if (IS_ERR(*new_nsp)) { diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index da5f881fbd3b..70972639932a 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -596,6 +596,17 @@ struct net *copy_net_ns(u64 flags, dec_net_namespaces(ucounts); return ERR_PTR(rv); } + + if (flags & CLONE_NEWNET_LOOPBACK) { + rtnl_net_lock(net); + rv = dev_open(net->loopback_dev, NULL); + rtnl_net_unlock(net); + if (rv < 0) { + put_net(net); + return ERR_PTR(rv); + } + } + return net; } -- 2.56.0.313.g2479322e68-goog