user_namespace.c 23 KB

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