user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 struct kobject uids_kobject; /* represents /sys/kernel/uids directory */
  75. static DEFINE_MUTEX(uids_mutex);
  76. static void sched_destroy_user(struct user_struct *up)
  77. {
  78. sched_destroy_group(up->tg);
  79. }
  80. static int sched_create_user(struct user_struct *up)
  81. {
  82. int rc = 0;
  83. up->tg = sched_create_group();
  84. if (IS_ERR(up->tg))
  85. rc = -ENOMEM;
  86. return rc;
  87. }
  88. static void sched_switch_user(struct task_struct *p)
  89. {
  90. sched_move_task(p);
  91. }
  92. static inline void uids_mutex_lock(void)
  93. {
  94. mutex_lock(&uids_mutex);
  95. }
  96. static inline void uids_mutex_unlock(void)
  97. {
  98. mutex_unlock(&uids_mutex);
  99. }
  100. /* return cpu shares held by the user */
  101. ssize_t cpu_shares_show(struct kset *kset, char *buffer)
  102. {
  103. struct user_struct *up = container_of(kset, struct user_struct, kset);
  104. return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
  105. }
  106. /* modify cpu shares held by the user */
  107. ssize_t cpu_shares_store(struct kset *kset, const char *buffer, size_t size)
  108. {
  109. struct user_struct *up = container_of(kset, struct user_struct, kset);
  110. unsigned long shares;
  111. int rc;
  112. sscanf(buffer, "%lu", &shares);
  113. rc = sched_group_set_shares(up->tg, shares);
  114. return (rc ? rc : size);
  115. }
  116. static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
  117. {
  118. sa->attr.name = name;
  119. sa->attr.mode = mode;
  120. sa->show = cpu_shares_show;
  121. sa->store = cpu_shares_store;
  122. }
  123. /* Create "/sys/kernel/uids/<uid>" directory and
  124. * "/sys/kernel/uids/<uid>/cpu_share" file for this user.
  125. */
  126. static int user_kobject_create(struct user_struct *up)
  127. {
  128. struct kset *kset = &up->kset;
  129. struct kobject *kobj = &kset->kobj;
  130. int error;
  131. memset(kset, 0, sizeof(struct kset));
  132. kobj->parent = &uids_kobject; /* create under /sys/kernel/uids dir */
  133. kobject_set_name(kobj, "%d", up->uid);
  134. kset_init(kset);
  135. user_attr_init(&up->user_attr, "cpu_share", 0644);
  136. error = kobject_add(kobj);
  137. if (error)
  138. goto done;
  139. error = sysfs_create_file(kobj, &up->user_attr.attr);
  140. if (error)
  141. kobject_del(kobj);
  142. kobject_uevent(kobj, KOBJ_ADD);
  143. done:
  144. return error;
  145. }
  146. /* create these in sysfs filesystem:
  147. * "/sys/kernel/uids" directory
  148. * "/sys/kernel/uids/0" directory (for root user)
  149. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  150. */
  151. int __init uids_kobject_init(void)
  152. {
  153. int error;
  154. /* create under /sys/kernel dir */
  155. uids_kobject.parent = &kernel_subsys.kobj;
  156. uids_kobject.kset = &kernel_subsys;
  157. kobject_set_name(&uids_kobject, "uids");
  158. kobject_init(&uids_kobject);
  159. error = kobject_add(&uids_kobject);
  160. if (!error)
  161. error = user_kobject_create(&root_user);
  162. return error;
  163. }
  164. /* work function to remove sysfs directory for a user and free up
  165. * corresponding structures.
  166. */
  167. static void remove_user_sysfs_dir(struct work_struct *w)
  168. {
  169. struct user_struct *up = container_of(w, struct user_struct, work);
  170. struct kobject *kobj = &up->kset.kobj;
  171. unsigned long flags;
  172. int remove_user = 0;
  173. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  174. * atomic.
  175. */
  176. uids_mutex_lock();
  177. local_irq_save(flags);
  178. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  179. uid_hash_remove(up);
  180. remove_user = 1;
  181. spin_unlock_irqrestore(&uidhash_lock, flags);
  182. } else {
  183. local_irq_restore(flags);
  184. }
  185. if (!remove_user)
  186. goto done;
  187. sysfs_remove_file(kobj, &up->user_attr.attr);
  188. kobject_uevent(kobj, KOBJ_REMOVE);
  189. kobject_del(kobj);
  190. sched_destroy_user(up);
  191. key_put(up->uid_keyring);
  192. key_put(up->session_keyring);
  193. kmem_cache_free(uid_cachep, up);
  194. done:
  195. uids_mutex_unlock();
  196. }
  197. /* IRQs are disabled and uidhash_lock is held upon function entry.
  198. * IRQ state (as stored in flags) is restored and uidhash_lock released
  199. * upon function exit.
  200. */
  201. static inline void free_user(struct user_struct *up, unsigned long flags)
  202. {
  203. /* restore back the count */
  204. atomic_inc(&up->__count);
  205. spin_unlock_irqrestore(&uidhash_lock, flags);
  206. INIT_WORK(&up->work, remove_user_sysfs_dir);
  207. schedule_work(&up->work);
  208. }
  209. #else /* CONFIG_FAIR_USER_SCHED */
  210. static void sched_destroy_user(struct user_struct *up) { }
  211. static int sched_create_user(struct user_struct *up) { return 0; }
  212. static void sched_switch_user(struct task_struct *p) { }
  213. static inline int user_kobject_create(struct user_struct *up) { return 0; }
  214. static inline void uids_mutex_lock(void) { }
  215. static inline void uids_mutex_unlock(void) { }
  216. /* IRQs are disabled and uidhash_lock is held upon function entry.
  217. * IRQ state (as stored in flags) is restored and uidhash_lock released
  218. * upon function exit.
  219. */
  220. static inline void free_user(struct user_struct *up, unsigned long flags)
  221. {
  222. uid_hash_remove(up);
  223. spin_unlock_irqrestore(&uidhash_lock, flags);
  224. sched_destroy_user(up);
  225. key_put(up->uid_keyring);
  226. key_put(up->session_keyring);
  227. kmem_cache_free(uid_cachep, up);
  228. }
  229. #endif /* CONFIG_FAIR_USER_SCHED */
  230. /*
  231. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  232. * caller must undo that ref with free_uid().
  233. *
  234. * If the user_struct could not be found, return NULL.
  235. */
  236. struct user_struct *find_user(uid_t uid)
  237. {
  238. struct user_struct *ret;
  239. unsigned long flags;
  240. struct user_namespace *ns = current->nsproxy->user_ns;
  241. spin_lock_irqsave(&uidhash_lock, flags);
  242. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  243. spin_unlock_irqrestore(&uidhash_lock, flags);
  244. return ret;
  245. }
  246. void free_uid(struct user_struct *up)
  247. {
  248. unsigned long flags;
  249. if (!up)
  250. return;
  251. local_irq_save(flags);
  252. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  253. free_user(up, flags);
  254. else
  255. local_irq_restore(flags);
  256. }
  257. struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
  258. {
  259. struct hlist_head *hashent = uidhashentry(ns, uid);
  260. struct user_struct *up;
  261. /* Make uid_hash_find() + user_kobject_create() + uid_hash_insert()
  262. * atomic.
  263. */
  264. uids_mutex_lock();
  265. spin_lock_irq(&uidhash_lock);
  266. up = uid_hash_find(uid, hashent);
  267. spin_unlock_irq(&uidhash_lock);
  268. if (!up) {
  269. struct user_struct *new;
  270. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  271. if (!new)
  272. return NULL;
  273. new->uid = uid;
  274. atomic_set(&new->__count, 1);
  275. atomic_set(&new->processes, 0);
  276. atomic_set(&new->files, 0);
  277. atomic_set(&new->sigpending, 0);
  278. #ifdef CONFIG_INOTIFY_USER
  279. atomic_set(&new->inotify_watches, 0);
  280. atomic_set(&new->inotify_devs, 0);
  281. #endif
  282. #ifdef CONFIG_POSIX_MQUEUE
  283. new->mq_bytes = 0;
  284. #endif
  285. new->locked_shm = 0;
  286. if (alloc_uid_keyring(new, current) < 0) {
  287. kmem_cache_free(uid_cachep, new);
  288. return NULL;
  289. }
  290. if (sched_create_user(new) < 0) {
  291. key_put(new->uid_keyring);
  292. key_put(new->session_keyring);
  293. kmem_cache_free(uid_cachep, new);
  294. return NULL;
  295. }
  296. if (user_kobject_create(new)) {
  297. sched_destroy_user(new);
  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. /*
  305. * Before adding this, check whether we raced
  306. * on adding the same user already..
  307. */
  308. spin_lock_irq(&uidhash_lock);
  309. up = uid_hash_find(uid, hashent);
  310. if (up) {
  311. /* This case is not possible when CONFIG_FAIR_USER_SCHED
  312. * is defined, since we serialize alloc_uid() using
  313. * uids_mutex. Hence no need to call
  314. * sched_destroy_user() or remove_user_sysfs_dir().
  315. */
  316. key_put(new->uid_keyring);
  317. key_put(new->session_keyring);
  318. kmem_cache_free(uid_cachep, new);
  319. } else {
  320. uid_hash_insert(new, hashent);
  321. up = new;
  322. }
  323. spin_unlock_irq(&uidhash_lock);
  324. }
  325. uids_mutex_unlock();
  326. return up;
  327. }
  328. void switch_uid(struct user_struct *new_user)
  329. {
  330. struct user_struct *old_user;
  331. /* What if a process setreuid()'s and this brings the
  332. * new uid over his NPROC rlimit? We can check this now
  333. * cheaply with the new uid cache, so if it matters
  334. * we should be checking for it. -DaveM
  335. */
  336. old_user = current->user;
  337. atomic_inc(&new_user->processes);
  338. atomic_dec(&old_user->processes);
  339. switch_uid_keyring(new_user);
  340. current->user = new_user;
  341. sched_switch_user(current);
  342. /*
  343. * We need to synchronize with __sigqueue_alloc()
  344. * doing a get_uid(p->user).. If that saw the old
  345. * user value, we need to wait until it has exited
  346. * its critical region before we can free the old
  347. * structure.
  348. */
  349. smp_mb();
  350. spin_unlock_wait(&current->sighand->siglock);
  351. free_uid(old_user);
  352. suid_keys(current);
  353. }
  354. void release_uids(struct user_namespace *ns)
  355. {
  356. int i;
  357. unsigned long flags;
  358. struct hlist_head *head;
  359. struct hlist_node *nd;
  360. spin_lock_irqsave(&uidhash_lock, flags);
  361. /*
  362. * collapse the chains so that the user_struct-s will
  363. * be still alive, but not in hashes. subsequent free_uid()
  364. * will free them.
  365. */
  366. for (i = 0; i < UIDHASH_SZ; i++) {
  367. head = ns->uidhash_table + i;
  368. while (!hlist_empty(head)) {
  369. nd = head->first;
  370. hlist_del_init(nd);
  371. }
  372. }
  373. spin_unlock_irqrestore(&uidhash_lock, flags);
  374. free_uid(ns->root_user);
  375. }
  376. static int __init uid_cache_init(void)
  377. {
  378. int n;
  379. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  380. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  381. for(n = 0; n < UIDHASH_SZ; ++n)
  382. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  383. /* Insert the root user immediately (init already runs as root) */
  384. spin_lock_irq(&uidhash_lock);
  385. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  386. spin_unlock_irq(&uidhash_lock);
  387. return 0;
  388. }
  389. module_init(uid_cache_init);