user_namespace.c 23 KB

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