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