user_namespace.c 21 KB

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