user_namespace.c 23 KB

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