user_namespace.c 15 KB

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