Remove possibility of being traced in a trace handler
[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 = 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->dx;
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 path *p = (struct path *) regs->si;
135
136     struct dentry *parent = nd->path.dentry;
137     struct inode *pinode = parent->d_inode;
138
139     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
140             && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {   
141         
142         ctx = (struct procprotect_ctx *) ri->data;
143         ctx->q = &nd->last;
144         ctx->flags = nd->flags;
145         ctx->path = p;
146         ret = 0;
147     }
148
149     return ret;
150 }
151
152 /* The entry hook ensures that the return hook is only called for
153    accesses to /proc */
154
155 static int print_once = 0;
156
157 static int lookup_slow_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
158 {
159     struct procprotect_ctx *ctx;
160     int ret;
161
162     if (!ri || !ri->data) {return 0;}
163     ctx = (struct procprotect_ctx *) ri->data;
164
165     ret = regs->ax;
166
167     if (ret==0) {
168         struct path *p = ctx->path;
169         if (!p || !p->dentry || !p->dentry->d_inode /* This last check was responsible for the f18 bug*/) {
170             return 0;
171         }
172         struct inode *inode = p->dentry->d_inode;
173         if (!run_acl(inode->i_ino)) {
174             regs->ax = -EPERM;
175         }
176     }
177
178     return 0;
179 }
180
181 struct open_flags {
182   int open_flag;
183   umode_t mode;
184   int acc_mode;
185   int intent;
186 };
187
188 static struct file *do_last_probe(struct nameidata *nd, struct path *path, struct file *file,
189                          struct open_flags *op, const char *pathname) {
190     struct dentry *parent = nd->path.dentry;
191     struct inode *pinode = parent->d_inode;
192     struct qstr *q = &nd->last;
193
194     
195     if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
196         /*if (!strncmp(q->name,"sysrq-trigger",13)) {
197             printk(KERN_CRIT "do_last sysrqtrigger: %d",op->open_flag);
198         }*/
199         op->open_flag &= ~O_CREAT;
200     }
201     jprobe_return();
202 }
203
204 static struct jprobe dolast_probe = {
205         .entry = (kprobe_opcode_t *) do_last_probe
206 };
207
208 static struct kretprobe fast_probe = {
209     .entry_handler = (kprobe_opcode_t *) lookup_fast_entry,
210     .handler = (kprobe_opcode_t *) lookup_fast_ret,
211     .maxactive = 20,
212     .data_size = sizeof(struct procprotect_ctx)
213 };
214
215 static struct kretprobe slow_probe = {
216     .entry_handler = (kprobe_opcode_t *) lookup_slow_entry,
217     .handler = (kprobe_opcode_t *) lookup_slow_ret,
218     .maxactive = 20,
219     .data_size = sizeof(struct procprotect_ctx)
220 };
221
222 static void add_entry(char *pathname) {
223     struct path path;
224     if (kern_path(pathname, 0, &path)) {
225         printk(KERN_CRIT "Path lookup failed for %s",pathname);
226     }   
227     else {
228         unsigned int ino = path.dentry->d_inode->i_ino;
229         struct acl_entry *entry;
230         entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
231         entry->ino = ino;
232
233         if (!entry) {
234             printk(KERN_CRIT "Could not allocate memory for %s",pathname);
235         }
236         else {
237             if (run_acl(ino)) {
238                 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
239                 printk(KERN_CRIT "Added inode %u",ino);
240             }
241             else {
242                 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
243             }
244         }
245     }
246 }
247
248
249 static void __exit procprotect_exit(void)
250 {
251     unregister_kretprobe(&fast_probe);
252     unregister_kretprobe(&slow_probe);
253     unregister_jprobe(&dolast_probe);    
254     struct acl_entry *entry;
255     int i;
256
257     for (i=0;i<HASH_SIZE;i++) {
258         hlist_for_each_entry_rcu(entry, 
259                                  &procprotect_hash[i],
260                                  hlist) {
261             kfree(entry);
262         }
263     }
264
265     remove_proc_entry("procprotect",NULL);
266     printk("Procprotect: Stopped procprotect.\n");
267 }
268
269
270
271 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {            
272     char pathname[PATH_MAX];
273
274     if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
275         return -EPERM;
276
277     if (copy_from_user(pathname, buffer, count)) {
278         return -EFAULT;
279     }
280     if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
281         pathname[count-1]='\0';
282     }
283     else
284         pathname[count]='\0';
285
286     add_entry(pathname);        
287     printk(KERN_CRIT "Length of buffer=%d",strlen(pathname));
288     return count;
289 }
290
291 static const struct file_operations procprotect_fops = {
292     .owner = THIS_MODULE,
293     .write = procfile_write
294 };
295                           
296 static int __init procprotect_init(void)
297 {
298     int ret;
299     int i;
300
301     printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
302             VERSION_STR, aclpath);
303
304     for(i=0;i<HASH_SIZE;i++) {
305         INIT_HLIST_HEAD(&procprotect_hash[i]);
306     }
307
308     add_entry("/proc/sysrq-trigger");
309
310     aclqpath.name = aclpath;
311     aclqpath.len = strnlen(aclpath, PATH_MAX);
312
313     dolast_probe.kp.addr = 
314         (kprobe_opcode_t *) kallsyms_lookup_name("do_last");
315
316     if (!dolast_probe.kp.addr) {
317         printk("Couldn't find %s to plant kretprobe\n", "do_last");
318         return -1;
319     }
320
321     if ((ret = register_jprobe(&dolast_probe)) <0) {
322                   printk("register_jprobe failed, returned %u\n", ret);
323                   return -1;
324     }
325     fast_probe.kp.addr = 
326         (kprobe_opcode_t *) kallsyms_lookup_name("lookup_fast");
327
328     if (!fast_probe.kp.addr) {
329         printk("Couldn't find %s to plant kretprobe\n", "lookup_fast");
330         return -1;
331     }
332
333     slow_probe.kp.addr = 
334         (kprobe_opcode_t *) kallsyms_lookup_name("lookup_slow");
335
336     if (!slow_probe.kp.addr) {
337         printk("Couldn't find %s to plant kretprobe\n", "lookup_slow");
338         return -1;
339     }
340
341     
342
343     if ((ret = register_kretprobe(&fast_probe)) <0) {
344         printk("register_kretprobe failed, returned %d\n", ret);
345         return -1;
346     }
347
348     printk("Planted kretprobe at %p, handler addr %p\n",
349             fast_probe.kp.addr, fast_probe.handler);
350
351     if ((ret = register_kretprobe(&slow_probe)) <0) {
352         printk("register_kretprobe failed, returned %d\n", ret);
353         return -1;
354     }
355     printk("Planted kretprobe at %p, handler addr %p\n",
356             slow_probe.kp.addr, slow_probe.handler);
357
358     proc_entry = proc_create("procprotect", 0644, NULL, &procprotect_fops);
359
360     return ret;
361 }
362
363
364
365 module_init(procprotect_init);
366 module_exit(procprotect_exit);