user.c 12 KB

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