ucount.c 5.5 KB

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