user_namespace.c 19 KB

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