user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. .mq_bytes = 0,
  42. .locked_shm = 0,
  43. #ifdef CONFIG_KEYS
  44. .uid_keyring = &root_user_keyring,
  45. .session_keyring = &root_session_keyring,
  46. #endif
  47. #ifdef CONFIG_FAIR_USER_SCHED
  48. .tg = &init_task_group,
  49. #endif
  50. };
  51. /*
  52. * These routines must be called with the uidhash spinlock held!
  53. */
  54. static inline void uid_hash_insert(struct user_struct *up,
  55. struct hlist_head *hashent)
  56. {
  57. hlist_add_head(&up->uidhash_node, hashent);
  58. }
  59. static inline void uid_hash_remove(struct user_struct *up)
  60. {
  61. hlist_del_init(&up->uidhash_node);
  62. }
  63. static inline struct user_struct *uid_hash_find(uid_t uid,
  64. struct hlist_head *hashent)
  65. {
  66. struct user_struct *user;
  67. struct hlist_node *h;
  68. hlist_for_each_entry(user, h, hashent, uidhash_node) {
  69. if (user->uid == uid) {
  70. atomic_inc(&user->__count);
  71. return user;
  72. }
  73. }
  74. return NULL;
  75. }
  76. #ifdef CONFIG_FAIR_USER_SCHED
  77. static struct kobject uids_kobject; /* represents /sys/kernel/uids directory */
  78. static DEFINE_MUTEX(uids_mutex);
  79. static void sched_destroy_user(struct user_struct *up)
  80. {
  81. sched_destroy_group(up->tg);
  82. }
  83. static int sched_create_user(struct user_struct *up)
  84. {
  85. int rc = 0;
  86. up->tg = sched_create_group();
  87. if (IS_ERR(up->tg))
  88. rc = -ENOMEM;
  89. return rc;
  90. }
  91. static void sched_switch_user(struct task_struct *p)
  92. {
  93. sched_move_task(p);
  94. }
  95. static inline void uids_mutex_lock(void)
  96. {
  97. mutex_lock(&uids_mutex);
  98. }
  99. static inline void uids_mutex_unlock(void)
  100. {
  101. mutex_unlock(&uids_mutex);
  102. }
  103. /* return cpu shares held by the user */
  104. ssize_t cpu_shares_show(struct kset *kset, char *buffer)
  105. {
  106. struct user_struct *up = container_of(kset, struct user_struct, kset);
  107. return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
  108. }
  109. /* modify cpu shares held by the user */
  110. ssize_t cpu_shares_store(struct kset *kset, const char *buffer, size_t size)
  111. {
  112. struct user_struct *up = container_of(kset, struct user_struct, kset);
  113. unsigned long shares;
  114. int rc;
  115. sscanf(buffer, "%lu", &shares);
  116. rc = sched_group_set_shares(up->tg, shares);
  117. return (rc ? rc : size);
  118. }
  119. static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
  120. {
  121. sa->attr.name = name;
  122. sa->attr.mode = mode;
  123. sa->show = cpu_shares_show;
  124. sa->store = cpu_shares_store;
  125. }
  126. /* Create "/sys/kernel/uids/<uid>" directory and
  127. * "/sys/kernel/uids/<uid>/cpu_share" file for this user.
  128. */
  129. static int user_kobject_create(struct user_struct *up)
  130. {
  131. struct kset *kset = &up->kset;
  132. struct kobject *kobj = &kset->kobj;
  133. int error;
  134. memset(kset, 0, sizeof(struct kset));
  135. kobj->parent = &uids_kobject; /* create under /sys/kernel/uids dir */
  136. kobject_set_name(kobj, "%d", up->uid);
  137. kset_init(kset);
  138. user_attr_init(&up->user_attr, "cpu_share", 0644);
  139. error = kobject_add(kobj);
  140. if (error)
  141. goto done;
  142. error = sysfs_create_file(kobj, &up->user_attr.attr);
  143. if (error)
  144. kobject_del(kobj);
  145. done:
  146. return error;
  147. }
  148. /* create these in sysfs filesystem:
  149. * "/sys/kernel/uids" directory
  150. * "/sys/kernel/uids/0" directory (for root user)
  151. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  152. */
  153. int __init uids_kobject_init(void)
  154. {
  155. int error;
  156. /* create under /sys/kernel dir */
  157. uids_kobject.parent = &kernel_subsys.kobj;
  158. kobject_set_name(&uids_kobject, "uids");
  159. kobject_init(&uids_kobject);
  160. error = kobject_add(&uids_kobject);
  161. if (!error)
  162. error = user_kobject_create(&root_user);
  163. return error;
  164. }
  165. /* work function to remove sysfs directory for a user and free up
  166. * corresponding structures.
  167. */
  168. static void remove_user_sysfs_dir(struct work_struct *w)
  169. {
  170. struct user_struct *up = container_of(w, struct user_struct, work);
  171. struct kobject *kobj = &up->kset.kobj;
  172. unsigned long flags;
  173. int remove_user = 0;
  174. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  175. * atomic.
  176. */
  177. uids_mutex_lock();
  178. local_irq_save(flags);
  179. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  180. uid_hash_remove(up);
  181. remove_user = 1;
  182. spin_unlock_irqrestore(&uidhash_lock, flags);
  183. } else {
  184. local_irq_restore(flags);
  185. }
  186. if (!remove_user)
  187. goto done;
  188. sysfs_remove_file(kobj, &up->user_attr.attr);
  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. new->mq_bytes = 0;
  283. new->locked_shm = 0;
  284. if (alloc_uid_keyring(new, current) < 0) {
  285. kmem_cache_free(uid_cachep, new);
  286. return NULL;
  287. }
  288. if (sched_create_user(new) < 0) {
  289. key_put(new->uid_keyring);
  290. key_put(new->session_keyring);
  291. kmem_cache_free(uid_cachep, new);
  292. return NULL;
  293. }
  294. if (user_kobject_create(new)) {
  295. sched_destroy_user(new);
  296. key_put(new->uid_keyring);
  297. key_put(new->session_keyring);
  298. kmem_cache_free(uid_cachep, new);
  299. uids_mutex_unlock();
  300. return NULL;
  301. }
  302. /*
  303. * Before adding this, check whether we raced
  304. * on adding the same user already..
  305. */
  306. spin_lock_irq(&uidhash_lock);
  307. up = uid_hash_find(uid, hashent);
  308. if (up) {
  309. /* This case is not possible when CONFIG_FAIR_USER_SCHED
  310. * is defined, since we serialize alloc_uid() using
  311. * uids_mutex. Hence no need to call
  312. * sched_destroy_user() or remove_user_sysfs_dir().
  313. */
  314. key_put(new->uid_keyring);
  315. key_put(new->session_keyring);
  316. kmem_cache_free(uid_cachep, new);
  317. } else {
  318. uid_hash_insert(new, hashent);
  319. up = new;
  320. }
  321. spin_unlock_irq(&uidhash_lock);
  322. }
  323. uids_mutex_unlock();
  324. return up;
  325. }
  326. void switch_uid(struct user_struct *new_user)
  327. {
  328. struct user_struct *old_user;
  329. /* What if a process setreuid()'s and this brings the
  330. * new uid over his NPROC rlimit? We can check this now
  331. * cheaply with the new uid cache, so if it matters
  332. * we should be checking for it. -DaveM
  333. */
  334. old_user = current->user;
  335. atomic_inc(&new_user->processes);
  336. atomic_dec(&old_user->processes);
  337. switch_uid_keyring(new_user);
  338. current->user = new_user;
  339. sched_switch_user(current);
  340. /*
  341. * We need to synchronize with __sigqueue_alloc()
  342. * doing a get_uid(p->user).. If that saw the old
  343. * user value, we need to wait until it has exited
  344. * its critical region before we can free the old
  345. * structure.
  346. */
  347. smp_mb();
  348. spin_unlock_wait(&current->sighand->siglock);
  349. free_uid(old_user);
  350. suid_keys(current);
  351. }
  352. void release_uids(struct user_namespace *ns)
  353. {
  354. int i;
  355. unsigned long flags;
  356. struct hlist_head *head;
  357. struct hlist_node *nd;
  358. spin_lock_irqsave(&uidhash_lock, flags);
  359. /*
  360. * collapse the chains so that the user_struct-s will
  361. * be still alive, but not in hashes. subsequent free_uid()
  362. * will free them.
  363. */
  364. for (i = 0; i < UIDHASH_SZ; i++) {
  365. head = ns->uidhash_table + i;
  366. while (!hlist_empty(head)) {
  367. nd = head->first;
  368. hlist_del_init(nd);
  369. }
  370. }
  371. spin_unlock_irqrestore(&uidhash_lock, flags);
  372. free_uid(ns->root_user);
  373. }
  374. static int __init uid_cache_init(void)
  375. {
  376. int n;
  377. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  378. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  379. for(n = 0; n < UIDHASH_SZ; ++n)
  380. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  381. /* Insert the root user immediately (init already runs as root) */
  382. spin_lock_irq(&uidhash_lock);
  383. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  384. spin_unlock_irq(&uidhash_lock);
  385. return 0;
  386. }
  387. module_init(uid_cache_init);