ucount.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation, version 2 of the
  5. * License.
  6. */
  7. #include <linux/stat.h>
  8. #include <linux/sysctl.h>
  9. #include <linux/slab.h>
  10. #include <linux/cred.h>
  11. #include <linux/hash.h>
  12. #include <linux/kmemleak.h>
  13. #include <linux/user_namespace.h>
  14. #define UCOUNTS_HASHTABLE_BITS 10
  15. static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
  16. static DEFINE_SPINLOCK(ucounts_lock);
  17. #define ucounts_hashfn(ns, uid) \
  18. hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
  19. UCOUNTS_HASHTABLE_BITS)
  20. #define ucounts_hashentry(ns, uid) \
  21. (ucounts_hashtable + ucounts_hashfn(ns, uid))
  22. #ifdef CONFIG_SYSCTL
  23. static struct ctl_table_set *
  24. set_lookup(struct ctl_table_root *root)
  25. {
  26. return &current_user_ns()->set;
  27. }
  28. static int set_is_seen(struct ctl_table_set *set)
  29. {
  30. return &current_user_ns()->set == set;
  31. }
  32. static int set_permissions(struct ctl_table_header *head,
  33. struct ctl_table *table)
  34. {
  35. struct user_namespace *user_ns =
  36. container_of(head->set, struct user_namespace, set);
  37. int mode;
  38. /* Allow users with CAP_SYS_RESOURCE unrestrained access */
  39. if (ns_capable(user_ns, CAP_SYS_RESOURCE))
  40. mode = (table->mode & S_IRWXU) >> 6;
  41. else
  42. /* Allow all others at most read-only access */
  43. mode = table->mode & S_IROTH;
  44. return (mode << 6) | (mode << 3) | mode;
  45. }
  46. static struct ctl_table_root set_root = {
  47. .lookup = set_lookup,
  48. .permissions = set_permissions,
  49. };
  50. static int zero = 0;
  51. static int int_max = INT_MAX;
  52. #define UCOUNT_ENTRY(name) \
  53. { \
  54. .procname = name, \
  55. .maxlen = sizeof(int), \
  56. .mode = 0644, \
  57. .proc_handler = proc_dointvec_minmax, \
  58. .extra1 = &zero, \
  59. .extra2 = &int_max, \
  60. }
  61. static struct ctl_table user_table[] = {
  62. UCOUNT_ENTRY("max_user_namespaces"),
  63. UCOUNT_ENTRY("max_pid_namespaces"),
  64. UCOUNT_ENTRY("max_uts_namespaces"),
  65. UCOUNT_ENTRY("max_ipc_namespaces"),
  66. UCOUNT_ENTRY("max_net_namespaces"),
  67. UCOUNT_ENTRY("max_mnt_namespaces"),
  68. UCOUNT_ENTRY("max_cgroup_namespaces"),
  69. #ifdef CONFIG_INOTIFY_USER
  70. UCOUNT_ENTRY("max_inotify_instances"),
  71. UCOUNT_ENTRY("max_inotify_watches"),
  72. #endif
  73. { }
  74. };
  75. #endif /* CONFIG_SYSCTL */
  76. bool setup_userns_sysctls(struct user_namespace *ns)
  77. {
  78. #ifdef CONFIG_SYSCTL
  79. struct ctl_table *tbl;
  80. setup_sysctl_set(&ns->set, &set_root, set_is_seen);
  81. tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
  82. if (tbl) {
  83. int i;
  84. for (i = 0; i < UCOUNT_COUNTS; i++) {
  85. tbl[i].data = &ns->ucount_max[i];
  86. }
  87. ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
  88. }
  89. if (!ns->sysctls) {
  90. kfree(tbl);
  91. retire_sysctl_set(&ns->set);
  92. return false;
  93. }
  94. #endif
  95. return true;
  96. }
  97. void retire_userns_sysctls(struct user_namespace *ns)
  98. {
  99. #ifdef CONFIG_SYSCTL
  100. struct ctl_table *tbl;
  101. tbl = ns->sysctls->ctl_table_arg;
  102. unregister_sysctl_table(ns->sysctls);
  103. retire_sysctl_set(&ns->set);
  104. kfree(tbl);
  105. #endif
  106. }
  107. static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
  108. {
  109. struct ucounts *ucounts;
  110. hlist_for_each_entry(ucounts, hashent, node) {
  111. if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
  112. return ucounts;
  113. }
  114. return NULL;
  115. }
  116. static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
  117. {
  118. struct hlist_head *hashent = ucounts_hashentry(ns, uid);
  119. struct ucounts *ucounts, *new;
  120. spin_lock_irq(&ucounts_lock);
  121. ucounts = find_ucounts(ns, uid, hashent);
  122. if (!ucounts) {
  123. spin_unlock_irq(&ucounts_lock);
  124. new = kzalloc(sizeof(*new), GFP_KERNEL);
  125. if (!new)
  126. return NULL;
  127. new->ns = ns;
  128. new->uid = uid;
  129. new->count = 0;
  130. spin_lock_irq(&ucounts_lock);
  131. ucounts = find_ucounts(ns, uid, hashent);
  132. if (ucounts) {
  133. kfree(new);
  134. } else {
  135. hlist_add_head(&new->node, hashent);
  136. ucounts = new;
  137. }
  138. }
  139. if (ucounts->count == INT_MAX)
  140. ucounts = NULL;
  141. else
  142. ucounts->count += 1;
  143. spin_unlock_irq(&ucounts_lock);
  144. return ucounts;
  145. }
  146. static void put_ucounts(struct ucounts *ucounts)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&ucounts_lock, flags);
  150. ucounts->count -= 1;
  151. if (!ucounts->count)
  152. hlist_del_init(&ucounts->node);
  153. else
  154. ucounts = NULL;
  155. spin_unlock_irqrestore(&ucounts_lock, flags);
  156. kfree(ucounts);
  157. }
  158. static inline bool atomic_inc_below(atomic_t *v, int u)
  159. {
  160. int c, old;
  161. c = atomic_read(v);
  162. for (;;) {
  163. if (unlikely(c >= u))
  164. return false;
  165. old = atomic_cmpxchg(v, c, c+1);
  166. if (likely(old == c))
  167. return true;
  168. c = old;
  169. }
  170. }
  171. struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
  172. enum ucount_type type)
  173. {
  174. struct ucounts *ucounts, *iter, *bad;
  175. struct user_namespace *tns;
  176. ucounts = get_ucounts(ns, uid);
  177. for (iter = ucounts; iter; iter = tns->ucounts) {
  178. int max;
  179. tns = iter->ns;
  180. max = READ_ONCE(tns->ucount_max[type]);
  181. if (!atomic_inc_below(&iter->ucount[type], max))
  182. goto fail;
  183. }
  184. return ucounts;
  185. fail:
  186. bad = iter;
  187. for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
  188. atomic_dec(&iter->ucount[type]);
  189. put_ucounts(ucounts);
  190. return NULL;
  191. }
  192. void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
  193. {
  194. struct ucounts *iter;
  195. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  196. int dec = atomic_dec_if_positive(&iter->ucount[type]);
  197. WARN_ON_ONCE(dec < 0);
  198. }
  199. put_ucounts(ucounts);
  200. }
  201. static __init int user_namespace_sysctl_init(void)
  202. {
  203. #ifdef CONFIG_SYSCTL
  204. static struct ctl_table_header *user_header;
  205. static struct ctl_table empty[1];
  206. /*
  207. * It is necessary to register the user directory in the
  208. * default set so that registrations in the child sets work
  209. * properly.
  210. */
  211. user_header = register_sysctl("user", empty);
  212. kmemleak_ignore(user_header);
  213. BUG_ON(!user_header);
  214. BUG_ON(!setup_userns_sysctls(&init_user_ns));
  215. #endif
  216. return 0;
  217. }
  218. subsys_initcall(user_namespace_sysctl_init);