Skip to content

Commit f077e88

Browse files
ebiedermgregkh
authored andcommitted
userns: Don't allow setgroups until a gid mapping has been setablished
commit 273d2c6 upstream. setgroups is unique in not needing a valid mapping before it can be called, in the case of setgroups(0, NULL) which drops all supplemental groups. The design of the user namespace assumes that CAP_SETGID can not actually be used until a gid mapping is established. Therefore add a helper function to see if the user namespace gid mapping has been established and call that function in the setgroups permission check. This is part of the fix for CVE-2014-8989, being able to drop groups without privilege using user namespaces. Reviewed-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 096b0c8 commit f077e88

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎include/linux/user_namespace.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern struct seq_operations proc_projid_seq_operations;
6363
extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
6464
extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
6565
extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
66+
extern bool userns_may_setgroups(const struct user_namespace *ns);
6667
#else
6768

6869
static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
@@ -87,6 +88,10 @@ static inline void put_user_ns(struct user_namespace *ns)
8788
{
8889
}
8990

91+
static inline bool userns_may_setgroups(const struct user_namespace *ns)
92+
{
93+
return true;
94+
}
9095
#endif
9196

9297
#endif /* _LINUX_USER_H */

‎kernel/groups.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/slab.h>
77
#include <linux/security.h>
88
#include <linux/syscalls.h>
9+
#include <linux/user_namespace.h>
910
#include <asm/uaccess.h>
1011

1112
/* init to 2 - one for init_task, one to ensure it is never freed */
@@ -227,7 +228,8 @@ bool may_setgroups(void)
227228
{
228229
struct user_namespace *user_ns = current_user_ns();
229230

230-
return ns_capable(user_ns, CAP_SETGID);
231+
return ns_capable(user_ns, CAP_SETGID) &&
232+
userns_may_setgroups(user_ns);
231233
}
232234

233235
/*

‎kernel/user_namespace.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,20 @@ static bool new_idmap_permitted(const struct file *file,
836836
return false;
837837
}
838838

839+
bool userns_may_setgroups(const struct user_namespace *ns)
840+
{
841+
bool allowed;
842+
843+
mutex_lock(&id_map_mutex);
844+
/* It is not safe to use setgroups until a gid mapping in
845+
* the user namespace has been established.
846+
*/
847+
allowed = ns->gid_map.nr_extents != 0;
848+
mutex_unlock(&id_map_mutex);
849+
850+
return allowed;
851+
}
852+
839853
static void *userns_get(struct task_struct *task)
840854
{
841855
struct user_namespace *user_ns;

0 commit comments

Comments
 (0)