user_namespace.c 27 KB

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