nsproxy.c 4.0 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 24

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

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.
 */
42
static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
S
Serge E. Hallyn 已提交
43 44 45
{
	struct nsproxy *ns;

46
	ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
47
	if (ns)
S
Serge E. Hallyn 已提交
48 49 50 51 52
		atomic_set(&ns->count, 1);
	return ns;
}

/*
53 54 55
 * 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 已提交
56
 */
57 58
static struct nsproxy *create_new_namespaces(int flags, struct task_struct *tsk,
			struct fs_struct *new_fs)
S
Serge E. Hallyn 已提交
59
{
60
	struct nsproxy *new_nsp;
61
	int err;
S
Serge E. Hallyn 已提交
62

63 64 65
	new_nsp = clone_nsproxy(tsk->nsproxy);
	if (!new_nsp)
		return ERR_PTR(-ENOMEM);
66

67
	new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
68 69
	if (IS_ERR(new_nsp->mnt_ns)) {
		err = PTR_ERR(new_nsp->mnt_ns);
70
		goto out_ns;
71
	}
72 73

	new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
74 75
	if (IS_ERR(new_nsp->uts_ns)) {
		err = PTR_ERR(new_nsp->uts_ns);
76
		goto out_uts;
77
	}
78 79

	new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
80 81
	if (IS_ERR(new_nsp->ipc_ns)) {
		err = PTR_ERR(new_nsp->ipc_ns);
82
		goto out_ipc;
83
	}
84 85

	new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
86 87
	if (IS_ERR(new_nsp->pid_ns)) {
		err = PTR_ERR(new_nsp->pid_ns);
88
		goto out_pid;
89
	}
90

91
	new_nsp->user_ns = copy_user_ns(flags, tsk->nsproxy->user_ns);
92 93
	if (IS_ERR(new_nsp->user_ns)) {
		err = PTR_ERR(new_nsp->user_ns);
94
		goto out_user;
95
	}
96

97 98
	return new_nsp;

99 100 101
out_user:
	if (new_nsp->pid_ns)
		put_pid_ns(new_nsp->pid_ns);
102 103 104 105 106 107 108 109 110 111 112
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:
	kfree(new_nsp);
113
	return ERR_PTR(err);
S
Serge E. Hallyn 已提交
114 115 116 117 118 119 120 121 122
}

/*
 * called from clone.  This now handles copy for nsproxy and all
 * namespaces therein.
 */
int copy_namespaces(int flags, struct task_struct *tsk)
{
	struct nsproxy *old_ns = tsk->nsproxy;
123 124
	struct nsproxy *new_ns;
	int err = 0;
S
Serge E. Hallyn 已提交
125 126 127 128 129 130

	if (!old_ns)
		return 0;

	get_nsproxy(old_ns);

S
Serge E. Hallyn 已提交
131
	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER)))
132 133
		return 0;

134 135
	if (!capable(CAP_SYS_ADMIN)) {
		err = -EPERM;
136 137 138
		goto out;
	}

139 140 141 142 143
	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 已提交
144

145
	tsk->nsproxy = new_ns;
146
out:
147
	put_nsproxy(old_ns);
148
	return err;
S
Serge E. Hallyn 已提交
149 150 151 152
}

void free_nsproxy(struct nsproxy *ns)
{
C
Cedric Le Goater 已提交
153 154 155 156 157 158 159 160
	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);
161 162
	if (ns->user_ns)
		put_user_ns(ns->user_ns);
C
Cedric Le Goater 已提交
163
	kfree(ns);
S
Serge E. Hallyn 已提交
164
}
165 166 167

/*
 * Called from unshare. Unshare all the namespaces part of nsproxy.
168
 * On success, returns the new nsproxy.
169 170 171 172 173 174
 */
int unshare_nsproxy_namespaces(unsigned long unshare_flags,
		struct nsproxy **new_nsp, struct fs_struct *new_fs)
{
	int err = 0;

S
Serge E. Hallyn 已提交
175 176
	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
			       CLONE_NEWUSER)))
177 178 179 180 181 182 183
		return 0;

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

	*new_nsp = create_new_namespaces(unshare_flags, current,
				new_fs ? new_fs : current->fs);
184
	if (IS_ERR(*new_nsp))
185 186 187
		err = PTR_ERR(*new_nsp);
	return err;
}