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