user.c 12 KB

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