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 #define VERSION_STR "0.0.1"
34 #error "This code does not support your architecture"
37 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
38 static char *aclpath = "procprotect";
40 static char *aclpath __devinitdata = "procprotect";
43 static struct qstr aclqpath;
45 module_param(aclpath, charp, 0);
46 MODULE_PARM_DESC(aclpath, "Root of directory that stores acl tags for /proc files.");
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);
53 struct procprotect_ctx {
62 struct hlist_node hlist;
65 #define HASH_SIZE (1<<16)
67 struct hlist_head procprotect_hash[HASH_SIZE];
69 struct proc_dir_entry *proc_entry;
71 static int run_acl(unsigned long ino) {
72 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
75 struct acl_entry *entry;
76 hlist_for_each_entry_rcu(entry,
77 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
80 &procprotect_hash[ino & (HASH_SIZE-1)],
82 if (entry->ino==ino) {
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
94 static int lookup_fast_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
96 struct procprotect_ctx *ctx;
97 struct nameidata *nd = (struct nameidata *) regs->di;
98 struct qstr *q = (struct qstr *) regs->si;
99 struct dentry *parent = nd->path.dentry;
100 struct inode *pinode = parent->d_inode;
102 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
103 && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
104 ctx = (struct procprotect_ctx *) ri->data;
105 ctx->inode = regs->cx;
106 ctx->flags = nd->flags;
113 /* The entry hook ensures that the return hook is only called for
118 static int lookup_fast_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
120 struct procprotect_ctx *ctx = (struct procprotect_ctx *) ri->data;
124 /* The kernel is going to honor the request. Here's where we step in */
125 struct inode *inode = *(ctx->inode);
126 if (!run_acl(inode->i_ino)) {
135 static int lookup_slow_entry(struct kretprobe_instance *ri, struct pt_regs *regs) {
137 struct procprotect_ctx *ctx;
138 struct nameidata *nd = (struct nameidata *) regs->di;
139 struct qstr *q = (struct qstr *) regs->si;
140 struct path *p = (struct path *) regs->dx;
142 struct dentry *parent = nd->path.dentry;
143 struct inode *pinode = parent->d_inode;
147 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC
148 && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
150 ctx = (struct procprotect_ctx *) ri->data;
152 ctx->flags = nd->flags;
160 /* The entry hook ensures that the return hook is only called for
163 static int print_once = 0;
165 static int lookup_slow_ret(struct kretprobe_instance *ri, struct pt_regs *regs)
167 struct procprotect_ctx *ctx;
170 if (!ri || !ri->data) {return 0;}
171 ctx = (struct procprotect_ctx *) ri->data;
176 struct path *p = ctx->path;
177 if (!p || !p->dentry || !p->dentry->d_inode /* This last check was responsible for the f18 bug*/) {
180 struct inode *inode = p->dentry->d_inode;
181 if (!run_acl(inode->i_ino)) {
196 static struct file *do_last_probe(struct nameidata *nd, struct path *path, struct file *file,
197 struct open_flags *op, const char *pathname) {
198 struct dentry *parent = nd->path.dentry;
199 struct inode *pinode = parent->d_inode;
200 struct qstr *q = &nd->last;
203 if (pinode->i_sb->s_magic == PROC_SUPER_MAGIC && current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns) {
204 /*if (!strncmp(q->name,"sysrq-trigger",13)) {
205 printk(KERN_CRIT "do_last sysrqtrigger: %d",op->open_flag);
207 op->open_flag &= ~O_CREAT;
212 static struct jprobe dolast_probe = {
213 .entry = (kprobe_opcode_t *) do_last_probe
216 static struct kretprobe fast_probe = {
217 .entry_handler = (kprobe_opcode_t *) lookup_fast_entry,
218 .handler = (kprobe_opcode_t *) lookup_fast_ret,
220 .data_size = sizeof(struct procprotect_ctx)
223 static struct kretprobe slow_probe = {
224 .entry_handler = (kprobe_opcode_t *) lookup_slow_entry,
225 .handler = (kprobe_opcode_t *) lookup_slow_ret,
227 .data_size = sizeof(struct procprotect_ctx)
230 static void add_entry(char *pathname) {
232 if (kern_path(pathname, 0, &path)) {
233 printk(KERN_CRIT "Path lookup failed for %s",pathname);
236 unsigned int ino = path.dentry->d_inode->i_ino;
237 struct acl_entry *entry;
238 entry = kmalloc(GFP_KERNEL, sizeof(struct acl_entry));
242 printk(KERN_CRIT "Could not allocate memory for %s",pathname);
246 hlist_add_head_rcu(&entry->hlist,&procprotect_hash[ino&(HASH_SIZE-1)]);
247 printk(KERN_CRIT "Added inode %u",ino);
250 printk(KERN_CRIT "Did not add inode %u, already in list", ino);
257 static void __exit procprotect_exit(void)
259 unregister_kretprobe(&fast_probe);
260 unregister_kretprobe(&slow_probe);
261 unregister_jprobe(&dolast_probe);
262 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
263 struct hlist_node *n;
265 struct acl_entry *entry;
268 for (i=0;i<HASH_SIZE;i++) {
269 hlist_for_each_entry_rcu(entry,
270 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
273 &procprotect_hash[i],
279 remove_proc_entry("procprotect",NULL);
280 printk("Procprotect: Stopped procprotect.\n");
285 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {
286 char pathname[PATH_MAX];
288 if (current->nsproxy->mnt_ns!=init_task.nsproxy->mnt_ns)
291 if (copy_from_user(pathname, buffer, count)) {
294 if (count && (pathname[count-1]==10 || pathname[count-1]==13)) {
295 pathname[count-1]='\0';
298 pathname[count]='\0';
301 printk(KERN_CRIT "Length of buffer=%d",strlen(pathname));
305 static int __init procprotect_init(void)
310 printk("Procprotect: starting procprotect version %s with ACLs at path %s.\n",
311 VERSION_STR, aclpath);
313 for(i=0;i<HASH_SIZE;i++) {
314 INIT_HLIST_HEAD(&procprotect_hash[i]);
317 aclqpath.name = aclpath;
318 aclqpath.len = strnlen(aclpath, PATH_MAX);
320 dolast_probe.kp.addr =
321 (kprobe_opcode_t *) kallsyms_lookup_name("do_last");
323 if (!dolast_probe.kp.addr) {
324 printk("Couldn't find %s to plant kretprobe\n", "do_last");
328 if ((ret = register_jprobe(&dolast_probe)) <0) {
329 printk("register_jprobe failed, returned %u\n", ret);
333 (kprobe_opcode_t *) kallsyms_lookup_name("lookup_fast");
334 if (!fast_probe.kp.addr) {
335 printk("Couldn't find %s to plant kretprobe\n", "lookup_fast");
340 (kprobe_opcode_t *) kallsyms_lookup_name("lookup_slow");
341 if (!slow_probe.kp.addr) {
342 printk("Couldn't find %s to plant kretprobe\n", "lookup_slow");
348 if ((ret = register_kretprobe(&fast_probe)) <0) {
349 printk("register_kretprobe failed, returned %d\n", ret);
353 printk("Planted kretprobe at %p, handler addr %p\n",
354 fast_probe.kp.addr, fast_probe.handler);
356 if ((ret = register_kretprobe(&slow_probe)) <0) {
357 printk("register_kretprobe failed, returned %d\n", ret);
360 printk("Planted kretprobe at %p, handler addr %p\n",
361 slow_probe.kp.addr, slow_probe.handler);
363 proc_entry = create_proc_entry("procprotect", 0644, NULL);
364 proc_entry->write_proc = procfile_write;
371 module_init(procprotect_init);
372 module_exit(procprotect_exit);