ext3: remove extra IS_RDONLY() check
[deliverable/linux.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
16#include <linux/module.h>
17#include <linux/version.h>
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>
0437eb59 23
98c0d07c
CLG
24static struct kmem_cache *nsproxy_cachep;
25
0437eb59 26struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
ab516013
SH
27
28static inline void get_nsproxy(struct nsproxy *ns)
29{
30 atomic_inc(&ns->count);
31}
32
33void get_task_namespaces(struct task_struct *tsk)
34{
35 struct nsproxy *ns = tsk->nsproxy;
36 if (ns) {
37 get_nsproxy(ns);
38 }
39}
40
41/*
42 * creates a copy of "orig" with refcount 1.
ab516013 43 */
e3222c4e 44static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
ab516013
SH
45{
46 struct nsproxy *ns;
47
98c0d07c
CLG
48 ns = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
49 if (ns) {
50 memcpy(ns, orig, sizeof(struct nsproxy));
ab516013 51 atomic_set(&ns->count, 1);
98c0d07c 52 }
ab516013
SH
53 return ns;
54}
55
56/*
e3222c4e
BP
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.
ab516013 60 */
e3222c4e
BP
61static struct nsproxy *create_new_namespaces(int flags, struct task_struct *tsk,
62 struct fs_struct *new_fs)
ab516013 63{
e3222c4e 64 struct nsproxy *new_nsp;
467e9f4b 65 int err;
ab516013 66
e3222c4e
BP
67 new_nsp = clone_nsproxy(tsk->nsproxy);
68 if (!new_nsp)
69 return ERR_PTR(-ENOMEM);
1651e14e 70
e3222c4e 71 new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
467e9f4b
CLG
72 if (IS_ERR(new_nsp->mnt_ns)) {
73 err = PTR_ERR(new_nsp->mnt_ns);
e3222c4e 74 goto out_ns;
467e9f4b 75 }
e3222c4e
BP
76
77 new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
467e9f4b
CLG
78 if (IS_ERR(new_nsp->uts_ns)) {
79 err = PTR_ERR(new_nsp->uts_ns);
e3222c4e 80 goto out_uts;
467e9f4b 81 }
e3222c4e
BP
82
83 new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
467e9f4b
CLG
84 if (IS_ERR(new_nsp->ipc_ns)) {
85 err = PTR_ERR(new_nsp->ipc_ns);
e3222c4e 86 goto out_ipc;
467e9f4b 87 }
e3222c4e
BP
88
89 new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
467e9f4b
CLG
90 if (IS_ERR(new_nsp->pid_ns)) {
91 err = PTR_ERR(new_nsp->pid_ns);
e3222c4e 92 goto out_pid;
467e9f4b 93 }
e3222c4e 94
acce292c 95 new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
467e9f4b
CLG
96 if (IS_ERR(new_nsp->user_ns)) {
97 err = PTR_ERR(new_nsp->user_ns);
acce292c 98 goto out_user;
467e9f4b 99 }
acce292c 100
e3222c4e
BP
101 return new_nsp;
102
acce292c
CLG
103out_user:
104 if (new_nsp->pid_ns)
105 put_pid_ns(new_nsp->pid_ns);
e3222c4e
BP
106out_pid:
107 if (new_nsp->ipc_ns)
108 put_ipc_ns(new_nsp->ipc_ns);
109out_ipc:
110 if (new_nsp->uts_ns)
111 put_uts_ns(new_nsp->uts_ns);
112out_uts:
113 if (new_nsp->mnt_ns)
114 put_mnt_ns(new_nsp->mnt_ns);
115out_ns:
98c0d07c 116 kmem_cache_free(nsproxy_cachep, new_nsp);
467e9f4b 117 return ERR_PTR(err);
ab516013
SH
118}
119
120/*
121 * called from clone. This now handles copy for nsproxy and all
122 * namespaces therein.
123 */
124int copy_namespaces(int flags, struct task_struct *tsk)
125{
126 struct nsproxy *old_ns = tsk->nsproxy;
1651e14e
SH
127 struct nsproxy *new_ns;
128 int err = 0;
ab516013
SH
129
130 if (!old_ns)
131 return 0;
132
133 get_nsproxy(old_ns);
134
77ec739d 135 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER)))
1651e14e
SH
136 return 0;
137
e3222c4e
BP
138 if (!capable(CAP_SYS_ADMIN)) {
139 err = -EPERM;
1651e14e
SH
140 goto out;
141 }
142
e3222c4e
BP
143 new_ns = create_new_namespaces(flags, tsk, tsk->fs);
144 if (IS_ERR(new_ns)) {
145 err = PTR_ERR(new_ns);
146 goto out;
147 }
9a575a92 148
e3222c4e 149 tsk->nsproxy = new_ns;
1651e14e 150out:
444f378b 151 put_nsproxy(old_ns);
1651e14e 152 return err;
ab516013
SH
153}
154
155void free_nsproxy(struct nsproxy *ns)
156{
9a575a92
CLG
157 if (ns->mnt_ns)
158 put_mnt_ns(ns->mnt_ns);
159 if (ns->uts_ns)
160 put_uts_ns(ns->uts_ns);
161 if (ns->ipc_ns)
162 put_ipc_ns(ns->ipc_ns);
163 if (ns->pid_ns)
164 put_pid_ns(ns->pid_ns);
acce292c
CLG
165 if (ns->user_ns)
166 put_user_ns(ns->user_ns);
98c0d07c 167 kmem_cache_free(nsproxy_cachep, ns);
ab516013 168}
e3222c4e
BP
169
170/*
171 * Called from unshare. Unshare all the namespaces part of nsproxy.
4e71e474 172 * On success, returns the new nsproxy.
e3222c4e
BP
173 */
174int unshare_nsproxy_namespaces(unsigned long unshare_flags,
175 struct nsproxy **new_nsp, struct fs_struct *new_fs)
176{
e3222c4e
BP
177 int err = 0;
178
77ec739d
SH
179 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
180 CLONE_NEWUSER)))
e3222c4e
BP
181 return 0;
182
e3222c4e
BP
183 if (!capable(CAP_SYS_ADMIN))
184 return -EPERM;
185
e3222c4e
BP
186 *new_nsp = create_new_namespaces(unshare_flags, current,
187 new_fs ? new_fs : current->fs);
4e71e474 188 if (IS_ERR(*new_nsp))
e3222c4e 189 err = PTR_ERR(*new_nsp);
e3222c4e
BP
190 return err;
191}
98c0d07c
CLG
192
193static int __init nsproxy_cache_init(void)
194{
195 nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy),
196 0, SLAB_PANIC, NULL, NULL);
197 return 0;
198}
199
200module_init(nsproxy_cache_init);
This page took 0.10753 seconds and 5 git commands to generate.