Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * The "user cache".
0004  *
0005  * (C) Copyright 1991-2000 Linus Torvalds
0006  *
0007  * We have a per-user structure to keep track of how many
0008  * processes, files etc the user has claimed, in order to be
0009  * able to have per-user limits for system resources. 
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/sched.h>
0014 #include <linux/slab.h>
0015 #include <linux/bitops.h>
0016 #include <linux/key.h>
0017 #include <linux/sched/user.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/export.h>
0020 #include <linux/user_namespace.h>
0021 #include <linux/proc_ns.h>
0022 
0023 /*
0024  * userns count is 1 for root user, 1 for init_uts_ns,
0025  * and 1 for... ?
0026  */
0027 struct user_namespace init_user_ns = {
0028     .uid_map = {
0029         .nr_extents = 1,
0030         {
0031             .extent[0] = {
0032                 .first = 0,
0033                 .lower_first = 0,
0034                 .count = 4294967295U,
0035             },
0036         },
0037     },
0038     .gid_map = {
0039         .nr_extents = 1,
0040         {
0041             .extent[0] = {
0042                 .first = 0,
0043                 .lower_first = 0,
0044                 .count = 4294967295U,
0045             },
0046         },
0047     },
0048     .projid_map = {
0049         .nr_extents = 1,
0050         {
0051             .extent[0] = {
0052                 .first = 0,
0053                 .lower_first = 0,
0054                 .count = 4294967295U,
0055             },
0056         },
0057     },
0058     .ns.count = REFCOUNT_INIT(3),
0059     .owner = GLOBAL_ROOT_UID,
0060     .group = GLOBAL_ROOT_GID,
0061     .ns.inum = PROC_USER_INIT_INO,
0062 #ifdef CONFIG_USER_NS
0063     .ns.ops = &userns_operations,
0064 #endif
0065     .flags = USERNS_INIT_FLAGS,
0066 #ifdef CONFIG_KEYS
0067     .keyring_name_list = LIST_HEAD_INIT(init_user_ns.keyring_name_list),
0068     .keyring_sem = __RWSEM_INITIALIZER(init_user_ns.keyring_sem),
0069 #endif
0070 };
0071 EXPORT_SYMBOL_GPL(init_user_ns);
0072 
0073 /*
0074  * UID task count cache, to get fast user lookup in "alloc_uid"
0075  * when changing user ID's (ie setuid() and friends).
0076  */
0077 
0078 #define UIDHASH_BITS    (CONFIG_BASE_SMALL ? 3 : 7)
0079 #define UIDHASH_SZ  (1 << UIDHASH_BITS)
0080 #define UIDHASH_MASK        (UIDHASH_SZ - 1)
0081 #define __uidhashfn(uid)    (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
0082 #define uidhashentry(uid)   (uidhash_table + __uidhashfn((__kuid_val(uid))))
0083 
0084 static struct kmem_cache *uid_cachep;
0085 static struct hlist_head uidhash_table[UIDHASH_SZ];
0086 
0087 /*
0088  * The uidhash_lock is mostly taken from process context, but it is
0089  * occasionally also taken from softirq/tasklet context, when
0090  * task-structs get RCU-freed. Hence all locking must be softirq-safe.
0091  * But free_uid() is also called with local interrupts disabled, and running
0092  * local_bh_enable() with local interrupts disabled is an error - we'll run
0093  * softirq callbacks, and they can unconditionally enable interrupts, and
0094  * the caller of free_uid() didn't expect that..
0095  */
0096 static DEFINE_SPINLOCK(uidhash_lock);
0097 
0098 /* root_user.__count is 1, for init task cred */
0099 struct user_struct root_user = {
0100     .__count    = REFCOUNT_INIT(1),
0101     .uid        = GLOBAL_ROOT_UID,
0102     .ratelimit  = RATELIMIT_STATE_INIT(root_user.ratelimit, 0, 0),
0103 };
0104 
0105 /*
0106  * These routines must be called with the uidhash spinlock held!
0107  */
0108 static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
0109 {
0110     hlist_add_head(&up->uidhash_node, hashent);
0111 }
0112 
0113 static void uid_hash_remove(struct user_struct *up)
0114 {
0115     hlist_del_init(&up->uidhash_node);
0116 }
0117 
0118 static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
0119 {
0120     struct user_struct *user;
0121 
0122     hlist_for_each_entry(user, hashent, uidhash_node) {
0123         if (uid_eq(user->uid, uid)) {
0124             refcount_inc(&user->__count);
0125             return user;
0126         }
0127     }
0128 
0129     return NULL;
0130 }
0131 
0132 static int user_epoll_alloc(struct user_struct *up)
0133 {
0134 #ifdef CONFIG_EPOLL
0135     return percpu_counter_init(&up->epoll_watches, 0, GFP_KERNEL);
0136 #else
0137     return 0;
0138 #endif
0139 }
0140 
0141 static void user_epoll_free(struct user_struct *up)
0142 {
0143 #ifdef CONFIG_EPOLL
0144     percpu_counter_destroy(&up->epoll_watches);
0145 #endif
0146 }
0147 
0148 /* IRQs are disabled and uidhash_lock is held upon function entry.
0149  * IRQ state (as stored in flags) is restored and uidhash_lock released
0150  * upon function exit.
0151  */
0152 static void free_user(struct user_struct *up, unsigned long flags)
0153     __releases(&uidhash_lock)
0154 {
0155     uid_hash_remove(up);
0156     spin_unlock_irqrestore(&uidhash_lock, flags);
0157     user_epoll_free(up);
0158     kmem_cache_free(uid_cachep, up);
0159 }
0160 
0161 /*
0162  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
0163  * caller must undo that ref with free_uid().
0164  *
0165  * If the user_struct could not be found, return NULL.
0166  */
0167 struct user_struct *find_user(kuid_t uid)
0168 {
0169     struct user_struct *ret;
0170     unsigned long flags;
0171 
0172     spin_lock_irqsave(&uidhash_lock, flags);
0173     ret = uid_hash_find(uid, uidhashentry(uid));
0174     spin_unlock_irqrestore(&uidhash_lock, flags);
0175     return ret;
0176 }
0177 
0178 void free_uid(struct user_struct *up)
0179 {
0180     unsigned long flags;
0181 
0182     if (!up)
0183         return;
0184 
0185     if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
0186         free_user(up, flags);
0187 }
0188 
0189 struct user_struct *alloc_uid(kuid_t uid)
0190 {
0191     struct hlist_head *hashent = uidhashentry(uid);
0192     struct user_struct *up, *new;
0193 
0194     spin_lock_irq(&uidhash_lock);
0195     up = uid_hash_find(uid, hashent);
0196     spin_unlock_irq(&uidhash_lock);
0197 
0198     if (!up) {
0199         new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
0200         if (!new)
0201             return NULL;
0202 
0203         new->uid = uid;
0204         refcount_set(&new->__count, 1);
0205         if (user_epoll_alloc(new)) {
0206             kmem_cache_free(uid_cachep, new);
0207             return NULL;
0208         }
0209         ratelimit_state_init(&new->ratelimit, HZ, 100);
0210         ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE);
0211 
0212         /*
0213          * Before adding this, check whether we raced
0214          * on adding the same user already..
0215          */
0216         spin_lock_irq(&uidhash_lock);
0217         up = uid_hash_find(uid, hashent);
0218         if (up) {
0219             user_epoll_free(new);
0220             kmem_cache_free(uid_cachep, new);
0221         } else {
0222             uid_hash_insert(new, hashent);
0223             up = new;
0224         }
0225         spin_unlock_irq(&uidhash_lock);
0226     }
0227 
0228     return up;
0229 }
0230 
0231 static int __init uid_cache_init(void)
0232 {
0233     int n;
0234 
0235     uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
0236             0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
0237 
0238     for(n = 0; n < UIDHASH_SZ; ++n)
0239         INIT_HLIST_HEAD(uidhash_table + n);
0240 
0241     if (user_epoll_alloc(&root_user))
0242         panic("root_user epoll percpu counter alloc failed");
0243 
0244     /* Insert the root user immediately (init already runs as root) */
0245     spin_lock_irq(&uidhash_lock);
0246     uid_hash_insert(&root_user, uidhashentry(GLOBAL_ROOT_UID));
0247     spin_unlock_irq(&uidhash_lock);
0248 
0249     return 0;
0250 }
0251 subsys_initcall(uid_cache_init);