ucount.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. struct ucounts init_ucounts = {
  10. .ns = &init_user_ns,
  11. .uid = GLOBAL_ROOT_UID,
  12. .count = RCUREF_INIT(1),
  13. };
  14. #define UCOUNTS_HASHTABLE_BITS 10
  15. #define UCOUNTS_HASHTABLE_ENTRIES (1 << UCOUNTS_HASHTABLE_BITS)
  16. static struct hlist_nulls_head ucounts_hashtable[UCOUNTS_HASHTABLE_ENTRIES] = {
  17. [0 ... UCOUNTS_HASHTABLE_ENTRIES - 1] = HLIST_NULLS_HEAD_INIT(0)
  18. };
  19. static DEFINE_SPINLOCK(ucounts_lock);
  20. #define ucounts_hashfn(ns, uid) \
  21. hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
  22. UCOUNTS_HASHTABLE_BITS)
  23. #define ucounts_hashentry(ns, uid) \
  24. (ucounts_hashtable + ucounts_hashfn(ns, uid))
  25. #ifdef CONFIG_SYSCTL
  26. static struct ctl_table_set *
  27. set_lookup(struct ctl_table_root *root)
  28. {
  29. return &current_user_ns()->set;
  30. }
  31. static int set_is_seen(struct ctl_table_set *set)
  32. {
  33. return &current_user_ns()->set == set;
  34. }
  35. static int set_permissions(struct ctl_table_header *head,
  36. const struct ctl_table *table)
  37. {
  38. struct user_namespace *user_ns =
  39. container_of(head->set, struct user_namespace, set);
  40. int mode;
  41. /* Allow users with CAP_SYS_RESOURCE unrestrained access */
  42. if (ns_capable_noaudit(user_ns, CAP_SYS_RESOURCE))
  43. mode = (table->mode & S_IRWXU) >> 6;
  44. else
  45. /* Allow all others at most read-only access */
  46. mode = table->mode & S_IROTH;
  47. return (mode << 6) | (mode << 3) | mode;
  48. }
  49. static struct ctl_table_root set_root = {
  50. .lookup = set_lookup,
  51. .permissions = set_permissions,
  52. };
  53. static long ue_zero = 0;
  54. static long ue_int_max = INT_MAX;
  55. #define UCOUNT_ENTRY(name) \
  56. { \
  57. .procname = name, \
  58. .maxlen = sizeof(long), \
  59. .mode = 0644, \
  60. .proc_handler = proc_doulongvec_minmax, \
  61. .extra1 = &ue_zero, \
  62. .extra2 = &ue_int_max, \
  63. }
  64. static const struct ctl_table user_table[] = {
  65. UCOUNT_ENTRY("max_user_namespaces"),
  66. UCOUNT_ENTRY("max_pid_namespaces"),
  67. UCOUNT_ENTRY("max_uts_namespaces"),
  68. UCOUNT_ENTRY("max_ipc_namespaces"),
  69. UCOUNT_ENTRY("max_net_namespaces"),
  70. UCOUNT_ENTRY("max_mnt_namespaces"),
  71. UCOUNT_ENTRY("max_cgroup_namespaces"),
  72. UCOUNT_ENTRY("max_time_namespaces"),
  73. #ifdef CONFIG_INOTIFY_USER
  74. UCOUNT_ENTRY("max_inotify_instances"),
  75. UCOUNT_ENTRY("max_inotify_watches"),
  76. #endif
  77. #ifdef CONFIG_FANOTIFY
  78. UCOUNT_ENTRY("max_fanotify_groups"),
  79. UCOUNT_ENTRY("max_fanotify_marks"),
  80. #endif
  81. };
  82. #endif /* CONFIG_SYSCTL */
  83. bool setup_userns_sysctls(struct user_namespace *ns)
  84. {
  85. #ifdef CONFIG_SYSCTL
  86. struct ctl_table *tbl;
  87. BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS);
  88. setup_sysctl_set(&ns->set, &set_root, set_is_seen);
  89. tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
  90. if (tbl) {
  91. int i;
  92. for (i = 0; i < UCOUNT_COUNTS; i++) {
  93. tbl[i].data = &ns->ucount_max[i];
  94. }
  95. ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl,
  96. ARRAY_SIZE(user_table));
  97. }
  98. if (!ns->sysctls) {
  99. kfree(tbl);
  100. retire_sysctl_set(&ns->set);
  101. return false;
  102. }
  103. #endif
  104. return true;
  105. }
  106. void retire_userns_sysctls(struct user_namespace *ns)
  107. {
  108. #ifdef CONFIG_SYSCTL
  109. const struct ctl_table *tbl;
  110. tbl = ns->sysctls->ctl_table_arg;
  111. unregister_sysctl_table(ns->sysctls);
  112. retire_sysctl_set(&ns->set);
  113. kfree(tbl);
  114. #endif
  115. }
  116. static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid,
  117. struct hlist_nulls_head *hashent)
  118. {
  119. struct ucounts *ucounts;
  120. struct hlist_nulls_node *pos;
  121. guard(rcu)();
  122. hlist_nulls_for_each_entry_rcu(ucounts, pos, hashent, node) {
  123. if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns)) {
  124. if (rcuref_get(&ucounts->count))
  125. return ucounts;
  126. }
  127. }
  128. return NULL;
  129. }
  130. static void hlist_add_ucounts(struct ucounts *ucounts)
  131. {
  132. struct hlist_nulls_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
  133. spin_lock_irq(&ucounts_lock);
  134. hlist_nulls_add_head_rcu(&ucounts->node, hashent);
  135. spin_unlock_irq(&ucounts_lock);
  136. }
  137. struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
  138. {
  139. struct hlist_nulls_head *hashent = ucounts_hashentry(ns, uid);
  140. struct ucounts *ucounts, *new;
  141. ucounts = find_ucounts(ns, uid, hashent);
  142. if (ucounts)
  143. return ucounts;
  144. new = kzalloc_obj(*new);
  145. if (!new)
  146. return NULL;
  147. new->ns = ns;
  148. new->uid = uid;
  149. rcuref_init(&new->count, 1);
  150. spin_lock_irq(&ucounts_lock);
  151. ucounts = find_ucounts(ns, uid, hashent);
  152. if (ucounts) {
  153. spin_unlock_irq(&ucounts_lock);
  154. kfree(new);
  155. return ucounts;
  156. }
  157. hlist_nulls_add_head_rcu(&new->node, hashent);
  158. get_user_ns(new->ns);
  159. spin_unlock_irq(&ucounts_lock);
  160. return new;
  161. }
  162. void put_ucounts(struct ucounts *ucounts)
  163. {
  164. unsigned long flags;
  165. if (rcuref_put(&ucounts->count)) {
  166. spin_lock_irqsave(&ucounts_lock, flags);
  167. hlist_nulls_del_rcu(&ucounts->node);
  168. spin_unlock_irqrestore(&ucounts_lock, flags);
  169. put_user_ns(ucounts->ns);
  170. kfree_rcu(ucounts, rcu);
  171. }
  172. }
  173. static inline bool atomic_long_inc_below(atomic_long_t *v, long u)
  174. {
  175. long c = atomic_long_read(v);
  176. do {
  177. if (unlikely(c >= u))
  178. return false;
  179. } while (!atomic_long_try_cmpxchg(v, &c, c+1));
  180. return true;
  181. }
  182. struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
  183. enum ucount_type type)
  184. {
  185. struct ucounts *ucounts, *iter, *bad;
  186. struct user_namespace *tns;
  187. ucounts = alloc_ucounts(ns, uid);
  188. for (iter = ucounts; iter; iter = tns->ucounts) {
  189. long max;
  190. tns = iter->ns;
  191. max = READ_ONCE(tns->ucount_max[type]);
  192. if (!atomic_long_inc_below(&iter->ucount[type], max))
  193. goto fail;
  194. }
  195. return ucounts;
  196. fail:
  197. bad = iter;
  198. for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
  199. atomic_long_dec(&iter->ucount[type]);
  200. put_ucounts(ucounts);
  201. return NULL;
  202. }
  203. void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
  204. {
  205. struct ucounts *iter;
  206. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  207. long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
  208. WARN_ON_ONCE(dec < 0);
  209. }
  210. put_ucounts(ucounts);
  211. }
  212. long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
  213. {
  214. struct ucounts *iter;
  215. long max = LONG_MAX;
  216. long ret = 0;
  217. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  218. long new = atomic_long_add_return(v, &iter->rlimit[type]);
  219. if (new < 0 || new > max)
  220. ret = LONG_MAX;
  221. else if (iter == ucounts)
  222. ret = new;
  223. max = get_userns_rlimit_max(iter->ns, type);
  224. }
  225. return ret;
  226. }
  227. bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
  228. {
  229. struct ucounts *iter;
  230. long new = -1; /* Silence compiler warning */
  231. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  232. long dec = atomic_long_sub_return(v, &iter->rlimit[type]);
  233. WARN_ON_ONCE(dec < 0);
  234. if (iter == ucounts)
  235. new = dec;
  236. }
  237. return (new == 0);
  238. }
  239. static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts,
  240. struct ucounts *last, enum rlimit_type type)
  241. {
  242. struct ucounts *iter, *next;
  243. for (iter = ucounts; iter != last; iter = next) {
  244. long dec = atomic_long_sub_return(1, &iter->rlimit[type]);
  245. WARN_ON_ONCE(dec < 0);
  246. next = iter->ns->ucounts;
  247. if (dec == 0)
  248. put_ucounts(iter);
  249. }
  250. }
  251. void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type)
  252. {
  253. do_dec_rlimit_put_ucounts(ucounts, NULL, type);
  254. }
  255. long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
  256. bool override_rlimit)
  257. {
  258. /* Caller must hold a reference to ucounts */
  259. struct ucounts *iter;
  260. long max = LONG_MAX;
  261. long dec, ret = 0;
  262. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  263. long new = atomic_long_add_return(1, &iter->rlimit[type]);
  264. if (new < 0 || new > max)
  265. goto dec_unwind;
  266. if (iter == ucounts)
  267. ret = new;
  268. if (!override_rlimit)
  269. max = get_userns_rlimit_max(iter->ns, type);
  270. /*
  271. * Grab an extra ucount reference for the caller when
  272. * the rlimit count was previously 0.
  273. */
  274. if (new != 1)
  275. continue;
  276. if (!get_ucounts(iter))
  277. goto dec_unwind;
  278. }
  279. return ret;
  280. dec_unwind:
  281. dec = atomic_long_sub_return(1, &iter->rlimit[type]);
  282. WARN_ON_ONCE(dec < 0);
  283. do_dec_rlimit_put_ucounts(ucounts, iter, type);
  284. return 0;
  285. }
  286. bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long rlimit)
  287. {
  288. struct ucounts *iter;
  289. long max = rlimit;
  290. if (rlimit > LONG_MAX)
  291. max = LONG_MAX;
  292. for (iter = ucounts; iter; iter = iter->ns->ucounts) {
  293. long val = get_rlimit_value(iter, type);
  294. if (val < 0 || val > max)
  295. return true;
  296. max = get_userns_rlimit_max(iter->ns, type);
  297. }
  298. return false;
  299. }
  300. static __init int user_namespace_sysctl_init(void)
  301. {
  302. #ifdef CONFIG_SYSCTL
  303. static struct ctl_table_header *user_header;
  304. static struct ctl_table empty[1];
  305. /*
  306. * It is necessary to register the user directory in the
  307. * default set so that registrations in the child sets work
  308. * properly.
  309. */
  310. user_header = register_sysctl_sz("user", empty, 0);
  311. kmemleak_ignore(user_header);
  312. BUG_ON(!user_header);
  313. BUG_ON(!setup_userns_sysctls(&init_user_ns));
  314. #endif
  315. hlist_add_ucounts(&init_ucounts);
  316. inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
  317. return 0;
  318. }
  319. subsys_initcall(user_namespace_sysctl_init);