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