Skip to content

Commit f333c70

Browse files
committed
pidns: Add a limit on the number of pid namespaces
Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 25f9c08 commit f333c70

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

include/linux/pid_namespace.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct pid_namespace {
4040
struct fs_pin *bacct;
4141
#endif
4242
struct user_namespace *user_ns;
43+
struct ucounts *ucounts;
4344
struct work_struct proc_work;
4445
kgid_t pid_gid;
4546
int hide_pid;

include/linux/user_namespace.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct ucounts;
2626

2727
enum ucount_type {
2828
UCOUNT_USER_NAMESPACES,
29+
UCOUNT_PID_NAMESPACES,
2930
UCOUNT_COUNTS,
3031
};
3132

kernel/pid_namespace.c

+21-4
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,36 @@ static void proc_cleanup_work(struct work_struct *work)
7979
/* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
8080
#define MAX_PID_NS_LEVEL 32
8181

82+
static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
83+
{
84+
return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
85+
}
86+
87+
static void dec_pid_namespaces(struct ucounts *ucounts)
88+
{
89+
dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
90+
}
91+
8292
static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
8393
struct pid_namespace *parent_pid_ns)
8494
{
8595
struct pid_namespace *ns;
8696
unsigned int level = parent_pid_ns->level + 1;
97+
struct ucounts *ucounts;
8798
int i;
8899
int err;
89100

90-
if (level > MAX_PID_NS_LEVEL) {
91-
err = -EINVAL;
101+
err = -EINVAL;
102+
if (level > MAX_PID_NS_LEVEL)
103+
goto out;
104+
ucounts = inc_pid_namespaces(user_ns);
105+
if (!ucounts)
92106
goto out;
93-
}
94107

95108
err = -ENOMEM;
96109
ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
97110
if (ns == NULL)
98-
goto out;
111+
goto out_dec;
99112

100113
ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
101114
if (!ns->pidmap[0].page)
@@ -114,6 +127,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
114127
ns->level = level;
115128
ns->parent = get_pid_ns(parent_pid_ns);
116129
ns->user_ns = get_user_ns(user_ns);
130+
ns->ucounts = ucounts;
117131
ns->nr_hashed = PIDNS_HASH_ADDING;
118132
INIT_WORK(&ns->proc_work, proc_cleanup_work);
119133

@@ -129,6 +143,8 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
129143
kfree(ns->pidmap[0].page);
130144
out_free:
131145
kmem_cache_free(pid_ns_cachep, ns);
146+
out_dec:
147+
dec_pid_namespaces(ucounts);
132148
out:
133149
return ERR_PTR(err);
134150
}
@@ -146,6 +162,7 @@ static void destroy_pid_namespace(struct pid_namespace *ns)
146162
ns_free_inum(&ns->ns);
147163
for (i = 0; i < PIDMAP_ENTRIES; i++)
148164
kfree(ns->pidmap[i].page);
165+
dec_pid_namespaces(ns->ucounts);
149166
put_user_ns(ns->user_ns);
150167
call_rcu(&ns->rcu, delayed_free_pidns);
151168
}

kernel/ucount.c

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int int_max = INT_MAX;
6868
}
6969
static struct ctl_table user_table[] = {
7070
UCOUNT_ENTRY("max_user_namespaces"),
71+
UCOUNT_ENTRY("max_pid_namespaces"),
7172
{ }
7273
};
7374
#endif /* CONFIG_SYSCTL */

0 commit comments

Comments
 (0)