Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-microblaze.git] / kernel / ucount.c
index 8d8874f..bb51849 100644 (file)
@@ -8,6 +8,12 @@
 #include <linux/kmemleak.h>
 #include <linux/user_namespace.h>
 
+struct ucounts init_ucounts = {
+       .ns    = &init_user_ns,
+       .uid   = GLOBAL_ROOT_UID,
+       .count = ATOMIC_INIT(1),
+};
+
 #define UCOUNTS_HASHTABLE_BITS 10
 static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
 static DEFINE_SPINLOCK(ucounts_lock);
@@ -52,14 +58,17 @@ static struct ctl_table_root set_root = {
        .permissions = set_permissions,
 };
 
-#define UCOUNT_ENTRY(name)                             \
-       {                                               \
-               .procname       = name,                 \
-               .maxlen         = sizeof(int),          \
-               .mode           = 0644,                 \
-               .proc_handler   = proc_dointvec_minmax, \
-               .extra1         = SYSCTL_ZERO,          \
-               .extra2         = SYSCTL_INT_MAX,       \
+static long ue_zero = 0;
+static long ue_int_max = INT_MAX;
+
+#define UCOUNT_ENTRY(name)                                     \
+       {                                                       \
+               .procname       = name,                         \
+               .maxlen         = sizeof(long),                 \
+               .mode           = 0644,                         \
+               .proc_handler   = proc_doulongvec_minmax,       \
+               .extra1         = &ue_zero,                     \
+               .extra2         = &ue_int_max,                  \
        }
 static struct ctl_table user_table[] = {
        UCOUNT_ENTRY("max_user_namespaces"),
@@ -78,6 +87,10 @@ static struct ctl_table user_table[] = {
        UCOUNT_ENTRY("max_fanotify_groups"),
        UCOUNT_ENTRY("max_fanotify_marks"),
 #endif
+       { },
+       { },
+       { },
+       { },
        { }
 };
 #endif /* CONFIG_SYSCTL */
@@ -129,10 +142,28 @@ static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struc
        return NULL;
 }
 
-static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
+static void hlist_add_ucounts(struct ucounts *ucounts)
+{
+       struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
+       spin_lock_irq(&ucounts_lock);
+       hlist_add_head(&ucounts->node, hashent);
+       spin_unlock_irq(&ucounts_lock);
+}
+
+struct ucounts *get_ucounts(struct ucounts *ucounts)
+{
+       if (ucounts && atomic_add_negative(1, &ucounts->count)) {
+               put_ucounts(ucounts);
+               ucounts = NULL;
+       }
+       return ucounts;
+}
+
+struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 {
        struct hlist_head *hashent = ucounts_hashentry(ns, uid);
        struct ucounts *ucounts, *new;
+       long overflow;
 
        spin_lock_irq(&ucounts_lock);
        ucounts = find_ucounts(ns, uid, hashent);
@@ -145,7 +176,7 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
 
                new->ns = ns;
                new->uid = uid;
-               new->count = 0;
+               atomic_set(&new->count, 1);
 
                spin_lock_irq(&ucounts_lock);
                ucounts = find_ucounts(ns, uid, hashent);
@@ -153,40 +184,38 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
                        kfree(new);
                } else {
                        hlist_add_head(&new->node, hashent);
-                       ucounts = new;
+                       spin_unlock_irq(&ucounts_lock);
+                       return new;
                }
        }
-       if (ucounts->count == INT_MAX)
-               ucounts = NULL;
-       else
-               ucounts->count += 1;
+       overflow = atomic_add_negative(1, &ucounts->count);
        spin_unlock_irq(&ucounts_lock);
+       if (overflow) {
+               put_ucounts(ucounts);
+               return NULL;
+       }
        return ucounts;
 }
 
-static void put_ucounts(struct ucounts *ucounts)
+void put_ucounts(struct ucounts *ucounts)
 {
        unsigned long flags;
 
-       spin_lock_irqsave(&ucounts_lock, flags);
-       ucounts->count -= 1;
-       if (!ucounts->count)
+       if (atomic_dec_and_lock_irqsave(&ucounts->count, &ucounts_lock, flags)) {
                hlist_del_init(&ucounts->node);
-       else
-               ucounts = NULL;
-       spin_unlock_irqrestore(&ucounts_lock, flags);
-
-       kfree(ucounts);
+               spin_unlock_irqrestore(&ucounts_lock, flags);
+               kfree(ucounts);
+       }
 }
 
-static inline bool atomic_inc_below(atomic_t *v, int u)
+static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
 {
-       int c, old;
-       c = atomic_read(v);
+       long c, old;
+       c = atomic_long_read(v);
        for (;;) {
                if (unlikely(c >= u))
                        return false;
-               old = atomic_cmpxchg(v, c, c+1);
+               old = atomic_long_cmpxchg(v, c, c+1);
                if (likely(old == c))
                        return true;
                c = old;
@@ -198,19 +227,19 @@ struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
 {
        struct ucounts *ucounts, *iter, *bad;
        struct user_namespace *tns;
-       ucounts = get_ucounts(ns, uid);
+       ucounts = alloc_ucounts(ns, uid);
        for (iter = ucounts; iter; iter = tns->ucounts) {
-               int max;
+               long max;
                tns = iter->ns;
                max = READ_ONCE(tns->ucount_max[type]);
-               if (!atomic_inc_below(&iter->ucount[type], max))
+               if (!atomic_long_inc_below(&iter->ucount[type], max))
                        goto fail;
        }
        return ucounts;
 fail:
        bad = iter;
        for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
-               atomic_dec(&iter->ucount[type]);
+               atomic_long_dec(&iter->ucount[type]);
 
        put_ucounts(ucounts);
        return NULL;
@@ -220,12 +249,54 @@ void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
 {
        struct ucounts *iter;
        for (iter = ucounts; iter; iter = iter->ns->ucounts) {
-               int dec = atomic_dec_if_positive(&iter->ucount[type]);
+               long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
                WARN_ON_ONCE(dec < 0);
        }
        put_ucounts(ucounts);
 }
 
+long inc_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
+{
+       struct ucounts *iter;
+       long ret = 0;
+
+       for (iter = ucounts; iter; iter = iter->ns->ucounts) {
+               long max = READ_ONCE(iter->ns->ucount_max[type]);
+               long new = atomic_long_add_return(v, &iter->ucount[type]);
+               if (new < 0 || new > max)
+                       ret = LONG_MAX;
+               else if (iter == ucounts)
+                       ret = new;
+       }
+       return ret;
+}
+
+bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
+{
+       struct ucounts *iter;
+       long new = -1; /* Silence compiler warning */
+       for (iter = ucounts; iter; iter = iter->ns->ucounts) {
+               long dec = atomic_long_add_return(-v, &iter->ucount[type]);
+               WARN_ON_ONCE(dec < 0);
+               if (iter == ucounts)
+                       new = dec;
+       }
+       return (new == 0);
+}
+
+bool is_ucounts_overlimit(struct ucounts *ucounts, enum ucount_type type, unsigned long max)
+{
+       struct ucounts *iter;
+       if (get_ucounts_value(ucounts, type) > max)
+               return true;
+       for (iter = ucounts; iter; iter = iter->ns->ucounts) {
+               max = READ_ONCE(iter->ns->ucount_max[type]);
+               if (get_ucounts_value(iter, type) > max)
+                       return true;
+       }
+       return false;
+}
+
 static __init int user_namespace_sysctl_init(void)
 {
 #ifdef CONFIG_SYSCTL
@@ -241,6 +312,8 @@ static __init int user_namespace_sysctl_init(void)
        BUG_ON(!user_header);
        BUG_ON(!setup_userns_sysctls(&init_user_ns));
 #endif
+       hlist_add_ucounts(&init_ucounts);
+       inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
        return 0;
 }
 subsys_initcall(user_namespace_sysctl_init);