user_namespace.c 26 KB

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