nsproxy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2006 IBM Corporation
  4. *
  5. * Author: Serge Hallyn <serue@us.ibm.com>
  6. *
  7. * Jun 2006 - namespaces support
  8. * OpenVZ, SWsoft Inc.
  9. * Pavel Emelianov <xemul@openvz.org>
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/export.h>
  13. #include <linux/nsproxy.h>
  14. #include <linux/init_task.h>
  15. #include <linux/mnt_namespace.h>
  16. #include <linux/utsname.h>
  17. #include <linux/pid_namespace.h>
  18. #include <net/net_namespace.h>
  19. #include <linux/ipc_namespace.h>
  20. #include <linux/time_namespace.h>
  21. #include <linux/fs_struct.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/proc_ns.h>
  24. #include <linux/file.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/cgroup.h>
  27. #include <linux/perf_event.h>
  28. #include <linux/nstree.h>
  29. static struct kmem_cache *nsproxy_cachep;
  30. struct nsproxy init_nsproxy = {
  31. .count = REFCOUNT_INIT(1),
  32. .uts_ns = &init_uts_ns,
  33. #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
  34. .ipc_ns = &init_ipc_ns,
  35. #endif
  36. .mnt_ns = NULL,
  37. .pid_ns_for_children = &init_pid_ns,
  38. #ifdef CONFIG_NET
  39. .net_ns = &init_net,
  40. #endif
  41. #ifdef CONFIG_CGROUPS
  42. .cgroup_ns = &init_cgroup_ns,
  43. #endif
  44. #ifdef CONFIG_TIME_NS
  45. .time_ns = &init_time_ns,
  46. .time_ns_for_children = &init_time_ns,
  47. #endif
  48. };
  49. static inline struct nsproxy *create_nsproxy(void)
  50. {
  51. struct nsproxy *nsproxy;
  52. nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
  53. if (nsproxy)
  54. refcount_set(&nsproxy->count, 1);
  55. return nsproxy;
  56. }
  57. static inline void nsproxy_free(struct nsproxy *ns)
  58. {
  59. put_mnt_ns(ns->mnt_ns);
  60. put_uts_ns(ns->uts_ns);
  61. put_ipc_ns(ns->ipc_ns);
  62. put_pid_ns(ns->pid_ns_for_children);
  63. put_time_ns(ns->time_ns);
  64. put_time_ns(ns->time_ns_for_children);
  65. put_cgroup_ns(ns->cgroup_ns);
  66. put_net(ns->net_ns);
  67. kmem_cache_free(nsproxy_cachep, ns);
  68. }
  69. void deactivate_nsproxy(struct nsproxy *ns)
  70. {
  71. nsproxy_ns_active_put(ns);
  72. nsproxy_free(ns);
  73. }
  74. /*
  75. * Create new nsproxy and all of its the associated namespaces.
  76. * Return the newly created nsproxy. Do not attach this to the task,
  77. * leave it to the caller to do proper locking and attach it to task.
  78. */
  79. static struct nsproxy *create_new_namespaces(u64 flags,
  80. struct task_struct *tsk, struct user_namespace *user_ns,
  81. struct fs_struct *new_fs)
  82. {
  83. struct nsproxy *new_nsp;
  84. int err;
  85. new_nsp = create_nsproxy();
  86. if (!new_nsp)
  87. return ERR_PTR(-ENOMEM);
  88. new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs);
  89. if (IS_ERR(new_nsp->mnt_ns)) {
  90. err = PTR_ERR(new_nsp->mnt_ns);
  91. goto out_ns;
  92. }
  93. new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns);
  94. if (IS_ERR(new_nsp->uts_ns)) {
  95. err = PTR_ERR(new_nsp->uts_ns);
  96. goto out_uts;
  97. }
  98. new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns);
  99. if (IS_ERR(new_nsp->ipc_ns)) {
  100. err = PTR_ERR(new_nsp->ipc_ns);
  101. goto out_ipc;
  102. }
  103. new_nsp->pid_ns_for_children =
  104. copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children);
  105. if (IS_ERR(new_nsp->pid_ns_for_children)) {
  106. err = PTR_ERR(new_nsp->pid_ns_for_children);
  107. goto out_pid;
  108. }
  109. new_nsp->cgroup_ns = copy_cgroup_ns(flags, user_ns,
  110. tsk->nsproxy->cgroup_ns);
  111. if (IS_ERR(new_nsp->cgroup_ns)) {
  112. err = PTR_ERR(new_nsp->cgroup_ns);
  113. goto out_cgroup;
  114. }
  115. new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns);
  116. if (IS_ERR(new_nsp->net_ns)) {
  117. err = PTR_ERR(new_nsp->net_ns);
  118. goto out_net;
  119. }
  120. new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns,
  121. tsk->nsproxy->time_ns_for_children);
  122. if (IS_ERR(new_nsp->time_ns_for_children)) {
  123. err = PTR_ERR(new_nsp->time_ns_for_children);
  124. goto out_time;
  125. }
  126. new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
  127. return new_nsp;
  128. out_time:
  129. put_net(new_nsp->net_ns);
  130. out_net:
  131. put_cgroup_ns(new_nsp->cgroup_ns);
  132. out_cgroup:
  133. put_pid_ns(new_nsp->pid_ns_for_children);
  134. out_pid:
  135. put_ipc_ns(new_nsp->ipc_ns);
  136. out_ipc:
  137. put_uts_ns(new_nsp->uts_ns);
  138. out_uts:
  139. put_mnt_ns(new_nsp->mnt_ns);
  140. out_ns:
  141. kmem_cache_free(nsproxy_cachep, new_nsp);
  142. return ERR_PTR(err);
  143. }
  144. /*
  145. * called from clone. This now handles copy for nsproxy and all
  146. * namespaces therein.
  147. */
  148. int copy_namespaces(u64 flags, struct task_struct *tsk)
  149. {
  150. struct nsproxy *old_ns = tsk->nsproxy;
  151. struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
  152. struct nsproxy *new_ns;
  153. if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  154. CLONE_NEWPID | CLONE_NEWNET |
  155. CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
  156. if ((flags & CLONE_VM) ||
  157. likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
  158. get_nsproxy(old_ns);
  159. return 0;
  160. }
  161. } else if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  162. return -EPERM;
  163. /*
  164. * CLONE_NEWIPC must detach from the undolist: after switching
  165. * to a new ipc namespace, the semaphore arrays from the old
  166. * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
  167. * means share undolist with parent, so we must forbid using
  168. * it along with CLONE_NEWIPC.
  169. */
  170. if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
  171. (CLONE_NEWIPC | CLONE_SYSVSEM))
  172. return -EINVAL;
  173. new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
  174. if (IS_ERR(new_ns))
  175. return PTR_ERR(new_ns);
  176. if ((flags & CLONE_VM) == 0)
  177. timens_on_fork(new_ns, tsk);
  178. nsproxy_ns_active_get(new_ns);
  179. tsk->nsproxy = new_ns;
  180. return 0;
  181. }
  182. /*
  183. * Called from unshare. Unshare all the namespaces part of nsproxy.
  184. * On success, returns the new nsproxy.
  185. */
  186. int unshare_nsproxy_namespaces(unsigned long unshare_flags,
  187. struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
  188. {
  189. struct user_namespace *user_ns;
  190. int err = 0;
  191. if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  192. CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
  193. CLONE_NEWTIME)))
  194. return 0;
  195. user_ns = new_cred ? new_cred->user_ns : current_user_ns();
  196. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  197. return -EPERM;
  198. *new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
  199. new_fs ? new_fs : current->fs);
  200. if (IS_ERR(*new_nsp)) {
  201. err = PTR_ERR(*new_nsp);
  202. goto out;
  203. }
  204. out:
  205. return err;
  206. }
  207. void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
  208. {
  209. struct nsproxy *ns;
  210. might_sleep();
  211. if (new)
  212. nsproxy_ns_active_get(new);
  213. task_lock(p);
  214. ns = p->nsproxy;
  215. p->nsproxy = new;
  216. task_unlock(p);
  217. if (ns)
  218. put_nsproxy(ns);
  219. }
  220. void exit_nsproxy_namespaces(struct task_struct *p)
  221. {
  222. switch_task_namespaces(p, NULL);
  223. }
  224. void switch_cred_namespaces(const struct cred *old, const struct cred *new)
  225. {
  226. ns_ref_active_get(new->user_ns);
  227. ns_ref_active_put(old->user_ns);
  228. }
  229. void get_cred_namespaces(struct task_struct *tsk)
  230. {
  231. ns_ref_active_get(tsk->real_cred->user_ns);
  232. }
  233. void exit_cred_namespaces(struct task_struct *tsk)
  234. {
  235. ns_ref_active_put(tsk->real_cred->user_ns);
  236. }
  237. int exec_task_namespaces(void)
  238. {
  239. struct task_struct *tsk = current;
  240. struct nsproxy *new;
  241. if (tsk->nsproxy->time_ns_for_children == tsk->nsproxy->time_ns)
  242. return 0;
  243. new = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs);
  244. if (IS_ERR(new))
  245. return PTR_ERR(new);
  246. timens_on_fork(new, tsk);
  247. switch_task_namespaces(tsk, new);
  248. return 0;
  249. }
  250. static int check_setns_flags(unsigned long flags)
  251. {
  252. if (!flags || (flags & ~(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
  253. CLONE_NEWNET | CLONE_NEWTIME | CLONE_NEWUSER |
  254. CLONE_NEWPID | CLONE_NEWCGROUP)))
  255. return -EINVAL;
  256. #ifndef CONFIG_USER_NS
  257. if (flags & CLONE_NEWUSER)
  258. return -EINVAL;
  259. #endif
  260. #ifndef CONFIG_PID_NS
  261. if (flags & CLONE_NEWPID)
  262. return -EINVAL;
  263. #endif
  264. #ifndef CONFIG_UTS_NS
  265. if (flags & CLONE_NEWUTS)
  266. return -EINVAL;
  267. #endif
  268. #ifndef CONFIG_IPC_NS
  269. if (flags & CLONE_NEWIPC)
  270. return -EINVAL;
  271. #endif
  272. #ifndef CONFIG_CGROUPS
  273. if (flags & CLONE_NEWCGROUP)
  274. return -EINVAL;
  275. #endif
  276. #ifndef CONFIG_NET_NS
  277. if (flags & CLONE_NEWNET)
  278. return -EINVAL;
  279. #endif
  280. #ifndef CONFIG_TIME_NS
  281. if (flags & CLONE_NEWTIME)
  282. return -EINVAL;
  283. #endif
  284. return 0;
  285. }
  286. static void put_nsset(struct nsset *nsset)
  287. {
  288. unsigned flags = nsset->flags;
  289. if (flags & CLONE_NEWUSER)
  290. put_cred(nsset_cred(nsset));
  291. /*
  292. * We only created a temporary copy if we attached to more than just
  293. * the mount namespace.
  294. */
  295. if (nsset->fs && (flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS))
  296. free_fs_struct(nsset->fs);
  297. if (nsset->nsproxy)
  298. nsproxy_free(nsset->nsproxy);
  299. }
  300. static int prepare_nsset(unsigned flags, struct nsset *nsset)
  301. {
  302. struct task_struct *me = current;
  303. nsset->nsproxy = create_new_namespaces(0, me, current_user_ns(), me->fs);
  304. if (IS_ERR(nsset->nsproxy))
  305. return PTR_ERR(nsset->nsproxy);
  306. if (flags & CLONE_NEWUSER)
  307. nsset->cred = prepare_creds();
  308. else
  309. nsset->cred = current_cred();
  310. if (!nsset->cred)
  311. goto out;
  312. /* Only create a temporary copy of fs_struct if we really need to. */
  313. if (flags == CLONE_NEWNS) {
  314. nsset->fs = me->fs;
  315. } else if (flags & CLONE_NEWNS) {
  316. nsset->fs = copy_fs_struct(me->fs);
  317. if (!nsset->fs)
  318. goto out;
  319. }
  320. nsset->flags = flags;
  321. return 0;
  322. out:
  323. put_nsset(nsset);
  324. return -ENOMEM;
  325. }
  326. static inline int validate_ns(struct nsset *nsset, struct ns_common *ns)
  327. {
  328. return ns->ops->install(nsset, ns);
  329. }
  330. /*
  331. * This is the inverse operation to unshare().
  332. * Ordering is equivalent to the standard ordering used everywhere else
  333. * during unshare and process creation. The switch to the new set of
  334. * namespaces occurs at the point of no return after installation of
  335. * all requested namespaces was successful in commit_nsset().
  336. */
  337. static int validate_nsset(struct nsset *nsset, struct pid *pid)
  338. {
  339. int ret = 0;
  340. unsigned flags = nsset->flags;
  341. struct user_namespace *user_ns = NULL;
  342. struct pid_namespace *pid_ns = NULL;
  343. struct nsproxy *nsp;
  344. struct task_struct *tsk;
  345. /* Take a "snapshot" of the target task's namespaces. */
  346. rcu_read_lock();
  347. tsk = pid_task(pid, PIDTYPE_PID);
  348. if (!tsk) {
  349. rcu_read_unlock();
  350. return -ESRCH;
  351. }
  352. if (!ptrace_may_access(tsk, PTRACE_MODE_READ_REALCREDS)) {
  353. rcu_read_unlock();
  354. return -EPERM;
  355. }
  356. task_lock(tsk);
  357. nsp = tsk->nsproxy;
  358. if (nsp)
  359. get_nsproxy(nsp);
  360. task_unlock(tsk);
  361. if (!nsp) {
  362. rcu_read_unlock();
  363. return -ESRCH;
  364. }
  365. #ifdef CONFIG_PID_NS
  366. if (flags & CLONE_NEWPID) {
  367. pid_ns = task_active_pid_ns(tsk);
  368. if (unlikely(!pid_ns)) {
  369. rcu_read_unlock();
  370. ret = -ESRCH;
  371. goto out;
  372. }
  373. get_pid_ns(pid_ns);
  374. }
  375. #endif
  376. #ifdef CONFIG_USER_NS
  377. if (flags & CLONE_NEWUSER)
  378. user_ns = get_user_ns(__task_cred(tsk)->user_ns);
  379. #endif
  380. rcu_read_unlock();
  381. /*
  382. * Install requested namespaces. The caller will have
  383. * verified earlier that the requested namespaces are
  384. * supported on this kernel. We don't report errors here
  385. * if a namespace is requested that isn't supported.
  386. */
  387. #ifdef CONFIG_USER_NS
  388. if (flags & CLONE_NEWUSER) {
  389. ret = validate_ns(nsset, &user_ns->ns);
  390. if (ret)
  391. goto out;
  392. }
  393. #endif
  394. if (flags & CLONE_NEWNS) {
  395. ret = validate_ns(nsset, from_mnt_ns(nsp->mnt_ns));
  396. if (ret)
  397. goto out;
  398. }
  399. #ifdef CONFIG_UTS_NS
  400. if (flags & CLONE_NEWUTS) {
  401. ret = validate_ns(nsset, &nsp->uts_ns->ns);
  402. if (ret)
  403. goto out;
  404. }
  405. #endif
  406. #ifdef CONFIG_IPC_NS
  407. if (flags & CLONE_NEWIPC) {
  408. ret = validate_ns(nsset, &nsp->ipc_ns->ns);
  409. if (ret)
  410. goto out;
  411. }
  412. #endif
  413. #ifdef CONFIG_PID_NS
  414. if (flags & CLONE_NEWPID) {
  415. ret = validate_ns(nsset, &pid_ns->ns);
  416. if (ret)
  417. goto out;
  418. }
  419. #endif
  420. #ifdef CONFIG_CGROUPS
  421. if (flags & CLONE_NEWCGROUP) {
  422. ret = validate_ns(nsset, &nsp->cgroup_ns->ns);
  423. if (ret)
  424. goto out;
  425. }
  426. #endif
  427. #ifdef CONFIG_NET_NS
  428. if (flags & CLONE_NEWNET) {
  429. ret = validate_ns(nsset, &nsp->net_ns->ns);
  430. if (ret)
  431. goto out;
  432. }
  433. #endif
  434. #ifdef CONFIG_TIME_NS
  435. if (flags & CLONE_NEWTIME) {
  436. ret = validate_ns(nsset, &nsp->time_ns->ns);
  437. if (ret)
  438. goto out;
  439. }
  440. #endif
  441. out:
  442. if (pid_ns)
  443. put_pid_ns(pid_ns);
  444. if (nsp)
  445. put_nsproxy(nsp);
  446. put_user_ns(user_ns);
  447. return ret;
  448. }
  449. /*
  450. * This is the point of no return. There are just a few namespaces
  451. * that do some actual work here and it's sufficiently minimal that
  452. * a separate ns_common operation seems unnecessary for now.
  453. * Unshare is doing the same thing. If we'll end up needing to do
  454. * more in a given namespace or a helper here is ultimately not
  455. * exported anymore a simple commit handler for each namespace
  456. * should be added to ns_common.
  457. */
  458. static void commit_nsset(struct nsset *nsset)
  459. {
  460. unsigned flags = nsset->flags;
  461. struct task_struct *me = current;
  462. #ifdef CONFIG_USER_NS
  463. if (flags & CLONE_NEWUSER) {
  464. /* transfer ownership */
  465. commit_creds(nsset_cred(nsset));
  466. nsset->cred = NULL;
  467. }
  468. #endif
  469. /* We only need to commit if we have used a temporary fs_struct. */
  470. if ((flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS)) {
  471. set_fs_root(me->fs, &nsset->fs->root);
  472. set_fs_pwd(me->fs, &nsset->fs->pwd);
  473. }
  474. #ifdef CONFIG_IPC_NS
  475. if (flags & CLONE_NEWIPC)
  476. exit_sem(me);
  477. #endif
  478. #ifdef CONFIG_TIME_NS
  479. if (flags & CLONE_NEWTIME)
  480. timens_commit(me, nsset->nsproxy->time_ns);
  481. #endif
  482. /* transfer ownership */
  483. switch_task_namespaces(me, nsset->nsproxy);
  484. nsset->nsproxy = NULL;
  485. }
  486. SYSCALL_DEFINE2(setns, int, fd, int, flags)
  487. {
  488. CLASS(fd, f)(fd);
  489. struct ns_common *ns = NULL;
  490. struct nsset nsset = {};
  491. int err = 0;
  492. if (fd_empty(f))
  493. return -EBADF;
  494. if (proc_ns_file(fd_file(f))) {
  495. ns = get_proc_ns(file_inode(fd_file(f)));
  496. if (flags && (ns->ns_type != flags))
  497. err = -EINVAL;
  498. flags = ns->ns_type;
  499. } else if (!IS_ERR(pidfd_pid(fd_file(f)))) {
  500. err = check_setns_flags(flags);
  501. } else {
  502. err = -EINVAL;
  503. }
  504. if (err)
  505. goto out;
  506. err = prepare_nsset(flags, &nsset);
  507. if (err)
  508. goto out;
  509. if (proc_ns_file(fd_file(f)))
  510. err = validate_ns(&nsset, ns);
  511. else
  512. err = validate_nsset(&nsset, pidfd_pid(fd_file(f)));
  513. if (!err) {
  514. commit_nsset(&nsset);
  515. perf_event_namespaces(current);
  516. }
  517. put_nsset(&nsset);
  518. out:
  519. return err;
  520. }
  521. int __init nsproxy_cache_init(void)
  522. {
  523. nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
  524. return 0;
  525. }