Tests for tonights nightly builds
[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/version.h>
6 #include <linux/fs_struct.h>
7 #include <linux/fs.h>
8 #include <linux/mm.h>
9 #include <linux/reboot.h>
10 #include <linux/delay.h>
11 #include <linux/proc_fs.h>
12 #include <asm/uaccess.h>
13 #include <linux/sysrq.h>
14 #include <linux/timer.h>
15 #include <linux/time.h>
16 #include <linux/lglock.h>
17 #include <linux/init.h>
18 #include <linux/idr.h>
19 #include <linux/namei.h>
20 #include <linux/bitops.h>
21 #include <linux/mount.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/kprobes.h>
28 #include <linux/kallsyms.h>
29 #include <linux/nsproxy.h>
30
31 #define VERSION_STR "0.0.1"
32
33 #ifndef CONFIG_X86_64
34 #error "This code does not support your architecture"
35 #endif
36
37 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
38 static char *aclpath = "procprotect";
39 #else
40 static char *aclpath __devinitdata = "procprotect";
41 #endif
42
43 static struct qstr aclqpath;
44
45 module_param(aclpath, charp, 0);
46 MODULE_PARM_DESC(aclpath, "Root of directory that stores acl tags for /proc files.");
47
48 MODULE_AUTHOR("Sapan Bhatia <sapanb@cs.princeton.edu>");
49 MODULE_DESCRIPTION("Lightweight ACLs for /proc.");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION(VERSION_STR);
52
53 struct procprotect_ctx {
54     struct inode **inode;
55     struct qstr *q;
56     struct path *path;
57     int flags;
58 };
59
60 struct acl_entry {
61     unsigned int ino;
62     struct hlist_node hlist;
63 };
64
65 #define HASH_SIZE (1<<16)
66
67 struct hlist_head procprotect_hash[HASH_SIZE];
68
69 struct proc_dir_entry *proc_entry;
70
71 static int run_acl(unsigned long ino) {
72     struct hlist_node *n;
73     struct acl_entry *entry;
74     hlist_for_each_entry_rcu(entry, 
75             n, &procprotect_hash[ino & (HASH_SIZE-1)],
76             hlist) {
77         if (entry->ino==ino) {
78             return 0;
79         }
80     }
81     return 1;
82 }
83
84 /*
85    Entry point of intercepted call. We need to do two things here:
86    - Decide if we need the heavier return hook to be called
87    - Save the first argument, which is in a register, for consideration in the return hook
88    */
89 static int lookup_fast_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
90     int ret = -1;
91     struct procprotect_ctx *ctx;
92     struct nameidata *nd = (struct nameidata *) regs->di;
93     struct qstr *q = (struct qstr *) regs->si;
94     struct dentry *parent = nd->path.dentry;
95     struct inode *pinode = parent->d_inode;
96
97     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
98             && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {   
99         ctx = (struct procprotect_ctx *) ri->data;
100         ctx->inode = regs->cx;
101         ctx->flags = nd->flags;
102         ret = 0;
103     }
104
105     return ret;
106 }
107
108 /* The entry hook ensures that the return hook is only called for
109    accesses to /proc */
110
111 int printed=0;
112
113 static int lookup_fast_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
114 {
115     struct procprotect_ctx *ctx = (struct procprotect_ctx *) ri->data;
116     int ret = regs->ax;
117
118     if (ret==0) {
119         /* The kernel is going to honor the request. Here's where we step in */
120         struct inode *inode = *(ctx->inode);
121         if (!run_acl(inode->i_ino)) {
122             regs->ax = -EPERM;
123         }
124     }
125
126
127     return 0;
128 }
129
130 static int lookup_slow_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
131     int ret = -1;
132     struct procprotect_ctx *ctx;
133     struct nameidata *nd = (struct nameidata *) regs->di;
134     struct qstr *q = (struct qstr *) regs->si;
135     struct path *p = (struct path *) regs->dx;
136
137     struct dentry *parent = nd->path.dentry;
138     struct inode *pinode = parent->d_inode;
139
140     
141
142     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
143             && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {   
144         
145         ctx = (struct procprotect_ctx *) ri->data;
146         ctx->q = q;
147         ctx->flags = nd->flags;
148         ctx->path = p;
149         ret = 0;
150     }
151
152     return ret;
153 }
154
155 /* The entry hook ensures that the return hook is only called for
156    accesses to /proc */
157
158 static int print_once = 0;
159
160 static int lookup_slow_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
161 {
162     struct procprotect_ctx *ctx;
163     int ret;
164
165     if (!ri) {printk(KERN_CRIT "ri is 0x0");/* Race condition?*/ return 0;}
166     ctx = (struct procprotect_ctx *) ri->data;
167
168     if (!regs) {
169         if (!print_once++) {
170             printk(KERN_CRIT "Regs is 0x0");
171         }
172         return 0;
173     }
174
175     ret = regs->ax;
176
177     if (ret==0) {
178         struct path *p = ctx->path;
179         if (!p) {
180             if (!print_once++) {
181                 printk(KERN_CRIT "P is 0x0");
182             }
183             return 0;
184         }
185         if (!p->dentry) {
186             if (!print_once++) {
187                 printk(KERN_CRIT "P->dentry is 0x0");
188             }
189             return 0;
190         }
191
192         struct inode *inode = p->dentry->d_inode;
193         if (!inode) {
194             if (!print_once++) {
195                 printk(KERN_CRIT "inode is 0x0");
196             }
197             return 0;
198         }
199         if (!run_acl(inode->i_ino)) {
200             regs->ax = -EPERM;
201         }
202     }
203
204     return 0;
205 }
206
207 struct open_flags {
208   int open_flag;
209   umode_t mode;
210   int acc_mode;
211   int intent;
212 };
213
214 static struct file *do_last_probe(struct nameidata *nd, struct path *path, struct file *file,
215                          struct open_flags *op, const char *pathname) {
216     struct dentry *parent = nd->path.dentry;
217     struct inode *pinode = parent->d_inode;
218     struct qstr *q = &nd->last;
219
220     
221     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
222         /*if (!strncmp(q->name,"sysrq-trigger",13)) {
223             printk(KERN_CRIT "do_last sysrqtrigger: %d",op->open_flag);
224         }*/
225         op->open_flag &= ~O_CREAT;
226     }
227     jprobe_return();
228 }
229
230 static struct jprobe dolast_probe = {
231         .entry = (kprobe_opcode_t *) do_last_probe
232 };
233
234 static struct kretprobe fast_probe = {
235     .entry_handler = (kprobe_opcode_t *) lookup_fast_entry,
236     .handler = (kprobe_opcode_t *) lookup_fast_ret,
237     .maxactive = 20,
238     .data_size = sizeof(struct procprotect_ctx)
239 };
240
241 static struct kretprobe slow_probe = {
242     .entry_handler = (kprobe_opcode_t *) lookup_slow_entry,
243     .handler = (kprobe_opcode_t *) lookup_slow_ret,
244     .maxactive = 20,
245     .data_size = sizeof(struct procprotect_ctx)
246 };
247
248 static void add_entry(char *pathname) {
249     struct path path;
250     if (kern_path(pathname, 0, &path)) {
251         printk(KERN_CRIT "Path lookup failed for %s",pathname);
252     }   
253     else {
254         unsigned int ino = path.dentry->d_inode->i_ino;
255         struct acl_entry *entry;
256         entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
257         entry->ino = ino;
258
259         if (!entry) {
260             printk(KERN_CRIT "Could not allocate memory for %s",pathname);
261         }
262         else {
263             if (run_acl(ino)) {
264                 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
265                 printk(KERN_CRIT "Added inode %u",ino);
266             }
267             else {
268                 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
269             }
270         }
271     }
272 }
273
274
275 static void __exit procprotect_exit(void)
276 {
277     unregister_kretprobe(&fast_probe);
278     unregister_kretprobe(&slow_probe);
279         unregister_jprobe(&dolast_probe);
280     struct hlist_node *n;
281     struct acl_entry *entry;
282     int i;
283
284     for (i=0;i<HASH_SIZE;i++) {
285         hlist_for_each_entry_rcu(entry, 
286                 n, &procprotect_hash[i],
287                 hlist) {
288             kfree(entry);
289         }
290     }
291
292     remove_proc_entry("procprotect",NULL);
293     printk("Procprotect: Stopped procprotect.\n");
294 }
295
296
297
298 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {            
299     char pathname[PATH_MAX];
300
301     if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
302         return -EPERM;
303
304     if (copy_from_user(pathname, buffer, count)) {
305         return -EFAULT;
306     }
307     if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
308         pathname[count-1]='\0';
309     }
310     else
311         pathname[count]='\0';
312
313     add_entry(pathname);        
314     printk(KERN_CRIT "Length of buffer=%d",strlen(pathname));
315     return count;
316 }
317
318 static int __init procprotect_init(void)
319 {
320     int ret;
321     int i;
322
323     printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
324             VERSION_STR, aclpath);
325
326     for(i=0;i<HASH_SIZE;i++) {
327         INIT_HLIST_HEAD(&procprotect_hash[i]);
328     }
329
330     aclqpath.name = aclpath;
331     aclqpath.len = strnlen(aclpath, PATH_MAX);
332
333     dolast_probe.kp.addr = 
334         (kprobe_opcode_t *) kallsyms_lookup_name("do_last");
335
336     if (!dolast_probe.kp.addr) {
337         printk("Couldn't find %s to plant kretprobe\n", "do_last");
338         return -1;
339     }
340
341     if ((ret = register_jprobe(&dolast_probe)) <0) {
342                   printk("register_jprobe failed, returned %u\n", ret);
343                   return -1;
344     }
345     fast_probe.kp.addr = 
346         (kprobe_opcode_t *) kallsyms_lookup_name("lookup_fast");
347     if (!fast_probe.kp.addr) {
348         printk("Couldn't find %s to plant kretprobe\n", "lookup_fast");
349         return -1;
350     }
351
352     slow_probe.kp.addr = 
353         (kprobe_opcode_t *) kallsyms_lookup_name("lookup_slow");
354     if (!slow_probe.kp.addr) {
355         printk("Couldn't find %s to plant kretprobe\n", "lookup_slow");
356         return -1;
357     }
358
359     
360
361     if ((ret = register_kretprobe(&fast_probe)) <0) {
362         printk("register_kretprobe failed, returned %d\n", ret);
363         return -1;
364     }
365
366     printk("Planted kretprobe at %p, handler addr %p\n",
367             fast_probe.kp.addr, fast_probe.handler);
368
369     if ((ret = register_kretprobe(&slow_probe)) <0) {
370         printk("register_kretprobe failed, returned %d\n", ret);
371         return -1;
372     }
373     printk("Planted kretprobe at %p, handler addr %p\n",
374             slow_probe.kp.addr, slow_probe.handler);
375
376     proc_entry = create_proc_entry("procprotect", 0644, NULL);
377     proc_entry->write_proc = procfile_write;
378
379     return ret;
380 }
381
382
383
384 module_init(procprotect_init);
385 module_exit(procprotect_exit);