user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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(1),
  22. },
  23. .creator = &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. /* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
  45. struct user_struct root_user = {
  46. .__count = ATOMIC_INIT(2),
  47. .processes = ATOMIC_INIT(1),
  48. .files = ATOMIC_INIT(0),
  49. .sigpending = ATOMIC_INIT(0),
  50. .locked_shm = 0,
  51. .user_ns = &init_user_ns,
  52. #ifdef CONFIG_USER_SCHED
  53. .tg = &init_task_group,
  54. #endif
  55. };
  56. /*
  57. * These routines must be called with the uidhash spinlock held!
  58. */
  59. static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
  60. {
  61. hlist_add_head(&up->uidhash_node, hashent);
  62. }
  63. static void uid_hash_remove(struct user_struct *up)
  64. {
  65. hlist_del_init(&up->uidhash_node);
  66. }
  67. static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  68. {
  69. struct user_struct *user;
  70. struct hlist_node *h;
  71. hlist_for_each_entry(user, h, hashent, uidhash_node) {
  72. if (user->uid == uid) {
  73. atomic_inc(&user->__count);
  74. return user;
  75. }
  76. }
  77. return NULL;
  78. }
  79. #ifdef CONFIG_USER_SCHED
  80. static void sched_destroy_user(struct user_struct *up)
  81. {
  82. sched_destroy_group(up->tg);
  83. }
  84. static int sched_create_user(struct user_struct *up)
  85. {
  86. int rc = 0;
  87. up->tg = sched_create_group(&root_task_group);
  88. if (IS_ERR(up->tg))
  89. rc = -ENOMEM;
  90. set_tg_uid(up);
  91. return rc;
  92. }
  93. #else /* CONFIG_USER_SCHED */
  94. static void sched_destroy_user(struct user_struct *up) { }
  95. static int sched_create_user(struct user_struct *up) { return 0; }
  96. #endif /* CONFIG_USER_SCHED */
  97. #if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
  98. static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
  99. static DEFINE_MUTEX(uids_mutex);
  100. static inline void uids_mutex_lock(void)
  101. {
  102. mutex_lock(&uids_mutex);
  103. }
  104. static inline void uids_mutex_unlock(void)
  105. {
  106. mutex_unlock(&uids_mutex);
  107. }
  108. /* uid directory attributes */
  109. #ifdef CONFIG_FAIR_GROUP_SCHED
  110. static ssize_t cpu_shares_show(struct kobject *kobj,
  111. struct kobj_attribute *attr,
  112. char *buf)
  113. {
  114. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  115. return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
  116. }
  117. static ssize_t cpu_shares_store(struct kobject *kobj,
  118. struct kobj_attribute *attr,
  119. const char *buf, size_t size)
  120. {
  121. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  122. unsigned long shares;
  123. int rc;
  124. sscanf(buf, "%lu", &shares);
  125. rc = sched_group_set_shares(up->tg, shares);
  126. return (rc ? rc : size);
  127. }
  128. static struct kobj_attribute cpu_share_attr =
  129. __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
  130. #endif
  131. #ifdef CONFIG_RT_GROUP_SCHED
  132. static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
  133. struct kobj_attribute *attr,
  134. char *buf)
  135. {
  136. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  137. return sprintf(buf, "%ld\n", sched_group_rt_runtime(up->tg));
  138. }
  139. static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
  140. struct kobj_attribute *attr,
  141. const char *buf, size_t size)
  142. {
  143. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  144. unsigned long rt_runtime;
  145. int rc;
  146. sscanf(buf, "%ld", &rt_runtime);
  147. rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
  148. return (rc ? rc : size);
  149. }
  150. static struct kobj_attribute cpu_rt_runtime_attr =
  151. __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
  152. static ssize_t cpu_rt_period_show(struct kobject *kobj,
  153. struct kobj_attribute *attr,
  154. char *buf)
  155. {
  156. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  157. return sprintf(buf, "%lu\n", sched_group_rt_period(up->tg));
  158. }
  159. static ssize_t cpu_rt_period_store(struct kobject *kobj,
  160. struct kobj_attribute *attr,
  161. const char *buf, size_t size)
  162. {
  163. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  164. unsigned long rt_period;
  165. int rc;
  166. sscanf(buf, "%lu", &rt_period);
  167. rc = sched_group_set_rt_period(up->tg, rt_period);
  168. return (rc ? rc : size);
  169. }
  170. static struct kobj_attribute cpu_rt_period_attr =
  171. __ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store);
  172. #endif
  173. /* default attributes per uid directory */
  174. static struct attribute *uids_attributes[] = {
  175. #ifdef CONFIG_FAIR_GROUP_SCHED
  176. &cpu_share_attr.attr,
  177. #endif
  178. #ifdef CONFIG_RT_GROUP_SCHED
  179. &cpu_rt_runtime_attr.attr,
  180. &cpu_rt_period_attr.attr,
  181. #endif
  182. NULL
  183. };
  184. /* the lifetime of user_struct is not managed by the core (now) */
  185. static void uids_release(struct kobject *kobj)
  186. {
  187. return;
  188. }
  189. static struct kobj_type uids_ktype = {
  190. .sysfs_ops = &kobj_sysfs_ops,
  191. .default_attrs = uids_attributes,
  192. .release = uids_release,
  193. };
  194. /*
  195. * Create /sys/kernel/uids/<uid>/cpu_share file for this user
  196. * We do not create this file for users in a user namespace (until
  197. * sysfs tagging is implemented).
  198. *
  199. * See Documentation/scheduler/sched-design-CFS.txt for ramifications.
  200. */
  201. static int uids_user_create(struct user_struct *up)
  202. {
  203. struct kobject *kobj = &up->kobj;
  204. int error;
  205. memset(kobj, 0, sizeof(struct kobject));
  206. if (up->user_ns != &init_user_ns)
  207. return 0;
  208. kobj->kset = uids_kset;
  209. error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
  210. if (error) {
  211. kobject_put(kobj);
  212. goto done;
  213. }
  214. kobject_uevent(kobj, KOBJ_ADD);
  215. done:
  216. return error;
  217. }
  218. /* create these entries in sysfs:
  219. * "/sys/kernel/uids" directory
  220. * "/sys/kernel/uids/0" directory (for root user)
  221. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  222. */
  223. int __init uids_sysfs_init(void)
  224. {
  225. uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
  226. if (!uids_kset)
  227. return -ENOMEM;
  228. return uids_user_create(&root_user);
  229. }
  230. /* work function to remove sysfs directory for a user and free up
  231. * corresponding structures.
  232. */
  233. static void remove_user_sysfs_dir(struct work_struct *w)
  234. {
  235. struct user_struct *up = container_of(w, struct user_struct, work);
  236. unsigned long flags;
  237. int remove_user = 0;
  238. if (up->user_ns != &init_user_ns)
  239. return;
  240. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  241. * atomic.
  242. */
  243. uids_mutex_lock();
  244. local_irq_save(flags);
  245. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  246. uid_hash_remove(up);
  247. remove_user = 1;
  248. spin_unlock_irqrestore(&uidhash_lock, flags);
  249. } else {
  250. local_irq_restore(flags);
  251. }
  252. if (!remove_user)
  253. goto done;
  254. kobject_uevent(&up->kobj, KOBJ_REMOVE);
  255. kobject_del(&up->kobj);
  256. kobject_put(&up->kobj);
  257. sched_destroy_user(up);
  258. key_put(up->uid_keyring);
  259. key_put(up->session_keyring);
  260. kmem_cache_free(uid_cachep, up);
  261. done:
  262. uids_mutex_unlock();
  263. }
  264. /* IRQs are disabled and uidhash_lock is held upon function entry.
  265. * IRQ state (as stored in flags) is restored and uidhash_lock released
  266. * upon function exit.
  267. */
  268. static void free_user(struct user_struct *up, unsigned long flags)
  269. {
  270. /* restore back the count */
  271. atomic_inc(&up->__count);
  272. spin_unlock_irqrestore(&uidhash_lock, flags);
  273. put_user_ns(up->user_ns);
  274. INIT_WORK(&up->work, remove_user_sysfs_dir);
  275. schedule_work(&up->work);
  276. }
  277. #else /* CONFIG_USER_SCHED && CONFIG_SYSFS */
  278. int uids_sysfs_init(void) { return 0; }
  279. static inline int uids_user_create(struct user_struct *up) { return 0; }
  280. static inline void uids_mutex_lock(void) { }
  281. static inline void uids_mutex_unlock(void) { }
  282. /* IRQs are disabled and uidhash_lock is held upon function entry.
  283. * IRQ state (as stored in flags) is restored and uidhash_lock released
  284. * upon function exit.
  285. */
  286. static void free_user(struct user_struct *up, unsigned long flags)
  287. {
  288. uid_hash_remove(up);
  289. spin_unlock_irqrestore(&uidhash_lock, flags);
  290. sched_destroy_user(up);
  291. key_put(up->uid_keyring);
  292. key_put(up->session_keyring);
  293. put_user_ns(up->user_ns);
  294. kmem_cache_free(uid_cachep, up);
  295. }
  296. #endif
  297. /*
  298. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  299. * caller must undo that ref with free_uid().
  300. *
  301. * If the user_struct could not be found, return NULL.
  302. */
  303. struct user_struct *find_user(uid_t uid)
  304. {
  305. struct user_struct *ret;
  306. unsigned long flags;
  307. struct user_namespace *ns = current_user_ns();
  308. spin_lock_irqsave(&uidhash_lock, flags);
  309. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  310. spin_unlock_irqrestore(&uidhash_lock, flags);
  311. return ret;
  312. }
  313. void free_uid(struct user_struct *up)
  314. {
  315. unsigned long flags;
  316. if (!up)
  317. return;
  318. local_irq_save(flags);
  319. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  320. free_user(up, flags);
  321. else
  322. local_irq_restore(flags);
  323. }
  324. struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
  325. {
  326. struct hlist_head *hashent = uidhashentry(ns, uid);
  327. struct user_struct *up, *new;
  328. /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
  329. * atomic.
  330. */
  331. uids_mutex_lock();
  332. spin_lock_irq(&uidhash_lock);
  333. up = uid_hash_find(uid, hashent);
  334. spin_unlock_irq(&uidhash_lock);
  335. if (!up) {
  336. new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
  337. if (!new)
  338. goto out_unlock;
  339. new->uid = uid;
  340. atomic_set(&new->__count, 1);
  341. if (sched_create_user(new) < 0)
  342. goto out_free_user;
  343. new->user_ns = get_user_ns(ns);
  344. if (uids_user_create(new))
  345. goto out_destoy_sched;
  346. /*
  347. * Before adding this, check whether we raced
  348. * on adding the same user already..
  349. */
  350. spin_lock_irq(&uidhash_lock);
  351. up = uid_hash_find(uid, hashent);
  352. if (up) {
  353. /* This case is not possible when CONFIG_USER_SCHED
  354. * is defined, since we serialize alloc_uid() using
  355. * uids_mutex. Hence no need to call
  356. * sched_destroy_user() or remove_user_sysfs_dir().
  357. */
  358. key_put(new->uid_keyring);
  359. key_put(new->session_keyring);
  360. kmem_cache_free(uid_cachep, new);
  361. } else {
  362. uid_hash_insert(new, hashent);
  363. up = new;
  364. }
  365. spin_unlock_irq(&uidhash_lock);
  366. }
  367. uids_mutex_unlock();
  368. return up;
  369. out_destoy_sched:
  370. sched_destroy_user(new);
  371. put_user_ns(new->user_ns);
  372. out_free_user:
  373. kmem_cache_free(uid_cachep, new);
  374. out_unlock:
  375. uids_mutex_unlock();
  376. return NULL;
  377. }
  378. static int __init uid_cache_init(void)
  379. {
  380. int n;
  381. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  382. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  383. for(n = 0; n < UIDHASH_SZ; ++n)
  384. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  385. /* Insert the root user immediately (init already runs as root) */
  386. spin_lock_irq(&uidhash_lock);
  387. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  388. spin_unlock_irq(&uidhash_lock);
  389. return 0;
  390. }
  391. module_init(uid_cache_init);