-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStMichael_filesystem_check.c
476 lines (378 loc) · 12.2 KB
/
StMichael_filesystem_check.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Saint Michael, Linux Kernel Module.
* Verions: 0.11
*
* August 6, 2002
*
*
* Copyright (C) 2001 Timothy Lalwess ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
* USA.
*
*
*
*/
#include <linux/sys.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/smp.h>
#include <linux/slab.h>
#include <asm/unistd.h>
#include <asm/current.h>
#include <sys/syscall.h>
#include <asm/errno.h>
#include <linux/reboot.h>
#include <linux/smp_lock.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include "StMichael_lkm.h"
#include "StMichael_string_util.h"
#include "StMichael_Ref.h"
#include "md5.h"
#if defined(FSCHECK) && defined(USE_CHECKSUM)
struct fscheck_record ext2_fscheck_record;
struct fscheck_record proc_fscheck_record;
md5_byte_t ext2_fscheck_digest[16];
md5_byte_t proc_fscheck_digest[16];
#endif
#ifdef REALLY_IMMUTABLE
int init_really_immutable ( char * filename )
{
#if defined(EXT_FILE_OPS)
struct file_operations * fops = (struct file_operations *) EXT_FILE_OPS;
#endif
#if ! defined(EXT_FILE_OPS)
char * us_name;
int fd = -1;
int size;
size = sjp_l_strlen(filename)+1;
us_name = sjp_l_malloc(size);
copy_to_user(us_name,filename,size);
fd = sm_open(us_name, O_RDONLY, 0);
sjp_l_free(us_name,size);
if (fd < 0) {
sjp_l_notice(0);
return -1;
}
if ((!sjp_l_strncmp("ext2",
current->files->fd[fd]->f_dentry->d_sb->s_type->name,5)
|| !sjp_l_strncmp("ext3",
current->files->fd[fd]->f_dentry->d_sb->s_type->name,5))
&& orig_ext2_ioctl == NULL)
{
orig_ext2_ioctl = current->files->fd[fd]->f_op->ioctl;
current->files->fd[fd]->f_op->ioctl = (void *) sm_ext2_ioctl;
}
sm_close(fd);
#else
orig_ext2_ioctl = fops->ioctl;
fops->ioctl = (void *) sm_ext2_ioctl;
#endif
return 0;
}
int sm_ext2_ioctl ( struct inode * inode, struct file * filp,
unsigned int cmd, unsigned long arg )
{
unsigned int flags;
if ( inode->u.ext2_i.i_flags & S_IMMUTABLE )
{
if ( get_user(flags, (int *) arg ))
return -EFAULT;
// Lets keep it that way..
flags |= S_IMMUTABLE ;
if ( put_user(flags, (int *) arg ))
return -EFAULT;
}
return orig_ext2_ioctl( inode, filp, cmd, arg ) ;
}
#endif
#if defined(FSCHECK) && defined(USE_CHECKSUM)
int init_fscheck_record( struct fscheck_record * rec, char * filename, char * dirname )
{
int fd = -1;
char * us_name;
int size;
md5_state_t state;
struct file_operations *fops;
struct super_operations *sops;
struct dentry_operations *dops;
rec->dir_operations = kmalloc(sizeof(struct file_operations), GFP_KERNEL);
rec->file_operations = kmalloc(sizeof(struct file_operations), GFP_KERNEL);
rec->super_operations = kmalloc(sizeof(struct super_operations), GFP_KERNEL);
rec->dentry_operations = kmalloc(sizeof(struct dentry_operations), GFP_KERNEL);
if ( ! (rec->dir_operations && rec->file_operations && rec->super_operations && rec->dentry_operations ) )
{
file_init_failure:
if (rec->dir_operations) kfree(rec->dir_operations);
if (rec->file_operations) kfree(rec->file_operations);
if (rec->super_operations) kfree(rec->super_operations);
if (rec->dentry_operations) kfree(rec->dentry_operations);
sjp_l_notice(0);
return -1;
}
#if !defined(EXT_FILE_OPS) || !defined(EXT_SUPER_OPS) || !defined(PROC_FILE_OPS) || !defined (PROC_DENT_OPS) || !defined(PROC_SUPER_OPS)
size = sjp_l_strlen(filename)+1;
us_name = sjp_l_malloc(size);
copy_to_user(us_name,filename,size);
fd = sm_open(us_name, O_RDONLY, 0);
sjp_l_free(us_name,size);
if (fd < 0) {
sjp_l_notice(0);
goto file_init_failure;
}
fops = current->files->fd[fd]->f_op;
if (!sjp_l_strncmp("/proc",filename,5))
dops = current->files->fd[fd]->f_dentry->d_op;
else
dops = NULL;
sops = current->files->fd[fd]->f_dentry->d_sb->s_op;
#else
if (sjp_l_strncmp("/proc",filename,5))
{
fops = (struct file_operations *) (EXT_FILE_OPS);
dops = NULL;
sops = (struct super_operations *) (EXT_SUPER_OPS);
}
else
{
fops = (struct file_operations *) (PROC_FILE_OPS);
dops = (struct dentry_operations *) (PROC_DENT_OPS);
sops = (struct super_operations *) (PROC_SUPER_OPS);
}
#endif
sjp_l_memcpy(rec->file_operations, fops,
sizeof(struct file_operations));
md5_init(&state);
md5_append(&state, (const md5_byte_t *) fops,
sizeof(struct file_operations));
md5_finish(&state, rec->file_operations_digest);
if (dops) {
sjp_l_memcpy(rec->dentry_operations,
dops,
sizeof(struct dentry_operations));
md5_init(&state);
md5_append(&state, (const md5_byte_t *) dops,
sizeof(struct dentry_operations));
md5_finish(&state, rec->dentry_operations_digest);
}
else
rec->dentry_operations = NULL;
sjp_l_memcpy(rec->super_operations, sops, sizeof(struct super_operations));
md5_init(&state);
md5_append(&state, (const md5_byte_t *) sops,
sizeof(struct super_operations));
md5_finish(&state, rec->super_operations_digest);
#if !defined(EXT_FILE_OPS) || !defined(EXT_SUPER_OPS) || !defined(PROC_FILE_OPS) || !defined (PROC_DENT_OPS) || !defined(PROC_SUPER_OPS)
sm_close(fd);
fd = -1;
#endif
#if !defined(EXT_DIR_OPS) || !defined(PROC_DIR_OPS)
size = sjp_l_strlen(dirname)+1;
us_name = sjp_l_malloc(size);
copy_to_user(us_name,dirname, size);
fd = sm_open(us_name, O_RDONLY | O_DIRECTORY, 0);
sjp_l_free(us_name,size);
if (fd < 0) {
sjp_l_notice(0);
goto file_init_failure;
}
fops = current->files->fd[fd]->f_op;
#else
if (sjp_l_strncmp("/proc",filename,5))
fops = (struct file_operations *) EXT_DIR_OPS;
else
fops = (struct file_operations *) PROC_DIR_OPS;
#endif
sjp_l_memcpy(rec->dir_operations, fops,
sizeof(struct file_operations));
md5_init(&state);
md5_append(&state, (const md5_byte_t *) fops,
sizeof(struct file_operations));
md5_finish(&state, rec->directory_operations_digest);
#if !defined(EXT_DIR_OPS) || !defined(PROC_DIR_OPS)
sm_close(fd); fd = -1;
#endif
return 0;
}
void check_fscheck_record( struct fscheck_record * rec, char * filename, char * dirname )
{
md5_state_t state;
md5_byte_t digest[16];
int i;
int fd = -1;
int size;
char * us_name;
// First, Dir options.
size = sjp_l_strlen(filename)+1;
us_name = sjp_l_malloc(size);
if (!us_name)
{
return;
}
copy_to_user(us_name,filename,size);
fd = sm_open(us_name, O_RDONLY, 0);
sjp_l_free(us_name,size);
if (fd < 0)
{
#if defined(EXT_FILE_OPS) && defined(PROC_FILE_OPS)
goto check_file_not_available;
#else
sjp_l_notice(1);
return;
#endif
}
// File Opperations Check.
md5_init(&state);
md5_append(&state, (const md5_byte_t *) current->files->fd[fd]->f_op,
sizeof(struct file_operations));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
if (digest[i] != rec->file_operations_digest[i])
{
sjp_l_notice(10);
sjp_l_memcpy( current->files->fd[fd]->f_op,
rec->file_operations,
sizeof(struct file_operations));
break;
}
sm_close(fd);
#if defined(EXT_FILE_OPS) && defined(PROC_FILE_OPS)
check_file_not_available:
#endif
fd = -1;
size = sjp_l_strlen(dirname)+1;
us_name = sjp_l_malloc(size);
if (!us_name)
{
sjp_l_notice(1);
return;
}
copy_to_user(us_name,dirname,size);
fd = sm_open(us_name, O_RDONLY, 0);
sjp_l_free(us_name,size);
if (fd < 0)
{
#if defined(EXT_FILE_OPS) && defined(PROC_FILE_OPS)
goto check_dir_not_available;
#else
sjp_l_notice(1);
return;
#endif
}
// Directory Opperations Check
md5_init(&state);
md5_append(&state, (const md5_byte_t *) current->files->fd[fd]->f_op,
sizeof(struct file_operations));
md5_finish(&state, digest);
#ifdef DEBUG
{ int i; printk("Digest: "); for (i = 0; i < 16; i++) printk("%X", rec->directory_operations_digest[i]); printk("\n"); }
{ int i; printk("Digest: "); for (i = 0; i < 16; i++) printk("%X", digest[i]); printk("\n"); }
#endif
for (i = 0; i < 16; i++)
if (digest[i] != rec->directory_operations_digest[i])
{
sjp_l_notice(1);
sjp_l_memcpy( current->files->fd[fd]->f_op,
rec->dir_operations,
sizeof(struct file_operations));
break;
}
// Dentry Opperations Check
//
if (current->files->fd[fd]->f_dentry->d_op)
{
md5_init(&state);
md5_append(&state, (const md5_byte_t *) current->files->fd[fd]->f_dentry->d_op,
sizeof(struct dentry_operations));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
if (digest[i] != rec->dentry_operations_digest[i])
{
sjp_l_notice(10);
sjp_l_memcpy( current->files->fd[fd]->f_dentry->d_op,
rec->dentry_operations,
sizeof(struct dentry_operations));
break;
}
}
// Super Opperations Check
md5_init(&state);
md5_append(&state, (const md5_byte_t *) current->files->fd[fd]->f_dentry->d_sb->s_op,
sizeof(struct super_operations));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
if (digest[i] != rec->super_operations_digest[i])
{
sjp_l_notice(3);
sjp_l_memcpy( current->files->fd[fd]->f_dentry->d_sb->s_op,
rec->super_operations,
sizeof(struct super_operations));
break;
}
sm_close(fd); fd = -1;
check_dir_not_available:
return;
}
int init_fscheck_records( void )
{
md5_state_t state;
if (init_fscheck_record( &ext2_fscheck_record, "/sbin/init" , "/"))
return -1;
if (init_fscheck_record( &proc_fscheck_record, "/proc/ksyms", "/proc" ))
return -1;
md5_init(&state);
md5_append(&state, (const md5_byte_t *) &ext2_fscheck_record, sizeof(struct fscheck_record));
md5_finish(&state, ext2_fscheck_digest);
md5_init(&state);
md5_append(&state, (const md5_byte_t *) &proc_fscheck_record, sizeof(struct fscheck_record));
md5_finish(&state, proc_fscheck_digest);
return 0;
}
void check_fscheck_records( void )
{
md5_state_t state;
md5_byte_t digest[16];
int i;
md5_init(&state);
md5_append(&state, (const md5_byte_t *) &ext2_fscheck_record, sizeof(struct fscheck_record));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
if (digest[i] != ext2_fscheck_digest[i])
{
sjp_l_notice(4);
handle_catastrophic_kernel_compromise();
return;
}
check_fscheck_record( &ext2_fscheck_record, "/sbin/init" , "/");
md5_init(&state);
md5_append(&state, (const md5_byte_t *) &proc_fscheck_record, sizeof(struct fscheck_record));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
if (digest[i] != proc_fscheck_digest[i])
{
sjp_l_notice(4);
handle_catastrophic_kernel_compromise();
return;
}
check_fscheck_record( &proc_fscheck_record, "/proc/ksyms", "/proc" );
return;
}
#endif
void sm_fs_end ( void )
{
return;
}