user_namespace.c 23 KB

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