user_namespace.c 26 KB

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