Merge branch 'master' of ssh://git.planet-lab.org/git/procprotect
[procprotect.git] / procprotect.c
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>
6 #include <linux/fs.h>
7 #include <linux/mm.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/mount.h>
21 #include <linux/dcache.h>
22 #include <linux/spinlock.h>
23 #include <linux/completion.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/kprobes.h>
27 #include <linux/kallsyms.h>
28 #include <linux/nsproxy.h>
29
30 #define VERSION_STR "0.0.1"
31
32 #ifndef CONFIG_X86_64
33 #error "This code does not support your architecture"
34 #endif
35
36 static char *aclpath __devinitdata = "procprotect";
37 static struct qstr aclqpath;
38
39 module_param(aclpath, charp, 0);
40 MODULE_PARM_DESC(aclpath, "Root of directory that stores acl tags for /proc files.");
41
42 MODULE_AUTHOR("Sapan Bhatia <sapanb@cs.princeton.edu>");
43 MODULE_DESCRIPTION("Lightweight ACLs for /proc.");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(VERSION_STR);
46
47 struct procprotect_ctx {
48     struct inode **inode;
49     int flags;
50 };
51
52 struct acl_entry {
53     unsigned int ino;
54     struct hlist_node hlist;
55 };
56
57 #define HASH_SIZE (1<<16)
58
59 struct hlist_head procprotect_hash[HASH_SIZE];
60
61 struct proc_dir_entry *proc_entry;
62
63 /*
64    Entry point of intercepted call. We need to do two things here:
65    - Decide if we need the heavier return hook to be called
66    - Save the first argument, which is in a register, for consideration in the return hook
67    */
68 static int do_lookup_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
69     int ret = -1;
70     struct procprotect_ctx *ctx;
71     struct nameidata *nd = (struct nameidata *) regs->di;
72     struct qstr *q = (struct qstr *) regs->si;
73     struct dentry *parent = nd->path.dentry;
74     struct inode *pinode = parent->d_inode;
75
76     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
77             && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {   
78         ctx = (struct procprotect_ctx *) ri->data;
79         ctx->inode = regs->cx;
80         ctx->flags = nd->flags;
81         ret = 0;
82     }
83
84     return ret;
85 }
86
87 static int run_acl(unsigned long ino) {
88     struct hlist_node *n;
89     struct acl_entry *entry;
90     hlist_for_each_entry_rcu(entry, 
91             n, &procprotect_hash[ino & (HASH_SIZE-1)],
92             hlist) {
93         if (entry->ino==ino) {
94             return 0;
95         }
96     }
97     return 1;
98 }
99
100 /* The entry hook ensures that the return hook is only called for
101    accesses to /proc */
102
103 static int do_lookup_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
104 {
105     struct procprotect_ctx *ctx = (struct procprotect_ctx *) ri->data;
106     int ret = regs->ax;
107
108     if (ret==0) {
109         /* The kernel is going to honor the request. Here's where we step in */
110         struct inode *inode = *(ctx->inode);
111         if (!run_acl(inode->i_ino)) {
112             if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
113                 regs->ax = -EPERM;
114             }
115         }
116     }
117
118     return 0;
119 }
120
121 struct open_flags {
122   int open_flag;
123   umode_t mode;
124   int acc_mode;
125   int intent;
126 };
127
128 static struct file *do_last_probe(struct nameidata *nd, struct path *path,
129                          struct open_flags *op, const char *pathname) {
130     struct dentry *parent = nd->path.dentry;
131     struct inode *pinode = parent->d_inode;
132
133     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
134         op->open_flag &= ~O_CREAT;
135     }
136     jprobe_return();
137 }
138
139 static struct jprobe dolast_probe = {
140         .entry = (kprobe_opcode_t *) do_last_probe
141 };
142
143 static struct kretprobe proc_probe = {
144     .entry_handler = (kprobe_opcode_t *) do_lookup_entry,
145     .handler = (kprobe_opcode_t *) do_lookup_ret,
146     .maxactive = 20,
147     .data_size = sizeof(struct procprotect_ctx)
148 };
149
150 static void add_entry(char *pathname) {
151     struct path path;
152     if (kern_path(pathname, 0, &path)) {
153         printk(KERN_CRIT "Path lookup failed for %s",pathname);
154     }   
155     else {
156         unsigned int ino = path.dentry->d_inode->i_ino;
157         struct acl_entry *entry;
158         entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
159         entry->ino = ino;
160
161         if (!entry) {
162             printk(KERN_CRIT "Could not allocate memory for %s",pathname);
163         }
164         else {
165             if (run_acl(ino)) {
166                 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
167                 printk(KERN_CRIT "Added inode %u",ino);
168             }
169             else {
170                 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
171             }
172         }
173     }
174 }
175
176
177 static void __exit procprotect_exit(void)
178 {
179     unregister_kretprobe(&proc_probe);
180         unregister_jprobe(&dolast_probe);
181     struct hlist_node *n;
182     struct acl_entry *entry;
183     int i;
184
185     for (i=0;i<HASH_SIZE;i++) {
186         hlist_for_each_entry_rcu(entry, 
187                 n, &procprotect_hash[i],
188                 hlist) {
189             kfree(entry);
190         }
191     }
192
193     remove_proc_entry("procprotect",NULL);
194     printk("Procprotect: Stopped procprotect.\n");
195 }
196
197
198
199 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {            
200     char pathname[PATH_MAX];
201
202     if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
203         return -EPERM;
204
205     if (copy_from_user(pathname, buffer, count)) {
206         return -EFAULT;
207     }
208     if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
209         pathname[count-1]='\0';
210     }
211     else
212         pathname[count]='\0';
213     add_entry(pathname);        
214     printk(KERN_CRIT "Length of buffer=%d",strlen(pathname));
215     return count;
216 }
217
218 static int __init procprotect_init(void)
219 {
220     printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
221             VERSION_STR, aclpath);
222     int ret;
223     int i;
224
225     for(i=0;i<HASH_SIZE;i++) {
226         INIT_HLIST_HEAD(&procprotect_hash[i]);
227     }
228
229     aclqpath.name = aclpath;
230     aclqpath.len = strnlen(aclpath, PATH_MAX);
231
232     proc_probe.kp.addr = 
233         (kprobe_opcode_t *) kallsyms_lookup_name("do_lookup");
234     if (!proc_probe.kp.addr) {
235         printk("Couldn't find %s to plant kretprobe\n", "do_execve");
236         return -1;
237     }
238
239     dolast_probe.kp.addr = 
240         (kprobe_opcode_t *) kallsyms_lookup_name("do_last");
241
242     if (!dolast_probe.kp.addr) {
243         printk("Couldn't find %s to plant kretprobe\n", "do_last");
244         return -1;
245     }
246
247     if ((ret = register_jprobe(&dolast_probe)) <0) {
248                   printk("register_jprobe failed, returned %u\n", ret);
249                   return -1;
250     }
251
252     if ((ret = register_kretprobe(&proc_probe)) <0) {
253         printk("register_kretprobe failed, returned %d\n", ret);
254         return -1;
255     }
256     printk("Planted kretprobe at %p, handler addr %p\n",
257             proc_probe.kp.addr, proc_probe.handler);
258
259     proc_entry = create_proc_entry("procprotect", 0644, NULL);
260     proc_entry->write_proc = procfile_write;
261
262     return ret;
263 }
264
265
266
267 module_init(procprotect_init);
268 module_exit(procprotect_exit);