1dbec2846SEric W. Biederman /* 2dbec2846SEric W. Biederman * This program is free software; you can redistribute it and/or 3dbec2846SEric W. Biederman * modify it under the terms of the GNU General Public License as 4dbec2846SEric W. Biederman * published by the Free Software Foundation, version 2 of the 5dbec2846SEric W. Biederman * License. 6dbec2846SEric W. Biederman */ 7dbec2846SEric W. Biederman 8dbec2846SEric W. Biederman #include <linux/stat.h> 9dbec2846SEric W. Biederman #include <linux/sysctl.h> 10dbec2846SEric W. Biederman #include <linux/slab.h> 11f6b2db1aSEric W. Biederman #include <linux/hash.h> 12dbec2846SEric W. Biederman #include <linux/user_namespace.h> 13dbec2846SEric W. Biederman 14f6b2db1aSEric W. Biederman #define UCOUNTS_HASHTABLE_BITS 10 15f6b2db1aSEric W. Biederman static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)]; 16f6b2db1aSEric W. Biederman static DEFINE_SPINLOCK(ucounts_lock); 17f6b2db1aSEric W. Biederman 18f6b2db1aSEric W. Biederman #define ucounts_hashfn(ns, uid) \ 19f6b2db1aSEric W. Biederman hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \ 20f6b2db1aSEric W. Biederman UCOUNTS_HASHTABLE_BITS) 21f6b2db1aSEric W. Biederman #define ucounts_hashentry(ns, uid) \ 22f6b2db1aSEric W. Biederman (ucounts_hashtable + ucounts_hashfn(ns, uid)) 23f6b2db1aSEric W. Biederman 24f6b2db1aSEric W. Biederman 25dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 26dbec2846SEric W. Biederman static struct ctl_table_set * 27dbec2846SEric W. Biederman set_lookup(struct ctl_table_root *root) 28dbec2846SEric W. Biederman { 29dbec2846SEric W. Biederman return ¤t_user_ns()->set; 30dbec2846SEric W. Biederman } 31dbec2846SEric W. Biederman 32dbec2846SEric W. Biederman static int set_is_seen(struct ctl_table_set *set) 33dbec2846SEric W. Biederman { 34dbec2846SEric W. Biederman return ¤t_user_ns()->set == set; 35dbec2846SEric W. Biederman } 36dbec2846SEric W. Biederman 37dbec2846SEric W. Biederman static int set_permissions(struct ctl_table_header *head, 38dbec2846SEric W. Biederman struct ctl_table *table) 39dbec2846SEric W. Biederman { 40dbec2846SEric W. Biederman struct user_namespace *user_ns = 41dbec2846SEric W. Biederman container_of(head->set, struct user_namespace, set); 42dbec2846SEric W. Biederman int mode; 43dbec2846SEric W. Biederman 44dbec2846SEric W. Biederman /* Allow users with CAP_SYS_RESOURCE unrestrained access */ 45dbec2846SEric W. Biederman if (ns_capable(user_ns, CAP_SYS_RESOURCE)) 46dbec2846SEric W. Biederman mode = (table->mode & S_IRWXU) >> 6; 47dbec2846SEric W. Biederman else 48dbec2846SEric W. Biederman /* Allow all others at most read-only access */ 49dbec2846SEric W. Biederman mode = table->mode & S_IROTH; 50dbec2846SEric W. Biederman return (mode << 6) | (mode << 3) | mode; 51dbec2846SEric W. Biederman } 52dbec2846SEric W. Biederman 53dbec2846SEric W. Biederman static struct ctl_table_root set_root = { 54dbec2846SEric W. Biederman .lookup = set_lookup, 55dbec2846SEric W. Biederman .permissions = set_permissions, 56dbec2846SEric W. Biederman }; 57dbec2846SEric W. Biederman 58b376c3e1SEric W. Biederman static int zero = 0; 59b376c3e1SEric W. Biederman static int int_max = INT_MAX; 6025f9c081SEric W. Biederman #define UCOUNT_ENTRY(name) \ 6125f9c081SEric W. Biederman { \ 6225f9c081SEric W. Biederman .procname = name, \ 6325f9c081SEric W. Biederman .maxlen = sizeof(int), \ 6425f9c081SEric W. Biederman .mode = 0644, \ 6525f9c081SEric W. Biederman .proc_handler = proc_dointvec_minmax, \ 6625f9c081SEric W. Biederman .extra1 = &zero, \ 6725f9c081SEric W. Biederman .extra2 = &int_max, \ 6825f9c081SEric W. Biederman } 69f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = { 7025f9c081SEric W. Biederman UCOUNT_ENTRY("max_user_namespaces"), 71f333c700SEric W. Biederman UCOUNT_ENTRY("max_pid_namespaces"), 72f7af3d1cSEric W. Biederman UCOUNT_ENTRY("max_uts_namespaces"), 73aba35661SEric W. Biederman UCOUNT_ENTRY("max_ipc_namespaces"), 7470328660SEric W. Biederman UCOUNT_ENTRY("max_net_namespaces"), 75537f7ccbSEric W. Biederman UCOUNT_ENTRY("max_mnt_namespaces"), 76d08311ddSEric W. Biederman UCOUNT_ENTRY("max_cgroup_namespaces"), 77*1cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER 78*1cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_instances"), 79*1cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_watches"), 80*1cce1eeaSNikolay Borisov #endif 81dbec2846SEric W. Biederman { } 82dbec2846SEric W. Biederman }; 83dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */ 84dbec2846SEric W. Biederman 85dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns) 86dbec2846SEric W. Biederman { 87dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 88dbec2846SEric W. Biederman struct ctl_table *tbl; 89dbec2846SEric W. Biederman setup_sysctl_set(&ns->set, &set_root, set_is_seen); 90f6b2db1aSEric W. Biederman tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL); 91dbec2846SEric W. Biederman if (tbl) { 9225f9c081SEric W. Biederman int i; 9325f9c081SEric W. Biederman for (i = 0; i < UCOUNT_COUNTS; i++) { 9425f9c081SEric W. Biederman tbl[i].data = &ns->ucount_max[i]; 9525f9c081SEric W. Biederman } 96f6b2db1aSEric W. Biederman ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl); 97dbec2846SEric W. Biederman } 98dbec2846SEric W. Biederman if (!ns->sysctls) { 99dbec2846SEric W. Biederman kfree(tbl); 100dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 101dbec2846SEric W. Biederman return false; 102dbec2846SEric W. Biederman } 103dbec2846SEric W. Biederman #endif 104dbec2846SEric W. Biederman return true; 105dbec2846SEric W. Biederman } 106dbec2846SEric W. Biederman 107dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns) 108dbec2846SEric W. Biederman { 109dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 110dbec2846SEric W. Biederman struct ctl_table *tbl; 111dbec2846SEric W. Biederman 112dbec2846SEric W. Biederman tbl = ns->sysctls->ctl_table_arg; 113dbec2846SEric W. Biederman unregister_sysctl_table(ns->sysctls); 114dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 115dbec2846SEric W. Biederman kfree(tbl); 116dbec2846SEric W. Biederman #endif 117dbec2846SEric W. Biederman } 118dbec2846SEric W. Biederman 119f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent) 120f6b2db1aSEric W. Biederman { 121f6b2db1aSEric W. Biederman struct ucounts *ucounts; 122f6b2db1aSEric W. Biederman 123f6b2db1aSEric W. Biederman hlist_for_each_entry(ucounts, hashent, node) { 124f6b2db1aSEric W. Biederman if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns)) 125f6b2db1aSEric W. Biederman return ucounts; 126f6b2db1aSEric W. Biederman } 127f6b2db1aSEric W. Biederman return NULL; 128f6b2db1aSEric W. Biederman } 129f6b2db1aSEric W. Biederman 130f6b2db1aSEric W. Biederman static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) 131f6b2db1aSEric W. Biederman { 132f6b2db1aSEric W. Biederman struct hlist_head *hashent = ucounts_hashentry(ns, uid); 133f6b2db1aSEric W. Biederman struct ucounts *ucounts, *new; 134f6b2db1aSEric W. Biederman 135880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 136f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 137f6b2db1aSEric W. Biederman if (!ucounts) { 138880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 139f6b2db1aSEric W. Biederman 140f6b2db1aSEric W. Biederman new = kzalloc(sizeof(*new), GFP_KERNEL); 141f6b2db1aSEric W. Biederman if (!new) 142f6b2db1aSEric W. Biederman return NULL; 143f6b2db1aSEric W. Biederman 144f6b2db1aSEric W. Biederman new->ns = ns; 145f6b2db1aSEric W. Biederman new->uid = uid; 146f6b2db1aSEric W. Biederman atomic_set(&new->count, 0); 147f6b2db1aSEric W. Biederman 148880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 149f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 150f6b2db1aSEric W. Biederman if (ucounts) { 151f6b2db1aSEric W. Biederman kfree(new); 152f6b2db1aSEric W. Biederman } else { 153f6b2db1aSEric W. Biederman hlist_add_head(&new->node, hashent); 154f6b2db1aSEric W. Biederman ucounts = new; 155f6b2db1aSEric W. Biederman } 156f6b2db1aSEric W. Biederman } 157f6b2db1aSEric W. Biederman if (!atomic_add_unless(&ucounts->count, 1, INT_MAX)) 158f6b2db1aSEric W. Biederman ucounts = NULL; 159880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 160f6b2db1aSEric W. Biederman return ucounts; 161f6b2db1aSEric W. Biederman } 162f6b2db1aSEric W. Biederman 163f6b2db1aSEric W. Biederman static void put_ucounts(struct ucounts *ucounts) 164f6b2db1aSEric W. Biederman { 165880a3854SNikolay Borisov unsigned long flags; 166880a3854SNikolay Borisov 167f6b2db1aSEric W. Biederman if (atomic_dec_and_test(&ucounts->count)) { 168880a3854SNikolay Borisov spin_lock_irqsave(&ucounts_lock, flags); 169f6b2db1aSEric W. Biederman hlist_del_init(&ucounts->node); 170880a3854SNikolay Borisov spin_unlock_irqrestore(&ucounts_lock, flags); 171f6b2db1aSEric W. Biederman 172f6b2db1aSEric W. Biederman kfree(ucounts); 173f6b2db1aSEric W. Biederman } 174f6b2db1aSEric W. Biederman } 175f6b2db1aSEric W. Biederman 176b376c3e1SEric W. Biederman static inline bool atomic_inc_below(atomic_t *v, int u) 177b376c3e1SEric W. Biederman { 178b376c3e1SEric W. Biederman int c, old; 179b376c3e1SEric W. Biederman c = atomic_read(v); 180b376c3e1SEric W. Biederman for (;;) { 181b376c3e1SEric W. Biederman if (unlikely(c >= u)) 182b376c3e1SEric W. Biederman return false; 183b376c3e1SEric W. Biederman old = atomic_cmpxchg(v, c, c+1); 184b376c3e1SEric W. Biederman if (likely(old == c)) 185b376c3e1SEric W. Biederman return true; 186b376c3e1SEric W. Biederman c = old; 187b376c3e1SEric W. Biederman } 188b376c3e1SEric W. Biederman } 189b376c3e1SEric W. Biederman 19025f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, 19125f9c081SEric W. Biederman enum ucount_type type) 192b376c3e1SEric W. Biederman { 193f6b2db1aSEric W. Biederman struct ucounts *ucounts, *iter, *bad; 194f6b2db1aSEric W. Biederman struct user_namespace *tns; 195f6b2db1aSEric W. Biederman ucounts = get_ucounts(ns, uid); 196f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = tns->ucounts) { 197f6b2db1aSEric W. Biederman int max; 198f6b2db1aSEric W. Biederman tns = iter->ns; 19925f9c081SEric W. Biederman max = READ_ONCE(tns->ucount_max[type]); 20025f9c081SEric W. Biederman if (!atomic_inc_below(&iter->ucount[type], max)) 201b376c3e1SEric W. Biederman goto fail; 202b376c3e1SEric W. Biederman } 203f6b2db1aSEric W. Biederman return ucounts; 204b376c3e1SEric W. Biederman fail: 205f6b2db1aSEric W. Biederman bad = iter; 206f6b2db1aSEric W. Biederman for (iter = ucounts; iter != bad; iter = iter->ns->ucounts) 20725f9c081SEric W. Biederman atomic_dec(&iter->ucount[type]); 208b376c3e1SEric W. Biederman 209f6b2db1aSEric W. Biederman put_ucounts(ucounts); 210f6b2db1aSEric W. Biederman return NULL; 211b376c3e1SEric W. Biederman } 212b376c3e1SEric W. Biederman 21325f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type) 214b376c3e1SEric W. Biederman { 215f6b2db1aSEric W. Biederman struct ucounts *iter; 216f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = iter->ns->ucounts) { 21725f9c081SEric W. Biederman int dec = atomic_dec_if_positive(&iter->ucount[type]); 218b376c3e1SEric W. Biederman WARN_ON_ONCE(dec < 0); 219b376c3e1SEric W. Biederman } 220f6b2db1aSEric W. Biederman put_ucounts(ucounts); 221b376c3e1SEric W. Biederman } 222b376c3e1SEric W. Biederman 223dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void) 224dbec2846SEric W. Biederman { 225dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 226f6b2db1aSEric W. Biederman static struct ctl_table_header *user_header; 227dbec2846SEric W. Biederman static struct ctl_table empty[1]; 228dbec2846SEric W. Biederman /* 229f6b2db1aSEric W. Biederman * It is necessary to register the user directory in the 230dbec2846SEric W. Biederman * default set so that registrations in the child sets work 231dbec2846SEric W. Biederman * properly. 232dbec2846SEric W. Biederman */ 233f6b2db1aSEric W. Biederman user_header = register_sysctl("user", empty); 234f6b2db1aSEric W. Biederman BUG_ON(!user_header); 235dbec2846SEric W. Biederman BUG_ON(!setup_userns_sysctls(&init_user_ns)); 236dbec2846SEric W. Biederman #endif 237dbec2846SEric W. Biederman return 0; 238dbec2846SEric W. Biederman } 239dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init); 240dbec2846SEric W. Biederman 241dbec2846SEric W. Biederman 242