user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * The "user cache".
  3. *
  4. * (C) Copyright 1991-2000 Linus Torvalds
  5. *
  6. * We have a per-user structure to keep track of how many
  7. * processes, files etc the user has claimed, in order to be
  8. * able to have per-user limits for system resources.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/bitops.h>
  14. #include <linux/key.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/user_namespace.h>
  18. #include "cred-internals.h"
  19. struct user_namespace init_user_ns = {
  20. .kref = {
  21. .refcount = ATOMIC_INIT(2),
  22. },
  23. .root_user = &root_user,
  24. };
  25. EXPORT_SYMBOL_GPL(init_user_ns);
  26. /*
  27. * UID task count cache, to get fast user lookup in "alloc_uid"
  28. * when changing user ID's (ie setuid() and friends).
  29. */
  30. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  31. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  32. #define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
  33. static struct kmem_cache *uid_cachep;
  34. /*
  35. * The uidhash_lock is mostly taken from process context, but it is
  36. * occasionally also taken from softirq/tasklet context, when
  37. * task-structs get RCU-freed. Hence all locking must be softirq-safe.
  38. * But free_uid() is also called with local interrupts disabled, and running
  39. * local_bh_enable() with local interrupts disabled is an error - we'll run
  40. * softirq callbacks, and they can unconditionally enable interrupts, and
  41. * the caller of free_uid() didn't expect that..
  42. */
  43. static DEFINE_SPINLOCK(uidhash_lock);
  44. struct user_struct root_user = {
  45. .__count = ATOMIC_INIT(1),
  46. .processes = ATOMIC_INIT(1),
  47. .files = ATOMIC_INIT(0),
  48. .sigpending = ATOMIC_INIT(0),
  49. .locked_shm = 0,
  50. #ifdef CONFIG_USER_SCHED
  51. .tg = &init_task_group,
  52. #endif
  53. };
  54. /*
  55. * These routines must be called with the uidhash spinlock held!
  56. */
  57. static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
  58. {
  59. hlist_add_head(&up->uidhash_node, hashent);
  60. }
  61. static void uid_hash_remove(struct user_struct *up)
  62. {
  63. hlist_del_init(&up->uidhash_node);
  64. }
  65. static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  66. {
  67. struct user_struct *user;
  68. struct hlist_node *h;
  69. hlist_for_each_entry(user, h, hashent, uidhash_node) {
  70. if (user->uid == uid) {
  71. atomic_inc(&user->__count);
  72. return user;
  73. }
  74. }
  75. return NULL;
  76. }
  77. #ifdef CONFIG_USER_SCHED
  78. static void sched_destroy_user(struct user_struct *up)
  79. {
  80. sched_destroy_group(up->tg);
  81. }
  82. static int sched_create_user(struct user_struct *up)
  83. {
  84. int rc = 0;
  85. up->tg = sched_create_group(&root_task_group);
  86. if (IS_ERR(up->tg))
  87. rc = -ENOMEM;
  88. return rc;
  89. }
  90. #else /* CONFIG_USER_SCHED */
  91. static void sched_destroy_user(struct user_struct *up) { }
  92. static int sched_create_user(struct user_struct *up) { return 0; }
  93. #endif /* CONFIG_USER_SCHED */
  94. #if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
  95. static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
  96. static DEFINE_MUTEX(uids_mutex);
  97. static inline void uids_mutex_lock(void)
  98. {
  99. mutex_lock(&uids_mutex);
  100. }
  101. static inline void uids_mutex_unlock(void)
  102. {
  103. mutex_unlock(&uids_mutex);
  104. }
  105. /* uid directory attributes */
  106. #ifdef CONFIG_FAIR_GROUP_SCHED
  107. static ssize_t cpu_shares_show(struct kobject *kobj,
  108. struct kobj_attribute *attr,
  109. char *buf)
  110. {
  111. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  112. return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
  113. }
  114. static ssize_t cpu_shares_store(struct kobject *kobj,
  115. struct kobj_attribute *attr,
  116. const char *buf, size_t size)
  117. {
  118. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  119. unsigned long shares;
  120. int rc;
  121. sscanf(buf, "%lu", &shares);
  122. rc = sched_group_set_shares(up->tg, shares);
  123. return (rc ? rc : size);
  124. }
  125. static struct kobj_attribute cpu_share_attr =
  126. __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
  127. #endif
  128. #ifdef CONFIG_RT_GROUP_SCHED
  129. static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
  130. struct kobj_attribute *attr,
  131. char *buf)
  132. {
  133. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  134. return sprintf(buf, "%ld\n", sched_group_rt_runtime(up->tg));
  135. }
  136. static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
  137. struct kobj_attribute *attr,
  138. const char *buf, size_t size)
  139. {
  140. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  141. unsigned long rt_runtime;
  142. int rc;
  143. sscanf(buf, "%ld", &rt_runtime);
  144. rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
  145. return (rc ? rc : size);
  146. }
  147. static struct kobj_attribute cpu_rt_runtime_attr =
  148. __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
  149. static ssize_t cpu_rt_period_show(struct kobject *kobj,
  150. struct kobj_attribute *attr,
  151. char *buf)
  152. {
  153. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  154. return sprintf(buf, "%lu\n", sched_group_rt_period(up->tg));
  155. }
  156. static ssize_t cpu_rt_period_store(struct kobject *kobj,
  157. struct kobj_attribute *attr,
  158. const char *buf, size_t size)
  159. {
  160. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  161. unsigned long rt_period;
  162. int rc;
  163. sscanf(buf, "%lu", &rt_period);
  164. rc = sched_group_set_rt_period(up->tg, rt_period);
  165. return (rc ? rc : size);
  166. }
  167. static struct kobj_attribute cpu_rt_period_attr =
  168. __ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store);
  169. #endif
  170. /* default attributes per uid directory */
  171. static struct attribute *uids_attributes[] = {
  172. #ifdef CONFIG_FAIR_GROUP_SCHED
  173. &cpu_share_attr.attr,
  174. #endif
  175. #ifdef CONFIG_RT_GROUP_SCHED
  176. &cpu_rt_runtime_attr.attr,
  177. &cpu_rt_period_attr.attr,
  178. #endif
  179. NULL
  180. };
  181. /* the lifetime of user_struct is not managed by the core (now) */
  182. static void uids_release(struct kobject *kobj)
  183. {
  184. return;
  185. }
  186. static struct kobj_type uids_ktype = {
  187. .sysfs_ops = &kobj_sysfs_ops,
  188. .default_attrs = uids_attributes,
  189. .release = uids_release,
  190. };
  191. /* create /sys/kernel/uids/<uid>/cpu_share file for this user */
  192. static int uids_user_create(struct user_struct *up)
  193. {
  194. struct kobject *kobj = &up->kobj;
  195. int error;
  196. memset(kobj, 0, sizeof(struct kobject));
  197. kobj->kset = uids_kset;
  198. error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
  199. if (error) {
  200. kobject_put(kobj);
  201. goto done;
  202. }
  203. kobject_uevent(kobj, KOBJ_ADD);
  204. done:
  205. return error;
  206. }
  207. /* create these entries in sysfs:
  208. * "/sys/kernel/uids" directory
  209. * "/sys/kernel/uids/0" directory (for root user)
  210. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  211. */
  212. int __init uids_sysfs_init(void)
  213. {
  214. uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
  215. if (!uids_kset)
  216. return -ENOMEM;
  217. return uids_user_create(&root_user);
  218. }
  219. /* work function to remove sysfs directory for a user and free up
  220. * corresponding structures.
  221. */
  222. static void remove_user_sysfs_dir(struct work_struct *w)
  223. {
  224. struct user_struct *up = container_of(w, struct user_struct, work);
  225. unsigned long flags;
  226. int remove_user = 0;
  227. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  228. * atomic.
  229. */
  230. uids_mutex_lock();
  231. local_irq_save(flags);
  232. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  233. uid_hash_remove(up);
  234. remove_user = 1;
  235. spin_unlock_irqrestore(&uidhash_lock, flags);
  236. } else {
  237. local_irq_restore(flags);
  238. }
  239. if (!remove_user)
  240. goto done;
  241. kobject_uevent(&up->kobj, KOBJ_REMOVE);
  242. kobject_del(&up->kobj);
  243. kobject_put(&up->kobj);
  244. sched_destroy_user(up);
  245. key_put(up->uid_keyring);
  246. key_put(up->session_keyring);
  247. kmem_cache_free(uid_cachep, up);
  248. done:
  249. uids_mutex_unlock();
  250. }
  251. /* IRQs are disabled and uidhash_lock is held upon function entry.
  252. * IRQ state (as stored in flags) is restored and uidhash_lock released
  253. * upon function exit.
  254. */
  255. static inline void free_user(struct user_struct *up, unsigned long flags)
  256. {
  257. /* restore back the count */
  258. atomic_inc(&up->__count);
  259. spin_unlock_irqrestore(&uidhash_lock, flags);
  260. INIT_WORK(&up->work, remove_user_sysfs_dir);
  261. schedule_work(&up->work);
  262. }
  263. #else /* CONFIG_USER_SCHED && CONFIG_SYSFS */
  264. int uids_sysfs_init(void) { return 0; }
  265. static inline int uids_user_create(struct user_struct *up) { return 0; }
  266. static inline void uids_mutex_lock(void) { }
  267. static inline void uids_mutex_unlock(void) { }
  268. /* IRQs are disabled and uidhash_lock is held upon function entry.
  269. * IRQ state (as stored in flags) is restored and uidhash_lock released
  270. * upon function exit.
  271. */
  272. static inline void free_user(struct user_struct *up, unsigned long flags)
  273. {
  274. uid_hash_remove(up);
  275. spin_unlock_irqrestore(&uidhash_lock, flags);
  276. sched_destroy_user(up);
  277. key_put(up->uid_keyring);
  278. key_put(up->session_keyring);
  279. kmem_cache_free(uid_cachep, up);
  280. }
  281. #endif
  282. /*
  283. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  284. * caller must undo that ref with free_uid().
  285. *
  286. * If the user_struct could not be found, return NULL.
  287. */
  288. struct user_struct *find_user(uid_t uid)
  289. {
  290. struct user_struct *ret;
  291. unsigned long flags;
  292. struct user_namespace *ns = current->nsproxy->user_ns;
  293. spin_lock_irqsave(&uidhash_lock, flags);
  294. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  295. spin_unlock_irqrestore(&uidhash_lock, flags);
  296. return ret;
  297. }
  298. void free_uid(struct user_struct *up)
  299. {
  300. unsigned long flags;
  301. if (!up)
  302. return;
  303. local_irq_save(flags);
  304. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  305. free_user(up, flags);
  306. else
  307. local_irq_restore(flags);
  308. }
  309. struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
  310. {
  311. struct hlist_head *hashent = uidhashentry(ns, uid);
  312. struct user_struct *up, *new;
  313. /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
  314. * atomic.
  315. */
  316. uids_mutex_lock();
  317. spin_lock_irq(&uidhash_lock);
  318. up = uid_hash_find(uid, hashent);
  319. spin_unlock_irq(&uidhash_lock);
  320. if (!up) {
  321. new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
  322. if (!new)
  323. goto out_unlock;
  324. new->uid = uid;
  325. atomic_set(&new->__count, 1);
  326. if (sched_create_user(new) < 0)
  327. goto out_free_user;
  328. if (uids_user_create(new))
  329. goto out_destoy_sched;
  330. /*
  331. * Before adding this, check whether we raced
  332. * on adding the same user already..
  333. */
  334. spin_lock_irq(&uidhash_lock);
  335. up = uid_hash_find(uid, hashent);
  336. if (up) {
  337. /* This case is not possible when CONFIG_USER_SCHED
  338. * is defined, since we serialize alloc_uid() using
  339. * uids_mutex. Hence no need to call
  340. * sched_destroy_user() or remove_user_sysfs_dir().
  341. */
  342. key_put(new->uid_keyring);
  343. key_put(new->session_keyring);
  344. kmem_cache_free(uid_cachep, new);
  345. } else {
  346. uid_hash_insert(new, hashent);
  347. up = new;
  348. }
  349. spin_unlock_irq(&uidhash_lock);
  350. }
  351. uids_mutex_unlock();
  352. return up;
  353. out_destoy_sched:
  354. sched_destroy_user(new);
  355. out_free_user:
  356. kmem_cache_free(uid_cachep, new);
  357. out_unlock:
  358. uids_mutex_unlock();
  359. return NULL;
  360. }
  361. #ifdef CONFIG_USER_NS
  362. void release_uids(struct user_namespace *ns)
  363. {
  364. int i;
  365. unsigned long flags;
  366. struct hlist_head *head;
  367. struct hlist_node *nd;
  368. spin_lock_irqsave(&uidhash_lock, flags);
  369. /*
  370. * collapse the chains so that the user_struct-s will
  371. * be still alive, but not in hashes. subsequent free_uid()
  372. * will free them.
  373. */
  374. for (i = 0; i < UIDHASH_SZ; i++) {
  375. head = ns->uidhash_table + i;
  376. while (!hlist_empty(head)) {
  377. nd = head->first;
  378. hlist_del_init(nd);
  379. }
  380. }
  381. spin_unlock_irqrestore(&uidhash_lock, flags);
  382. free_uid(ns->root_user);
  383. }
  384. #endif
  385. static int __init uid_cache_init(void)
  386. {
  387. int n;
  388. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  389. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  390. for(n = 0; n < UIDHASH_SZ; ++n)
  391. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  392. /* Insert the root user immediately (init already runs as root) */
  393. spin_lock_irq(&uidhash_lock);
  394. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  395. spin_unlock_irq(&uidhash_lock);
  396. return 0;
  397. }
  398. module_init(uid_cache_init);