user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. /*
  19. * UID task count cache, to get fast user lookup in "alloc_uid"
  20. * when changing user ID's (ie setuid() and friends).
  21. */
  22. #define UIDHASH_MASK (UIDHASH_SZ - 1)
  23. #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
  24. #define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
  25. static struct kmem_cache *uid_cachep;
  26. /*
  27. * The uidhash_lock is mostly taken from process context, but it is
  28. * occasionally also taken from softirq/tasklet context, when
  29. * task-structs get RCU-freed. Hence all locking must be softirq-safe.
  30. * But free_uid() is also called with local interrupts disabled, and running
  31. * local_bh_enable() with local interrupts disabled is an error - we'll run
  32. * softirq callbacks, and they can unconditionally enable interrupts, and
  33. * the caller of free_uid() didn't expect that..
  34. */
  35. static DEFINE_SPINLOCK(uidhash_lock);
  36. struct user_struct root_user = {
  37. .__count = ATOMIC_INIT(1),
  38. .processes = ATOMIC_INIT(1),
  39. .files = ATOMIC_INIT(0),
  40. .sigpending = ATOMIC_INIT(0),
  41. .locked_shm = 0,
  42. #ifdef CONFIG_KEYS
  43. .uid_keyring = &root_user_keyring,
  44. .session_keyring = &root_session_keyring,
  45. #endif
  46. #ifdef CONFIG_FAIR_USER_SCHED
  47. .tg = &init_task_group,
  48. #endif
  49. };
  50. /*
  51. * These routines must be called with the uidhash spinlock held!
  52. */
  53. static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
  54. {
  55. hlist_add_head(&up->uidhash_node, hashent);
  56. }
  57. static void uid_hash_remove(struct user_struct *up)
  58. {
  59. hlist_del_init(&up->uidhash_node);
  60. }
  61. static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  62. {
  63. struct user_struct *user;
  64. struct hlist_node *h;
  65. hlist_for_each_entry(user, h, hashent, uidhash_node) {
  66. if (user->uid == uid) {
  67. atomic_inc(&user->__count);
  68. return user;
  69. }
  70. }
  71. return NULL;
  72. }
  73. #ifdef CONFIG_FAIR_USER_SCHED
  74. static void sched_destroy_user(struct user_struct *up)
  75. {
  76. sched_destroy_group(up->tg);
  77. }
  78. static int sched_create_user(struct user_struct *up)
  79. {
  80. int rc = 0;
  81. up->tg = sched_create_group();
  82. if (IS_ERR(up->tg))
  83. rc = -ENOMEM;
  84. return rc;
  85. }
  86. static void sched_switch_user(struct task_struct *p)
  87. {
  88. sched_move_task(p);
  89. }
  90. #else /* CONFIG_FAIR_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. static void sched_switch_user(struct task_struct *p) { }
  94. #endif /* CONFIG_FAIR_USER_SCHED */
  95. #if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS)
  96. static struct kobject uids_kobject; /* represents /sys/kernel/uids directory */
  97. static DEFINE_MUTEX(uids_mutex);
  98. static inline void uids_mutex_lock(void)
  99. {
  100. mutex_lock(&uids_mutex);
  101. }
  102. static inline void uids_mutex_unlock(void)
  103. {
  104. mutex_unlock(&uids_mutex);
  105. }
  106. /* return cpu shares held by the user */
  107. static ssize_t cpu_shares_show(struct kset *kset, char *buffer)
  108. {
  109. struct user_struct *up = container_of(kset, struct user_struct, kset);
  110. return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
  111. }
  112. /* modify cpu shares held by the user */
  113. static ssize_t cpu_shares_store(struct kset *kset, const char *buffer,
  114. size_t size)
  115. {
  116. struct user_struct *up = container_of(kset, struct user_struct, kset);
  117. unsigned long shares;
  118. int rc;
  119. sscanf(buffer, "%lu", &shares);
  120. rc = sched_group_set_shares(up->tg, shares);
  121. return (rc ? rc : size);
  122. }
  123. static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
  124. {
  125. sa->attr.name = name;
  126. sa->attr.mode = mode;
  127. sa->show = cpu_shares_show;
  128. sa->store = cpu_shares_store;
  129. }
  130. /* Create "/sys/kernel/uids/<uid>" directory and
  131. * "/sys/kernel/uids/<uid>/cpu_share" file for this user.
  132. */
  133. static int user_kobject_create(struct user_struct *up)
  134. {
  135. struct kset *kset = &up->kset;
  136. struct kobject *kobj = &kset->kobj;
  137. int error;
  138. memset(kset, 0, sizeof(struct kset));
  139. kobj->parent = &uids_kobject; /* create under /sys/kernel/uids dir */
  140. kobject_set_name(kobj, "%d", up->uid);
  141. kset_init(kset);
  142. user_attr_init(&up->user_attr, "cpu_share", 0644);
  143. error = kobject_add(kobj);
  144. if (error)
  145. goto done;
  146. error = sysfs_create_file(kobj, &up->user_attr.attr);
  147. if (error)
  148. kobject_del(kobj);
  149. kobject_uevent(kobj, KOBJ_ADD);
  150. done:
  151. return error;
  152. }
  153. /* create these in sysfs filesystem:
  154. * "/sys/kernel/uids" directory
  155. * "/sys/kernel/uids/0" directory (for root user)
  156. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  157. */
  158. int __init uids_kobject_init(void)
  159. {
  160. int error;
  161. /* create under /sys/kernel dir */
  162. uids_kobject.parent = &kernel_kset->kobj;
  163. uids_kobject.kset = kernel_kset;
  164. kobject_set_name(&uids_kobject, "uids");
  165. kobject_init(&uids_kobject);
  166. error = kobject_add(&uids_kobject);
  167. if (!error)
  168. error = user_kobject_create(&root_user);
  169. return error;
  170. }
  171. /* work function to remove sysfs directory for a user and free up
  172. * corresponding structures.
  173. */
  174. static void remove_user_sysfs_dir(struct work_struct *w)
  175. {
  176. struct user_struct *up = container_of(w, struct user_struct, work);
  177. struct kobject *kobj = &up->kset.kobj;
  178. unsigned long flags;
  179. int remove_user = 0;
  180. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  181. * atomic.
  182. */
  183. uids_mutex_lock();
  184. local_irq_save(flags);
  185. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  186. uid_hash_remove(up);
  187. remove_user = 1;
  188. spin_unlock_irqrestore(&uidhash_lock, flags);
  189. } else {
  190. local_irq_restore(flags);
  191. }
  192. if (!remove_user)
  193. goto done;
  194. sysfs_remove_file(kobj, &up->user_attr.attr);
  195. kobject_uevent(kobj, KOBJ_REMOVE);
  196. kobject_del(kobj);
  197. sched_destroy_user(up);
  198. key_put(up->uid_keyring);
  199. key_put(up->session_keyring);
  200. kmem_cache_free(uid_cachep, up);
  201. done:
  202. uids_mutex_unlock();
  203. }
  204. /* IRQs are disabled and uidhash_lock is held upon function entry.
  205. * IRQ state (as stored in flags) is restored and uidhash_lock released
  206. * upon function exit.
  207. */
  208. static inline void free_user(struct user_struct *up, unsigned long flags)
  209. {
  210. /* restore back the count */
  211. atomic_inc(&up->__count);
  212. spin_unlock_irqrestore(&uidhash_lock, flags);
  213. INIT_WORK(&up->work, remove_user_sysfs_dir);
  214. schedule_work(&up->work);
  215. }
  216. #else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
  217. static inline int user_kobject_create(struct user_struct *up) { return 0; }
  218. static inline void uids_mutex_lock(void) { }
  219. static inline void uids_mutex_unlock(void) { }
  220. /* IRQs are disabled and uidhash_lock is held upon function entry.
  221. * IRQ state (as stored in flags) is restored and uidhash_lock released
  222. * upon function exit.
  223. */
  224. static inline void free_user(struct user_struct *up, unsigned long flags)
  225. {
  226. uid_hash_remove(up);
  227. spin_unlock_irqrestore(&uidhash_lock, flags);
  228. sched_destroy_user(up);
  229. key_put(up->uid_keyring);
  230. key_put(up->session_keyring);
  231. kmem_cache_free(uid_cachep, up);
  232. }
  233. #endif
  234. /*
  235. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  236. * caller must undo that ref with free_uid().
  237. *
  238. * If the user_struct could not be found, return NULL.
  239. */
  240. struct user_struct *find_user(uid_t uid)
  241. {
  242. struct user_struct *ret;
  243. unsigned long flags;
  244. struct user_namespace *ns = current->nsproxy->user_ns;
  245. spin_lock_irqsave(&uidhash_lock, flags);
  246. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  247. spin_unlock_irqrestore(&uidhash_lock, flags);
  248. return ret;
  249. }
  250. void free_uid(struct user_struct *up)
  251. {
  252. unsigned long flags;
  253. if (!up)
  254. return;
  255. local_irq_save(flags);
  256. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  257. free_user(up, flags);
  258. else
  259. local_irq_restore(flags);
  260. }
  261. struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
  262. {
  263. struct hlist_head *hashent = uidhashentry(ns, uid);
  264. struct user_struct *up;
  265. /* Make uid_hash_find() + user_kobject_create() + uid_hash_insert()
  266. * atomic.
  267. */
  268. uids_mutex_lock();
  269. spin_lock_irq(&uidhash_lock);
  270. up = uid_hash_find(uid, hashent);
  271. spin_unlock_irq(&uidhash_lock);
  272. if (!up) {
  273. struct user_struct *new;
  274. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  275. if (!new) {
  276. uids_mutex_unlock();
  277. return NULL;
  278. }
  279. new->uid = uid;
  280. atomic_set(&new->__count, 1);
  281. atomic_set(&new->processes, 0);
  282. atomic_set(&new->files, 0);
  283. atomic_set(&new->sigpending, 0);
  284. #ifdef CONFIG_INOTIFY_USER
  285. atomic_set(&new->inotify_watches, 0);
  286. atomic_set(&new->inotify_devs, 0);
  287. #endif
  288. #ifdef CONFIG_POSIX_MQUEUE
  289. new->mq_bytes = 0;
  290. #endif
  291. new->locked_shm = 0;
  292. if (alloc_uid_keyring(new, current) < 0) {
  293. kmem_cache_free(uid_cachep, new);
  294. uids_mutex_unlock();
  295. return NULL;
  296. }
  297. if (sched_create_user(new) < 0) {
  298. key_put(new->uid_keyring);
  299. key_put(new->session_keyring);
  300. kmem_cache_free(uid_cachep, new);
  301. uids_mutex_unlock();
  302. return NULL;
  303. }
  304. if (user_kobject_create(new)) {
  305. sched_destroy_user(new);
  306. key_put(new->uid_keyring);
  307. key_put(new->session_keyring);
  308. kmem_cache_free(uid_cachep, new);
  309. uids_mutex_unlock();
  310. return NULL;
  311. }
  312. /*
  313. * Before adding this, check whether we raced
  314. * on adding the same user already..
  315. */
  316. spin_lock_irq(&uidhash_lock);
  317. up = uid_hash_find(uid, hashent);
  318. if (up) {
  319. /* This case is not possible when CONFIG_FAIR_USER_SCHED
  320. * is defined, since we serialize alloc_uid() using
  321. * uids_mutex. Hence no need to call
  322. * sched_destroy_user() or remove_user_sysfs_dir().
  323. */
  324. key_put(new->uid_keyring);
  325. key_put(new->session_keyring);
  326. kmem_cache_free(uid_cachep, new);
  327. } else {
  328. uid_hash_insert(new, hashent);
  329. up = new;
  330. }
  331. spin_unlock_irq(&uidhash_lock);
  332. }
  333. uids_mutex_unlock();
  334. return up;
  335. }
  336. void switch_uid(struct user_struct *new_user)
  337. {
  338. struct user_struct *old_user;
  339. /* What if a process setreuid()'s and this brings the
  340. * new uid over his NPROC rlimit? We can check this now
  341. * cheaply with the new uid cache, so if it matters
  342. * we should be checking for it. -DaveM
  343. */
  344. old_user = current->user;
  345. atomic_inc(&new_user->processes);
  346. atomic_dec(&old_user->processes);
  347. switch_uid_keyring(new_user);
  348. current->user = new_user;
  349. sched_switch_user(current);
  350. /*
  351. * We need to synchronize with __sigqueue_alloc()
  352. * doing a get_uid(p->user).. If that saw the old
  353. * user value, we need to wait until it has exited
  354. * its critical region before we can free the old
  355. * structure.
  356. */
  357. smp_mb();
  358. spin_unlock_wait(&current->sighand->siglock);
  359. free_uid(old_user);
  360. suid_keys(current);
  361. }
  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. static int __init uid_cache_init(void)
  385. {
  386. int n;
  387. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  388. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  389. for(n = 0; n < UIDHASH_SZ; ++n)
  390. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  391. /* Insert the root user immediately (init already runs as root) */
  392. spin_lock_irq(&uidhash_lock);
  393. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  394. spin_unlock_irq(&uidhash_lock);
  395. return 0;
  396. }
  397. module_init(uid_cache_init);