ucount.c 5.6 KB

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