user_namespace.c 20 KB

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