1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006 IBM Corporation
5 * Author: Serge Hallyn <serue@us.ibm.com>
7 * Jun 2006 - namespaces support
9 * Pavel Emelianov <xemul@openvz.org>
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <linux/nsproxy.h>
15 #include <linux/init_task.h>
16 #include <linux/mnt_namespace.h>
17 #include <linux/utsname.h>
18 #include <linux/pid_namespace.h>
19 #include <net/net_namespace.h>
20 #include <linux/ipc_namespace.h>
21 #include <linux/time_namespace.h>
22 #include <linux/proc_ns.h>
23 #include <linux/file.h>
24 #include <linux/syscalls.h>
25 #include <linux/cgroup.h>
26 #include <linux/perf_event.h>
28 static struct kmem_cache
*nsproxy_cachep
;
30 struct nsproxy init_nsproxy
= {
31 .count
= ATOMIC_INIT(1),
32 .uts_ns
= &init_uts_ns
,
33 #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
34 .ipc_ns
= &init_ipc_ns
,
37 .pid_ns_for_children
= &init_pid_ns
,
42 .cgroup_ns
= &init_cgroup_ns
,
45 .time_ns
= &init_time_ns
,
46 .time_ns_for_children
= &init_time_ns
,
50 static inline struct nsproxy
*create_nsproxy(void)
52 struct nsproxy
*nsproxy
;
54 nsproxy
= kmem_cache_alloc(nsproxy_cachep
, GFP_KERNEL
);
56 atomic_set(&nsproxy
->count
, 1);
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.
65 static struct nsproxy
*create_new_namespaces(unsigned long flags
,
66 struct task_struct
*tsk
, struct user_namespace
*user_ns
,
67 struct fs_struct
*new_fs
)
69 struct nsproxy
*new_nsp
;
72 new_nsp
= create_nsproxy();
74 return ERR_PTR(-ENOMEM
);
76 new_nsp
->mnt_ns
= copy_mnt_ns(flags
, tsk
->nsproxy
->mnt_ns
, user_ns
, new_fs
);
77 if (IS_ERR(new_nsp
->mnt_ns
)) {
78 err
= PTR_ERR(new_nsp
->mnt_ns
);
82 new_nsp
->uts_ns
= copy_utsname(flags
, user_ns
, tsk
->nsproxy
->uts_ns
);
83 if (IS_ERR(new_nsp
->uts_ns
)) {
84 err
= PTR_ERR(new_nsp
->uts_ns
);
88 new_nsp
->ipc_ns
= copy_ipcs(flags
, user_ns
, tsk
->nsproxy
->ipc_ns
);
89 if (IS_ERR(new_nsp
->ipc_ns
)) {
90 err
= PTR_ERR(new_nsp
->ipc_ns
);
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
);
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
);
108 new_nsp
->net_ns
= copy_net_ns(flags
, user_ns
, tsk
->nsproxy
->net_ns
);
109 if (IS_ERR(new_nsp
->net_ns
)) {
110 err
= PTR_ERR(new_nsp
->net_ns
);
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
);
120 new_nsp
->time_ns
= get_time_ns(tsk
->nsproxy
->time_ns
);
125 put_net(new_nsp
->net_ns
);
127 put_cgroup_ns(new_nsp
->cgroup_ns
);
129 if (new_nsp
->pid_ns_for_children
)
130 put_pid_ns(new_nsp
->pid_ns_for_children
);
133 put_ipc_ns(new_nsp
->ipc_ns
);
136 put_uts_ns(new_nsp
->uts_ns
);
139 put_mnt_ns(new_nsp
->mnt_ns
);
141 kmem_cache_free(nsproxy_cachep
, new_nsp
);
146 * called from clone. This now handles copy for nsproxy and all
147 * namespaces therein.
149 int copy_namespaces(unsigned long flags
, struct task_struct
*tsk
)
151 struct nsproxy
*old_ns
= tsk
->nsproxy
;
152 struct user_namespace
*user_ns
= task_cred_xxx(tsk
, user_ns
);
153 struct nsproxy
*new_ns
;
156 if (likely(!(flags
& (CLONE_NEWNS
| CLONE_NEWUTS
| CLONE_NEWIPC
|
157 CLONE_NEWPID
| CLONE_NEWNET
|
158 CLONE_NEWCGROUP
| CLONE_NEWTIME
)))) {
159 if (likely(old_ns
->time_ns_for_children
== old_ns
->time_ns
)) {
163 } else if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
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.
173 if ((flags
& (CLONE_NEWIPC
| CLONE_SYSVSEM
)) ==
174 (CLONE_NEWIPC
| CLONE_SYSVSEM
))
177 new_ns
= create_new_namespaces(flags
, tsk
, user_ns
, tsk
->fs
);
179 return PTR_ERR(new_ns
);
181 ret
= timens_on_fork(new_ns
, tsk
);
183 free_nsproxy(new_ns
);
187 tsk
->nsproxy
= new_ns
;
191 void free_nsproxy(struct nsproxy
*ns
)
194 put_mnt_ns(ns
->mnt_ns
);
196 put_uts_ns(ns
->uts_ns
);
198 put_ipc_ns(ns
->ipc_ns
);
199 if (ns
->pid_ns_for_children
)
200 put_pid_ns(ns
->pid_ns_for_children
);
202 put_time_ns(ns
->time_ns
);
203 if (ns
->time_ns_for_children
)
204 put_time_ns(ns
->time_ns_for_children
);
205 put_cgroup_ns(ns
->cgroup_ns
);
207 kmem_cache_free(nsproxy_cachep
, ns
);
211 * Called from unshare. Unshare all the namespaces part of nsproxy.
212 * On success, returns the new nsproxy.
214 int unshare_nsproxy_namespaces(unsigned long unshare_flags
,
215 struct nsproxy
**new_nsp
, struct cred
*new_cred
, struct fs_struct
*new_fs
)
217 struct user_namespace
*user_ns
;
220 if (!(unshare_flags
& (CLONE_NEWNS
| CLONE_NEWUTS
| CLONE_NEWIPC
|
221 CLONE_NEWNET
| CLONE_NEWPID
| CLONE_NEWCGROUP
|
225 user_ns
= new_cred
? new_cred
->user_ns
: current_user_ns();
226 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
229 *new_nsp
= create_new_namespaces(unshare_flags
, current
, user_ns
,
230 new_fs
? new_fs
: current
->fs
);
231 if (IS_ERR(*new_nsp
)) {
232 err
= PTR_ERR(*new_nsp
);
240 void switch_task_namespaces(struct task_struct
*p
, struct nsproxy
*new)
251 if (ns
&& atomic_dec_and_test(&ns
->count
))
255 void exit_task_namespaces(struct task_struct
*p
)
257 switch_task_namespaces(p
, NULL
);
260 SYSCALL_DEFINE2(setns
, int, fd
, int, nstype
)
262 struct task_struct
*tsk
= current
;
263 struct nsproxy
*new_nsproxy
;
265 struct ns_common
*ns
;
268 file
= proc_ns_fget(fd
);
270 return PTR_ERR(file
);
273 ns
= get_proc_ns(file_inode(file
));
274 if (nstype
&& (ns
->ops
->type
!= nstype
))
277 new_nsproxy
= create_new_namespaces(0, tsk
, current_user_ns(), tsk
->fs
);
278 if (IS_ERR(new_nsproxy
)) {
279 err
= PTR_ERR(new_nsproxy
);
283 err
= ns
->ops
->install(new_nsproxy
, ns
);
285 free_nsproxy(new_nsproxy
);
288 switch_task_namespaces(tsk
, new_nsproxy
);
290 perf_event_namespaces(tsk
);
296 int __init
nsproxy_cache_init(void)
298 nsproxy_cachep
= KMEM_CACHE(nsproxy
, SLAB_PANIC
);