1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2dbec2846SEric W. Biederman 3dbec2846SEric W. Biederman #include <linux/stat.h> 4dbec2846SEric W. Biederman #include <linux/sysctl.h> 5dbec2846SEric W. Biederman #include <linux/slab.h> 65b825c3aSIngo Molnar #include <linux/cred.h> 7f6b2db1aSEric W. Biederman #include <linux/hash.h> 8514c6032SRandy Dunlap #include <linux/kmemleak.h> 9dbec2846SEric W. Biederman #include <linux/user_namespace.h> 10dbec2846SEric W. Biederman 11f6b2db1aSEric W. Biederman #define UCOUNTS_HASHTABLE_BITS 10 12f6b2db1aSEric W. Biederman static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)]; 13f6b2db1aSEric W. Biederman static DEFINE_SPINLOCK(ucounts_lock); 14f6b2db1aSEric W. Biederman 15f6b2db1aSEric W. Biederman #define ucounts_hashfn(ns, uid) \ 16f6b2db1aSEric W. Biederman hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \ 17f6b2db1aSEric W. Biederman UCOUNTS_HASHTABLE_BITS) 18f6b2db1aSEric W. Biederman #define ucounts_hashentry(ns, uid) \ 19f6b2db1aSEric W. Biederman (ucounts_hashtable + ucounts_hashfn(ns, uid)) 20f6b2db1aSEric W. Biederman 21f6b2db1aSEric W. Biederman 22dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 23dbec2846SEric W. Biederman static struct ctl_table_set * 24dbec2846SEric W. Biederman set_lookup(struct ctl_table_root *root) 25dbec2846SEric W. Biederman { 26dbec2846SEric W. Biederman return ¤t_user_ns()->set; 27dbec2846SEric W. Biederman } 28dbec2846SEric W. Biederman 29dbec2846SEric W. Biederman static int set_is_seen(struct ctl_table_set *set) 30dbec2846SEric W. Biederman { 31dbec2846SEric W. Biederman return ¤t_user_ns()->set == set; 32dbec2846SEric W. Biederman } 33dbec2846SEric W. Biederman 34dbec2846SEric W. Biederman static int set_permissions(struct ctl_table_header *head, 35dbec2846SEric W. Biederman struct ctl_table *table) 36dbec2846SEric W. Biederman { 37dbec2846SEric W. Biederman struct user_namespace *user_ns = 38dbec2846SEric W. Biederman container_of(head->set, struct user_namespace, set); 39dbec2846SEric W. Biederman int mode; 40dbec2846SEric W. Biederman 41dbec2846SEric W. Biederman /* Allow users with CAP_SYS_RESOURCE unrestrained access */ 42dbec2846SEric W. Biederman if (ns_capable(user_ns, CAP_SYS_RESOURCE)) 43dbec2846SEric W. Biederman mode = (table->mode & S_IRWXU) >> 6; 44dbec2846SEric W. Biederman else 45dbec2846SEric W. Biederman /* Allow all others at most read-only access */ 46dbec2846SEric W. Biederman mode = table->mode & S_IROTH; 47dbec2846SEric W. Biederman return (mode << 6) | (mode << 3) | mode; 48dbec2846SEric W. Biederman } 49dbec2846SEric W. Biederman 50dbec2846SEric W. Biederman static struct ctl_table_root set_root = { 51dbec2846SEric W. Biederman .lookup = set_lookup, 52dbec2846SEric W. Biederman .permissions = set_permissions, 53dbec2846SEric W. Biederman }; 54dbec2846SEric W. Biederman 5525f9c081SEric W. Biederman #define UCOUNT_ENTRY(name) \ 5625f9c081SEric W. Biederman { \ 5725f9c081SEric W. Biederman .procname = name, \ 5825f9c081SEric W. Biederman .maxlen = sizeof(int), \ 5925f9c081SEric W. Biederman .mode = 0644, \ 6025f9c081SEric W. Biederman .proc_handler = proc_dointvec_minmax, \ 61*eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO, \ 62*eec4844fSMatteo Croce .extra2 = SYSCTL_INT_MAX, \ 6325f9c081SEric W. Biederman } 64f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = { 6525f9c081SEric W. Biederman UCOUNT_ENTRY("max_user_namespaces"), 66f333c700SEric W. Biederman UCOUNT_ENTRY("max_pid_namespaces"), 67f7af3d1cSEric W. Biederman UCOUNT_ENTRY("max_uts_namespaces"), 68aba35661SEric W. Biederman UCOUNT_ENTRY("max_ipc_namespaces"), 6970328660SEric W. Biederman UCOUNT_ENTRY("max_net_namespaces"), 70537f7ccbSEric W. Biederman UCOUNT_ENTRY("max_mnt_namespaces"), 71d08311ddSEric W. Biederman UCOUNT_ENTRY("max_cgroup_namespaces"), 721cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER 731cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_instances"), 741cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_watches"), 751cce1eeaSNikolay Borisov #endif 76dbec2846SEric W. Biederman { } 77dbec2846SEric W. Biederman }; 78dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */ 79dbec2846SEric W. Biederman 80dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns) 81dbec2846SEric W. Biederman { 82dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 83dbec2846SEric W. Biederman struct ctl_table *tbl; 84dbec2846SEric W. Biederman setup_sysctl_set(&ns->set, &set_root, set_is_seen); 85f6b2db1aSEric W. Biederman tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL); 86dbec2846SEric W. Biederman if (tbl) { 8725f9c081SEric W. Biederman int i; 8825f9c081SEric W. Biederman for (i = 0; i < UCOUNT_COUNTS; i++) { 8925f9c081SEric W. Biederman tbl[i].data = &ns->ucount_max[i]; 9025f9c081SEric W. Biederman } 91f6b2db1aSEric W. Biederman ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl); 92dbec2846SEric W. Biederman } 93dbec2846SEric W. Biederman if (!ns->sysctls) { 94dbec2846SEric W. Biederman kfree(tbl); 95dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 96dbec2846SEric W. Biederman return false; 97dbec2846SEric W. Biederman } 98dbec2846SEric W. Biederman #endif 99dbec2846SEric W. Biederman return true; 100dbec2846SEric W. Biederman } 101dbec2846SEric W. Biederman 102dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns) 103dbec2846SEric W. Biederman { 104dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 105dbec2846SEric W. Biederman struct ctl_table *tbl; 106dbec2846SEric W. Biederman 107dbec2846SEric W. Biederman tbl = ns->sysctls->ctl_table_arg; 108dbec2846SEric W. Biederman unregister_sysctl_table(ns->sysctls); 109dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 110dbec2846SEric W. Biederman kfree(tbl); 111dbec2846SEric W. Biederman #endif 112dbec2846SEric W. Biederman } 113dbec2846SEric W. Biederman 114f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent) 115f6b2db1aSEric W. Biederman { 116f6b2db1aSEric W. Biederman struct ucounts *ucounts; 117f6b2db1aSEric W. Biederman 118f6b2db1aSEric W. Biederman hlist_for_each_entry(ucounts, hashent, node) { 119f6b2db1aSEric W. Biederman if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns)) 120f6b2db1aSEric W. Biederman return ucounts; 121f6b2db1aSEric W. Biederman } 122f6b2db1aSEric W. Biederman return NULL; 123f6b2db1aSEric W. Biederman } 124f6b2db1aSEric W. Biederman 125f6b2db1aSEric W. Biederman static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) 126f6b2db1aSEric W. Biederman { 127f6b2db1aSEric W. Biederman struct hlist_head *hashent = ucounts_hashentry(ns, uid); 128f6b2db1aSEric W. Biederman struct ucounts *ucounts, *new; 129f6b2db1aSEric W. Biederman 130880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 131f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 132f6b2db1aSEric W. Biederman if (!ucounts) { 133880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 134f6b2db1aSEric W. Biederman 135f6b2db1aSEric W. Biederman new = kzalloc(sizeof(*new), GFP_KERNEL); 136f6b2db1aSEric W. Biederman if (!new) 137f6b2db1aSEric W. Biederman return NULL; 138f6b2db1aSEric W. Biederman 139f6b2db1aSEric W. Biederman new->ns = ns; 140f6b2db1aSEric W. Biederman new->uid = uid; 141040757f7SEric W. Biederman new->count = 0; 142f6b2db1aSEric W. Biederman 143880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 144f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 145f6b2db1aSEric W. Biederman if (ucounts) { 146f6b2db1aSEric W. Biederman kfree(new); 147f6b2db1aSEric W. Biederman } else { 148f6b2db1aSEric W. Biederman hlist_add_head(&new->node, hashent); 149f6b2db1aSEric W. Biederman ucounts = new; 150f6b2db1aSEric W. Biederman } 151f6b2db1aSEric W. Biederman } 152040757f7SEric W. Biederman if (ucounts->count == INT_MAX) 153f6b2db1aSEric W. Biederman ucounts = NULL; 154040757f7SEric W. Biederman else 155040757f7SEric W. Biederman ucounts->count += 1; 156880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 157f6b2db1aSEric W. Biederman return ucounts; 158f6b2db1aSEric W. Biederman } 159f6b2db1aSEric W. Biederman 160f6b2db1aSEric W. Biederman static void put_ucounts(struct ucounts *ucounts) 161f6b2db1aSEric W. Biederman { 162880a3854SNikolay Borisov unsigned long flags; 163880a3854SNikolay Borisov 164880a3854SNikolay Borisov spin_lock_irqsave(&ucounts_lock, flags); 165040757f7SEric W. Biederman ucounts->count -= 1; 166040757f7SEric W. Biederman if (!ucounts->count) 167f6b2db1aSEric W. Biederman hlist_del_init(&ucounts->node); 168040757f7SEric W. Biederman else 169040757f7SEric W. Biederman ucounts = NULL; 170880a3854SNikolay Borisov spin_unlock_irqrestore(&ucounts_lock, flags); 171f6b2db1aSEric W. Biederman 172f6b2db1aSEric W. Biederman kfree(ucounts); 173f6b2db1aSEric W. Biederman } 174f6b2db1aSEric W. Biederman 175b376c3e1SEric W. Biederman static inline bool atomic_inc_below(atomic_t *v, int u) 176b376c3e1SEric W. Biederman { 177b376c3e1SEric W. Biederman int c, old; 178b376c3e1SEric W. Biederman c = atomic_read(v); 179b376c3e1SEric W. Biederman for (;;) { 180b376c3e1SEric W. Biederman if (unlikely(c >= u)) 181b376c3e1SEric W. Biederman return false; 182b376c3e1SEric W. Biederman old = atomic_cmpxchg(v, c, c+1); 183b376c3e1SEric W. Biederman if (likely(old == c)) 184b376c3e1SEric W. Biederman return true; 185b376c3e1SEric W. Biederman c = old; 186b376c3e1SEric W. Biederman } 187b376c3e1SEric W. Biederman } 188b376c3e1SEric W. Biederman 18925f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, 19025f9c081SEric W. Biederman enum ucount_type type) 191b376c3e1SEric W. Biederman { 192f6b2db1aSEric W. Biederman struct ucounts *ucounts, *iter, *bad; 193f6b2db1aSEric W. Biederman struct user_namespace *tns; 194f6b2db1aSEric W. Biederman ucounts = get_ucounts(ns, uid); 195f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = tns->ucounts) { 196f6b2db1aSEric W. Biederman int max; 197f6b2db1aSEric W. Biederman tns = iter->ns; 19825f9c081SEric W. Biederman max = READ_ONCE(tns->ucount_max[type]); 19925f9c081SEric W. Biederman if (!atomic_inc_below(&iter->ucount[type], max)) 200b376c3e1SEric W. Biederman goto fail; 201b376c3e1SEric W. Biederman } 202f6b2db1aSEric W. Biederman return ucounts; 203b376c3e1SEric W. Biederman fail: 204f6b2db1aSEric W. Biederman bad = iter; 205f6b2db1aSEric W. Biederman for (iter = ucounts; iter != bad; iter = iter->ns->ucounts) 20625f9c081SEric W. Biederman atomic_dec(&iter->ucount[type]); 207b376c3e1SEric W. Biederman 208f6b2db1aSEric W. Biederman put_ucounts(ucounts); 209f6b2db1aSEric W. Biederman return NULL; 210b376c3e1SEric W. Biederman } 211b376c3e1SEric W. Biederman 21225f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type) 213b376c3e1SEric W. Biederman { 214f6b2db1aSEric W. Biederman struct ucounts *iter; 215f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = iter->ns->ucounts) { 21625f9c081SEric W. Biederman int dec = atomic_dec_if_positive(&iter->ucount[type]); 217b376c3e1SEric W. Biederman WARN_ON_ONCE(dec < 0); 218b376c3e1SEric W. Biederman } 219f6b2db1aSEric W. Biederman put_ucounts(ucounts); 220b376c3e1SEric W. Biederman } 221b376c3e1SEric W. Biederman 222dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void) 223dbec2846SEric W. Biederman { 224dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 225f6b2db1aSEric W. Biederman static struct ctl_table_header *user_header; 226dbec2846SEric W. Biederman static struct ctl_table empty[1]; 227dbec2846SEric W. Biederman /* 228f6b2db1aSEric W. Biederman * It is necessary to register the user directory in the 229dbec2846SEric W. Biederman * default set so that registrations in the child sets work 230dbec2846SEric W. Biederman * properly. 231dbec2846SEric W. Biederman */ 232f6b2db1aSEric W. Biederman user_header = register_sysctl("user", empty); 233ed5bd7dcSLuis R. Rodriguez kmemleak_ignore(user_header); 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); 240