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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/**
* Capture events from various VFS operations; export them to
* /dev/ecryptfs via a ring buffer.
*
* Originally written to collect data for a term paper in Dr. Adam
* Klivans' course on computational machine learning theory (Fall
* '07).
*
* Author: Mike Halcrow <mike@halcrow.us>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/bit_spinlock.h>
#include <linux/sched.h>
#include "ecryptfs_kernel.h"
struct jprobe_mapping_elem {
struct jprobe *jp;
char *symbol;
void *fp;
};
#define MAX_CLO_MSGS 32768
spinlock_t clo_msg_list_spinlock;
size_t num_clo_msgs = 0;
struct clo_msg;
struct clo_msg {
struct clo_msg *next;
char *msg;
size_t size;
size_t current_read_offset;
};
struct clo_msg *tail_clo_msg = NULL;
struct clo_msg *head_clo_msg = NULL;
/**
* Copies msg; callee must deallocate msg, and can do so immediately
* upon return.
*/
static int queue_msg(char *msg)
{
ssize_t rc = 0;
spin_lock(&clo_msg_list_spinlock);
if (num_clo_msgs > MAX_CLO_MSGS) {
rc = -EBUSY;
goto out;
}
if (!tail_clo_msg) {
tail_clo_msg = head_clo_msg = kmalloc(sizeof(struct clo_msg),
GFP_KERNEL);
if (!head_clo_msg) {
rc = -ENOMEM;
goto out;
}
} else {
tail_clo_msg->next = kmalloc(sizeof(struct clo_msg),
GFP_KERNEL);
if (!tail_clo_msg->next) {
rc = -ENOMEM;
goto out;
}
tail_clo_msg = tail_clo_msg->next;
}
memset(tail_clo_msg, 0, sizeof(*tail_clo_msg));
tail_clo_msg->size = strlen(msg) + 1;
tail_clo_msg->msg = kmalloc(tail_clo_msg->size, GFP_KERNEL);
memcpy(tail_clo_msg->msg, msg, tail_clo_msg->size);
num_clo_msgs++;
out:
spin_unlock(&clo_msg_list_spinlock);
return rc;
}
atomic_t readno;
int ignore_header_reads = 1;
int jp_ecryptfs_read_lower(char *data, loff_t offset, size_t size,
struct inode *ecryptfs_inode)
{
char tmp;
char *msg;
size_t sz;
struct timespec ts = CURRENT_TIME;
size_t readno_tmp = atomic_read(&readno);
struct task_struct *task = current;
char *task_command;
uid_t task_uid;
char const *filepath;
struct file *lower_file;
struct dentry *lower_dentry;
struct inode *lower_inode;
char *fmt =
"'read','%Zd','%s','%d','%s','%lld','%Zd',"
"'%lld','%ld'\n";
struct ecryptfs_inode_info *inode_info =
ecryptfs_inode_to_private(ecryptfs_inode);
u64 lower_file_size;
if (offset == 0 && ignore_header_reads == 1)
goto out;
atomic_inc(&readno);
task_command = kmalloc(sizeof(task->comm), GFP_KERNEL);
if (!task_command) {
printk(KERN_WARNING "%s: Out of memory\n", __FUNCTION__);
goto out;
}
task_lock(task);
strncpy(task_command, task->comm, sizeof(task->comm));
task_unlock(task);
task_uid = task->euid;
lower_file = inode_info->lower_file;
lower_dentry = lower_file->f_path.dentry;
lower_inode = lower_dentry->d_inode;
lower_file_size = i_size_read(lower_inode);
filepath = lower_dentry->d_name.name;
sz = (snprintf(&tmp, 0, fmt,
readno_tmp, task_command, task_uid, filepath, offset,
size, lower_file_size, ts.tv_sec) + 1);
msg = kmalloc(sz, GFP_KERNEL);
if (!msg)
goto out;
snprintf(msg, sz, fmt,
readno_tmp, task_command, task_uid, filepath, offset,
size, lower_file_size, ts.tv_sec);
kfree(task_command);
queue_msg(msg);
kfree(msg);
out:
jprobe_return();
return 0;
}
atomic_t closeno;
int jp_ecryptfs_release(struct inode *inode, struct file *file)
{
atomic_inc(&closeno);
jprobe_return();
return 0;
}
atomic_t openno;
int jp_ecryptfs_open(struct inode *inode, struct file *file)
{
char tmp;
char *msg;
size_t sz;
struct timespec ts = CURRENT_TIME;
struct task_struct *task = current;
char *task_command;
uid_t task_uid;
char const *filepath;
struct file *lower_file;
struct dentry *lower_dentry;
char *fmt =
"'open','%Zd','%s','%d','%s',"
"'%ld'\n";
struct ecryptfs_inode_info *inode_info =
ecryptfs_inode_to_private(inode);
size_t openno_tmp = atomic_read(&openno);
atomic_inc(&openno);
task_command = kmalloc(sizeof(task->comm), GFP_KERNEL);
if (!task_command) {
printk(KERN_WARNING "%s: Out of memory\n", __FUNCTION__);
goto out;
}
task_lock(task);
strncpy(task_command, task->comm, sizeof(task->comm));
task_unlock(task);
task_uid = task->euid;
lower_file = inode_info->lower_file;
lower_dentry = lower_file->f_path.dentry;
filepath = lower_dentry->d_name.name;
sz = (snprintf(&tmp, 0, fmt,
openno_tmp, task_command, task_uid, filepath,
ts.tv_sec) + 1);
msg = kmalloc(sz, GFP_KERNEL);
if (!msg)
goto out;
snprintf(msg, sz, fmt,
openno_tmp, task_command, task_uid, filepath,
ts.tv_sec);
kfree(task_command);
queue_msg(msg);
kfree(msg);
out:
jprobe_return();
return 0;
}
atomic_t writeno;
int ignore_header_writes = 1;
int jp_ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
loff_t offset, size_t size)
{
char tmp;
char *msg;
size_t sz;
struct timespec ts = CURRENT_TIME;
size_t writeno_tmp = atomic_read(&writeno);
struct task_struct *task = current;
char *task_command;
uid_t task_uid;
char const *filepath;
struct file *lower_file;
struct dentry *lower_dentry;
struct inode *lower_inode;
char *fmt =
"'write','%Zd','%s','%d','%s','%lld','%Zd',"
"'%lld','%ld'\n";
struct ecryptfs_inode_info *inode_info =
ecryptfs_inode_to_private(ecryptfs_inode);
u64 lower_file_size;
if (offset == 0 && ignore_header_writes == 1)
goto out;
atomic_inc(&writeno);
task_command = kmalloc(sizeof(task->comm), GFP_KERNEL);
if (!task_command) {
printk(KERN_WARNING "%s: Out of memory\n", __FUNCTION__);
goto out;
}
task_lock(task);
strncpy(task_command, task->comm, sizeof(task->comm));
task_unlock(task);
task_uid = task->euid;
lower_file = inode_info->lower_file;
lower_dentry = lower_file->f_path.dentry;
lower_inode = lower_dentry->d_inode;
lower_file_size = i_size_read(lower_inode);
filepath = lower_dentry->d_name.name;
sz = (snprintf(&tmp, 0, fmt,
writeno_tmp, task_command, task_uid, filepath, offset,
size, lower_file_size, ts.tv_sec) + 1);
msg = kmalloc(sz, GFP_KERNEL);
if (!msg)
goto out;
snprintf(msg, sz, fmt,
writeno_tmp, task_command, task_uid, filepath, offset,
size, lower_file_size, ts.tv_sec);
kfree(task_command);
queue_msg(msg);
kfree(msg);
out:
jprobe_return();
return 0;
}
static ssize_t ecryptfs_dev_read(struct file *filp, char __user *buf,
size_t len, loff_t *ppos);
static int ecryptfs_dev_open(struct inode *inode, struct file *file);
static int ecryptfs_dev_release(struct inode *inode, struct file *file);
struct file_operations ecryptfs_fops = {
.read = ecryptfs_dev_read,
.open = ecryptfs_dev_open,
.release = ecryptfs_dev_release,
};
static ssize_t ecryptfs_dev_read(struct file *filp, char __user *buf,
size_t len, loff_t *ppos)
{
ssize_t rc = 0;
spin_lock(&clo_msg_list_spinlock);
if (!head_clo_msg)
goto out;
if (len >= head_clo_msg->size) {
struct clo_msg *to_delete;
if (head_clo_msg == tail_clo_msg)
tail_clo_msg = NULL;
memcpy(buf, head_clo_msg->msg, head_clo_msg->size);
rc = head_clo_msg->size;
kfree(head_clo_msg->msg);
to_delete = head_clo_msg;
head_clo_msg = head_clo_msg->next;
kfree(to_delete);
num_clo_msgs--;
}
out:
spin_unlock(&clo_msg_list_spinlock);
return rc;
}
static int ecryptfs_dev_open(struct inode *inode, struct file *file)
{
return 0;
}
static int ecryptfs_dev_release(struct inode *inode, struct file *file)
{
return 0;
}
struct jprobe_mapping_elem jprobe_mapping[] = {
{NULL, "ecryptfs_write_lower", jp_ecryptfs_write_lower},
{NULL, "ecryptfs_read_lower", jp_ecryptfs_read_lower},
{NULL, "ecryptfs_open", jp_ecryptfs_open},
{NULL, "ecryptfs_release", jp_ecryptfs_release},
};
int major;
int minor;
#define ECRYPTFS_DEVICE_NAME "ecryptfs"
static int __init jprobe_ecryptfs_init(void)
{
int i;
int rc;
for (i = 0; i < ARRAY_SIZE(jprobe_mapping); i++) {
jprobe_mapping[i].jp = kzalloc(sizeof(struct jprobe),
GFP_KERNEL);
jprobe_mapping[i].jp->entry = jprobe_mapping[i].fp;
jprobe_mapping[i].jp->kp.symbol_name = jprobe_mapping[i].symbol;
printk(KERN_INFO "%s: Registering jprobe for symbol [%s]\n",
__FUNCTION__, jprobe_mapping[i].symbol);
rc = register_jprobe(jprobe_mapping[i].jp);
if (rc < 0) {
int j;
printk(KERN_NOTICE "Unable to register symbol [%s]\n",
jprobe_mapping[i].symbol);
for (j = 0; j < i; j++) {
unregister_jprobe(jprobe_mapping[j].jp);
kfree(jprobe_mapping[j].jp);
}
return -EINVAL;
}
}
rc = register_chrdev(0, ECRYPTFS_DEVICE_NAME, &ecryptfs_fops);
if (rc < 0) {
printk(KERN_ERR
"%s: Error registering chrdev [%s]; rc = [%d]\n",
__FUNCTION__, ECRYPTFS_DEVICE_NAME, rc);
major = -1;
} else {
major = rc;
printk(KERN_INFO "%s: Registered major device [%d]; if your "
"userspace mechanism for generating device files "
"automatically is not configured yet, then you can "
"manually create the node with the command "
"[mknod /dev/ecryptfs c %d 0]\n",
__FUNCTION__, major, major);
}
spin_lock_init(&clo_msg_list_spinlock);
atomic_set(&writeno, 0);
atomic_set(&openno, 0);
atomic_set(&readno, 0);
atomic_set(&closeno, 0);
return 0;
}
static void __exit jprobe_ecryptfs_exit(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(jprobe_mapping); i++) {
unregister_jprobe(jprobe_mapping[i].jp);
kfree(jprobe_mapping[i].jp);
}
if (major >= 0) {
unregister_chrdev(major, ECRYPTFS_DEVICE_NAME);
} else {
printk(KERN_WARNING "%s: Not unregistering device, since there "
"was an error during registration\n", __FUNCTION__);
}
spin_lock(&clo_msg_list_spinlock);
while (head_clo_msg) {
struct clo_msg *to_delete;
kfree(head_clo_msg->msg);
to_delete = head_clo_msg;
head_clo_msg = head_clo_msg->next;
kfree(to_delete);
}
spin_unlock(&clo_msg_list_spinlock);
}
module_init(jprobe_ecryptfs_init);
module_exit(jprobe_ecryptfs_exit);
MODULE_LICENSE("GPL");
|