user_namespace.c 21 KB

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