Added specfile
[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 };
50
51 struct acl_entry {
52         unsigned int ino;
53         struct hlist_node hlist;
54 };
55
56 #define HASH_SIZE (1<<16)
57
58 struct hlist_head procprotect_hash[HASH_SIZE];
59
60 struct proc_dir_entry *proc_entry;
61
62 /*
63   Entry point of intercepted call. We need to do two things here:
64   - Decide if we need the heavier return hook to be called
65   - Save the first argument, which is in a register, for consideration in the return hook
66 */
67 static int do_lookup_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
68         int ret = -1;
69         struct procprotect_ctx *ctx;
70         struct nameidata *nd = (struct nameidata *) regs->di;
71         struct qstr *q = (struct qstr *) regs->si;
72         struct dentry *parent = nd->path.dentry;
73         struct inode *pinode = parent->d_inode;
74         
75         if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC) {        
76                 ctx = (struct procprotect_ctx *) ri->data;
77                 ctx->inode = (struct inode **) regs->cx;
78                 ret = 0;
79         }
80
81         return ret;
82 }
83
84 static int run_acl(unsigned long ino) {
85         struct hlist_node *n;
86         struct acl_entry *entry;
87         hlist_for_each_entry_rcu(entry, 
88                 n, &procprotect_hash[ino & (HASH_SIZE-1)],
89                 hlist) {
90                 if (entry->ino==ino) {
91                         return 0;
92                 }
93         }
94         return 1;
95 }
96
97 /* The entry hook ensures that the return hook is only called for
98 accesses to /proc */
99
100 static int do_lookup_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
101 {
102         struct procprotect_ctx *ctx = (struct procprotect_ctx *) ri->data;
103         int ret = regs->ax;
104         
105         if (ret==0) {
106                 /* The kernel is going to honor the request. Here's where we step in */
107                 struct inode *inode = *(ctx->inode);
108                 //printk(KERN_CRIT "Checking inode %x number %u",inode,inode->i_ino);
109                 if (!run_acl(inode->i_ino)) {
110                         if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
111                                 regs->ax = -EPERM;
112                 }
113         }
114         
115         return 0;
116 }
117
118 static struct kretprobe proc_probe = {
119         .entry_handler = (kprobe_opcode_t *) do_lookup_entry,
120         .handler = (kprobe_opcode_t *) do_lookup_ret,
121         .maxactive = 20,
122         .data_size = sizeof(struct procprotect_ctx)
123 };
124
125 static void add_entry(char *pathname) {
126         struct path path;
127         if (kern_path(pathname, 0, &path)) {
128                 printk(KERN_CRIT "Path lookup failed for %s",pathname);
129         }       
130         else {
131                 unsigned int ino = path.dentry->d_inode->i_ino;
132                 struct acl_entry *entry;
133                 entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
134                 entry->ino = ino;
135                 
136                 if (!entry) {
137                         printk(KERN_CRIT "Could not allocate memory for %s",pathname);
138                 }
139                 else {
140                         if (run_acl(ino)) {
141                                 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
142                                 printk(KERN_CRIT "Added inode %u",ino);
143                         }
144                         else {
145                                 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
146                         }
147                 }
148         }
149 }
150
151
152 static void __exit procprotect_exit(void)
153 {
154         unregister_kretprobe(&proc_probe);
155         struct hlist_node *n;
156         struct acl_entry *entry;
157         int i;
158
159         for (i=0;i<HASH_SIZE;i++) {
160                 hlist_for_each_entry_rcu(entry, 
161                         n, &procprotect_hash[i],
162                         hlist) {
163                                 kfree(entry);
164                 }
165         }
166         
167         remove_proc_entry("procprotect",NULL);
168         printk("Procprotect: Stopped procprotect.\n");
169 }
170
171
172
173 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {            
174         char pathname[PATH_MAX];
175
176         if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
177                 return -EPERM;
178
179         if (copy_from_user(pathname, buffer, count)) {
180                 return -EFAULT;
181         }
182         if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
183                 pathname[count-1]='\0';
184         }
185         else
186                 pathname[count]='\0';
187         add_entry(pathname);    
188         printk(KERN_CRIT "Length of buffer=%d",strlen(pathname));
189         return count;
190 }
191
192 static int __init procprotect_init(void)
193 {
194         printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
195                VERSION_STR, aclpath);
196         int ret;
197         int i;
198
199         for(i=0;i<HASH_SIZE;i++) {
200                 INIT_HLIST_HEAD(&procprotect_hash[i]);
201         }
202
203           aclqpath.name = aclpath;
204           aclqpath.len = strnlen(aclpath, PATH_MAX);
205
206           proc_probe.kp.addr = 
207                   (kprobe_opcode_t *) kallsyms_lookup_name("do_lookup");
208           if (!proc_probe.kp.addr) {
209                   printk("Couldn't find %s to plant kretprobe\n", "do_execve");
210                   return -1;
211           }
212   
213           if ((ret = register_kretprobe(&proc_probe)) <0) {
214                   printk("register_kretprobe failed, returned %d\n", ret);
215                   return -1;
216           }
217           printk("Planted kretprobe at %p, handler addr %p\n",
218                  proc_probe.kp.addr, proc_probe.handler);
219
220         proc_entry = create_proc_entry("procprotect", 0644, NULL);
221         proc_entry->write_proc = procfile_write;
222         return ret;
223 }
224
225
226
227 module_init(procprotect_init);
228 module_exit(procprotect_exit);