utsns: Add a limit on the number of uts namespaces
authorEric W. Biederman <ebiederm@xmission.com>
Mon, 8 Aug 2016 19:11:25 +0000 (14:11 -0500)
committerEric W. Biederman <ebiederm@xmission.com>
Mon, 8 Aug 2016 19:42:02 +0000 (14:42 -0500)
Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
include/linux/user_namespace.h
include/linux/utsname.h
kernel/ucount.c
kernel/utsname.c

index 9ee94827728d90b2cd8af2fb0bc986c24ff01ae4..f9df7dd2609ad08f734e6229f4000dc3d96089ea 100644 (file)
@@ -27,6 +27,7 @@ struct ucounts;
 enum ucount_type {
        UCOUNT_USER_NAMESPACES,
        UCOUNT_PID_NAMESPACES,
+       UCOUNT_UTS_NAMESPACES,
        UCOUNT_COUNTS,
 };
 
index 5093f58ae192c379b13b9e5e74605a0e6c571d25..60f0bb83b313af0e5429264bb83296735c8cdf01 100644 (file)
@@ -24,6 +24,7 @@ struct uts_namespace {
        struct kref kref;
        struct new_utsname name;
        struct user_namespace *user_ns;
+       struct ucounts *ucounts;
        struct ns_common ns;
 };
 extern struct uts_namespace init_uts_ns;
index 66eca94e4ada5f23408a338dc51813671d6b7d19..866850e2eb9292f15fa2ca977929f250a8d75ba6 100644 (file)
@@ -69,6 +69,7 @@ static int int_max = INT_MAX;
 static struct ctl_table user_table[] = {
        UCOUNT_ENTRY("max_user_namespaces"),
        UCOUNT_ENTRY("max_pid_namespaces"),
+       UCOUNT_ENTRY("max_uts_namespaces"),
        { }
 };
 #endif /* CONFIG_SYSCTL */
index 831ea71082320e1859daa1f434008816c3052333..f3b0bb4ac3ba7c02fec473e4b54001f29de87c54 100644 (file)
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
 
+static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
+{
+       return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
+}
+
+static void dec_uts_namespaces(struct ucounts *ucounts)
+{
+       dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
+}
+
 static struct uts_namespace *create_uts_ns(void)
 {
        struct uts_namespace *uts_ns;
@@ -36,18 +46,24 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
                                          struct uts_namespace *old_ns)
 {
        struct uts_namespace *ns;
+       struct ucounts *ucounts;
        int err;
 
+       err = -ENFILE;
+       ucounts = inc_uts_namespaces(user_ns);
+       if (!ucounts)
+               goto fail;
+
+       err = -ENOMEM;
        ns = create_uts_ns();
        if (!ns)
-               return ERR_PTR(-ENOMEM);
+               goto fail_dec;
 
        err = ns_alloc_inum(&ns->ns);
-       if (err) {
-               kfree(ns);
-               return ERR_PTR(err);
-       }
+       if (err)
+               goto fail_free;
 
+       ns->ucounts = ucounts;
        ns->ns.ops = &utsns_operations;
 
        down_read(&uts_sem);
@@ -55,6 +71,13 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
        ns->user_ns = get_user_ns(user_ns);
        up_read(&uts_sem);
        return ns;
+
+fail_free:
+       kfree(ns);
+fail_dec:
+       dec_uts_namespaces(ucounts);
+fail:
+       return ERR_PTR(err);
 }
 
 /*
@@ -85,6 +108,7 @@ void free_uts_ns(struct kref *kref)
        struct uts_namespace *ns;
 
        ns = container_of(kref, struct uts_namespace, kref);
+       dec_uts_namespaces(ns->ucounts);
        put_user_ns(ns->user_ns);
        ns_free_inum(&ns->ns);
        kfree(ns);