nsproxy.c 4.6 KB
Newer Older
S
Serge E. Hallyn 已提交
1 2 3 4 5 6 7 8 9
/*
 *  Copyright (C) 2006 IBM Corporation
 *
 *  Author: Serge Hallyn <serue@us.ibm.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, version 2 of the
 *  License.
K
Kirill Korotaev 已提交
10 11 12 13
 *
 *  Jun 2006 - namespaces support
 *             OpenVZ, SWsoft Inc.
 *             Pavel Emelianov <xemul@openvz.org>
S
Serge E. Hallyn 已提交
14 15 16 17 18
 */

#include <linux/module.h>
#include <linux/version.h>
#include <linux/nsproxy.h>
19
#include <linux/init_task.h>
20
#include <linux/mnt_namespace.h>
21
#include <linux/utsname.h>
C
Cedric Le Goater 已提交
22
#include <linux/pid_namespace.h>
23
#include <net/net_namespace.h>
24

25 26
static struct kmem_cache *nsproxy_cachep;

27
struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
S
Serge E. Hallyn 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

static inline void get_nsproxy(struct nsproxy *ns)
{
	atomic_inc(&ns->count);
}

void get_task_namespaces(struct task_struct *tsk)
{
	struct nsproxy *ns = tsk->nsproxy;
	if (ns) {
		get_nsproxy(ns);
	}
}

/*
 * creates a copy of "orig" with refcount 1.
 */
45
static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
S
Serge E. Hallyn 已提交
46 47 48
{
	struct nsproxy *ns;

49 50 51
	ns = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
	if (ns) {
		memcpy(ns, orig, sizeof(struct nsproxy));
S
Serge E. Hallyn 已提交
52
		atomic_set(&ns->count, 1);
53
	}
S
Serge E. Hallyn 已提交
54 55 56 57
	return ns;
}

/*
58 59 60
 * Create new nsproxy and all of its the associated namespaces.
 * Return the newly created nsproxy.  Do not attach this to the task,
 * leave it to the caller to do proper locking and attach it to task.
S
Serge E. Hallyn 已提交
61
 */
62 63
static struct nsproxy *create_new_namespaces(unsigned long flags,
			struct task_struct *tsk, struct fs_struct *new_fs)
S
Serge E. Hallyn 已提交
64
{
65
	struct nsproxy *new_nsp;
66
	int err;
S
Serge E. Hallyn 已提交
67

68 69 70
	new_nsp = clone_nsproxy(tsk->nsproxy);
	if (!new_nsp)
		return ERR_PTR(-ENOMEM);
71

72
	new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
73 74
	if (IS_ERR(new_nsp->mnt_ns)) {
		err = PTR_ERR(new_nsp->mnt_ns);
75
		goto out_ns;
76
	}
77 78

	new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
79 80
	if (IS_ERR(new_nsp->uts_ns)) {
		err = PTR_ERR(new_nsp->uts_ns);
81
		goto out_uts;
82
	}
83 84

	new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
85 86
	if (IS_ERR(new_nsp->ipc_ns)) {
		err = PTR_ERR(new_nsp->ipc_ns);
87
		goto out_ipc;
88
	}
89 90

	new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
91 92
	if (IS_ERR(new_nsp->pid_ns)) {
		err = PTR_ERR(new_nsp->pid_ns);
93
		goto out_pid;
94
	}
95

96
	new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
97 98
	if (IS_ERR(new_nsp->user_ns)) {
		err = PTR_ERR(new_nsp->user_ns);
99
		goto out_user;
100
	}
101

102 103 104 105 106 107
	new_nsp->net_ns = copy_net_ns(flags, tsk->nsproxy->net_ns);
	if (IS_ERR(new_nsp->net_ns)) {
		err = PTR_ERR(new_nsp->net_ns);
		goto out_net;
	}

108 109
	return new_nsp;

110 111 112
out_net:
	if (new_nsp->user_ns)
		put_user_ns(new_nsp->user_ns);
113 114 115
out_user:
	if (new_nsp->pid_ns)
		put_pid_ns(new_nsp->pid_ns);
116 117 118 119 120 121 122 123 124 125
out_pid:
	if (new_nsp->ipc_ns)
		put_ipc_ns(new_nsp->ipc_ns);
out_ipc:
	if (new_nsp->uts_ns)
		put_uts_ns(new_nsp->uts_ns);
out_uts:
	if (new_nsp->mnt_ns)
		put_mnt_ns(new_nsp->mnt_ns);
out_ns:
126
	kmem_cache_free(nsproxy_cachep, new_nsp);
127
	return ERR_PTR(err);
S
Serge E. Hallyn 已提交
128 129 130 131 132 133
}

/*
 * called from clone.  This now handles copy for nsproxy and all
 * namespaces therein.
 */
134
int copy_namespaces(unsigned long flags, struct task_struct *tsk)
S
Serge E. Hallyn 已提交
135 136
{
	struct nsproxy *old_ns = tsk->nsproxy;
137 138
	struct nsproxy *new_ns;
	int err = 0;
S
Serge E. Hallyn 已提交
139 140 141 142 143 144

	if (!old_ns)
		return 0;

	get_nsproxy(old_ns);

145
	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWNET)))
146 147
		return 0;

148 149
	if (!capable(CAP_SYS_ADMIN)) {
		err = -EPERM;
150 151 152
		goto out;
	}

153 154 155 156 157
	new_ns = create_new_namespaces(flags, tsk, tsk->fs);
	if (IS_ERR(new_ns)) {
		err = PTR_ERR(new_ns);
		goto out;
	}
C
Cedric Le Goater 已提交
158

159
	tsk->nsproxy = new_ns;
160
out:
161
	put_nsproxy(old_ns);
162
	return err;
S
Serge E. Hallyn 已提交
163 164 165 166
}

void free_nsproxy(struct nsproxy *ns)
{
C
Cedric Le Goater 已提交
167 168 169 170 171 172 173 174
	if (ns->mnt_ns)
		put_mnt_ns(ns->mnt_ns);
	if (ns->uts_ns)
		put_uts_ns(ns->uts_ns);
	if (ns->ipc_ns)
		put_ipc_ns(ns->ipc_ns);
	if (ns->pid_ns)
		put_pid_ns(ns->pid_ns);
175 176
	if (ns->user_ns)
		put_user_ns(ns->user_ns);
177
	put_net(ns->net_ns);
178
	kmem_cache_free(nsproxy_cachep, ns);
S
Serge E. Hallyn 已提交
179
}
180 181 182

/*
 * Called from unshare. Unshare all the namespaces part of nsproxy.
183
 * On success, returns the new nsproxy.
184 185 186 187 188 189
 */
int unshare_nsproxy_namespaces(unsigned long unshare_flags,
		struct nsproxy **new_nsp, struct fs_struct *new_fs)
{
	int err = 0;

S
Serge E. Hallyn 已提交
190
	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
191
			       CLONE_NEWUSER | CLONE_NEWNET)))
192 193 194 195 196 197 198
		return 0;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	*new_nsp = create_new_namespaces(unshare_flags, current,
				new_fs ? new_fs : current->fs);
199
	if (IS_ERR(*new_nsp))
200 201 202
		err = PTR_ERR(*new_nsp);
	return err;
}
203 204 205 206

static int __init nsproxy_cache_init(void)
{
	nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy),
207
					   0, SLAB_PANIC, NULL);
208 209 210 211
	return 0;
}

module_init(nsproxy_cache_init);