user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 kset *uids_kset; /* represents the /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. /* uid directory attributes */
  107. static ssize_t cpu_shares_show(struct kobject *kobj,
  108. struct kobj_attribute *attr,
  109. char *buf)
  110. {
  111. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  112. return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
  113. }
  114. static ssize_t cpu_shares_store(struct kobject *kobj,
  115. struct kobj_attribute *attr,
  116. const char *buf, size_t size)
  117. {
  118. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  119. unsigned long shares;
  120. int rc;
  121. sscanf(buf, "%lu", &shares);
  122. rc = sched_group_set_shares(up->tg, shares);
  123. return (rc ? rc : size);
  124. }
  125. static struct kobj_attribute cpu_share_attr =
  126. __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
  127. /* default attributes per uid directory */
  128. static struct attribute *uids_attributes[] = {
  129. &cpu_share_attr.attr,
  130. NULL
  131. };
  132. /* the lifetime of user_struct is not managed by the core (now) */
  133. static void uids_release(struct kobject *kobj)
  134. {
  135. return;
  136. }
  137. static struct kobj_type uids_ktype = {
  138. .sysfs_ops = &kobj_sysfs_ops,
  139. .default_attrs = uids_attributes,
  140. .release = uids_release,
  141. };
  142. /* create /sys/kernel/uids/<uid>/cpu_share file for this user */
  143. static int uids_user_create(struct user_struct *up)
  144. {
  145. struct kobject *kobj = &up->kobj;
  146. int error;
  147. memset(kobj, 0, sizeof(struct kobject));
  148. kobj->kset = uids_kset;
  149. error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
  150. if (error) {
  151. kobject_put(kobj);
  152. goto done;
  153. }
  154. kobject_uevent(kobj, KOBJ_ADD);
  155. done:
  156. return error;
  157. }
  158. /* create these entries in sysfs:
  159. * "/sys/kernel/uids" directory
  160. * "/sys/kernel/uids/0" directory (for root user)
  161. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  162. */
  163. int __init uids_sysfs_init(void)
  164. {
  165. uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
  166. if (!uids_kset)
  167. return -ENOMEM;
  168. return uids_user_create(&root_user);
  169. }
  170. /* work function to remove sysfs directory for a user and free up
  171. * corresponding structures.
  172. */
  173. static void remove_user_sysfs_dir(struct work_struct *w)
  174. {
  175. struct user_struct *up = container_of(w, struct user_struct, work);
  176. unsigned long flags;
  177. int remove_user = 0;
  178. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  179. * atomic.
  180. */
  181. uids_mutex_lock();
  182. local_irq_save(flags);
  183. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  184. uid_hash_remove(up);
  185. remove_user = 1;
  186. spin_unlock_irqrestore(&uidhash_lock, flags);
  187. } else {
  188. local_irq_restore(flags);
  189. }
  190. if (!remove_user)
  191. goto done;
  192. kobject_uevent(&up->kobj, KOBJ_REMOVE);
  193. kobject_del(&up->kobj);
  194. kobject_put(&up->kobj);
  195. sched_destroy_user(up);
  196. key_put(up->uid_keyring);
  197. key_put(up->session_keyring);
  198. kmem_cache_free(uid_cachep, up);
  199. done:
  200. uids_mutex_unlock();
  201. }
  202. /* IRQs are disabled and uidhash_lock is held upon function entry.
  203. * IRQ state (as stored in flags) is restored and uidhash_lock released
  204. * upon function exit.
  205. */
  206. static inline void free_user(struct user_struct *up, unsigned long flags)
  207. {
  208. /* restore back the count */
  209. atomic_inc(&up->__count);
  210. spin_unlock_irqrestore(&uidhash_lock, flags);
  211. INIT_WORK(&up->work, remove_user_sysfs_dir);
  212. schedule_work(&up->work);
  213. }
  214. #else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
  215. int uids_sysfs_init(void) { return 0; }
  216. static inline int uids_user_create(struct user_struct *up) { return 0; }
  217. static inline void uids_mutex_lock(void) { }
  218. static inline void uids_mutex_unlock(void) { }
  219. /* IRQs are disabled and uidhash_lock is held upon function entry.
  220. * IRQ state (as stored in flags) is restored and uidhash_lock released
  221. * upon function exit.
  222. */
  223. static inline void free_user(struct user_struct *up, unsigned long flags)
  224. {
  225. uid_hash_remove(up);
  226. spin_unlock_irqrestore(&uidhash_lock, flags);
  227. sched_destroy_user(up);
  228. key_put(up->uid_keyring);
  229. key_put(up->session_keyring);
  230. kmem_cache_free(uid_cachep, up);
  231. }
  232. #endif
  233. /*
  234. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  235. * caller must undo that ref with free_uid().
  236. *
  237. * If the user_struct could not be found, return NULL.
  238. */
  239. struct user_struct *find_user(uid_t uid)
  240. {
  241. struct user_struct *ret;
  242. unsigned long flags;
  243. struct user_namespace *ns = current->nsproxy->user_ns;
  244. spin_lock_irqsave(&uidhash_lock, flags);
  245. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  246. spin_unlock_irqrestore(&uidhash_lock, flags);
  247. return ret;
  248. }
  249. void free_uid(struct user_struct *up)
  250. {
  251. unsigned long flags;
  252. if (!up)
  253. return;
  254. local_irq_save(flags);
  255. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  256. free_user(up, flags);
  257. else
  258. local_irq_restore(flags);
  259. }
  260. struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
  261. {
  262. struct hlist_head *hashent = uidhashentry(ns, uid);
  263. struct user_struct *up, *new;
  264. /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
  265. * atomic.
  266. */
  267. uids_mutex_lock();
  268. spin_lock_irq(&uidhash_lock);
  269. up = uid_hash_find(uid, hashent);
  270. spin_unlock_irq(&uidhash_lock);
  271. if (!up) {
  272. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  273. if (!new)
  274. goto out_unlock;
  275. new->uid = uid;
  276. atomic_set(&new->__count, 1);
  277. atomic_set(&new->processes, 0);
  278. atomic_set(&new->files, 0);
  279. atomic_set(&new->sigpending, 0);
  280. #ifdef CONFIG_INOTIFY_USER
  281. atomic_set(&new->inotify_watches, 0);
  282. atomic_set(&new->inotify_devs, 0);
  283. #endif
  284. #ifdef CONFIG_POSIX_MQUEUE
  285. new->mq_bytes = 0;
  286. #endif
  287. new->locked_shm = 0;
  288. if (alloc_uid_keyring(new, current) < 0)
  289. goto out_free_user;
  290. if (sched_create_user(new) < 0)
  291. goto out_put_keys;
  292. if (uids_user_create(new))
  293. goto out_destoy_sched;
  294. /*
  295. * Before adding this, check whether we raced
  296. * on adding the same user already..
  297. */
  298. spin_lock_irq(&uidhash_lock);
  299. up = uid_hash_find(uid, hashent);
  300. if (up) {
  301. /* This case is not possible when CONFIG_FAIR_USER_SCHED
  302. * is defined, since we serialize alloc_uid() using
  303. * uids_mutex. Hence no need to call
  304. * sched_destroy_user() or remove_user_sysfs_dir().
  305. */
  306. key_put(new->uid_keyring);
  307. key_put(new->session_keyring);
  308. kmem_cache_free(uid_cachep, new);
  309. } else {
  310. uid_hash_insert(new, hashent);
  311. up = new;
  312. }
  313. spin_unlock_irq(&uidhash_lock);
  314. }
  315. uids_mutex_unlock();
  316. return up;
  317. out_destoy_sched:
  318. sched_destroy_user(new);
  319. out_put_keys:
  320. key_put(new->uid_keyring);
  321. key_put(new->session_keyring);
  322. out_free_user:
  323. kmem_cache_free(uid_cachep, new);
  324. out_unlock:
  325. uids_mutex_unlock();
  326. return NULL;
  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);