user_namespace.c 35 KB

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