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