user.c 12 KB

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