Skip to content

Commit 5e4a084

Browse files
committed
userns: Require CAP_SYS_ADMIN for most uses of setns.
Andy Lutomirski <luto@amacapital.net> found a nasty little bug in the permissions of setns. With unprivileged user namespaces it became possible to create new namespaces without privilege. However the setns calls were relaxed to only require CAP_SYS_ADMIN in the user nameapce of the targed namespace. Which made the following nasty sequence possible. pid = clone(CLONE_NEWUSER | CLONE_NEWNS); if (pid == 0) { /* child */ system("mount --bind /home/me/passwd /etc/passwd"); } else if (pid != 0) { /* parent */ char path[PATH_MAX]; snprintf(path, sizeof(path), "/proc/%u/ns/mnt"); fd = open(path, O_RDONLY); setns(fd, 0); system("su -"); } Prevent this possibility by requiring CAP_SYS_ADMIN in the current user namespace when joing all but the user namespace. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 520d9ea commit 5e4a084

5 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎fs/namespace.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,8 @@ static int mntns_install(struct nsproxy *nsproxy, void *ns)
27812781
struct path root;
27822782

27832783
if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
2784-
!nsown_capable(CAP_SYS_CHROOT))
2784+
!nsown_capable(CAP_SYS_CHROOT) ||
2785+
!nsown_capable(CAP_SYS_ADMIN))
27852786
return -EPERM;
27862787

27872788
if (fs->users != 1)

‎ipc/namespace.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ static void ipcns_put(void *ns)
170170
static int ipcns_install(struct nsproxy *nsproxy, void *new)
171171
{
172172
struct ipc_namespace *ns = new;
173-
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
173+
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
174+
!nsown_capable(CAP_SYS_ADMIN))
174175
return -EPERM;
175176

176177
/* Ditch state from the old ipc namespace */

‎kernel/pid_namespace.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ static int pidns_install(struct nsproxy *nsproxy, void *ns)
325325
struct pid_namespace *active = task_active_pid_ns(current);
326326
struct pid_namespace *ancestor, *new = ns;
327327

328-
if (!ns_capable(new->user_ns, CAP_SYS_ADMIN))
328+
if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
329+
!nsown_capable(CAP_SYS_ADMIN))
329330
return -EPERM;
330331

331332
/*

‎kernel/utsname.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ static int utsns_install(struct nsproxy *nsproxy, void *new)
113113
{
114114
struct uts_namespace *ns = new;
115115

116-
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
116+
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
117+
!nsown_capable(CAP_SYS_ADMIN))
117118
return -EPERM;
118119

119120
get_uts_ns(ns);

‎net/core/net_namespace.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ static int netns_install(struct nsproxy *nsproxy, void *ns)
649649
{
650650
struct net *net = ns;
651651

652-
if (!ns_capable(net->user_ns, CAP_SYS_ADMIN))
652+
if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
653+
!nsown_capable(CAP_SYS_ADMIN))
653654
return -EPERM;
654655

655656
put_net(nsproxy->net_ns);

0 commit comments

Comments
 (0)