user_namespace.c 21 KB

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