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>
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>
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 */
37 #define VERSION_STR "0.0.1"
40 #error "This code does not support your architecture"
43 static char *aclpath = "procprotect";
45 static struct qstr aclqpath;
47 module_param(aclpath, charp, 0);
48 MODULE_PARM_DESC(aclpath, "Root of directory that stores acl tags for /proc files.");
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);
55 struct procprotect_ctx {
64 struct hlist_node hlist;
67 #define HASH_SIZE (1<<10)
69 struct hlist_head procprotect_hash[HASH_SIZE];
71 struct proc_dir_entry *proc_entry;
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)],
78 if (entry->ino==ino) {
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
90 static int lookup_fast_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
92 struct procprotect_ctx *ctx;
93 struct nameidata *nd = (struct nameidata *) regs->di;
94 struct dentry *parent;
98 parent = nd->path.dentry;
100 if (!parent) return ret;
101 pinode = parent->d_inode;
103 if (!pinode || !pinode->i_sb || !current || !current->nsproxy) return ret;
105 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
106 && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
107 ctx = (struct procprotect_ctx *) ri->data;
108 ctx->inode = (struct inode **)regs->dx;
109 ctx->flags = nd->flags;
116 /* The entry hook ensures that the return hook is only called for
121 static int lookup_fast_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
123 struct procprotect_ctx *ctx = (struct procprotect_ctx *) ri->data;
127 /* The kernel is going to honor the request. Here's where we step in */
128 struct inode *inode = *(ctx->inode);
129 if (!run_acl(inode->i_ino)) {
138 static int lookup_slow_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
140 struct procprotect_ctx *ctx;
141 struct nameidata *nd = (struct nameidata *) regs->di;
142 struct path *p = (struct path *) regs->si;
143 struct dentry *parent;
144 struct inode *pinode;
147 parent = nd->path.dentry;
148 if (!parent) return ret;
149 pinode= parent->d_inode;
151 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
152 && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
154 ctx = (struct procprotect_ctx *) ri->data;
156 ctx->flags = nd->flags;
164 /* The entry hook ensures that the return hook is only called for
167 /*static int print_once = 0;*/
169 static int lookup_slow_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
171 struct procprotect_ctx *ctx;
175 if (!ri || !ri->data) {return 0;}
176 ctx = (struct procprotect_ctx *) ri->data;
181 struct path *p = ctx->path;
182 if (!p || !p->dentry || !p->dentry->d_inode /* This last check was responsible for the f18 bug*/) {
185 inode = p->dentry->d_inode;
186 if (!run_acl(inode->i_ino)) {
201 static struct file *do_last_probe(struct nameidata *nd, struct path *path, struct file *file,
202 struct open_flags *op, const char *pathname) {
203 struct dentry *parent = nd->path.dentry;
204 struct inode *pinode = parent->d_inode;
205 /*struct qstr *q = &nd->last;*/
208 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
209 /*if (!strncmp(q->name,"sysrq-trigger",13)) {
210 printk(KERN_CRIT "do_last sysrqtrigger: %d",op->open_flag);
212 op->open_flag &= ~O_CREAT;
218 static struct jprobe dolast_probe = {
219 .entry = do_last_probe
222 static struct kretprobe fast_probe = {
223 .entry_handler = lookup_fast_entry,
224 .handler = lookup_fast_ret,
226 .data_size = sizeof(struct procprotect_ctx)
229 static struct kretprobe slow_probe = {
230 .entry_handler = lookup_slow_entry,
231 .handler = lookup_slow_ret,
233 .data_size = sizeof(struct procprotect_ctx)
238 static int init_probes(void) {
240 dolast_probe.kp.addr =
241 (kprobe_opcode_t *) kallsyms_lookup_name("do_last");
243 if (!dolast_probe.kp.addr) {
244 printk("Couldn't find %s to plant kretprobe\n", "do_last");
248 if ((ret = register_jprobe(&dolast_probe)) <0) {
249 printk("register_jprobe failed, returned %u\n", ret);
253 (kprobe_opcode_t *) kallsyms_lookup_name("lookup_fast");
255 if (!fast_probe.kp.addr) {
256 printk("Couldn't find %s to plant kretprobe\n", "lookup_fast");
261 (kprobe_opcode_t *) kallsyms_lookup_name("lookup_slow");
263 if (!slow_probe.kp.addr) {
264 printk("Couldn't find %s to plant kretprobe\n", "lookup_slow");
268 if ((ret = register_kretprobe(&fast_probe)) <0) {
269 printk("register_kretprobe failed, returned %d\n", ret);
273 printk("Planted kretprobe at %p, handler addr %p\n",
274 fast_probe.kp.addr, fast_probe.handler);
276 if ((ret = register_kretprobe(&slow_probe)) <0) {
277 printk("register_kretprobe failed, returned %d\n", ret);
280 printk("Planted kretprobe at %p, handler addr %p\n",
281 slow_probe.kp.addr, slow_probe.handler);
285 static void add_entry(char *pathname) {
287 if (kern_path(pathname, 0, &path)) {
288 printk(KERN_CRIT "Path lookup failed for %s",pathname);
291 unsigned int ino = path.dentry->d_inode->i_ino;
292 struct acl_entry *entry;
293 entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
297 printk(KERN_CRIT "Could not allocate memory for %s",pathname);
301 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
302 printk(KERN_CRIT "Added inode %u",ino);
305 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
313 static void __exit procprotect_exit(void)
315 struct acl_entry *entry;
318 unregister_kretprobe(&fast_probe);
319 unregister_kretprobe(&slow_probe);
320 unregister_jprobe(&dolast_probe);
322 for (i=0;i<HASH_SIZE;i++) {
323 hlist_for_each_entry_rcu(entry,
324 &procprotect_hash[i],
330 remove_proc_entry("procprotect",NULL);
331 printk("Procprotect: Stopped procprotect.\n");
336 ssize_t procfile_write(struct file *file, const char *buffer, size_t count, loff_t *data) {
338 pathname = (char *) kmalloc(count, GFP_KERNEL);
341 printk(KERN_CRIT "Could not allocate memory for pathname buffer");
345 if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
348 if (copy_from_user(pathname, buffer, count)) {
351 if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
352 pathname[count-1]='\0';
355 pathname[count]='\0';
361 if (init_probes()==-1)
362 printk(KERN_CRIT "Could not install procprotect probes. Reload module to retry.");
364 printk(KERN_CRIT "Length of buffer=%d",(int)strlen(pathname));
369 static const struct file_operations procprotect_fops = {
370 .owner = THIS_MODULE,
371 .write = procfile_write
375 static int __init procprotect_init(void)
380 printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
381 VERSION_STR, aclpath);
383 for(i=0;i<HASH_SIZE;i++) {
384 INIT_HLIST_HEAD(&procprotect_hash[i]);
387 aclqpath.name = aclpath;
388 aclqpath.len = strnlen(aclpath, PATH_MAX);
390 proc_entry = proc_create("procprotect", 0644, NULL, &procprotect_fops);
397 module_init(procprotect_init);
398 module_exit(procprotect_exit);