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