user_namespace.c 23 KB

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