aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/nsproxy.c
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederm@xmission.com>2010-03-07 20:48:52 -0500
committerEric W. Biederman <ebiederm@xmission.com>2011-05-10 17:32:56 -0400
commit0663c6f8fa37d777ede74ff991a0cba3a42fcbd7 (patch)
tree83275d8fd4e0bcc9cb8fdde5c15bb5e4bead92fd /kernel/nsproxy.c
parent6b4e306aa3dc94a0545eb9279475b1ab6209a31f (diff)
ns: Introduce the setns syscall
With the networking stack today there is demand to handle multiple network stacks at a time. Not in the context of containers but in the context of people doing interesting things with routing. There is also demand in the context of containers to have an efficient way to execute some code in the container itself. If nothing else it is very useful ad a debugging technique. Both problems can be solved by starting some form of login daemon in the namespaces people want access to, or you can play games by ptracing a process and getting the traced process to do things you want it to do. However it turns out that a login daemon or a ptrace puppet controller are more code, they are more prone to failure, and generally they are less efficient than simply changing the namespace of a process to a specified one. Pieces of this puzzle can also be solved by instead of coming up with a general purpose system call coming up with targed system calls perhaps socketat that solve a subset of the larger problem. Overall that appears to be more work for less reward. int setns(int fd, int nstype); The fd argument is a file descriptor referring to a proc file of the namespace you want to switch the process to. In the setns system call the nstype is 0 or specifies an clone flag of the namespace you intend to change to prevent changing a namespace unintentionally. v2: Most of the architecture support added by Daniel Lezcano <dlezcano@fr.ibm.com> v3: ported to v2.6.36-rc4 by: Eric W. Biederman <ebiederm@xmission.com> v4: Moved wiring up of the system call to another patch v5: Cleaned up the system call arguments - Changed the order. - Modified nstype to take the standard clone flags. v6: Added missing error handling as pointed out by Matt Helsley <matthltc@us.ibm.com> Acked-by: Daniel Lezcano <daniel.lezcano@free.fr> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'kernel/nsproxy.c')
-rw-r--r--kernel/nsproxy.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
index a05d191ffdd9..5424e37673ed 100644
--- a/kernel/nsproxy.c
+++ b/kernel/nsproxy.c
@@ -22,6 +22,9 @@
22#include <linux/pid_namespace.h> 22#include <linux/pid_namespace.h>
23#include <net/net_namespace.h> 23#include <net/net_namespace.h>
24#include <linux/ipc_namespace.h> 24#include <linux/ipc_namespace.h>
25#include <linux/proc_fs.h>
26#include <linux/file.h>
27#include <linux/syscalls.h>
25 28
26static struct kmem_cache *nsproxy_cachep; 29static struct kmem_cache *nsproxy_cachep;
27 30
@@ -233,6 +236,45 @@ void exit_task_namespaces(struct task_struct *p)
233 switch_task_namespaces(p, NULL); 236 switch_task_namespaces(p, NULL);
234} 237}
235 238
239SYSCALL_DEFINE2(setns, int, fd, int, nstype)
240{
241 const struct proc_ns_operations *ops;
242 struct task_struct *tsk = current;
243 struct nsproxy *new_nsproxy;
244 struct proc_inode *ei;
245 struct file *file;
246 int err;
247
248 if (!capable(CAP_SYS_ADMIN))
249 return -EPERM;
250
251 file = proc_ns_fget(fd);
252 if (IS_ERR(file))
253 return PTR_ERR(file);
254
255 err = -EINVAL;
256 ei = PROC_I(file->f_dentry->d_inode);
257 ops = ei->ns_ops;
258 if (nstype && (ops->type != nstype))
259 goto out;
260
261 new_nsproxy = create_new_namespaces(0, tsk, tsk->fs);
262 if (IS_ERR(new_nsproxy)) {
263 err = PTR_ERR(new_nsproxy);
264 goto out;
265 }
266
267 err = ops->install(new_nsproxy, ei->ns);
268 if (err) {
269 free_nsproxy(new_nsproxy);
270 goto out;
271 }
272 switch_task_namespaces(tsk, new_nsproxy);
273out:
274 fput(file);
275 return err;
276}
277
236static int __init nsproxy_cache_init(void) 278static int __init nsproxy_cache_init(void)
237{ 279{
238 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC); 280 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);