nsproxy.c 3.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 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;
S
Serge E. Hallyn 已提交
61

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_fs);
	if (IS_ERR(new_nsp->mnt_ns))
		goto out_ns;

	new_nsp->uts_ns = copy_utsname(flags, tsk->nsproxy->uts_ns);
	if (IS_ERR(new_nsp->uts_ns))
		goto out_uts;

	new_nsp->ipc_ns = copy_ipcs(flags, tsk->nsproxy->ipc_ns);
	if (IS_ERR(new_nsp->ipc_ns))
		goto out_ipc;

	new_nsp->pid_ns = copy_pid_ns(flags, tsk->nsproxy->pid_ns);
	if (IS_ERR(new_nsp->pid_ns))
		goto out_pid;

	return new_nsp;

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);
	return ERR_PTR(-ENOMEM);
S
Serge E. Hallyn 已提交
96 97 98 99 100 101 102 103 104
}

/*
 * 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;
105 106
	struct nsproxy *new_ns;
	int err = 0;
S
Serge E. Hallyn 已提交
107 108 109 110 111 112

	if (!old_ns)
		return 0;

	get_nsproxy(old_ns);

K
Kirill Korotaev 已提交
113
	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
114 115
		return 0;

116 117
	if (!capable(CAP_SYS_ADMIN)) {
		err = -EPERM;
118 119 120
		goto out;
	}

121 122 123 124 125
	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 已提交
126

127
	tsk->nsproxy = new_ns;
128
out:
129
	put_nsproxy(old_ns);
130
	return err;
S
Serge E. Hallyn 已提交
131 132 133 134
}

void free_nsproxy(struct nsproxy *ns)
{
C
Cedric Le Goater 已提交
135 136 137 138 139 140 141 142 143
	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);
	kfree(ns);
S
Serge E. Hallyn 已提交
144
}
145 146 147

/*
 * Called from unshare. Unshare all the namespaces part of nsproxy.
148
 * On success, returns the new nsproxy.
149 150 151 152 153 154 155 156 157 158 159 160 161 162
 */
int unshare_nsproxy_namespaces(unsigned long unshare_flags,
		struct nsproxy **new_nsp, struct fs_struct *new_fs)
{
	int err = 0;

	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
		return 0;

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

	*new_nsp = create_new_namespaces(unshare_flags, current,
				new_fs ? new_fs : current->fs);
163
	if (IS_ERR(*new_nsp))
164 165 166
		err = PTR_ERR(*new_nsp);
	return err;
}