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/mnt_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 mount 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;
46 static void *mntns_get(struct task_struct *task)
48 struct mnt_namespace *ns;
50 ns = task->nsproxy->mnt_ns;
51 atomic_inc(&ns->count);
56 void (*put_mnt_ns2)(struct mnt_namespace *ns);
58 static void mntns_put(void *ns)
63 static int mntns_install(struct nsproxy *nsproxy, void *ns)
65 struct fs_struct *fs = current->fs;
66 struct mnt_namespace *mnt_ns = ns;
72 atomic_inc(&mnt_ns->count);
73 (*put_mnt_ns2)(nsproxy->mnt_ns);
74 nsproxy->mnt_ns = mnt_ns;
76 root.mnt = &mnt_ns->root->mnt;
77 root.dentry = mnt_ns->root->mnt.mnt_root;
79 while(d_mountpoint(root.dentry) && follow_down(&root))
93 const struct proc_ns_operations mntns_operations = {
97 .install = mntns_install,
100 void (*free_nsproxy2)(struct nsproxy *ns);
102 struct nsproxy * (*create_new_namespaces)(unsigned long flags,
103 struct task_struct *tsk, struct fs_struct *new_fs);
105 void (*switch_task_namespaces2)(struct task_struct *p, struct nsproxy *new);
107 static int setns(struct task_struct *reftask) {
108 const struct proc_ns_operations *ops;
109 struct task_struct *tsk = current;
110 struct nsproxy *new_nsproxy;
113 if (!capable(CAP_SYS_ADMIN))
117 ops = &mntns_operations;
119 new_nsproxy = (*create_new_namespaces)(0, tsk, tsk->fs);
120 if (IS_ERR(new_nsproxy)) {
121 err = PTR_ERR(new_nsproxy);
125 err = ops->install(new_nsproxy, reftask->nsproxy->mnt_ns);
127 (*free_nsproxy2)(new_nsproxy);
130 (*switch_task_namespaces2)(tsk, new_nsproxy);
137 static void __exit lxcsu_exit(void)
139 remove_proc_entry("lxcsu",NULL);
140 printk("Procprotect: Stopped lxcsu.\n");
143 int lxcsu_write(struct file *file, const char *buffer, unsigned long count, void *data) {
144 char pidname[PATH_MAX];
148 struct task_struct *task;
150 if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
153 if (copy_from_user(pidname, buffer, count)) {
156 if (count && (pidname[count-1]==10 || pidname[count-1]==13)) {
157 pidname[count-1]='\0';
162 pid = simple_strtol(pidname,s,10);
164 task = get_pid_task(p,PIDTYPE_PID);
169 printk(KERN_CRIT "Task %d not found",pid);
176 static int __init lxcsu_init(void)
179 printk("Mntsu: starting lxcsu version %s.\n",
183 free_nsproxy2 = kallsyms_lookup_name("free_nsproxy");
184 printk(KERN_CRIT "free_nsproxy = %x",free_nsproxy2);
186 switch_task_namespaces2 = kallsyms_lookup_name("switch_task_namespaces");
187 printk(KERN_CRIT "switch_task_namespaces = %x",switch_task_namespaces2);
189 create_new_namespaces = kallsyms_lookup_name("create_new_namespaces");
190 printk(KERN_CRIT "create_new_namespaces = %x",create_new_namespaces);
192 put_mnt_ns2 = kallsyms_lookup_name("put_mnt_ns");
193 printk(KERN_CRIT "Put mnt ns = %x",put_mnt_ns2);
195 proc_entry = create_proc_entry("lxcsu", 0644, NULL);
196 proc_entry->write_proc = lxcsu_write;
200 module_init(lxcsu_init);
201 module_exit(lxcsu_exit);