user_namespace.c 23 KB

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