user_namespace.c 22 KB

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