Merge tag 'for-5.7/dm-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git/devic...
[linux-block.git] / kernel / nsproxy.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
ab516013
SH
2/*
3 * Copyright (C) 2006 IBM Corporation
4 *
5 * Author: Serge Hallyn <serue@us.ibm.com>
6 *
25b21cb2
KK
7 * Jun 2006 - namespaces support
8 * OpenVZ, SWsoft Inc.
9 * Pavel Emelianov <xemul@openvz.org>
ab516013
SH
10 */
11
5a0e3ad6 12#include <linux/slab.h>
9984de1a 13#include <linux/export.h>
ab516013 14#include <linux/nsproxy.h>
0437eb59 15#include <linux/init_task.h>
6b3286ed 16#include <linux/mnt_namespace.h>
4865ecf1 17#include <linux/utsname.h>
9a575a92 18#include <linux/pid_namespace.h>
9dd776b6 19#include <net/net_namespace.h>
ae5e1b22 20#include <linux/ipc_namespace.h>
769071ac 21#include <linux/time_namespace.h>
0bb80f24 22#include <linux/proc_ns.h>
0663c6f8
EB
23#include <linux/file.h>
24#include <linux/syscalls.h>
a79a908f 25#include <linux/cgroup.h>
e4222673 26#include <linux/perf_event.h>
0437eb59 27
98c0d07c
CLG
28static struct kmem_cache *nsproxy_cachep;
29
8467005d 30struct nsproxy init_nsproxy = {
c2b1df2e
AL
31 .count = ATOMIC_INIT(1),
32 .uts_ns = &init_uts_ns,
8467005d 33#if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
c2b1df2e 34 .ipc_ns = &init_ipc_ns,
8467005d 35#endif
c2b1df2e
AL
36 .mnt_ns = NULL,
37 .pid_ns_for_children = &init_pid_ns,
8467005d 38#ifdef CONFIG_NET
c2b1df2e 39 .net_ns = &init_net,
8467005d 40#endif
a79a908f
AK
41#ifdef CONFIG_CGROUPS
42 .cgroup_ns = &init_cgroup_ns,
43#endif
769071ac
AV
44#ifdef CONFIG_TIME_NS
45 .time_ns = &init_time_ns,
46 .time_ns_for_children = &init_time_ns,
47#endif
8467005d 48};
ab516013 49
90af90d7 50static inline struct nsproxy *create_nsproxy(void)
ab516013 51{
90af90d7 52 struct nsproxy *nsproxy;
ab516013 53
90af90d7
AD
54 nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
55 if (nsproxy)
56 atomic_set(&nsproxy->count, 1);
57 return nsproxy;
ab516013
SH
58}
59
60/*
e3222c4e
BP
61 * Create new nsproxy and all of its the associated namespaces.
62 * Return the newly created nsproxy. Do not attach this to the task,
63 * leave it to the caller to do proper locking and attach it to task.
ab516013 64 */
213dd266 65static struct nsproxy *create_new_namespaces(unsigned long flags,
bcf58e72
EB
66 struct task_struct *tsk, struct user_namespace *user_ns,
67 struct fs_struct *new_fs)
ab516013 68{
e3222c4e 69 struct nsproxy *new_nsp;
467e9f4b 70 int err;
ab516013 71
90af90d7 72 new_nsp = create_nsproxy();
e3222c4e
BP
73 if (!new_nsp)
74 return ERR_PTR(-ENOMEM);
1651e14e 75
bcf58e72 76 new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs);
467e9f4b
CLG
77 if (IS_ERR(new_nsp->mnt_ns)) {
78 err = PTR_ERR(new_nsp->mnt_ns);
e3222c4e 79 goto out_ns;
467e9f4b 80 }
e3222c4e 81
bcf58e72 82 new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns);
467e9f4b
CLG
83 if (IS_ERR(new_nsp->uts_ns)) {
84 err = PTR_ERR(new_nsp->uts_ns);
e3222c4e 85 goto out_uts;
467e9f4b 86 }
e3222c4e 87
bcf58e72 88 new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns);
467e9f4b
CLG
89 if (IS_ERR(new_nsp->ipc_ns)) {
90 err = PTR_ERR(new_nsp->ipc_ns);
e3222c4e 91 goto out_ipc;
467e9f4b 92 }
e3222c4e 93
c2b1df2e
AL
94 new_nsp->pid_ns_for_children =
95 copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children);
96 if (IS_ERR(new_nsp->pid_ns_for_children)) {
97 err = PTR_ERR(new_nsp->pid_ns_for_children);
e3222c4e 98 goto out_pid;
467e9f4b 99 }
e3222c4e 100
a79a908f
AK
101 new_nsp->cgroup_ns = copy_cgroup_ns(flags, user_ns,
102 tsk->nsproxy->cgroup_ns);
103 if (IS_ERR(new_nsp->cgroup_ns)) {
104 err = PTR_ERR(new_nsp->cgroup_ns);
105 goto out_cgroup;
106 }
107
bcf58e72 108 new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns);
9dd776b6
EB
109 if (IS_ERR(new_nsp->net_ns)) {
110 err = PTR_ERR(new_nsp->net_ns);
111 goto out_net;
112 }
113
769071ac
AV
114 new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns,
115 tsk->nsproxy->time_ns_for_children);
116 if (IS_ERR(new_nsp->time_ns_for_children)) {
117 err = PTR_ERR(new_nsp->time_ns_for_children);
118 goto out_time;
119 }
120 new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
121
e3222c4e
BP
122 return new_nsp;
123
769071ac
AV
124out_time:
125 put_net(new_nsp->net_ns);
9dd776b6 126out_net:
a79a908f
AK
127 put_cgroup_ns(new_nsp->cgroup_ns);
128out_cgroup:
c2b1df2e
AL
129 if (new_nsp->pid_ns_for_children)
130 put_pid_ns(new_nsp->pid_ns_for_children);
e3222c4e
BP
131out_pid:
132 if (new_nsp->ipc_ns)
133 put_ipc_ns(new_nsp->ipc_ns);
134out_ipc:
135 if (new_nsp->uts_ns)
136 put_uts_ns(new_nsp->uts_ns);
137out_uts:
138 if (new_nsp->mnt_ns)
139 put_mnt_ns(new_nsp->mnt_ns);
140out_ns:
98c0d07c 141 kmem_cache_free(nsproxy_cachep, new_nsp);
467e9f4b 142 return ERR_PTR(err);
ab516013
SH
143}
144
145/*
146 * called from clone. This now handles copy for nsproxy and all
147 * namespaces therein.
148 */
213dd266 149int copy_namespaces(unsigned long flags, struct task_struct *tsk)
ab516013
SH
150{
151 struct nsproxy *old_ns = tsk->nsproxy;
b33c77ef 152 struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
1651e14e 153 struct nsproxy *new_ns;
769071ac 154 int ret;
ab516013 155
dbef0c1c 156 if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
a79a908f 157 CLONE_NEWPID | CLONE_NEWNET |
769071ac
AV
158 CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
159 if (likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
160 get_nsproxy(old_ns);
161 return 0;
162 }
163 } else if (!ns_capable(user_ns, CAP_SYS_ADMIN))
dbef0c1c
EB
164 return -EPERM;
165
02fdb36a
SH
166 /*
167 * CLONE_NEWIPC must detach from the undolist: after switching
168 * to a new ipc namespace, the semaphore arrays from the old
169 * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
170 * means share undolist with parent, so we must forbid using
171 * it along with CLONE_NEWIPC.
172 */
21e85194 173 if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
dbef0c1c
EB
174 (CLONE_NEWIPC | CLONE_SYSVSEM))
175 return -EINVAL;
02fdb36a 176
d7d48f62 177 new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
dbef0c1c
EB
178 if (IS_ERR(new_ns))
179 return PTR_ERR(new_ns);
9a575a92 180
769071ac
AV
181 ret = timens_on_fork(new_ns, tsk);
182 if (ret) {
183 free_nsproxy(new_ns);
184 return ret;
185 }
186
e3222c4e 187 tsk->nsproxy = new_ns;
dbef0c1c 188 return 0;
ab516013
SH
189}
190
191void free_nsproxy(struct nsproxy *ns)
192{
9a575a92
CLG
193 if (ns->mnt_ns)
194 put_mnt_ns(ns->mnt_ns);
195 if (ns->uts_ns)
196 put_uts_ns(ns->uts_ns);
197 if (ns->ipc_ns)
198 put_ipc_ns(ns->ipc_ns);
c2b1df2e
AL
199 if (ns->pid_ns_for_children)
200 put_pid_ns(ns->pid_ns_for_children);
769071ac
AV
201 if (ns->time_ns)
202 put_time_ns(ns->time_ns);
203 if (ns->time_ns_for_children)
204 put_time_ns(ns->time_ns_for_children);
a79a908f 205 put_cgroup_ns(ns->cgroup_ns);
9dd776b6 206 put_net(ns->net_ns);
98c0d07c 207 kmem_cache_free(nsproxy_cachep, ns);
ab516013 208}
e3222c4e
BP
209
210/*
211 * Called from unshare. Unshare all the namespaces part of nsproxy.
4e71e474 212 * On success, returns the new nsproxy.
e3222c4e
BP
213 */
214int unshare_nsproxy_namespaces(unsigned long unshare_flags,
b2e0d987 215 struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
e3222c4e 216{
bcf58e72 217 struct user_namespace *user_ns;
e3222c4e
BP
218 int err = 0;
219
77ec739d 220 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
769071ac
AV
221 CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
222 CLONE_NEWTIME)))
e3222c4e
BP
223 return 0;
224
b2e0d987
EB
225 user_ns = new_cred ? new_cred->user_ns : current_user_ns();
226 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
e3222c4e
BP
227 return -EPERM;
228
bcf58e72 229 *new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
b2e0d987 230 new_fs ? new_fs : current->fs);
858d72ea 231 if (IS_ERR(*new_nsp)) {
e3222c4e 232 err = PTR_ERR(*new_nsp);
858d72ea
SH
233 goto out;
234 }
235
858d72ea 236out:
e3222c4e
BP
237 return err;
238}
98c0d07c 239
cf7b708c
PE
240void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
241{
242 struct nsproxy *ns;
243
244 might_sleep();
245
728dba3a 246 task_lock(p);
cf7b708c 247 ns = p->nsproxy;
728dba3a
EB
248 p->nsproxy = new;
249 task_unlock(p);
cf7b708c 250
728dba3a 251 if (ns && atomic_dec_and_test(&ns->count))
cf7b708c 252 free_nsproxy(ns);
cf7b708c
PE
253}
254
255void exit_task_namespaces(struct task_struct *p)
256{
257 switch_task_namespaces(p, NULL);
258}
259
0663c6f8
EB
260SYSCALL_DEFINE2(setns, int, fd, int, nstype)
261{
0663c6f8
EB
262 struct task_struct *tsk = current;
263 struct nsproxy *new_nsproxy;
0663c6f8 264 struct file *file;
33c42940 265 struct ns_common *ns;
0663c6f8
EB
266 int err;
267
0663c6f8
EB
268 file = proc_ns_fget(fd);
269 if (IS_ERR(file))
270 return PTR_ERR(file);
271
272 err = -EINVAL;
f77c8014 273 ns = get_proc_ns(file_inode(file));
33c42940 274 if (nstype && (ns->ops->type != nstype))
0663c6f8
EB
275 goto out;
276
bcf58e72 277 new_nsproxy = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs);
0663c6f8
EB
278 if (IS_ERR(new_nsproxy)) {
279 err = PTR_ERR(new_nsproxy);
280 goto out;
281 }
282
33c42940 283 err = ns->ops->install(new_nsproxy, ns);
0663c6f8
EB
284 if (err) {
285 free_nsproxy(new_nsproxy);
286 goto out;
287 }
288 switch_task_namespaces(tsk, new_nsproxy);
e4222673
HB
289
290 perf_event_namespaces(tsk);
0663c6f8
EB
291out:
292 fput(file);
293 return err;
294}
295
66577193 296int __init nsproxy_cache_init(void)
98c0d07c 297{
db8906da 298 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
98c0d07c
CLG
299 return 0;
300}