1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/types.h>
4 #include <linux/kernel.h>
5 #include <linux/fs_struct.h>
8 #include <linux/reboot.h>
9 #include <linux/delay.h>
10 #include <linux/proc_fs.h>
11 #include <asm/uaccess.h>
12 #include <linux/sysrq.h>
13 #include <linux/timer.h>
14 #include <linux/time.h>
15 #include <linux/lglock.h>
16 #include <linux/init.h>
17 #include <linux/idr.h>
18 #include <linux/namei.h>
19 #include <linux/bitops.h>
20 #include <linux/pid_namespace.h>
21 #include <linux/path.h>
22 #include <linux/dcache.h>
23 #include <linux/spinlock.h>
24 #include <linux/completion.h>
25 #include <linux/sched.h>
26 #include <linux/seq_file.h>
27 #include <linux/file.h>
28 #include <linux/kprobes.h>
29 #include <linux/kallsyms.h>
30 #include <linux/nsproxy.h>
33 #define VERSION_STR "0.0.1"
36 #error "This code does not support your architecture"
39 MODULE_AUTHOR("Sapan Bhatia <sapanb@cs.princeton.edu>");
40 MODULE_DESCRIPTION("Hack to enable pid namespace switching via setns. Based on a patch by Eric Biederman.");
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(VERSION_STR);
44 struct proc_dir_entry *proc_entry;
50 * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
51 * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
52 * Many thanks to Oleg Nesterov for comments and help
56 #define BITS_PER_PAGE (PAGE_SIZE*8)
58 void (*free_nsproxy2)(struct nsproxy *ns);
59 struct nsproxy * (*create_new_namespaces)(unsigned long flags,
60 struct task_struct *tsk, struct fs_struct *new_fs);
62 void (*switch_task_namespaces2)(struct task_struct *p, struct nsproxy *new);
63 void (*free_pid_ns2)(struct kref *kref);
69 struct kmem_cache *cachep;
70 struct list_head list;
73 static LIST_HEAD(pid_caches_lh);
74 static DEFINE_MUTEX(pid_caches_mutex);
76 static void *pidns_get(struct task_struct *task)
78 struct pid_namespace *ns;
81 ns = get_pid_ns(task_active_pid_ns(task));
87 static void pidns_put(void *ns)
89 struct pid_namespace *pidns = ns;
90 if (pidns != &init_pid_ns)
91 kref_put(&pidns->kref, free_pid_ns2);
94 static int pidns_install(struct nsproxy *nsproxy, void *ns)
96 //printk(KERN_CRIT "Entering pid ns");
97 pidns_put(nsproxy->pid_ns);
98 nsproxy->pid_ns = get_pid_ns(ns);
102 const struct proc_ns_operations pidns_operations = {
104 .type = CLONE_NEWPID,
107 .install = pidns_install,
110 static int setns(struct task_struct *reftask) {
111 const struct proc_ns_operations *ops;
112 struct task_struct *tsk = current;
113 struct nsproxy *new_nsproxy;
116 if (!capable(CAP_SYS_ADMIN))
120 ops = &pidns_operations;
122 new_nsproxy = (*create_new_namespaces)(0, tsk, tsk->fs);
123 if (IS_ERR(new_nsproxy)) {
124 err = PTR_ERR(new_nsproxy);
128 err = ops->install(new_nsproxy, reftask->nsproxy->pid_ns);
130 (*free_nsproxy2)(new_nsproxy);
133 (*switch_task_namespaces2)(tsk, new_nsproxy);
140 static void __exit pidsu_exit(void)
142 remove_proc_entry("pidsu",NULL);
143 printk("Procprotect: Stopped pidsu.\n");
146 int pidsu_write(struct file *file, const char *buffer, unsigned long count, void *data) {
151 struct task_struct *task;
153 if (current->nsproxy->pid_ns!=init_task.nsproxy->pid_ns)
156 if (copy_from_user(pidname, buffer, count)) {
159 if (count && (pidname[count-1]==10 || pidname[count-1]==13)) {
160 pidname[count-1]='\0';
165 pid = simple_strtol(pidname,s,10);
167 task = get_pid_task(p,PIDTYPE_PID);
172 printk(KERN_CRIT "Task %d not found",pid);
178 static int __init pidsu_init(void)
181 printk("Mntsu: starting pidsu version %s.\n",
184 free_pid_ns2 = kallsyms_lookup_name("free_pid_ns");
185 printk(KERN_CRIT "free_pid_ns = %x",free_pid_ns2);
187 free_nsproxy2 = kallsyms_lookup_name("free_nsproxy");
188 printk(KERN_CRIT "free_nsproxy = %x",(unsigned int) free_nsproxy2);
190 switch_task_namespaces2 = kallsyms_lookup_name("switch_task_namespaces");
191 printk(KERN_CRIT "switch_task_namespaces = %x",(unsigned int) switch_task_namespaces2);
193 create_new_namespaces = kallsyms_lookup_name("create_new_namespaces");
194 printk(KERN_CRIT "create_new_namespaces = %x",(unsigned int) create_new_namespaces);
196 /*put_pid_ns2 = kallsyms_lookup_name("put_pid_ns");
197 printk(KERN_CRIT "Put pid ns = %x",put_pid_ns2);*/
199 proc_entry = create_proc_entry("pidsu", 0644, NULL);
200 proc_entry->write_proc = pidsu_write;
204 module_init(pidsu_init);
205 module_exit(pidsu_exit);