nsproxy.c 13 KB

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