user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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_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_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_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_USER_SCHED */
  102. #if defined(CONFIG_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. #ifdef CONFIG_FAIR_GROUP_SCHED
  115. static ssize_t cpu_shares_show(struct kobject *kobj,
  116. struct kobj_attribute *attr,
  117. char *buf)
  118. {
  119. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  120. return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
  121. }
  122. static ssize_t cpu_shares_store(struct kobject *kobj,
  123. struct kobj_attribute *attr,
  124. const char *buf, size_t size)
  125. {
  126. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  127. unsigned long shares;
  128. int rc;
  129. sscanf(buf, "%lu", &shares);
  130. rc = sched_group_set_shares(up->tg, shares);
  131. return (rc ? rc : size);
  132. }
  133. static struct kobj_attribute cpu_share_attr =
  134. __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
  135. #endif
  136. #ifdef CONFIG_RT_GROUP_SCHED
  137. static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
  138. struct kobj_attribute *attr,
  139. char *buf)
  140. {
  141. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  142. return sprintf(buf, "%lu\n", sched_group_rt_runtime(up->tg));
  143. }
  144. static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
  145. struct kobj_attribute *attr,
  146. const char *buf, size_t size)
  147. {
  148. struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  149. unsigned long rt_runtime;
  150. int rc;
  151. sscanf(buf, "%lu", &rt_runtime);
  152. rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
  153. return (rc ? rc : size);
  154. }
  155. static struct kobj_attribute cpu_rt_runtime_attr =
  156. __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
  157. #endif
  158. /* default attributes per uid directory */
  159. static struct attribute *uids_attributes[] = {
  160. #ifdef CONFIG_FAIR_GROUP_SCHED
  161. &cpu_share_attr.attr,
  162. #endif
  163. #ifdef CONFIG_RT_GROUP_SCHED
  164. &cpu_rt_runtime_attr.attr,
  165. #endif
  166. NULL
  167. };
  168. /* the lifetime of user_struct is not managed by the core (now) */
  169. static void uids_release(struct kobject *kobj)
  170. {
  171. return;
  172. }
  173. static struct kobj_type uids_ktype = {
  174. .sysfs_ops = &kobj_sysfs_ops,
  175. .default_attrs = uids_attributes,
  176. .release = uids_release,
  177. };
  178. /* create /sys/kernel/uids/<uid>/cpu_share file for this user */
  179. static int uids_user_create(struct user_struct *up)
  180. {
  181. struct kobject *kobj = &up->kobj;
  182. int error;
  183. memset(kobj, 0, sizeof(struct kobject));
  184. kobj->kset = uids_kset;
  185. error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
  186. if (error) {
  187. kobject_put(kobj);
  188. goto done;
  189. }
  190. kobject_uevent(kobj, KOBJ_ADD);
  191. done:
  192. return error;
  193. }
  194. /* create these entries in sysfs:
  195. * "/sys/kernel/uids" directory
  196. * "/sys/kernel/uids/0" directory (for root user)
  197. * "/sys/kernel/uids/0/cpu_share" file (for root user)
  198. */
  199. int __init uids_sysfs_init(void)
  200. {
  201. uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
  202. if (!uids_kset)
  203. return -ENOMEM;
  204. return uids_user_create(&root_user);
  205. }
  206. /* work function to remove sysfs directory for a user and free up
  207. * corresponding structures.
  208. */
  209. static void remove_user_sysfs_dir(struct work_struct *w)
  210. {
  211. struct user_struct *up = container_of(w, struct user_struct, work);
  212. unsigned long flags;
  213. int remove_user = 0;
  214. /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  215. * atomic.
  216. */
  217. uids_mutex_lock();
  218. local_irq_save(flags);
  219. if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
  220. uid_hash_remove(up);
  221. remove_user = 1;
  222. spin_unlock_irqrestore(&uidhash_lock, flags);
  223. } else {
  224. local_irq_restore(flags);
  225. }
  226. if (!remove_user)
  227. goto done;
  228. kobject_uevent(&up->kobj, KOBJ_REMOVE);
  229. kobject_del(&up->kobj);
  230. kobject_put(&up->kobj);
  231. sched_destroy_user(up);
  232. key_put(up->uid_keyring);
  233. key_put(up->session_keyring);
  234. kmem_cache_free(uid_cachep, up);
  235. done:
  236. uids_mutex_unlock();
  237. }
  238. /* IRQs are disabled and uidhash_lock is held upon function entry.
  239. * IRQ state (as stored in flags) is restored and uidhash_lock released
  240. * upon function exit.
  241. */
  242. static inline void free_user(struct user_struct *up, unsigned long flags)
  243. {
  244. /* restore back the count */
  245. atomic_inc(&up->__count);
  246. spin_unlock_irqrestore(&uidhash_lock, flags);
  247. INIT_WORK(&up->work, remove_user_sysfs_dir);
  248. schedule_work(&up->work);
  249. }
  250. #else /* CONFIG_USER_SCHED && CONFIG_SYSFS */
  251. int uids_sysfs_init(void) { return 0; }
  252. static inline int uids_user_create(struct user_struct *up) { return 0; }
  253. static inline void uids_mutex_lock(void) { }
  254. static inline void uids_mutex_unlock(void) { }
  255. /* IRQs are disabled and uidhash_lock is held upon function entry.
  256. * IRQ state (as stored in flags) is restored and uidhash_lock released
  257. * upon function exit.
  258. */
  259. static inline void free_user(struct user_struct *up, unsigned long flags)
  260. {
  261. uid_hash_remove(up);
  262. spin_unlock_irqrestore(&uidhash_lock, flags);
  263. sched_destroy_user(up);
  264. key_put(up->uid_keyring);
  265. key_put(up->session_keyring);
  266. kmem_cache_free(uid_cachep, up);
  267. }
  268. #endif
  269. /*
  270. * Locate the user_struct for the passed UID. If found, take a ref on it. The
  271. * caller must undo that ref with free_uid().
  272. *
  273. * If the user_struct could not be found, return NULL.
  274. */
  275. struct user_struct *find_user(uid_t uid)
  276. {
  277. struct user_struct *ret;
  278. unsigned long flags;
  279. struct user_namespace *ns = current->nsproxy->user_ns;
  280. spin_lock_irqsave(&uidhash_lock, flags);
  281. ret = uid_hash_find(uid, uidhashentry(ns, uid));
  282. spin_unlock_irqrestore(&uidhash_lock, flags);
  283. return ret;
  284. }
  285. void free_uid(struct user_struct *up)
  286. {
  287. unsigned long flags;
  288. if (!up)
  289. return;
  290. local_irq_save(flags);
  291. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  292. free_user(up, flags);
  293. else
  294. local_irq_restore(flags);
  295. }
  296. struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
  297. {
  298. struct hlist_head *hashent = uidhashentry(ns, uid);
  299. struct user_struct *up, *new;
  300. /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
  301. * atomic.
  302. */
  303. uids_mutex_lock();
  304. spin_lock_irq(&uidhash_lock);
  305. up = uid_hash_find(uid, hashent);
  306. spin_unlock_irq(&uidhash_lock);
  307. if (!up) {
  308. new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
  309. if (!new)
  310. goto out_unlock;
  311. new->uid = uid;
  312. atomic_set(&new->__count, 1);
  313. atomic_set(&new->processes, 0);
  314. atomic_set(&new->files, 0);
  315. atomic_set(&new->sigpending, 0);
  316. #ifdef CONFIG_INOTIFY_USER
  317. atomic_set(&new->inotify_watches, 0);
  318. atomic_set(&new->inotify_devs, 0);
  319. #endif
  320. #ifdef CONFIG_POSIX_MQUEUE
  321. new->mq_bytes = 0;
  322. #endif
  323. new->locked_shm = 0;
  324. if (alloc_uid_keyring(new, current) < 0)
  325. goto out_free_user;
  326. if (sched_create_user(new) < 0)
  327. goto out_put_keys;
  328. if (uids_user_create(new))
  329. goto out_destoy_sched;
  330. /*
  331. * Before adding this, check whether we raced
  332. * on adding the same user already..
  333. */
  334. spin_lock_irq(&uidhash_lock);
  335. up = uid_hash_find(uid, hashent);
  336. if (up) {
  337. /* This case is not possible when CONFIG_USER_SCHED
  338. * is defined, since we serialize alloc_uid() using
  339. * uids_mutex. Hence no need to call
  340. * sched_destroy_user() or remove_user_sysfs_dir().
  341. */
  342. key_put(new->uid_keyring);
  343. key_put(new->session_keyring);
  344. kmem_cache_free(uid_cachep, new);
  345. } else {
  346. uid_hash_insert(new, hashent);
  347. up = new;
  348. }
  349. spin_unlock_irq(&uidhash_lock);
  350. }
  351. uids_mutex_unlock();
  352. return up;
  353. out_destoy_sched:
  354. sched_destroy_user(new);
  355. out_put_keys:
  356. key_put(new->uid_keyring);
  357. key_put(new->session_keyring);
  358. out_free_user:
  359. kmem_cache_free(uid_cachep, new);
  360. out_unlock:
  361. uids_mutex_unlock();
  362. return NULL;
  363. }
  364. void switch_uid(struct user_struct *new_user)
  365. {
  366. struct user_struct *old_user;
  367. /* What if a process setreuid()'s and this brings the
  368. * new uid over his NPROC rlimit? We can check this now
  369. * cheaply with the new uid cache, so if it matters
  370. * we should be checking for it. -DaveM
  371. */
  372. old_user = current->user;
  373. atomic_inc(&new_user->processes);
  374. atomic_dec(&old_user->processes);
  375. switch_uid_keyring(new_user);
  376. current->user = new_user;
  377. sched_switch_user(current);
  378. /*
  379. * We need to synchronize with __sigqueue_alloc()
  380. * doing a get_uid(p->user).. If that saw the old
  381. * user value, we need to wait until it has exited
  382. * its critical region before we can free the old
  383. * structure.
  384. */
  385. smp_mb();
  386. spin_unlock_wait(&current->sighand->siglock);
  387. free_uid(old_user);
  388. suid_keys(current);
  389. }
  390. #ifdef CONFIG_USER_NS
  391. void release_uids(struct user_namespace *ns)
  392. {
  393. int i;
  394. unsigned long flags;
  395. struct hlist_head *head;
  396. struct hlist_node *nd;
  397. spin_lock_irqsave(&uidhash_lock, flags);
  398. /*
  399. * collapse the chains so that the user_struct-s will
  400. * be still alive, but not in hashes. subsequent free_uid()
  401. * will free them.
  402. */
  403. for (i = 0; i < UIDHASH_SZ; i++) {
  404. head = ns->uidhash_table + i;
  405. while (!hlist_empty(head)) {
  406. nd = head->first;
  407. hlist_del_init(nd);
  408. }
  409. }
  410. spin_unlock_irqrestore(&uidhash_lock, flags);
  411. free_uid(ns->root_user);
  412. }
  413. #endif
  414. static int __init uid_cache_init(void)
  415. {
  416. int n;
  417. uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
  418. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  419. for(n = 0; n < UIDHASH_SZ; ++n)
  420. INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
  421. /* Insert the root user immediately (init already runs as root) */
  422. spin_lock_irq(&uidhash_lock);
  423. uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
  424. spin_unlock_irq(&uidhash_lock);
  425. return 0;
  426. }
  427. module_init(uid_cache_init);