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