user.c 13 KB

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