user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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->ktype = &uids_ktype;
  149. kobj->kset = uids_kset;
  150. kobject_init(kobj);
  151. kobject_set_name(&up->kobj, "%d", up->uid);
  152. error = kobject_add(kobj);
  153. if (error)
  154. goto done;
  155. kobject_uevent(kobj, KOBJ_ADD);
  156. done:
  157. return error;
  158. }
  159. /* create these entries in sysfs:
  160. * "/sys/kernel/uids" directory
  161. * "/sys/kernel/uids/0" directory (for root user)
  162. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  163. */
  164. int __init uids_sysfs_init(void)
  165. {
  166. uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
  167. if (!uids_kset)
  168. return -ENOMEM;
  169. return uids_user_create(&root_user);
  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. unsigned long flags;
  178. int remove_user = 0;
  179. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  180. * atomic.
  181. */
  182. uids_mutex_lock();
  183. local_irq_save(flags);
  184. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  185. uid_hash_remove(up);
  186. remove_user = 1;
  187. spin_unlock_irqrestore(&uidhash_lock, flags);
  188. } else {
  189. local_irq_restore(flags);
  190. }
  191. if (!remove_user)
  192. goto done;
  193. kobject_uevent(&up->kobj, KOBJ_REMOVE);
  194. kobject_del(&up->kobj);
  195. kobject_put(&up->kobj);
  196. sched_destroy_user(up);
  197. key_put(up->uid_keyring);
  198. key_put(up->session_keyring);
  199. kmem_cache_free(uid_cachep, up);
  200. done:
  201. uids_mutex_unlock();
  202. }
  203. /* IRQs are disabled and uidhash_lock is held upon function entry.
  204. * IRQ state (as stored in flags) is restored and uidhash_lock released
  205. * upon function exit.
  206. */
  207. static inline void free_user(struct user_struct *up, unsigned long flags)
  208. {
  209. /* restore back the count */
  210. atomic_inc(&up->__count);
  211. spin_unlock_irqrestore(&uidhash_lock, flags);
  212. INIT_WORK(&up->work, remove_user_sysfs_dir);
  213. schedule_work(&up->work);
  214. }
  215. #else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
  216. int uids_sysfs_init(void) { return 0; }
  217. static inline int uids_user_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() + uids_user_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 (uids_user_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);