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

index d10e54f03c09661cc724cfb66965b79acf48cf3e..848e5796400e797a57fb0ab253c6accc215ed9d4 100644 (file)
@@ -58,6 +58,7 @@ struct ipc_namespace {
 
        /* user_ns which owns the ipc ns */
        struct user_namespace *user_ns;
+       struct ucounts *ucounts;
 
        struct ns_common ns;
 };
index f9df7dd2609ad08f734e6229f4000dc3d96089ea..e1d672186f007ba169133df202ecb84882448573 100644 (file)
@@ -28,6 +28,7 @@ enum ucount_type {
        UCOUNT_USER_NAMESPACES,
        UCOUNT_PID_NAMESPACES,
        UCOUNT_UTS_NAMESPACES,
+       UCOUNT_IPC_NAMESPACES,
        UCOUNT_COUNTS,
 };
 
index d87e6baa13238d11aca72f0edaae53ca1c220f61..7309142141358e5fd4d4164ec9221594336356a0 100644 (file)
 
 #include "util.h"
 
+static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
+{
+       return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
+}
+
+static void dec_ipc_namespaces(struct ucounts *ucounts)
+{
+       dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
+}
+
 static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
                                           struct ipc_namespace *old_ns)
 {
        struct ipc_namespace *ns;
+       struct ucounts *ucounts;
        int err;
 
+       err = -ENFILE;
+       ucounts = inc_ipc_namespaces(user_ns);
+       if (!ucounts)
+               goto fail;
+
+       err = -ENOMEM;
        ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
        if (ns == NULL)
-               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->ns.ops = &ipcns_operations;
 
        atomic_set(&ns->count, 1);
        ns->user_ns = get_user_ns(user_ns);
+       ns->ucounts = ucounts;
 
        err = mq_init_ns(ns);
-       if (err) {
-               put_user_ns(ns->user_ns);
-               ns_free_inum(&ns->ns);
-               kfree(ns);
-               return ERR_PTR(err);
-       }
+       if (err)
+               goto fail_put;
 
        sem_init_ns(ns);
        msg_init_ns(ns);
        shm_init_ns(ns);
 
        return ns;
+
+fail_put:
+       put_user_ns(ns->user_ns);
+       ns_free_inum(&ns->ns);
+fail_free:
+       kfree(ns);
+fail_dec:
+       dec_ipc_namespaces(ucounts);
+fail:
+       return ERR_PTR(err);
 }
 
 struct ipc_namespace *copy_ipcs(unsigned long flags,
@@ -96,6 +118,7 @@ static void free_ipc_ns(struct ipc_namespace *ns)
        msg_exit_ns(ns);
        shm_exit_ns(ns);
 
+       dec_ipc_namespaces(ns->ucounts);
        put_user_ns(ns->user_ns);
        ns_free_inum(&ns->ns);
        kfree(ns);
index 866850e2eb9292f15fa2ca977929f250a8d75ba6..fbab75424da625016f6e2395db24c3bc61dd52e4 100644 (file)
@@ -70,6 +70,7 @@ static struct ctl_table user_table[] = {
        UCOUNT_ENTRY("max_user_namespaces"),
        UCOUNT_ENTRY("max_pid_namespaces"),
        UCOUNT_ENTRY("max_uts_namespaces"),
+       UCOUNT_ENTRY("max_ipc_namespaces"),
        { }
 };
 #endif /* CONFIG_SYSCTL */