user_namespace.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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/export.h>
  8. #include <linux/nsproxy.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/user_namespace.h>
  12. #include <linux/proc_ns.h>
  13. #include <linux/highuid.h>
  14. #include <linux/cred.h>
  15. #include <linux/securebits.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/key-type.h>
  18. #include <keys/user-type.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/fs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ctype.h>
  23. #include <linux/projid.h>
  24. #include <linux/fs_struct.h>
  25. #include <linux/bsearch.h>
  26. #include <linux/sort.h>
  27. static struct kmem_cache *user_ns_cachep __read_mostly;
  28. static DEFINE_MUTEX(userns_state_mutex);
  29. static bool new_idmap_permitted(const struct file *file,
  30. struct user_namespace *ns, int cap_setid,
  31. struct uid_gid_map *map);
  32. static void free_user_ns(struct work_struct *work);
  33. static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid)
  34. {
  35. return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES);
  36. }
  37. static void dec_user_namespaces(struct ucounts *ucounts)
  38. {
  39. return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES);
  40. }
  41. static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
  42. {
  43. /* Start with the same capabilities as init but useless for doing
  44. * anything as the capabilities are bound to the new user namespace.
  45. */
  46. cred->securebits = SECUREBITS_DEFAULT;
  47. cred->cap_inheritable = CAP_EMPTY_SET;
  48. cred->cap_permitted = CAP_FULL_SET;
  49. cred->cap_effective = CAP_FULL_SET;
  50. cred->cap_ambient = CAP_EMPTY_SET;
  51. cred->cap_bset = CAP_FULL_SET;
  52. #ifdef CONFIG_KEYS
  53. key_put(cred->request_key_auth);
  54. cred->request_key_auth = NULL;
  55. #endif
  56. /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
  57. cred->user_ns = user_ns;
  58. }
  59. /*
  60. * Create a new user namespace, deriving the creator from the user in the
  61. * passed credentials, and replacing that user with the new root user for the
  62. * new namespace.
  63. *
  64. * This is called by copy_creds(), which will finish setting the target task's
  65. * credentials.
  66. */
  67. int create_user_ns(struct cred *new)
  68. {
  69. struct user_namespace *ns, *parent_ns = new->user_ns;
  70. kuid_t owner = new->euid;
  71. kgid_t group = new->egid;
  72. struct ucounts *ucounts;
  73. int ret, i;
  74. ret = -ENOSPC;
  75. if (parent_ns->level > 32)
  76. goto fail;
  77. ucounts = inc_user_namespaces(parent_ns, owner);
  78. if (!ucounts)
  79. goto fail;
  80. /*
  81. * Verify that we can not violate the policy of which files
  82. * may be accessed that is specified by the root directory,
  83. * by verifing that the root directory is at the root of the
  84. * mount namespace which allows all files to be accessed.
  85. */
  86. ret = -EPERM;
  87. if (current_chrooted())
  88. goto fail_dec;
  89. /* The creator needs a mapping in the parent user namespace
  90. * or else we won't be able to reasonably tell userspace who
  91. * created a user_namespace.
  92. */
  93. ret = -EPERM;
  94. if (!kuid_has_mapping(parent_ns, owner) ||
  95. !kgid_has_mapping(parent_ns, group))
  96. goto fail_dec;
  97. ret = -ENOMEM;
  98. ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
  99. if (!ns)
  100. goto fail_dec;
  101. ret = ns_alloc_inum(&ns->ns);
  102. if (ret)
  103. goto fail_free;
  104. ns->ns.ops = &userns_operations;
  105. atomic_set(&ns->count, 1);
  106. /* Leave the new->user_ns reference with the new user namespace. */
  107. ns->parent = parent_ns;
  108. ns->level = parent_ns->level + 1;
  109. ns->owner = owner;
  110. ns->group = group;
  111. INIT_WORK(&ns->work, free_user_ns);
  112. for (i = 0; i < UCOUNT_COUNTS; i++) {
  113. ns->ucount_max[i] = INT_MAX;
  114. }
  115. ns->ucounts = ucounts;
  116. /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
  117. mutex_lock(&userns_state_mutex);
  118. ns->flags = parent_ns->flags;
  119. mutex_unlock(&userns_state_mutex);
  120. #ifdef CONFIG_PERSISTENT_KEYRINGS
  121. init_rwsem(&ns->persistent_keyring_register_sem);
  122. #endif
  123. ret = -ENOMEM;
  124. if (!setup_userns_sysctls(ns))
  125. goto fail_keyring;
  126. set_cred_user_ns(new, ns);
  127. return 0;
  128. fail_keyring:
  129. #ifdef CONFIG_PERSISTENT_KEYRINGS
  130. key_put(ns->persistent_keyring_register);
  131. #endif
  132. ns_free_inum(&ns->ns);
  133. fail_free:
  134. kmem_cache_free(user_ns_cachep, ns);
  135. fail_dec:
  136. dec_user_namespaces(ucounts);
  137. fail:
  138. return ret;
  139. }
  140. int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
  141. {
  142. struct cred *cred;
  143. int err = -ENOMEM;
  144. if (!(unshare_flags & CLONE_NEWUSER))
  145. return 0;
  146. cred = prepare_creds();
  147. if (cred) {
  148. err = create_user_ns(cred);
  149. if (err)
  150. put_cred(cred);
  151. else
  152. *new_cred = cred;
  153. }
  154. return err;
  155. }
  156. static void free_user_ns(struct work_struct *work)
  157. {
  158. struct user_namespace *parent, *ns =
  159. container_of(work, struct user_namespace, work);
  160. do {
  161. struct ucounts *ucounts = ns->ucounts;
  162. parent = ns->parent;
  163. if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  164. kfree(ns->gid_map.forward);
  165. kfree(ns->gid_map.reverse);
  166. }
  167. if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  168. kfree(ns->uid_map.forward);
  169. kfree(ns->uid_map.reverse);
  170. }
  171. if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  172. kfree(ns->projid_map.forward);
  173. kfree(ns->projid_map.reverse);
  174. }
  175. retire_userns_sysctls(ns);
  176. #ifdef CONFIG_PERSISTENT_KEYRINGS
  177. key_put(ns->persistent_keyring_register);
  178. #endif
  179. ns_free_inum(&ns->ns);
  180. kmem_cache_free(user_ns_cachep, ns);
  181. dec_user_namespaces(ucounts);
  182. ns = parent;
  183. } while (atomic_dec_and_test(&parent->count));
  184. }
  185. void __put_user_ns(struct user_namespace *ns)
  186. {
  187. schedule_work(&ns->work);
  188. }
  189. EXPORT_SYMBOL(__put_user_ns);
  190. /**
  191. * idmap_key struct holds the information necessary to find an idmapping in a
  192. * sorted idmap array. It is passed to cmp_map_id() as first argument.
  193. */
  194. struct idmap_key {
  195. bool map_up; /* true -> id from kid; false -> kid from id */
  196. u32 id; /* id to find */
  197. u32 count; /* == 0 unless used with map_id_range_down() */
  198. };
  199. /**
  200. * cmp_map_id - Function to be passed to bsearch() to find the requested
  201. * idmapping. Expects struct idmap_key to be passed via @k.
  202. */
  203. static int cmp_map_id(const void *k, const void *e)
  204. {
  205. u32 first, last, id2;
  206. const struct idmap_key *key = k;
  207. const struct uid_gid_extent *el = e;
  208. id2 = key->id + key->count - 1;
  209. /* handle map_id_{down,up}() */
  210. if (key->map_up)
  211. first = el->lower_first;
  212. else
  213. first = el->first;
  214. last = first + el->count - 1;
  215. if (key->id >= first && key->id <= last &&
  216. (id2 >= first && id2 <= last))
  217. return 0;
  218. if (key->id < first || id2 < first)
  219. return -1;
  220. return 1;
  221. }
  222. /**
  223. * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
  224. * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
  225. */
  226. static struct uid_gid_extent *
  227. map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
  228. {
  229. struct idmap_key key;
  230. key.map_up = false;
  231. key.count = count;
  232. key.id = id;
  233. return bsearch(&key, map->forward, extents,
  234. sizeof(struct uid_gid_extent), cmp_map_id);
  235. }
  236. /**
  237. * map_id_range_down_base - Find idmap via binary search in static extent array.
  238. * Can only be called if number of mappings is equal or less than
  239. * UID_GID_MAP_MAX_BASE_EXTENTS.
  240. */
  241. static struct uid_gid_extent *
  242. map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
  243. {
  244. unsigned idx;
  245. u32 first, last, id2;
  246. id2 = id + count - 1;
  247. /* Find the matching extent */
  248. for (idx = 0; idx < extents; idx++) {
  249. first = map->extent[idx].first;
  250. last = first + map->extent[idx].count - 1;
  251. if (id >= first && id <= last &&
  252. (id2 >= first && id2 <= last))
  253. return &map->extent[idx];
  254. }
  255. return NULL;
  256. }
  257. static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
  258. {
  259. struct uid_gid_extent *extent;
  260. unsigned extents = map->nr_extents;
  261. smp_rmb();
  262. if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  263. extent = map_id_range_down_base(extents, map, id, count);
  264. else
  265. extent = map_id_range_down_max(extents, map, id, count);
  266. /* Map the id or note failure */
  267. if (extent)
  268. id = (id - extent->first) + extent->lower_first;
  269. else
  270. id = (u32) -1;
  271. return id;
  272. }
  273. static u32 map_id_down(struct uid_gid_map *map, u32 id)
  274. {
  275. return map_id_range_down(map, id, 1);
  276. }
  277. /**
  278. * map_id_up_base - Find idmap via binary search in static extent array.
  279. * Can only be called if number of mappings is equal or less than
  280. * UID_GID_MAP_MAX_BASE_EXTENTS.
  281. */
  282. static struct uid_gid_extent *
  283. map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id)
  284. {
  285. unsigned idx;
  286. u32 first, last;
  287. /* Find the matching extent */
  288. for (idx = 0; idx < extents; idx++) {
  289. first = map->extent[idx].lower_first;
  290. last = first + map->extent[idx].count - 1;
  291. if (id >= first && id <= last)
  292. return &map->extent[idx];
  293. }
  294. return NULL;
  295. }
  296. /**
  297. * map_id_up_max - Find idmap via binary search in ordered idmap array.
  298. * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
  299. */
  300. static struct uid_gid_extent *
  301. map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id)
  302. {
  303. struct idmap_key key;
  304. key.map_up = true;
  305. key.count = 1;
  306. key.id = id;
  307. return bsearch(&key, map->reverse, extents,
  308. sizeof(struct uid_gid_extent), cmp_map_id);
  309. }
  310. static u32 map_id_up(struct uid_gid_map *map, u32 id)
  311. {
  312. struct uid_gid_extent *extent;
  313. unsigned extents = map->nr_extents;
  314. smp_rmb();
  315. if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  316. extent = map_id_up_base(extents, map, id);
  317. else
  318. extent = map_id_up_max(extents, map, id);
  319. /* Map the id or note failure */
  320. if (extent)
  321. id = (id - extent->lower_first) + extent->first;
  322. else
  323. id = (u32) -1;
  324. return id;
  325. }
  326. /**
  327. * make_kuid - Map a user-namespace uid pair into a kuid.
  328. * @ns: User namespace that the uid is in
  329. * @uid: User identifier
  330. *
  331. * Maps a user-namespace uid pair into a kernel internal kuid,
  332. * and returns that kuid.
  333. *
  334. * When there is no mapping defined for the user-namespace uid
  335. * pair INVALID_UID is returned. Callers are expected to test
  336. * for and handle INVALID_UID being returned. INVALID_UID
  337. * may be tested for using uid_valid().
  338. */
  339. kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
  340. {
  341. /* Map the uid to a global kernel uid */
  342. return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
  343. }
  344. EXPORT_SYMBOL(make_kuid);
  345. /**
  346. * from_kuid - Create a uid from a kuid user-namespace pair.
  347. * @targ: The user namespace we want a uid in.
  348. * @kuid: The kernel internal uid to start with.
  349. *
  350. * Map @kuid into the user-namespace specified by @targ and
  351. * return the resulting uid.
  352. *
  353. * There is always a mapping into the initial user_namespace.
  354. *
  355. * If @kuid has no mapping in @targ (uid_t)-1 is returned.
  356. */
  357. uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
  358. {
  359. /* Map the uid from a global kernel uid */
  360. return map_id_up(&targ->uid_map, __kuid_val(kuid));
  361. }
  362. EXPORT_SYMBOL(from_kuid);
  363. /**
  364. * from_kuid_munged - Create a uid from a kuid user-namespace pair.
  365. * @targ: The user namespace we want a uid in.
  366. * @kuid: The kernel internal uid to start with.
  367. *
  368. * Map @kuid into the user-namespace specified by @targ and
  369. * return the resulting uid.
  370. *
  371. * There is always a mapping into the initial user_namespace.
  372. *
  373. * Unlike from_kuid from_kuid_munged never fails and always
  374. * returns a valid uid. This makes from_kuid_munged appropriate
  375. * for use in syscalls like stat and getuid where failing the
  376. * system call and failing to provide a valid uid are not an
  377. * options.
  378. *
  379. * If @kuid has no mapping in @targ overflowuid is returned.
  380. */
  381. uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
  382. {
  383. uid_t uid;
  384. uid = from_kuid(targ, kuid);
  385. if (uid == (uid_t) -1)
  386. uid = overflowuid;
  387. return uid;
  388. }
  389. EXPORT_SYMBOL(from_kuid_munged);
  390. /**
  391. * make_kgid - Map a user-namespace gid pair into a kgid.
  392. * @ns: User namespace that the gid is in
  393. * @gid: group identifier
  394. *
  395. * Maps a user-namespace gid pair into a kernel internal kgid,
  396. * and returns that kgid.
  397. *
  398. * When there is no mapping defined for the user-namespace gid
  399. * pair INVALID_GID is returned. Callers are expected to test
  400. * for and handle INVALID_GID being returned. INVALID_GID may be
  401. * tested for using gid_valid().
  402. */
  403. kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
  404. {
  405. /* Map the gid to a global kernel gid */
  406. return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
  407. }
  408. EXPORT_SYMBOL(make_kgid);
  409. /**
  410. * from_kgid - Create a gid from a kgid user-namespace pair.
  411. * @targ: The user namespace we want a gid in.
  412. * @kgid: The kernel internal gid to start with.
  413. *
  414. * Map @kgid into the user-namespace specified by @targ and
  415. * return the resulting gid.
  416. *
  417. * There is always a mapping into the initial user_namespace.
  418. *
  419. * If @kgid has no mapping in @targ (gid_t)-1 is returned.
  420. */
  421. gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
  422. {
  423. /* Map the gid from a global kernel gid */
  424. return map_id_up(&targ->gid_map, __kgid_val(kgid));
  425. }
  426. EXPORT_SYMBOL(from_kgid);
  427. /**
  428. * from_kgid_munged - Create a gid from a kgid user-namespace pair.
  429. * @targ: The user namespace we want a gid in.
  430. * @kgid: The kernel internal gid to start with.
  431. *
  432. * Map @kgid into the user-namespace specified by @targ and
  433. * return the resulting gid.
  434. *
  435. * There is always a mapping into the initial user_namespace.
  436. *
  437. * Unlike from_kgid from_kgid_munged never fails and always
  438. * returns a valid gid. This makes from_kgid_munged appropriate
  439. * for use in syscalls like stat and getgid where failing the
  440. * system call and failing to provide a valid gid are not options.
  441. *
  442. * If @kgid has no mapping in @targ overflowgid is returned.
  443. */
  444. gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
  445. {
  446. gid_t gid;
  447. gid = from_kgid(targ, kgid);
  448. if (gid == (gid_t) -1)
  449. gid = overflowgid;
  450. return gid;
  451. }
  452. EXPORT_SYMBOL(from_kgid_munged);
  453. /**
  454. * make_kprojid - Map a user-namespace projid pair into a kprojid.
  455. * @ns: User namespace that the projid is in
  456. * @projid: Project identifier
  457. *
  458. * Maps a user-namespace uid pair into a kernel internal kuid,
  459. * and returns that kuid.
  460. *
  461. * When there is no mapping defined for the user-namespace projid
  462. * pair INVALID_PROJID is returned. Callers are expected to test
  463. * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
  464. * may be tested for using projid_valid().
  465. */
  466. kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
  467. {
  468. /* Map the uid to a global kernel uid */
  469. return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
  470. }
  471. EXPORT_SYMBOL(make_kprojid);
  472. /**
  473. * from_kprojid - Create a projid from a kprojid user-namespace pair.
  474. * @targ: The user namespace we want a projid in.
  475. * @kprojid: The kernel internal project identifier to start with.
  476. *
  477. * Map @kprojid into the user-namespace specified by @targ and
  478. * return the resulting projid.
  479. *
  480. * There is always a mapping into the initial user_namespace.
  481. *
  482. * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
  483. */
  484. projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
  485. {
  486. /* Map the uid from a global kernel uid */
  487. return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
  488. }
  489. EXPORT_SYMBOL(from_kprojid);
  490. /**
  491. * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
  492. * @targ: The user namespace we want a projid in.
  493. * @kprojid: The kernel internal projid to start with.
  494. *
  495. * Map @kprojid into the user-namespace specified by @targ and
  496. * return the resulting projid.
  497. *
  498. * There is always a mapping into the initial user_namespace.
  499. *
  500. * Unlike from_kprojid from_kprojid_munged never fails and always
  501. * returns a valid projid. This makes from_kprojid_munged
  502. * appropriate for use in syscalls like stat and where
  503. * failing the system call and failing to provide a valid projid are
  504. * not an options.
  505. *
  506. * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
  507. */
  508. projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
  509. {
  510. projid_t projid;
  511. projid = from_kprojid(targ, kprojid);
  512. if (projid == (projid_t) -1)
  513. projid = OVERFLOW_PROJID;
  514. return projid;
  515. }
  516. EXPORT_SYMBOL(from_kprojid_munged);
  517. static int uid_m_show(struct seq_file *seq, void *v)
  518. {
  519. struct user_namespace *ns = seq->private;
  520. struct uid_gid_extent *extent = v;
  521. struct user_namespace *lower_ns;
  522. uid_t lower;
  523. lower_ns = seq_user_ns(seq);
  524. if ((lower_ns == ns) && lower_ns->parent)
  525. lower_ns = lower_ns->parent;
  526. lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
  527. seq_printf(seq, "%10u %10u %10u\n",
  528. extent->first,
  529. lower,
  530. extent->count);
  531. return 0;
  532. }
  533. static int gid_m_show(struct seq_file *seq, void *v)
  534. {
  535. struct user_namespace *ns = seq->private;
  536. struct uid_gid_extent *extent = v;
  537. struct user_namespace *lower_ns;
  538. gid_t lower;
  539. lower_ns = seq_user_ns(seq);
  540. if ((lower_ns == ns) && lower_ns->parent)
  541. lower_ns = lower_ns->parent;
  542. lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
  543. seq_printf(seq, "%10u %10u %10u\n",
  544. extent->first,
  545. lower,
  546. extent->count);
  547. return 0;
  548. }
  549. static int projid_m_show(struct seq_file *seq, void *v)
  550. {
  551. struct user_namespace *ns = seq->private;
  552. struct uid_gid_extent *extent = v;
  553. struct user_namespace *lower_ns;
  554. projid_t lower;
  555. lower_ns = seq_user_ns(seq);
  556. if ((lower_ns == ns) && lower_ns->parent)
  557. lower_ns = lower_ns->parent;
  558. lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
  559. seq_printf(seq, "%10u %10u %10u\n",
  560. extent->first,
  561. lower,
  562. extent->count);
  563. return 0;
  564. }
  565. static void *m_start(struct seq_file *seq, loff_t *ppos,
  566. struct uid_gid_map *map)
  567. {
  568. loff_t pos = *ppos;
  569. unsigned extents = map->nr_extents;
  570. smp_rmb();
  571. if (pos >= extents)
  572. return NULL;
  573. if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  574. return &map->extent[pos];
  575. return &map->forward[pos];
  576. }
  577. static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
  578. {
  579. struct user_namespace *ns = seq->private;
  580. return m_start(seq, ppos, &ns->uid_map);
  581. }
  582. static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
  583. {
  584. struct user_namespace *ns = seq->private;
  585. return m_start(seq, ppos, &ns->gid_map);
  586. }
  587. static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
  588. {
  589. struct user_namespace *ns = seq->private;
  590. return m_start(seq, ppos, &ns->projid_map);
  591. }
  592. static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
  593. {
  594. (*pos)++;
  595. return seq->op->start(seq, pos);
  596. }
  597. static void m_stop(struct seq_file *seq, void *v)
  598. {
  599. return;
  600. }
  601. const struct seq_operations proc_uid_seq_operations = {
  602. .start = uid_m_start,
  603. .stop = m_stop,
  604. .next = m_next,
  605. .show = uid_m_show,
  606. };
  607. const struct seq_operations proc_gid_seq_operations = {
  608. .start = gid_m_start,
  609. .stop = m_stop,
  610. .next = m_next,
  611. .show = gid_m_show,
  612. };
  613. const struct seq_operations proc_projid_seq_operations = {
  614. .start = projid_m_start,
  615. .stop = m_stop,
  616. .next = m_next,
  617. .show = projid_m_show,
  618. };
  619. static bool mappings_overlap(struct uid_gid_map *new_map,
  620. struct uid_gid_extent *extent)
  621. {
  622. u32 upper_first, lower_first, upper_last, lower_last;
  623. unsigned idx;
  624. upper_first = extent->first;
  625. lower_first = extent->lower_first;
  626. upper_last = upper_first + extent->count - 1;
  627. lower_last = lower_first + extent->count - 1;
  628. for (idx = 0; idx < new_map->nr_extents; idx++) {
  629. u32 prev_upper_first, prev_lower_first;
  630. u32 prev_upper_last, prev_lower_last;
  631. struct uid_gid_extent *prev;
  632. if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  633. prev = &new_map->extent[idx];
  634. else
  635. prev = &new_map->forward[idx];
  636. prev_upper_first = prev->first;
  637. prev_lower_first = prev->lower_first;
  638. prev_upper_last = prev_upper_first + prev->count - 1;
  639. prev_lower_last = prev_lower_first + prev->count - 1;
  640. /* Does the upper range intersect a previous extent? */
  641. if ((prev_upper_first <= upper_last) &&
  642. (prev_upper_last >= upper_first))
  643. return true;
  644. /* Does the lower range intersect a previous extent? */
  645. if ((prev_lower_first <= lower_last) &&
  646. (prev_lower_last >= lower_first))
  647. return true;
  648. }
  649. return false;
  650. }
  651. /**
  652. * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
  653. * Takes care to allocate a 4K block of memory if the number of mappings exceeds
  654. * UID_GID_MAP_MAX_BASE_EXTENTS.
  655. */
  656. static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
  657. {
  658. struct uid_gid_extent *dest;
  659. if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
  660. struct uid_gid_extent *forward;
  661. /* Allocate memory for 340 mappings. */
  662. forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS,
  663. sizeof(struct uid_gid_extent),
  664. GFP_KERNEL);
  665. if (!forward)
  666. return -ENOMEM;
  667. /* Copy over memory. Only set up memory for the forward pointer.
  668. * Defer the memory setup for the reverse pointer.
  669. */
  670. memcpy(forward, map->extent,
  671. map->nr_extents * sizeof(map->extent[0]));
  672. map->forward = forward;
  673. map->reverse = NULL;
  674. }
  675. if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
  676. dest = &map->extent[map->nr_extents];
  677. else
  678. dest = &map->forward[map->nr_extents];
  679. *dest = *extent;
  680. map->nr_extents++;
  681. return 0;
  682. }
  683. /* cmp function to sort() forward mappings */
  684. static int cmp_extents_forward(const void *a, const void *b)
  685. {
  686. const struct uid_gid_extent *e1 = a;
  687. const struct uid_gid_extent *e2 = b;
  688. if (e1->first < e2->first)
  689. return -1;
  690. if (e1->first > e2->first)
  691. return 1;
  692. return 0;
  693. }
  694. /* cmp function to sort() reverse mappings */
  695. static int cmp_extents_reverse(const void *a, const void *b)
  696. {
  697. const struct uid_gid_extent *e1 = a;
  698. const struct uid_gid_extent *e2 = b;
  699. if (e1->lower_first < e2->lower_first)
  700. return -1;
  701. if (e1->lower_first > e2->lower_first)
  702. return 1;
  703. return 0;
  704. }
  705. /**
  706. * sort_idmaps - Sorts an array of idmap entries.
  707. * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
  708. */
  709. static int sort_idmaps(struct uid_gid_map *map)
  710. {
  711. if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  712. return 0;
  713. /* Sort forward array. */
  714. sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
  715. cmp_extents_forward, NULL);
  716. /* Only copy the memory from forward we actually need. */
  717. map->reverse = kmemdup(map->forward,
  718. map->nr_extents * sizeof(struct uid_gid_extent),
  719. GFP_KERNEL);
  720. if (!map->reverse)
  721. return -ENOMEM;
  722. /* Sort reverse array. */
  723. sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
  724. cmp_extents_reverse, NULL);
  725. return 0;
  726. }
  727. static ssize_t map_write(struct file *file, const char __user *buf,
  728. size_t count, loff_t *ppos,
  729. int cap_setid,
  730. struct uid_gid_map *map,
  731. struct uid_gid_map *parent_map)
  732. {
  733. struct seq_file *seq = file->private_data;
  734. struct user_namespace *ns = seq->private;
  735. struct uid_gid_map new_map;
  736. unsigned idx;
  737. struct uid_gid_extent extent;
  738. char *kbuf = NULL, *pos, *next_line;
  739. ssize_t ret;
  740. /* Only allow < page size writes at the beginning of the file */
  741. if ((*ppos != 0) || (count >= PAGE_SIZE))
  742. return -EINVAL;
  743. /* Slurp in the user data */
  744. kbuf = memdup_user_nul(buf, count);
  745. if (IS_ERR(kbuf))
  746. return PTR_ERR(kbuf);
  747. /*
  748. * The userns_state_mutex serializes all writes to any given map.
  749. *
  750. * Any map is only ever written once.
  751. *
  752. * An id map fits within 1 cache line on most architectures.
  753. *
  754. * On read nothing needs to be done unless you are on an
  755. * architecture with a crazy cache coherency model like alpha.
  756. *
  757. * There is a one time data dependency between reading the
  758. * count of the extents and the values of the extents. The
  759. * desired behavior is to see the values of the extents that
  760. * were written before the count of the extents.
  761. *
  762. * To achieve this smp_wmb() is used on guarantee the write
  763. * order and smp_rmb() is guaranteed that we don't have crazy
  764. * architectures returning stale data.
  765. */
  766. mutex_lock(&userns_state_mutex);
  767. memset(&new_map, 0, sizeof(struct uid_gid_map));
  768. ret = -EPERM;
  769. /* Only allow one successful write to the map */
  770. if (map->nr_extents != 0)
  771. goto out;
  772. /*
  773. * Adjusting namespace settings requires capabilities on the target.
  774. */
  775. if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
  776. goto out;
  777. /* Parse the user data */
  778. ret = -EINVAL;
  779. pos = kbuf;
  780. for (; pos; pos = next_line) {
  781. /* Find the end of line and ensure I don't look past it */
  782. next_line = strchr(pos, '\n');
  783. if (next_line) {
  784. *next_line = '\0';
  785. next_line++;
  786. if (*next_line == '\0')
  787. next_line = NULL;
  788. }
  789. pos = skip_spaces(pos);
  790. extent.first = simple_strtoul(pos, &pos, 10);
  791. if (!isspace(*pos))
  792. goto out;
  793. pos = skip_spaces(pos);
  794. extent.lower_first = simple_strtoul(pos, &pos, 10);
  795. if (!isspace(*pos))
  796. goto out;
  797. pos = skip_spaces(pos);
  798. extent.count = simple_strtoul(pos, &pos, 10);
  799. if (*pos && !isspace(*pos))
  800. goto out;
  801. /* Verify there is not trailing junk on the line */
  802. pos = skip_spaces(pos);
  803. if (*pos != '\0')
  804. goto out;
  805. /* Verify we have been given valid starting values */
  806. if ((extent.first == (u32) -1) ||
  807. (extent.lower_first == (u32) -1))
  808. goto out;
  809. /* Verify count is not zero and does not cause the
  810. * extent to wrap
  811. */
  812. if ((extent.first + extent.count) <= extent.first)
  813. goto out;
  814. if ((extent.lower_first + extent.count) <=
  815. extent.lower_first)
  816. goto out;
  817. /* Do the ranges in extent overlap any previous extents? */
  818. if (mappings_overlap(&new_map, &extent))
  819. goto out;
  820. if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
  821. (next_line != NULL))
  822. goto out;
  823. ret = insert_extent(&new_map, &extent);
  824. if (ret < 0)
  825. goto out;
  826. ret = -EINVAL;
  827. }
  828. /* Be very certaint the new map actually exists */
  829. if (new_map.nr_extents == 0)
  830. goto out;
  831. ret = -EPERM;
  832. /* Validate the user is allowed to use user id's mapped to. */
  833. if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
  834. goto out;
  835. ret = sort_idmaps(&new_map);
  836. if (ret < 0)
  837. goto out;
  838. ret = -EPERM;
  839. /* Map the lower ids from the parent user namespace to the
  840. * kernel global id space.
  841. */
  842. for (idx = 0; idx < new_map.nr_extents; idx++) {
  843. struct uid_gid_extent *e;
  844. u32 lower_first;
  845. if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  846. e = &new_map.extent[idx];
  847. else
  848. e = &new_map.forward[idx];
  849. lower_first = map_id_range_down(parent_map,
  850. e->lower_first,
  851. e->count);
  852. /* Fail if we can not map the specified extent to
  853. * the kernel global id space.
  854. */
  855. if (lower_first == (u32) -1)
  856. goto out;
  857. e->lower_first = lower_first;
  858. }
  859. /* Install the map */
  860. if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
  861. memcpy(map->extent, new_map.extent,
  862. new_map.nr_extents * sizeof(new_map.extent[0]));
  863. } else {
  864. map->forward = new_map.forward;
  865. map->reverse = new_map.reverse;
  866. }
  867. smp_wmb();
  868. map->nr_extents = new_map.nr_extents;
  869. *ppos = count;
  870. ret = count;
  871. out:
  872. if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  873. kfree(new_map.forward);
  874. kfree(new_map.reverse);
  875. map->forward = NULL;
  876. map->reverse = NULL;
  877. map->nr_extents = 0;
  878. }
  879. mutex_unlock(&userns_state_mutex);
  880. kfree(kbuf);
  881. return ret;
  882. }
  883. ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
  884. size_t size, loff_t *ppos)
  885. {
  886. struct seq_file *seq = file->private_data;
  887. struct user_namespace *ns = seq->private;
  888. struct user_namespace *seq_ns = seq_user_ns(seq);
  889. if (!ns->parent)
  890. return -EPERM;
  891. if ((seq_ns != ns) && (seq_ns != ns->parent))
  892. return -EPERM;
  893. return map_write(file, buf, size, ppos, CAP_SETUID,
  894. &ns->uid_map, &ns->parent->uid_map);
  895. }
  896. ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
  897. size_t size, loff_t *ppos)
  898. {
  899. struct seq_file *seq = file->private_data;
  900. struct user_namespace *ns = seq->private;
  901. struct user_namespace *seq_ns = seq_user_ns(seq);
  902. if (!ns->parent)
  903. return -EPERM;
  904. if ((seq_ns != ns) && (seq_ns != ns->parent))
  905. return -EPERM;
  906. return map_write(file, buf, size, ppos, CAP_SETGID,
  907. &ns->gid_map, &ns->parent->gid_map);
  908. }
  909. ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
  910. size_t size, loff_t *ppos)
  911. {
  912. struct seq_file *seq = file->private_data;
  913. struct user_namespace *ns = seq->private;
  914. struct user_namespace *seq_ns = seq_user_ns(seq);
  915. if (!ns->parent)
  916. return -EPERM;
  917. if ((seq_ns != ns) && (seq_ns != ns->parent))
  918. return -EPERM;
  919. /* Anyone can set any valid project id no capability needed */
  920. return map_write(file, buf, size, ppos, -1,
  921. &ns->projid_map, &ns->parent->projid_map);
  922. }
  923. static bool new_idmap_permitted(const struct file *file,
  924. struct user_namespace *ns, int cap_setid,
  925. struct uid_gid_map *new_map)
  926. {
  927. const struct cred *cred = file->f_cred;
  928. /* Don't allow mappings that would allow anything that wouldn't
  929. * be allowed without the establishment of unprivileged mappings.
  930. */
  931. if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
  932. uid_eq(ns->owner, cred->euid)) {
  933. u32 id = new_map->extent[0].lower_first;
  934. if (cap_setid == CAP_SETUID) {
  935. kuid_t uid = make_kuid(ns->parent, id);
  936. if (uid_eq(uid, cred->euid))
  937. return true;
  938. } else if (cap_setid == CAP_SETGID) {
  939. kgid_t gid = make_kgid(ns->parent, id);
  940. if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
  941. gid_eq(gid, cred->egid))
  942. return true;
  943. }
  944. }
  945. /* Allow anyone to set a mapping that doesn't require privilege */
  946. if (!cap_valid(cap_setid))
  947. return true;
  948. /* Allow the specified ids if we have the appropriate capability
  949. * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
  950. * And the opener of the id file also had the approprpiate capability.
  951. */
  952. if (ns_capable(ns->parent, cap_setid) &&
  953. file_ns_capable(file, ns->parent, cap_setid))
  954. return true;
  955. return false;
  956. }
  957. int proc_setgroups_show(struct seq_file *seq, void *v)
  958. {
  959. struct user_namespace *ns = seq->private;
  960. unsigned long userns_flags = READ_ONCE(ns->flags);
  961. seq_printf(seq, "%s\n",
  962. (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
  963. "allow" : "deny");
  964. return 0;
  965. }
  966. ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
  967. size_t count, loff_t *ppos)
  968. {
  969. struct seq_file *seq = file->private_data;
  970. struct user_namespace *ns = seq->private;
  971. char kbuf[8], *pos;
  972. bool setgroups_allowed;
  973. ssize_t ret;
  974. /* Only allow a very narrow range of strings to be written */
  975. ret = -EINVAL;
  976. if ((*ppos != 0) || (count >= sizeof(kbuf)))
  977. goto out;
  978. /* What was written? */
  979. ret = -EFAULT;
  980. if (copy_from_user(kbuf, buf, count))
  981. goto out;
  982. kbuf[count] = '\0';
  983. pos = kbuf;
  984. /* What is being requested? */
  985. ret = -EINVAL;
  986. if (strncmp(pos, "allow", 5) == 0) {
  987. pos += 5;
  988. setgroups_allowed = true;
  989. }
  990. else if (strncmp(pos, "deny", 4) == 0) {
  991. pos += 4;
  992. setgroups_allowed = false;
  993. }
  994. else
  995. goto out;
  996. /* Verify there is not trailing junk on the line */
  997. pos = skip_spaces(pos);
  998. if (*pos != '\0')
  999. goto out;
  1000. ret = -EPERM;
  1001. mutex_lock(&userns_state_mutex);
  1002. if (setgroups_allowed) {
  1003. /* Enabling setgroups after setgroups has been disabled
  1004. * is not allowed.
  1005. */
  1006. if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
  1007. goto out_unlock;
  1008. } else {
  1009. /* Permanently disabling setgroups after setgroups has
  1010. * been enabled by writing the gid_map is not allowed.
  1011. */
  1012. if (ns->gid_map.nr_extents != 0)
  1013. goto out_unlock;
  1014. ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
  1015. }
  1016. mutex_unlock(&userns_state_mutex);
  1017. /* Report a successful write */
  1018. *ppos = count;
  1019. ret = count;
  1020. out:
  1021. return ret;
  1022. out_unlock:
  1023. mutex_unlock(&userns_state_mutex);
  1024. goto out;
  1025. }
  1026. bool userns_may_setgroups(const struct user_namespace *ns)
  1027. {
  1028. bool allowed;
  1029. mutex_lock(&userns_state_mutex);
  1030. /* It is not safe to use setgroups until a gid mapping in
  1031. * the user namespace has been established.
  1032. */
  1033. allowed = ns->gid_map.nr_extents != 0;
  1034. /* Is setgroups allowed? */
  1035. allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
  1036. mutex_unlock(&userns_state_mutex);
  1037. return allowed;
  1038. }
  1039. /*
  1040. * Returns true if @child is the same namespace or a descendant of
  1041. * @ancestor.
  1042. */
  1043. bool in_userns(const struct user_namespace *ancestor,
  1044. const struct user_namespace *child)
  1045. {
  1046. const struct user_namespace *ns;
  1047. for (ns = child; ns->level > ancestor->level; ns = ns->parent)
  1048. ;
  1049. return (ns == ancestor);
  1050. }
  1051. bool current_in_userns(const struct user_namespace *target_ns)
  1052. {
  1053. return in_userns(target_ns, current_user_ns());
  1054. }
  1055. EXPORT_SYMBOL(current_in_userns);
  1056. static inline struct user_namespace *to_user_ns(struct ns_common *ns)
  1057. {
  1058. return container_of(ns, struct user_namespace, ns);
  1059. }
  1060. static struct ns_common *userns_get(struct task_struct *task)
  1061. {
  1062. struct user_namespace *user_ns;
  1063. rcu_read_lock();
  1064. user_ns = get_user_ns(__task_cred(task)->user_ns);
  1065. rcu_read_unlock();
  1066. return user_ns ? &user_ns->ns : NULL;
  1067. }
  1068. static void userns_put(struct ns_common *ns)
  1069. {
  1070. put_user_ns(to_user_ns(ns));
  1071. }
  1072. static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  1073. {
  1074. struct user_namespace *user_ns = to_user_ns(ns);
  1075. struct cred *cred;
  1076. /* Don't allow gaining capabilities by reentering
  1077. * the same user namespace.
  1078. */
  1079. if (user_ns == current_user_ns())
  1080. return -EINVAL;
  1081. /* Tasks that share a thread group must share a user namespace */
  1082. if (!thread_group_empty(current))
  1083. return -EINVAL;
  1084. if (current->fs->users != 1)
  1085. return -EINVAL;
  1086. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  1087. return -EPERM;
  1088. cred = prepare_creds();
  1089. if (!cred)
  1090. return -ENOMEM;
  1091. put_user_ns(cred->user_ns);
  1092. set_cred_user_ns(cred, get_user_ns(user_ns));
  1093. return commit_creds(cred);
  1094. }
  1095. struct ns_common *ns_get_owner(struct ns_common *ns)
  1096. {
  1097. struct user_namespace *my_user_ns = current_user_ns();
  1098. struct user_namespace *owner, *p;
  1099. /* See if the owner is in the current user namespace */
  1100. owner = p = ns->ops->owner(ns);
  1101. for (;;) {
  1102. if (!p)
  1103. return ERR_PTR(-EPERM);
  1104. if (p == my_user_ns)
  1105. break;
  1106. p = p->parent;
  1107. }
  1108. return &get_user_ns(owner)->ns;
  1109. }
  1110. static struct user_namespace *userns_owner(struct ns_common *ns)
  1111. {
  1112. return to_user_ns(ns)->parent;
  1113. }
  1114. const struct proc_ns_operations userns_operations = {
  1115. .name = "user",
  1116. .type = CLONE_NEWUSER,
  1117. .get = userns_get,
  1118. .put = userns_put,
  1119. .install = userns_install,
  1120. .owner = userns_owner,
  1121. .get_parent = ns_get_owner,
  1122. };
  1123. static __init int user_namespaces_init(void)
  1124. {
  1125. user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  1126. return 0;
  1127. }
  1128. subsys_initcall(user_namespaces_init);