ucount.c 5.6 KB

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