user_namespace.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 = -EPERM;
  836. /* Map the lower ids from the parent user namespace to the
  837. * kernel global id space.
  838. */
  839. for (idx = 0; idx < new_map.nr_extents; idx++) {
  840. struct uid_gid_extent *e;
  841. u32 lower_first;
  842. if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
  843. e = &new_map.extent[idx];
  844. else
  845. e = &new_map.forward[idx];
  846. lower_first = map_id_range_down(parent_map,
  847. e->lower_first,
  848. e->count);
  849. /* Fail if we can not map the specified extent to
  850. * the kernel global id space.
  851. */
  852. if (lower_first == (u32) -1)
  853. goto out;
  854. e->lower_first = lower_first;
  855. }
  856. /*
  857. * If we want to use binary search for lookup, this clones the extent
  858. * array and sorts both copies.
  859. */
  860. ret = sort_idmaps(&new_map);
  861. if (ret < 0)
  862. goto out;
  863. /* Install the map */
  864. if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
  865. memcpy(map->extent, new_map.extent,
  866. new_map.nr_extents * sizeof(new_map.extent[0]));
  867. } else {
  868. map->forward = new_map.forward;
  869. map->reverse = new_map.reverse;
  870. }
  871. smp_wmb();
  872. map->nr_extents = new_map.nr_extents;
  873. *ppos = count;
  874. ret = count;
  875. out:
  876. if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
  877. kfree(new_map.forward);
  878. kfree(new_map.reverse);
  879. map->forward = NULL;
  880. map->reverse = NULL;
  881. map->nr_extents = 0;
  882. }
  883. mutex_unlock(&userns_state_mutex);
  884. kfree(kbuf);
  885. return ret;
  886. }
  887. ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
  888. size_t size, loff_t *ppos)
  889. {
  890. struct seq_file *seq = file->private_data;
  891. struct user_namespace *ns = seq->private;
  892. struct user_namespace *seq_ns = seq_user_ns(seq);
  893. if (!ns->parent)
  894. return -EPERM;
  895. if ((seq_ns != ns) && (seq_ns != ns->parent))
  896. return -EPERM;
  897. return map_write(file, buf, size, ppos, CAP_SETUID,
  898. &ns->uid_map, &ns->parent->uid_map);
  899. }
  900. ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
  901. size_t size, loff_t *ppos)
  902. {
  903. struct seq_file *seq = file->private_data;
  904. struct user_namespace *ns = seq->private;
  905. struct user_namespace *seq_ns = seq_user_ns(seq);
  906. if (!ns->parent)
  907. return -EPERM;
  908. if ((seq_ns != ns) && (seq_ns != ns->parent))
  909. return -EPERM;
  910. return map_write(file, buf, size, ppos, CAP_SETGID,
  911. &ns->gid_map, &ns->parent->gid_map);
  912. }
  913. ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
  914. size_t size, loff_t *ppos)
  915. {
  916. struct seq_file *seq = file->private_data;
  917. struct user_namespace *ns = seq->private;
  918. struct user_namespace *seq_ns = seq_user_ns(seq);
  919. if (!ns->parent)
  920. return -EPERM;
  921. if ((seq_ns != ns) && (seq_ns != ns->parent))
  922. return -EPERM;
  923. /* Anyone can set any valid project id no capability needed */
  924. return map_write(file, buf, size, ppos, -1,
  925. &ns->projid_map, &ns->parent->projid_map);
  926. }
  927. static bool new_idmap_permitted(const struct file *file,
  928. struct user_namespace *ns, int cap_setid,
  929. struct uid_gid_map *new_map)
  930. {
  931. const struct cred *cred = file->f_cred;
  932. /* Don't allow mappings that would allow anything that wouldn't
  933. * be allowed without the establishment of unprivileged mappings.
  934. */
  935. if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
  936. uid_eq(ns->owner, cred->euid)) {
  937. u32 id = new_map->extent[0].lower_first;
  938. if (cap_setid == CAP_SETUID) {
  939. kuid_t uid = make_kuid(ns->parent, id);
  940. if (uid_eq(uid, cred->euid))
  941. return true;
  942. } else if (cap_setid == CAP_SETGID) {
  943. kgid_t gid = make_kgid(ns->parent, id);
  944. if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
  945. gid_eq(gid, cred->egid))
  946. return true;
  947. }
  948. }
  949. /* Allow anyone to set a mapping that doesn't require privilege */
  950. if (!cap_valid(cap_setid))
  951. return true;
  952. /* Allow the specified ids if we have the appropriate capability
  953. * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
  954. * And the opener of the id file also had the approprpiate capability.
  955. */
  956. if (ns_capable(ns->parent, cap_setid) &&
  957. file_ns_capable(file, ns->parent, cap_setid))
  958. return true;
  959. return false;
  960. }
  961. int proc_setgroups_show(struct seq_file *seq, void *v)
  962. {
  963. struct user_namespace *ns = seq->private;
  964. unsigned long userns_flags = READ_ONCE(ns->flags);
  965. seq_printf(seq, "%s\n",
  966. (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
  967. "allow" : "deny");
  968. return 0;
  969. }
  970. ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
  971. size_t count, loff_t *ppos)
  972. {
  973. struct seq_file *seq = file->private_data;
  974. struct user_namespace *ns = seq->private;
  975. char kbuf[8], *pos;
  976. bool setgroups_allowed;
  977. ssize_t ret;
  978. /* Only allow a very narrow range of strings to be written */
  979. ret = -EINVAL;
  980. if ((*ppos != 0) || (count >= sizeof(kbuf)))
  981. goto out;
  982. /* What was written? */
  983. ret = -EFAULT;
  984. if (copy_from_user(kbuf, buf, count))
  985. goto out;
  986. kbuf[count] = '\0';
  987. pos = kbuf;
  988. /* What is being requested? */
  989. ret = -EINVAL;
  990. if (strncmp(pos, "allow", 5) == 0) {
  991. pos += 5;
  992. setgroups_allowed = true;
  993. }
  994. else if (strncmp(pos, "deny", 4) == 0) {
  995. pos += 4;
  996. setgroups_allowed = false;
  997. }
  998. else
  999. goto out;
  1000. /* Verify there is not trailing junk on the line */
  1001. pos = skip_spaces(pos);
  1002. if (*pos != '\0')
  1003. goto out;
  1004. ret = -EPERM;
  1005. mutex_lock(&userns_state_mutex);
  1006. if (setgroups_allowed) {
  1007. /* Enabling setgroups after setgroups has been disabled
  1008. * is not allowed.
  1009. */
  1010. if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
  1011. goto out_unlock;
  1012. } else {
  1013. /* Permanently disabling setgroups after setgroups has
  1014. * been enabled by writing the gid_map is not allowed.
  1015. */
  1016. if (ns->gid_map.nr_extents != 0)
  1017. goto out_unlock;
  1018. ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
  1019. }
  1020. mutex_unlock(&userns_state_mutex);
  1021. /* Report a successful write */
  1022. *ppos = count;
  1023. ret = count;
  1024. out:
  1025. return ret;
  1026. out_unlock:
  1027. mutex_unlock(&userns_state_mutex);
  1028. goto out;
  1029. }
  1030. bool userns_may_setgroups(const struct user_namespace *ns)
  1031. {
  1032. bool allowed;
  1033. mutex_lock(&userns_state_mutex);
  1034. /* It is not safe to use setgroups until a gid mapping in
  1035. * the user namespace has been established.
  1036. */
  1037. allowed = ns->gid_map.nr_extents != 0;
  1038. /* Is setgroups allowed? */
  1039. allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
  1040. mutex_unlock(&userns_state_mutex);
  1041. return allowed;
  1042. }
  1043. /*
  1044. * Returns true if @child is the same namespace or a descendant of
  1045. * @ancestor.
  1046. */
  1047. bool in_userns(const struct user_namespace *ancestor,
  1048. const struct user_namespace *child)
  1049. {
  1050. const struct user_namespace *ns;
  1051. for (ns = child; ns->level > ancestor->level; ns = ns->parent)
  1052. ;
  1053. return (ns == ancestor);
  1054. }
  1055. bool current_in_userns(const struct user_namespace *target_ns)
  1056. {
  1057. return in_userns(target_ns, current_user_ns());
  1058. }
  1059. EXPORT_SYMBOL(current_in_userns);
  1060. static inline struct user_namespace *to_user_ns(struct ns_common *ns)
  1061. {
  1062. return container_of(ns, struct user_namespace, ns);
  1063. }
  1064. static struct ns_common *userns_get(struct task_struct *task)
  1065. {
  1066. struct user_namespace *user_ns;
  1067. rcu_read_lock();
  1068. user_ns = get_user_ns(__task_cred(task)->user_ns);
  1069. rcu_read_unlock();
  1070. return user_ns ? &user_ns->ns : NULL;
  1071. }
  1072. static void userns_put(struct ns_common *ns)
  1073. {
  1074. put_user_ns(to_user_ns(ns));
  1075. }
  1076. static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  1077. {
  1078. struct user_namespace *user_ns = to_user_ns(ns);
  1079. struct cred *cred;
  1080. /* Don't allow gaining capabilities by reentering
  1081. * the same user namespace.
  1082. */
  1083. if (user_ns == current_user_ns())
  1084. return -EINVAL;
  1085. /* Tasks that share a thread group must share a user namespace */
  1086. if (!thread_group_empty(current))
  1087. return -EINVAL;
  1088. if (current->fs->users != 1)
  1089. return -EINVAL;
  1090. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  1091. return -EPERM;
  1092. cred = prepare_creds();
  1093. if (!cred)
  1094. return -ENOMEM;
  1095. put_user_ns(cred->user_ns);
  1096. set_cred_user_ns(cred, get_user_ns(user_ns));
  1097. return commit_creds(cred);
  1098. }
  1099. struct ns_common *ns_get_owner(struct ns_common *ns)
  1100. {
  1101. struct user_namespace *my_user_ns = current_user_ns();
  1102. struct user_namespace *owner, *p;
  1103. /* See if the owner is in the current user namespace */
  1104. owner = p = ns->ops->owner(ns);
  1105. for (;;) {
  1106. if (!p)
  1107. return ERR_PTR(-EPERM);
  1108. if (p == my_user_ns)
  1109. break;
  1110. p = p->parent;
  1111. }
  1112. return &get_user_ns(owner)->ns;
  1113. }
  1114. static struct user_namespace *userns_owner(struct ns_common *ns)
  1115. {
  1116. return to_user_ns(ns)->parent;
  1117. }
  1118. const struct proc_ns_operations userns_operations = {
  1119. .name = "user",
  1120. .type = CLONE_NEWUSER,
  1121. .get = userns_get,
  1122. .put = userns_put,
  1123. .install = userns_install,
  1124. .owner = userns_owner,
  1125. .get_parent = ns_get_owner,
  1126. };
  1127. static __init int user_namespaces_init(void)
  1128. {
  1129. user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  1130. return 0;
  1131. }
  1132. subsys_initcall(user_namespaces_init);