1*b886d83cSThomas 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 55b376c3e1SEric W. Biederman static int zero = 0; 56b376c3e1SEric W. Biederman static int int_max = INT_MAX; 5725f9c081SEric W. Biederman #define UCOUNT_ENTRY(name) \ 5825f9c081SEric W. Biederman { \ 5925f9c081SEric W. Biederman .procname = name, \ 6025f9c081SEric W. Biederman .maxlen = sizeof(int), \ 6125f9c081SEric W. Biederman .mode = 0644, \ 6225f9c081SEric W. Biederman .proc_handler = proc_dointvec_minmax, \ 6325f9c081SEric W. Biederman .extra1 = &zero, \ 6425f9c081SEric W. Biederman .extra2 = &int_max, \ 6525f9c081SEric W. Biederman } 66f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = { 6725f9c081SEric W. Biederman UCOUNT_ENTRY("max_user_namespaces"), 68f333c700SEric W. Biederman UCOUNT_ENTRY("max_pid_namespaces"), 69f7af3d1cSEric W. Biederman UCOUNT_ENTRY("max_uts_namespaces"), 70aba35661SEric W. Biederman UCOUNT_ENTRY("max_ipc_namespaces"), 7170328660SEric W. Biederman UCOUNT_ENTRY("max_net_namespaces"), 72537f7ccbSEric W. Biederman UCOUNT_ENTRY("max_mnt_namespaces"), 73d08311ddSEric W. Biederman UCOUNT_ENTRY("max_cgroup_namespaces"), 741cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER 751cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_instances"), 761cce1eeaSNikolay Borisov UCOUNT_ENTRY("max_inotify_watches"), 771cce1eeaSNikolay Borisov #endif 78dbec2846SEric W. Biederman { } 79dbec2846SEric W. Biederman }; 80dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */ 81dbec2846SEric W. Biederman 82dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns) 83dbec2846SEric W. Biederman { 84dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 85dbec2846SEric W. Biederman struct ctl_table *tbl; 86dbec2846SEric W. Biederman setup_sysctl_set(&ns->set, &set_root, set_is_seen); 87f6b2db1aSEric W. Biederman tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL); 88dbec2846SEric W. Biederman if (tbl) { 8925f9c081SEric W. Biederman int i; 9025f9c081SEric W. Biederman for (i = 0; i < UCOUNT_COUNTS; i++) { 9125f9c081SEric W. Biederman tbl[i].data = &ns->ucount_max[i]; 9225f9c081SEric W. Biederman } 93f6b2db1aSEric W. Biederman ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl); 94dbec2846SEric W. Biederman } 95dbec2846SEric W. Biederman if (!ns->sysctls) { 96dbec2846SEric W. Biederman kfree(tbl); 97dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 98dbec2846SEric W. Biederman return false; 99dbec2846SEric W. Biederman } 100dbec2846SEric W. Biederman #endif 101dbec2846SEric W. Biederman return true; 102dbec2846SEric W. Biederman } 103dbec2846SEric W. Biederman 104dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns) 105dbec2846SEric W. Biederman { 106dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 107dbec2846SEric W. Biederman struct ctl_table *tbl; 108dbec2846SEric W. Biederman 109dbec2846SEric W. Biederman tbl = ns->sysctls->ctl_table_arg; 110dbec2846SEric W. Biederman unregister_sysctl_table(ns->sysctls); 111dbec2846SEric W. Biederman retire_sysctl_set(&ns->set); 112dbec2846SEric W. Biederman kfree(tbl); 113dbec2846SEric W. Biederman #endif 114dbec2846SEric W. Biederman } 115dbec2846SEric W. Biederman 116f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent) 117f6b2db1aSEric W. Biederman { 118f6b2db1aSEric W. Biederman struct ucounts *ucounts; 119f6b2db1aSEric W. Biederman 120f6b2db1aSEric W. Biederman hlist_for_each_entry(ucounts, hashent, node) { 121f6b2db1aSEric W. Biederman if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns)) 122f6b2db1aSEric W. Biederman return ucounts; 123f6b2db1aSEric W. Biederman } 124f6b2db1aSEric W. Biederman return NULL; 125f6b2db1aSEric W. Biederman } 126f6b2db1aSEric W. Biederman 127f6b2db1aSEric W. Biederman static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) 128f6b2db1aSEric W. Biederman { 129f6b2db1aSEric W. Biederman struct hlist_head *hashent = ucounts_hashentry(ns, uid); 130f6b2db1aSEric W. Biederman struct ucounts *ucounts, *new; 131f6b2db1aSEric W. Biederman 132880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 133f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 134f6b2db1aSEric W. Biederman if (!ucounts) { 135880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 136f6b2db1aSEric W. Biederman 137f6b2db1aSEric W. Biederman new = kzalloc(sizeof(*new), GFP_KERNEL); 138f6b2db1aSEric W. Biederman if (!new) 139f6b2db1aSEric W. Biederman return NULL; 140f6b2db1aSEric W. Biederman 141f6b2db1aSEric W. Biederman new->ns = ns; 142f6b2db1aSEric W. Biederman new->uid = uid; 143040757f7SEric W. Biederman new->count = 0; 144f6b2db1aSEric W. Biederman 145880a3854SNikolay Borisov spin_lock_irq(&ucounts_lock); 146f6b2db1aSEric W. Biederman ucounts = find_ucounts(ns, uid, hashent); 147f6b2db1aSEric W. Biederman if (ucounts) { 148f6b2db1aSEric W. Biederman kfree(new); 149f6b2db1aSEric W. Biederman } else { 150f6b2db1aSEric W. Biederman hlist_add_head(&new->node, hashent); 151f6b2db1aSEric W. Biederman ucounts = new; 152f6b2db1aSEric W. Biederman } 153f6b2db1aSEric W. Biederman } 154040757f7SEric W. Biederman if (ucounts->count == INT_MAX) 155f6b2db1aSEric W. Biederman ucounts = NULL; 156040757f7SEric W. Biederman else 157040757f7SEric W. Biederman ucounts->count += 1; 158880a3854SNikolay Borisov spin_unlock_irq(&ucounts_lock); 159f6b2db1aSEric W. Biederman return ucounts; 160f6b2db1aSEric W. Biederman } 161f6b2db1aSEric W. Biederman 162f6b2db1aSEric W. Biederman static void put_ucounts(struct ucounts *ucounts) 163f6b2db1aSEric W. Biederman { 164880a3854SNikolay Borisov unsigned long flags; 165880a3854SNikolay Borisov 166880a3854SNikolay Borisov spin_lock_irqsave(&ucounts_lock, flags); 167040757f7SEric W. Biederman ucounts->count -= 1; 168040757f7SEric W. Biederman if (!ucounts->count) 169f6b2db1aSEric W. Biederman hlist_del_init(&ucounts->node); 170040757f7SEric W. Biederman else 171040757f7SEric W. Biederman ucounts = NULL; 172880a3854SNikolay Borisov spin_unlock_irqrestore(&ucounts_lock, flags); 173f6b2db1aSEric W. Biederman 174f6b2db1aSEric W. Biederman kfree(ucounts); 175f6b2db1aSEric W. Biederman } 176f6b2db1aSEric W. Biederman 177b376c3e1SEric W. Biederman static inline bool atomic_inc_below(atomic_t *v, int u) 178b376c3e1SEric W. Biederman { 179b376c3e1SEric W. Biederman int c, old; 180b376c3e1SEric W. Biederman c = atomic_read(v); 181b376c3e1SEric W. Biederman for (;;) { 182b376c3e1SEric W. Biederman if (unlikely(c >= u)) 183b376c3e1SEric W. Biederman return false; 184b376c3e1SEric W. Biederman old = atomic_cmpxchg(v, c, c+1); 185b376c3e1SEric W. Biederman if (likely(old == c)) 186b376c3e1SEric W. Biederman return true; 187b376c3e1SEric W. Biederman c = old; 188b376c3e1SEric W. Biederman } 189b376c3e1SEric W. Biederman } 190b376c3e1SEric W. Biederman 19125f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, 19225f9c081SEric W. Biederman enum ucount_type type) 193b376c3e1SEric W. Biederman { 194f6b2db1aSEric W. Biederman struct ucounts *ucounts, *iter, *bad; 195f6b2db1aSEric W. Biederman struct user_namespace *tns; 196f6b2db1aSEric W. Biederman ucounts = get_ucounts(ns, uid); 197f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = tns->ucounts) { 198f6b2db1aSEric W. Biederman int max; 199f6b2db1aSEric W. Biederman tns = iter->ns; 20025f9c081SEric W. Biederman max = READ_ONCE(tns->ucount_max[type]); 20125f9c081SEric W. Biederman if (!atomic_inc_below(&iter->ucount[type], max)) 202b376c3e1SEric W. Biederman goto fail; 203b376c3e1SEric W. Biederman } 204f6b2db1aSEric W. Biederman return ucounts; 205b376c3e1SEric W. Biederman fail: 206f6b2db1aSEric W. Biederman bad = iter; 207f6b2db1aSEric W. Biederman for (iter = ucounts; iter != bad; iter = iter->ns->ucounts) 20825f9c081SEric W. Biederman atomic_dec(&iter->ucount[type]); 209b376c3e1SEric W. Biederman 210f6b2db1aSEric W. Biederman put_ucounts(ucounts); 211f6b2db1aSEric W. Biederman return NULL; 212b376c3e1SEric W. Biederman } 213b376c3e1SEric W. Biederman 21425f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type) 215b376c3e1SEric W. Biederman { 216f6b2db1aSEric W. Biederman struct ucounts *iter; 217f6b2db1aSEric W. Biederman for (iter = ucounts; iter; iter = iter->ns->ucounts) { 21825f9c081SEric W. Biederman int dec = atomic_dec_if_positive(&iter->ucount[type]); 219b376c3e1SEric W. Biederman WARN_ON_ONCE(dec < 0); 220b376c3e1SEric W. Biederman } 221f6b2db1aSEric W. Biederman put_ucounts(ucounts); 222b376c3e1SEric W. Biederman } 223b376c3e1SEric W. Biederman 224dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void) 225dbec2846SEric W. Biederman { 226dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL 227f6b2db1aSEric W. Biederman static struct ctl_table_header *user_header; 228dbec2846SEric W. Biederman static struct ctl_table empty[1]; 229dbec2846SEric W. Biederman /* 230f6b2db1aSEric W. Biederman * It is necessary to register the user directory in the 231dbec2846SEric W. Biederman * default set so that registrations in the child sets work 232dbec2846SEric W. Biederman * properly. 233dbec2846SEric W. Biederman */ 234f6b2db1aSEric W. Biederman user_header = register_sysctl("user", empty); 235ed5bd7dcSLuis R. Rodriguez kmemleak_ignore(user_header); 236f6b2db1aSEric W. Biederman BUG_ON(!user_header); 237dbec2846SEric W. Biederman BUG_ON(!setup_userns_sysctls(&init_user_ns)); 238dbec2846SEric W. Biederman #endif 239dbec2846SEric W. Biederman return 0; 240dbec2846SEric W. Biederman } 241dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init); 242