summaryrefslogtreecommitdiffstats
path: root/trunk/kmod/readlog.c
blob: 8a1c5ade88f432dc9cc1ce6daab5a9fb83f33b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <linux/init.h>
#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/syscalls.h>
#include <linux/time.h>

#include <asm/unistd.h>

#include <asm/current.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Olivier Blin <blino@mandriva.com>");

struct opened_file {
    struct list_head s_list;
    char *name;
};

unsigned long **sys_call_table;
static struct dentry *dbg_dir;
static struct dentry *dbg_file;
struct opened_file bp;
DECLARE_MUTEX(files_mutex);

ssize_t (*read)(int f, const void *buf, size_t n);
long (*open)(const char __user *filename, int flags, int mode);

static void *profile_seq_start(struct seq_file *file, loff_t *pos)
{
    struct list_head *p;
    loff_t l = *pos;
    down(&files_mutex);
    list_for_each(p, &bp.s_list)
        if (!l--)
            return list_entry(p, struct opened_file, s_list);
    return NULL;
}

static void *profile_seq_next(struct seq_file *file, void *v, loff_t *pos)
{
    struct list_head *p = ((struct opened_file *)v)->s_list.next;
    (*pos)++;
    return p == &bp.s_list ? NULL : list_entry(p, struct opened_file, s_list);
}

static void profile_seq_stop(struct seq_file *file, void *v)
{
    up(&files_mutex);
}

static int profile_seq_show(struct seq_file *file, void *v)
{
    seq_printf(file, ((struct opened_file *)v)->name);
    seq_putc(file, '\n');
    return 0;
}

static struct seq_operations profile_seq_ops = {
        .start = profile_seq_start,
        .next  = profile_seq_next,
        .stop  = profile_seq_stop,
        .show  = profile_seq_show,
};

static int profile_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &profile_seq_ops);
}

static struct file_operations profile_fops = {
        .owner   = THIS_MODULE,
        .open    = profile_open,
        .read    = seq_read,
        .llseek  = seq_lseek,
        .release = seq_release
};

/* Borrowed from
 * http://downloads.securityfocus.com/downloads/scprint.tar.gz
 * http://www.gnome.org/~lcolitti/gnome-startup/linux-iolog/readlog.c
 * http://kerneltrap.org/node/5793
 */
unsigned long **find_sys_call_table(void) {
    unsigned long *p;

    for (p = (unsigned long *)((init_mm.end_code + 4) & 0xfffffffc);
	 p < (unsigned long *)init_mm.end_data; p++) {
        if (p[__NR_close] == (unsigned long) sys_close){
            return (unsigned long **) p;
        }
    }

    return NULL;
}

void addfilename(const char *filename) {
    struct opened_file *f;

    f = kmalloc(sizeof(struct opened_file), GFP_KERNEL);
    if (f == NULL)
	return;

    f->name = kmalloc(strlen(filename) + 1, GFP_KERNEL);
    if (f->name == NULL)
	return;
    strcpy(f->name, filename);

    INIT_LIST_HEAD(&f->s_list);

    printk(KERN_INFO "locking while adding: %s\n", filename);

    down(&files_mutex);
    list_add_tail(&f->s_list, &bp.s_list);
    up(&files_mutex);

    printk(KERN_INFO "unlocking after adding: %s\n", filename);
}

long loggingopen(const char __user *filename, int flags, int mode) {
    struct file *f = NULL;
    long fd;

    fd = open(filename, flags, mode);

    printk(KERN_INFO "opening fd %ld for %s\n", fd, filename);

    if(fd > 0 && current) {
        f = current->files->fd_array[fd];
        if(f && f->f_dentry && f->f_vfsmnt) {
            char tmpname[PATH_MAX];
            char *name = d_path(f->f_dentry, f->f_vfsmnt, tmpname, sizeof(tmpname) - 1);
            if (name)
                addfilename(name);
        }
    }

    return fd;
}


ssize_t loggingread(int fd, const void *buf, size_t n) {
    struct file *f = NULL;
	char *name = NULL;
    loff_t offset = 0;
	struct timeval tv;
    char tmpname[256];

	do_gettimeofday(&tv);

    if(current)
        f = current->files->fd_array[fd];

    printk(KERN_DEBUG "READ: f: %p\n", f);


    if(f)
        offset = f->f_pos;

    if (f)
        printk(KERN_DEBUG "READ: d_entry: %p, vfsmnt: %p\n", f->f_dentry, f->f_vfsmnt);

    if(f && f->f_dentry && f->f_vfsmnt) {
        printk(KERN_DEBUG "READ: d_path\n");
        name = d_path(f->f_dentry, f->f_vfsmnt, tmpname, sizeof(tmpname) - 1);
        if (name)
            addfilename(name);
    }

    printk(KERN_DEBUG "READ: %lu.%lu (%s/%d) %Lu %s\n",
           tv.tv_sec, tv.tv_usec,
           current ? current->comm : "?",
           current ? current->pid : 0,
           offset,
           name ? name : "<no dentry>"
        );

	return read(fd, buf, n);
}

static int __init readlog_init(void) {
    sys_call_table = find_sys_call_table();
    /* compare with grep sys_call_table /boot/System.map */
    printk(KERN_INFO "Found sys_call_table at %p\n", sys_call_table);

    open = (void *) (sys_call_table[__NR_open]);
    sys_call_table[__NR_open] = (void *) loggingopen;

    read = (void *) (sys_call_table[__NR_read]);
    /* sys_call_table[__NR_read] = (void *) loggingread; */

    dbg_dir = debugfs_create_dir("dmc", NULL);
    if (IS_ERR(dbg_dir)) {
	printk(KERN_NOTICE ": debugfs is not available\n");
	return -ENODEV;
    }
    if (dbg_dir == NULL) {
	printk(KERN_NOTICE ": unable to create usbmon directory\n");
	return -ENODEV;
    }

    dbg_file = debugfs_create_file("bp", 0600, dbg_dir, NULL, &profile_fops);
    if (dbg_file == NULL) {
	debugfs_remove(dbg_dir);
	return -ENODEV;
    }

    INIT_LIST_HEAD(&bp.s_list);

    return 0;
}

static void __exit readlog_exit(void) {
    debugfs_remove(dbg_file);
    debugfs_remove(dbg_dir);

    sys_call_table[__NR_open] = (void *) open;
    sys_call_table[__NR_read] = (void *) read;

    printk(KERN_INFO "sys_call_table restored\n");

}

module_init(readlog_init);
module_exit(readlog_exit);