ucount.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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(&ucounts_lock);
  115. ucounts = find_ucounts(ns, uid, hashent);
  116. if (!ucounts) {
  117. spin_unlock(&ucounts_lock);
  118. new = kzalloc(sizeof(*new), GFP_KERNEL);
  119. if (!new)
  120. return NULL;
  121. new->ns = ns;
  122. new->uid = uid;
  123. atomic_set(&new->count, 0);
  124. spin_lock(&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 (!atomic_add_unless(&ucounts->count, 1, INT_MAX))
  134. ucounts = NULL;
  135. spin_unlock(&ucounts_lock);
  136. return ucounts;
  137. }
  138. static void put_ucounts(struct ucounts *ucounts)
  139. {
  140. if (atomic_dec_and_test(&ucounts->count)) {
  141. spin_lock(&ucounts_lock);
  142. hlist_del_init(&ucounts->node);
  143. spin_unlock(&ucounts_lock);
  144. kfree(ucounts);
  145. }
  146. }
  147. static inline bool atomic_inc_below(atomic_t *v, int u)
  148. {
  149. int c, old;
  150. c = atomic_read(v);
  151. for (;;) {
  152. if (unlikely(c >= u))
  153. return false;
  154. old = atomic_cmpxchg(v, c, c+1);
  155. if (likely(old == c))
  156. return true;
  157. c = old;
  158. }
  159. }
  160. struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
  161. enum ucount_type type)
  162. {
  163. struct ucounts *ucounts, *iter, *bad;
  164. struct user_namespace *tns;
  165. ucounts = get_ucounts(ns, uid);
  166. for (iter = ucounts; iter; iter = tns->ucounts) {
  167. int max;
  168. tns = iter->ns;
  169. max = READ_ONCE(tns->ucount_max[type]);
  170. if (!atomic_inc_below(&iter->ucount[type], max))
  171. goto fail;
  172. }
  173. return ucounts;
  174. fail:
  175. bad = iter;
  176. for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
  177. atomic_dec(&iter->ucount[type]);
  178. put_ucounts(ucounts);
  179. return NULL;
  180. }
  181. void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
  182. {
  183. struct ucounts *iter;
  184. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  185. int dec = atomic_dec_if_positive(&iter->ucount[type]);
  186. WARN_ON_ONCE(dec < 0);
  187. }
  188. put_ucounts(ucounts);
  189. }
  190. static __init int user_namespace_sysctl_init(void)
  191. {
  192. #ifdef CONFIG_SYSCTL
  193. static struct ctl_table_header *user_header;
  194. static struct ctl_table empty[1];
  195. /*
  196. * It is necessary to register the user directory in the
  197. * default set so that registrations in the child sets work
  198. * properly.
  199. */
  200. user_header = register_sysctl("user", empty);
  201. BUG_ON(!user_header);
  202. BUG_ON(!setup_userns_sysctls(&init_user_ns));
  203. #endif
  204. return 0;
  205. }
  206. subsys_initcall(user_namespace_sysctl_init);