user_namespace.c 22 KB

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