user.c 12 KB

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