source: trunk/src/sh_kern.c@ 145

Last change on this file since 145 was 143, checked in by rainer, 17 years ago

Bugfixes and threaded process check.

File size: 48.2 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2001 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config_xor.h"
22
23#define SH_SYSCALL_CODE
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <limits.h>
35#include <sys/wait.h>
36#include <signal.h>
37#include <sys/mman.h>
38
39
40#ifdef SH_USE_KERN
41
42#undef FIL__
43#define FIL__ _("sh_kern.c")
44
45#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
46
47#if TIME_WITH_SYS_TIME
48#include <sys/time.h>
49#include <time.h>
50#else
51#if HAVE_SYS_TIME_H
52#include <sys/time.h>
53#else
54#include <time.h>
55#endif
56#endif
57
58
59#include "samhain.h"
60#include "sh_pthread.h"
61#include "sh_utils.h"
62#include "sh_error.h"
63#include "sh_modules.h"
64#include "sh_kern.h"
65#include "sh_ks_xor.h"
66
67#include "sh_unix.h"
68#include "sh_hash.h"
69
70
71
72sh_rconf sh_kern_table[] = {
73 {
74 N_("severitykernel"),
75 sh_kern_set_severity
76 },
77 {
78 N_("kernelcheckactive"),
79 sh_kern_set_activate
80 },
81 {
82 N_("kernelcheckinterval"),
83 sh_kern_set_timer
84 },
85 {
86 N_("kernelcheckidt"),
87 sh_kern_set_idt
88 },
89 {
90 N_("kernelsystemcall"),
91 sh_kern_set_sc_addr
92 },
93 {
94 N_("kernelsyscalltable"),
95 sh_kern_set_sct_addr
96 },
97 {
98 N_("kernelprocrootlookup"),
99 sh_kern_set_proc_root_lookup
100 },
101 {
102 N_("kernelprocrootiops"),
103 sh_kern_set_proc_root_iops
104 },
105 {
106 N_("kernelprocroot"),
107 sh_kern_set_proc_root
108 },
109 {
110 NULL,
111 NULL
112 },
113};
114
115
116static time_t lastcheck;
117static int ShKernActive = S_TRUE;
118static int ShKernInterval = 300;
119static int ShKernSeverity = SH_ERR_SEVERE;
120static int ShKernDelay = 100; /* milliseconds */
121static int ShKernIDT = S_TRUE;
122
123/* The address of system_call
124 */
125#ifdef SH_SYS_CALL_ADDR
126static unsigned long system_call_addr = SH_SYS_CALL_ADDR;
127#else
128static unsigned long system_call_addr = 0;
129#endif
130
131/* The address of the sys_call_table
132 */
133#ifdef SH_SYS_CALL_TABLE
134static unsigned int kaddr = SH_SYS_CALL_TABLE;
135#else
136static unsigned int kaddr = 0;
137#endif
138
139#ifdef PROC_ROOT_LOC
140static unsigned long proc_root = PROC_ROOT_LOC;
141#else
142static unsigned long proc_root = 0;
143#endif
144#ifdef PROC_ROOT_IOPS_LOC
145static unsigned long proc_root_iops = PROC_ROOT_IOPS_LOC;
146#else
147static unsigned long proc_root_iops = 0;
148#endif
149#ifdef PROC_ROOT_LOOKUP_LOC
150static unsigned long proc_root_lookup = PROC_ROOT_LOOKUP_LOC;
151#else
152static unsigned long proc_root_lookup = 0;
153#endif
154
155/* This is the module 'reconfigure' function, which is a no-op.
156 */
157int sh_kern_null()
158{
159 return 0;
160}
161
162#define SH_KERN_DBPUSH 0
163#define SH_KERN_DBPOP 1
164
165char * sh_kern_db_syscall (int num, char * prefix,
166 void * in_name, unsigned long * addr,
167 unsigned int * code1, unsigned int * code2,
168 int * size, int direction)
169{
170 char path[128];
171 char * p = NULL;
172 unsigned long x1 = 0, x2 = 0;
173 unsigned char * name = (unsigned char *) in_name;
174
175 sl_snprintf(path, 128, "K_%s_%04d", prefix, num);
176
177 if (direction == SH_KERN_DBPUSH)
178 {
179 x1 = *code1;
180 x2 = *code2;
181
182 sh_hash_push2db (path, *addr, x1, x2,
183 name, (name == NULL) ? 0 : (*size));
184 }
185 else
186 {
187 p = sh_hash_db2pop (path, addr, &x1, &x2, size);
188 *code1 = (unsigned int) x1;
189 *code2 = (unsigned int) x2;
190 }
191 return p;
192}
193
194static char * sh_kern_pathmsg (char * msg, size_t msg_len,
195 int num, char * prefix,
196 unsigned char * old, size_t old_len,
197 unsigned char * new, size_t new_len)
198{
199 size_t k;
200 char tmp[128];
201 char *p;
202 char *linkpath_old;
203 char *linkpath_new;
204 char i2h[2];
205
206#ifdef SH_USE_XML
207 sl_snprintf(tmp, sizeof(tmp), _("path=\"K_%s_%04d\" "),
208 prefix, num);
209#else
210 sl_snprintf(tmp, sizeof(tmp), _("path=<K_%s_%04d> "),
211 prefix, num);
212#endif
213 sl_strlcpy(msg, tmp, msg_len);
214
215 if (SL_TRUE == sl_ok_muls(old_len, 2) &&
216 SL_TRUE == sl_ok_adds(old_len * 2, 1))
217 linkpath_old = SH_ALLOC(old_len * 2 + 1);
218 else
219 return msg;
220
221 if (SL_TRUE == sl_ok_muls(new_len, 2) &&
222 SL_TRUE == sl_ok_adds(new_len * 2, 1))
223 linkpath_new = SH_ALLOC(new_len * 2 + 1);
224 else
225 return msg;
226
227 for (k = 0; k < old_len; ++k)
228 {
229 p = sh_util_charhex (old[k], i2h);
230 linkpath_old[2*k] = p[0];
231 linkpath_old[2*k+1] = p[1];
232 linkpath_old[2*k+2] = '\0';
233 }
234
235 for (k = 0; k < new_len; ++k)
236 {
237 p = sh_util_charhex (new[k], i2h);
238 linkpath_new[2*k] = p[0];
239 linkpath_new[2*k+1] = p[1];
240 linkpath_new[2*k+2] = '\0';
241
242}
243#ifdef SH_USE_XML
244 sl_strlcat(msg, _("link_old=\""), msg_len);
245 sl_strlcat(msg, linkpath_old, msg_len);
246 sl_strlcat(msg, _("\" link_new=\""), msg_len);
247 sl_strlcat(msg, linkpath_new, msg_len);
248 sl_strlcat(msg, _("\""), msg_len);
249#else
250 sl_strlcat(msg, _("link_old=<"), msg_len);
251 sl_strlcat(msg, linkpath_old, msg_len);
252 sl_strlcat(msg, _(">, link_new=<"), msg_len);
253 sl_strlcat(msg, linkpath_new, msg_len);
254 sl_strlcat(msg, _(">"), msg_len);
255#endif
256
257 SH_FREE(linkpath_old);
258 SH_FREE(linkpath_new);
259
260 return msg;
261}
262
263#ifdef HOST_IS_LINUX
264
265/*
266 * Interrupt Descriptor Table
267 */
268
269#include <asm/segment.h>
270
271#define SH_MAXIDT 256
272
273static unsigned char sh_idt_table[SH_MAXIDT * 8];
274
275static char * sh_strseg(unsigned short segment)
276{
277 switch (segment) {
278 case __KERNEL_CS:
279 return _("KERNEL_CS");
280 case __KERNEL_DS:
281 return _("KERNEL_DS");
282 case __USER_CS:
283 return _("USER_CS");
284 case __USER_DS:
285 return _("USER_DS");
286 default:
287 return _("unknown");
288 }
289}
290
291
292static int sh_kern_data_init ()
293{
294 unsigned long store0 = 0;
295 unsigned int store1 = 0, store2 = 0;
296 int datasize, i, j;
297 char * databuf;
298
299 /* system_call code
300 */
301 databuf = sh_kern_db_syscall (0, _("system_call"),
302 NULL, &store0, &store1, &store2,
303 &datasize, SH_KERN_DBPOP);
304 if (datasize == sizeof(system_call_code))
305 {
306 memcpy (system_call_code, databuf, sizeof(system_call_code));
307 SH_FREE(databuf);
308 }
309 else
310 {
311 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
312 _("system_call_code not found in database"),
313 _("sh_kern_data_init"));
314 return -1;
315 }
316
317 /* syscall address and code
318 */
319 for (i = 0; i < SH_MAXCALLS; ++i)
320 {
321 databuf = sh_kern_db_syscall (i, _("syscall"),
322 NULL, &store0, &store1, &store2,
323 &datasize, SH_KERN_DBPOP);
324 sh_syscalls[i].addr = store0;
325 if (store0 == 0) {
326 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, i, MSG_E_SUBGEN,
327 _("syscall address not found in database"),
328 _("sh_kern_data_init"));
329 return -1;
330 }
331
332 sh_syscalls[i].code[0] = (unsigned int) store1;
333 sh_syscalls[i].code[1] = (unsigned int) store2;
334 if ((store1 == 0) || (store2 == 0)) {
335 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, i, MSG_E_SUBGEN,
336 _("syscall code not found in database"),
337 _("sh_kern_data_init"));
338 }
339
340 if (databuf != NULL) {
341 SH_FREE(databuf);
342 }
343
344 }
345
346 if (ShKernIDT == S_TRUE)
347 {
348 for (j = 0; j < SH_MAXIDT; ++j)
349 {
350 databuf = sh_kern_db_syscall (j, _("idt_table"),
351 NULL,
352 &store0, &store1, &store2,
353 &datasize, SH_KERN_DBPOP);
354 if (datasize == 8) {
355 memcpy(&idt_table[j*8], databuf, 8);
356 SH_FREE(databuf);
357 } else {
358 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, j, MSG_E_SUBGEN,
359 _("idt table not found in database"),
360 _("sh_kern_data_init"));
361 return -1;
362 }
363 }
364 }
365
366 return 0;
367}
368
369
370/*
371 * Defined in include/linux/fs.h
372 */
373
374/* Here. we are only interested in 'lookup'. I.e. the struct
375 * must be <= the real one, and 'lookup' must be at the
376 * correct position.
377 */
378struct inode_operations {
379 int (*create) (int *,int *,int);
380 int * (*lookup) (int *,int *);
381 int (*link) (int *,int *,int *);
382 int (*unlink) (int *,int *);
383 int (*symlink) (int *,int *,const char *);
384 int (*mkdir) (int *,int *,int);
385 int (*rmdir) (int *,int *);
386 int (*mknod) (int *,int *,int,int);
387 int (*rename) (int *, int *,
388 int *, int *);
389 /* flawfinder: ignore */
390 int (*readlink) (int *, char *,int);
391 int (*follow_link) (int *, int *);
392 void (*truncate) (int *);
393 int (*permission) (int *, int);
394 int (*revalidate) (int *);
395 /*
396 int (*setattr) (int *, int *);
397 int (*getattr) (int *, int *);
398 int (*setxattr) (int *, const char *, void *, size_t, int);
399 ssize_t (*getxattr) (int *, const char *, void *, size_t);
400 ssize_t (*listxattr) (int *, char *, size_t);
401 int (*removexattr) (int *, const char *);
402 */
403};
404
405/*
406 * this one is just for dummy purposes
407 */
408struct file_operations {
409 int (*create) (int *,int *,int);
410};
411
412/* Defined in include/linux/proc_fs.h
413 * Here we are interested in the 'proc_iops' member.
414 */
415struct proc_dir_entry {
416 unsigned short low_ino;
417 unsigned short namelen;
418 const char * name;
419 mode_t mode;
420 nlink_t nlink;
421 uid_t uid;
422 gid_t gid;
423#if defined TWO_SIX_SEVENTEEN_PLUS
424 /* size is loff_t in 2.6.17+ kernels */
425 unsigned long dummy;
426#endif
427 unsigned long size;
428 struct inode_operations * proc_iops;
429 struct file_operations * proc_fops;
430 /*
431 get_info_t *get_info;
432 struct module *owner;
433 struct proc_dir_entry *next, *parent, *subdir;
434 void *data;
435 read_proc_t *read_proc;
436 write_proc_t *write_proc;
437 atomic_t count;
438 int deleted;
439 */
440};
441
442
443static int sh_kern_kmem_read (int fd, unsigned long addr,
444 unsigned char * buf, int len)
445{
446 if (lseek(fd, addr, SEEK_SET) == (off_t) (-1))
447 {
448 return -1;
449 }
450 if (read(fd, buf, len) < 0)
451 {
452 return -1;
453 }
454 return 0;
455}
456
457static int sh_kern_read_data (int fd, unsigned long addr,
458 unsigned char * buf, size_t len)
459{
460 size_t moff, roff;
461 size_t sz;
462 char * kmap;
463
464 /* first, try read()
465 */
466 if (0 == sh_kern_kmem_read (fd, addr, buf, len))
467 return 0;
468
469 /* next, try mmap()
470 */
471 sz = getpagesize(); /* unistd.h */
472
473 moff = ((size_t)(addr/sz)) * sz; /* lower page boundary */
474 roff = addr - moff; /* off relative to lower address of mmapped area */
475
476 kmap = mmap(0, len+sz, PROT_READ, MAP_PRIVATE, fd, moff);/* sys/mman.h */
477
478 if (kmap == MAP_FAILED)
479 {
480 return -1;
481 }
482 memcpy (buf, &kmap[roff], len);
483 return munmap(kmap, len+sz);
484}
485
486
487static int check_init (int * init_retval)
488{
489 static int is_init = 0;
490
491 SL_ENTER(_("check_init"));
492
493 if (is_init == 0)
494 {
495 if (sh.flag.checkSum != SH_CHECK_INIT && sh.flag.update != S_TRUE)
496 {
497 if (0 == sh_kern_data_init()) {
498 is_init = 1;
499 } else {
500 sh_error_handle (ShKernSeverity, FIL__, __LINE__, 1,
501 MSG_E_SUBGEN,
502 _("could not initialize kernel check - switching off"),
503 _("check_init") );
504 ShKernActive = S_FALSE;
505 *init_retval = is_init;
506 SL_RETURN( (-1), _("check_init"));
507 }
508 }
509 else if ((sh.flag.checkSum == SH_CHECK_INIT ||
510 sh.flag.checkSum == SH_CHECK_CHECK) &&
511 (sh.flag.update == S_TRUE))
512 {
513 if (0 == sh_kern_data_init()) {
514 is_init = 1;
515 } else {
516 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 0,
517 MSG_E_SUBGEN,
518 _("no or incomplete data in baseline database for kernel check"),
519 _("check_init") );
520 }
521 }
522 }
523 *init_retval = is_init;
524 SL_RETURN( (0), _("check_init"));
525}
526
527#define SH_KERN_SIZ 512
528#define SH_KERN_SCC 256
529
530static void run_child(int kd, int mpipe[2])
531{
532 int j;
533
534 unsigned long kmem_call_table[SH_KERN_SIZ];
535 unsigned int kmem_code_table[SH_KERN_SIZ][2];
536
537 unsigned char buf[6];
538 unsigned short idt_size;
539 unsigned long idt_addr;
540
541 unsigned char new_system_call_code[SH_KERN_SCC];
542
543 struct inode_operations proc_root_inode;
544 struct proc_dir_entry proc_root_dir;
545
546 int status = close(mpipe[0]);
547
548 setpgid(0, 0);
549
550 /* Seek to the system call table (at kaddr) and read it into
551 * the kmem_call_table array
552 */
553 if(status == 0)
554 {
555 retry_msleep (0, ShKernDelay); /* milliseconds */
556
557 if (sh_kern_read_data (kd, kaddr,
558 (unsigned char *) &kmem_call_table,
559 sizeof(kmem_call_table)))
560 {
561 status = -2;
562 }
563 }
564
565 /*
566 * Seek to the system call address (at sh_syscalls[j].addr) and
567 * read first 8 bytes into the array kmem_code_table[j][] (2 * unsigned int)
568 */
569 if(status == 0)
570 {
571 memset(kmem_code_table, 0, sizeof(kmem_code_table));
572 for (j = 0; j < SH_MAXCALLS; ++j)
573 {
574 if (sh_syscalls[j].addr == 0UL) {
575 sh_syscalls[j].addr = kmem_call_table[j];
576 }
577
578 if (sh_syscalls[j].name == NULL ||
579 sh_syscalls[j].addr == 0UL)
580 break;
581
582 if ((sh.flag.checkSum == SH_CHECK_INIT ||
583 sh.flag.checkSum == SH_CHECK_CHECK) &&
584 (sh.flag.update == S_TRUE))
585 {
586 sh_kern_read_data (kd, kmem_call_table[j],
587 (unsigned char *) &(kmem_code_table[j][0]),
588 2 * sizeof(unsigned int));
589 }
590 else
591 {
592 sh_kern_read_data (kd, sh_syscalls[j].addr,
593 (unsigned char *) &(kmem_code_table[j][0]),
594 2 * sizeof(unsigned int));
595 }
596 }
597 }
598
599 if(status == 0)
600 {
601 /*
602 * Get the address and size of Interrupt Descriptor Table,
603 * and read the content into the global array sh_idt_table[]
604 */
605 __asm__ volatile ("sidt %0": "=m" (buf));
606 idt_size = *((unsigned short *) &buf[0]);
607 idt_addr = *((unsigned long *) &buf[2]);
608 idt_size = (idt_size + 1)/8;
609
610 if (idt_size > SH_MAXIDT)
611 idt_size = SH_MAXIDT;
612
613 memset(sh_idt_table, '\0', SH_MAXIDT*8);
614 sh_kern_read_data (kd, idt_addr,
615 (unsigned char *) sh_idt_table, idt_size*8);
616 }
617
618 /*
619 * Seek to the system_call address (at system_call_addr) and
620 * read first 256 bytes into new_system_call_code[]
621 *
622 * system_call_addr is defined in the include file.
623 */
624 if(status == 0)
625 {
626 sh_kern_read_data (kd, system_call_addr,
627 (unsigned char *) new_system_call_code, SH_KERN_SCC);
628 }
629
630 /*
631 * Seek to proc_root and read the structure.
632 * Seek to proc_root_inode_operations and get the structure.
633 */
634 if(status == 0)
635 {
636 sh_kern_read_data (kd, proc_root,
637 (unsigned char *) &proc_root_dir,
638 sizeof(proc_root_dir));
639 sh_kern_read_data (kd, proc_root_iops,
640 (unsigned char *) &proc_root_inode,
641 sizeof(proc_root_inode));
642 }
643
644 /*
645 * Write out data to the pipe
646 */
647 if(status == 0)
648 {
649 status = write(mpipe[1], &kmem_call_table, sizeof(kmem_call_table));
650
651 if(status > 0)
652 status = write(mpipe[1], &kmem_code_table, sizeof(kmem_code_table));
653
654 if(status > 0)
655 status = write(mpipe[1], &sh_idt_table, sizeof(sh_idt_table));
656
657 if(status > 0)
658 status = write(mpipe[1], new_system_call_code, SH_KERN_SCC);
659
660 if(status > 0)
661 status = write(mpipe[1], &proc_root_dir, sizeof(proc_root_dir));
662
663 if(status > 0)
664 status = write(mpipe[1], &proc_root_inode, sizeof(proc_root_inode));
665 }
666 _exit( (status >= 0) ? 0 : status);
667}
668
669
670struct sh_kernel_info {
671 unsigned long kmem_call_table[SH_KERN_SIZ];
672 unsigned int kmem_code_table[SH_KERN_SIZ][2];
673
674 unsigned char new_system_call_code[SH_KERN_SCC];
675
676 struct inode_operations proc_root_inode;
677 struct proc_dir_entry proc_root_dir;
678};
679
680static int read_from_child(pid_t mpid, int * mpipe,
681 struct sh_kernel_info * kinfo)
682{
683 int res;
684 int status;
685 long size;
686
687 /* Close reading side of pipe, and wait some milliseconds
688 */
689 close (mpipe[1]);
690 retry_msleep (0, ShKernDelay); /* milliseconds */
691
692 size = SH_KERN_SIZ * sizeof(unsigned long);
693
694 if (size != read(mpipe[0], &(kinfo->kmem_call_table), size))
695 status = -4;
696 else
697 status = 0;
698
699 if(status == 0)
700 {
701 size = sizeof(unsigned int) * 2 * SH_KERN_SIZ;
702
703 if (size != read(mpipe[0], &(kinfo->kmem_code_table), size))
704 status = -5;
705 else
706 status = 0;
707 }
708
709 if(status == 0)
710 {
711 memset(sh_idt_table, '\0', SH_MAXIDT*8);
712 if (sizeof(sh_idt_table) !=
713 read(mpipe[0], &sh_idt_table, sizeof(sh_idt_table)))
714 status = -5;
715 else
716 status = 0;
717 }
718
719 if(status == 0)
720 {
721 size = SH_KERN_SCC;
722
723 if (size != read(mpipe[0], &(kinfo->new_system_call_code), size))
724 status = -6;
725 else
726 status = 0;
727 }
728
729 if(status == 0)
730 {
731 size = sizeof (struct proc_dir_entry);
732
733 if (size != read(mpipe[0], &(kinfo->proc_root_dir), size))
734 status = -7;
735 else
736 status = 0;
737 }
738
739 if(status == 0)
740 {
741 size = sizeof (struct inode_operations);
742
743 if (size != read(mpipe[0], &(kinfo->proc_root_inode), size))
744 status = -8;
745 else
746 status = 0;
747 }
748
749 if (status < 0)
750 res = waitpid(mpid, NULL, WNOHANG|WUNTRACED);
751 else
752 {
753 res = waitpid(mpid, &status, WNOHANG|WUNTRACED);
754 if (res == 0 && 0 != WIFEXITED(status))
755 status = WEXITSTATUS(status);
756 }
757 close (mpipe[0]);
758 if (res <= 0)
759 {
760 aud_kill(FIL__, __LINE__, mpid, 9);
761 waitpid(mpid, NULL, 0);
762 }
763 return status;
764}
765
766
767static void check_idt_table(int is_init)
768{
769 int i, j;
770
771 unsigned short idt_offset_lo, idt_offset_hi, idt_selector;
772 unsigned char idt_reserved, idt_flag;
773 unsigned short sh_idt_offset_lo, sh_idt_offset_hi, sh_idt_selector;
774 unsigned char sh_idt_reserved, sh_idt_flag;
775 int dpl;
776 unsigned long idt_iaddr;
777 int sh_dpl;
778 unsigned long sh_idt_iaddr;
779 char idt_type, sh_idt_type;
780
781 unsigned long store0;
782 unsigned int store1, store2;
783 int datasize;
784 char msg[2*SH_BUFSIZE];
785
786 if (ShKernIDT == S_TRUE)
787 {
788 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
789 {
790 datasize = 8;
791 for (j = 0; j < SH_MAXIDT; ++j)
792 {
793 sh_kern_db_syscall (j, _("idt_table"),
794 &sh_idt_table[j*8],
795 &store0, &store1, &store2,
796 &datasize, SH_KERN_DBPUSH);
797 }
798 }
799
800 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
801 (sh.flag.update == S_TRUE && is_init == 1))
802 {
803 /* Check the Interrupt Descriptor Table
804 *
805 * Stored(old) is idt_table[]
806 */
807 for (j = 0; j < SH_MAXIDT; ++j)
808 {
809 i = j * 8;
810
811 sh_idt_offset_lo = *((unsigned short *) &sh_idt_table[i]);
812 sh_idt_selector = *((unsigned short *) &sh_idt_table[i+2]);
813 sh_idt_reserved = (unsigned char) sh_idt_table[i+4];
814 sh_idt_flag = (unsigned char) sh_idt_table[i+5];
815 sh_idt_offset_hi = *((unsigned short *) &sh_idt_table[i+6]);
816 sh_idt_iaddr = (unsigned long)(sh_idt_offset_hi << 16)
817 + sh_idt_offset_lo;
818
819 if (sh_idt_iaddr == 0)
820 {
821 sh_idt_table[i+2] = '\0';
822 sh_idt_table[i+3] = '\0';
823 sh_idt_table[i+5] = '\0';
824
825 idt_offset_lo = *((unsigned short *) &idt_table[i]);
826 idt_offset_hi = *((unsigned short *) &idt_table[i+6]);
827 idt_iaddr = (unsigned long)(idt_offset_hi << 16)
828 + idt_offset_lo;
829 if (idt_iaddr == 0)
830 {
831 idt_table[i+2] = '\0';
832 idt_table[i+3] = '\0';
833 idt_table[i+5] = '\0';
834 }
835
836 }
837
838 if (memcmp(&sh_idt_table[i], &idt_table[i], 8) != 0)
839 {
840
841 idt_offset_lo = *((unsigned short *) &idt_table[i]);
842 idt_selector = *((unsigned short *) &idt_table[i+2]);
843 idt_reserved = (unsigned char) idt_table[i+4];
844 idt_flag = (unsigned char) idt_table[i+5];
845 idt_offset_hi = *((unsigned short *) &idt_table[i+6]);
846 idt_iaddr = (unsigned long)(idt_offset_hi << 16)
847 + idt_offset_lo;
848
849 if (idt_iaddr != 0)
850 {
851 if (idt_flag & 64) { dpl = 3; }
852 else { dpl = 0; }
853 if (idt_flag & 1) {
854 if (dpl == 3) idt_type = 'S';
855 else idt_type = 'T'; }
856 else { idt_type = 'I'; }
857 }
858 else { dpl = -1; idt_type = 'U'; }
859
860 if (sh_idt_iaddr != 0)
861 {
862 if (sh_idt_flag & 64) { sh_dpl = 3; }
863 else { sh_dpl = 0; }
864 if (sh_idt_flag & 1) {
865 if (sh_dpl == 3) sh_idt_type = 'S';
866 else sh_idt_type = 'T'; }
867 else { sh_idt_type = 'I'; }
868 }
869 else { sh_dpl = -1; sh_idt_type = 'U'; }
870
871 sh_kern_pathmsg (msg, SH_BUFSIZE,
872 j, _("idt_table"),
873 &idt_table[i], 8,
874 &sh_idt_table[i], 8);
875
876 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
877 0, MSG_KERN_IDT,
878 j,
879 sh_idt_iaddr, sh_strseg(sh_idt_selector),
880 (int) sh_dpl, sh_idt_type,
881 idt_iaddr, sh_strseg(idt_selector),
882 (int) dpl, idt_type, msg);
883
884 memcpy(&idt_table[i], &sh_idt_table[i], 8);
885 }
886 }
887 }
888 }
889}
890
891
892#define SYS_BUS_PCI _("/sys/bus/pci/devices")
893#include <dirent.h>
894
895static void check_rom (char * pcipath, char * name)
896{
897 file_type theFile;
898 char fileHash[2*(KEY_LEN + 1)];
899 int status;
900 char * tmp;
901 extern unsigned long sh_files_maskof (int class);
902
903 (void) sl_strlcpy (theFile.fullpath, pcipath, PATH_MAX);
904 theFile.check_mask = sh_files_maskof(SH_LEVEL_READONLY);
905 CLEAR_SH_FFLAG_REPORTED(theFile.file_reported);
906 theFile.attr_string = NULL;
907
908 status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_RO],
909 name, &theFile, fileHash, 0);
910
911 if (status != 0)
912 {
913 tmp = sh_util_safe_name(pcipath);
914 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
915 0, MSG_E_SUBGPATH,
916 _("Could not check PCI ROM"),
917 _("check_rom"),
918 tmp);
919 SH_FREE(tmp);
920 return;
921 }
922
923 if ( sh.flag.checkSum == SH_CHECK_INIT )
924 {
925 sh_hash_pushdata (&theFile, fileHash);
926 }
927 else if (sh.flag.checkSum == SH_CHECK_CHECK )
928 {
929 sh_hash_compdata (SH_LEVEL_READONLY, &theFile, fileHash, NULL, -1);
930 }
931
932 return;
933}
934
935static void check_pci_rom (char * pcipath, char * name)
936{
937 struct stat buf;
938 int fd;
939
940 if (0 == stat(pcipath, &buf))
941 {
942 /* Need to write "1" to the file to enable the ROM. Afterwards,
943 * write "0" to disable it.
944 */
945 fd = open ( pcipath, O_RDWR );
946 write( fd, "1", 1 );
947 close ( fd );
948
949 check_rom(pcipath, name);
950
951 fd = open ( pcipath, O_RDWR );
952 write( fd, "0", 1 );
953 close ( fd );
954 }
955 return;
956}
957
958static void check_pci()
959{
960 char pci_dir[256];
961 char * pcipath;
962 DIR * df;
963 struct dirent * entry;
964
965 sl_strlcpy(pci_dir, SYS_BUS_PCI, sizeof(pci_dir));
966
967 df = opendir(pci_dir);
968 if (df)
969 {
970 while (1)
971 {
972 SH_MUTEX_LOCK(mutex_readdir);
973 entry = readdir(df);
974 SH_MUTEX_UNLOCK(mutex_readdir);
975
976 if (entry == NULL)
977 break;
978
979 if (0 == strcmp(entry->d_name, ".") &&
980 0 == strcmp(entry->d_name, ".."))
981 continue;
982
983 pcipath = sh_util_strconcat(pci_dir, "/",
984 entry->d_name, "/rom", NULL);
985 check_pci_rom(pcipath, entry->d_name);
986 SH_FREE(pcipath);
987 }
988
989 closedir(df);
990 }
991}
992
993/* -- Check the proc_root inode.
994 *
995 * This will detect adore-ng.
996 */
997static void check_proc_root (struct sh_kernel_info * kinfo)
998{
999 struct proc_dir_entry proc_root_dir;
1000
1001/* 2.6.21 (((2) << 16) + ((6) << 8) + (21)) */
1002#if SH_KERNEL_NUMBER < 132629
1003 struct inode_operations proc_root_inode;
1004
1005 memcpy (&proc_root_inode, &(kinfo->proc_root_inode), sizeof(struct inode_operations));
1006
1007 /* Seems that the info does not relate anymore to proc_root_lookup(?)
1008 */
1009 if ( (unsigned int) *proc_root_inode.lookup != proc_root_lookup)
1010 {
1011 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
1012 _("proc_root_inode_operations.lookup != proc_root_lookup"));
1013 }
1014#endif
1015
1016 memcpy (&proc_root_dir, &(kinfo->proc_root_dir), sizeof(struct proc_dir_entry));
1017 if ( (((unsigned int) * &proc_root_dir.proc_iops) != proc_root_iops)
1018 && (proc_root_dir.size != proc_root_iops)
1019 && (((unsigned int) * &proc_root_dir.proc_fops) != proc_root_iops)
1020 )
1021 {
1022 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
1023 _("proc_root.proc_iops != proc_root_inode_operations"));
1024 }
1025
1026 return;
1027}
1028
1029/* -- Check the system_call syscall gate.
1030 *
1031 * Stored(old) is system_call_code[]
1032 */
1033static void check_syscall_gate(int is_init, struct sh_kernel_info * kinfo)
1034{
1035 int i, j;
1036 unsigned long store0;
1037 unsigned int store1, store2;
1038 int datasize;
1039 int max_system_call = (SYS_CALL_LOC < 128) ? 128 : SYS_CALL_LOC;
1040 char msg[2*SH_BUFSIZE];
1041
1042 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1043 {
1044 store0 = 0; store1 = 0; store2 = 0;
1045 datasize = SH_KERN_SCC;
1046 sh_kern_db_syscall (0, _("system_call"),
1047 &(kinfo->new_system_call_code), &store0, &store1, &store2,
1048 &datasize, SH_KERN_DBPUSH);
1049 }
1050
1051 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1052 (sh.flag.update == S_TRUE && is_init == 1))
1053 {
1054 for (i = 0; i < (max_system_call + 4); ++i)
1055 {
1056 if (system_call_code[i] != kinfo->new_system_call_code[i])
1057 {
1058
1059 sh_kern_pathmsg (msg, sizeof(msg),
1060 0, _("system_call"),
1061 system_call_code, SH_KERN_SCC,
1062 kinfo->new_system_call_code, SH_KERN_SCC);
1063
1064 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1065 0, MSG_KERN_GATE,
1066 kinfo->new_system_call_code[i], 0,
1067 system_call_code[i], 0,
1068 0, _("system_call (interrupt handler)"),
1069 msg);
1070
1071 for (j = 0; j < (max_system_call + 4); ++j)
1072 system_call_code[j] = kinfo->new_system_call_code[j];
1073 break;
1074 }
1075 }
1076 }
1077 return;
1078}
1079
1080static void check_system_calls (int is_init, struct sh_kernel_info * kinfo)
1081{
1082 int i;
1083
1084#ifdef SH_USE_LKM
1085 static int check_getdents = 0;
1086 /* #ifdef __NR_getdents64 */
1087 static int check_getdents64 = 0;
1088 /* #endif */
1089 static int copy_if_next = -1;
1090 static int copy_if_next_64 = -1;
1091#endif
1092
1093 unsigned long store0;
1094 unsigned int store1, store2;
1095 int mod_syscall_addr = 0;
1096 int mod_syscall_code = 0;
1097 UINT64 size_old = 0, size_new = 0;
1098 UINT64 mtime_old = 0, mtime_new = 0;
1099 UINT64 ctime_old = 0, ctime_new = 0;
1100 char tmp[128];
1101 char msg[2*SH_BUFSIZE];
1102 char timstr_o[32];
1103 char timstr_n[32];
1104
1105 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1106 {
1107 for (i = 0; i < SH_MAXCALLS; ++i)
1108 {
1109 store0 = kinfo->kmem_call_table[i];
1110 store1 = kinfo->kmem_code_table[i][0]; store2 = kinfo->kmem_code_table[i][1];
1111 sh_kern_db_syscall (i, _("syscall"),
1112 NULL, &store0, &store1, &store2,
1113 0, SH_KERN_DBPUSH);
1114 }
1115 }
1116
1117 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1118 (sh.flag.update == S_TRUE && is_init == 1))
1119 {
1120 for (i = 0; i < SH_MAXCALLS; ++i)
1121 {
1122 if (sh_syscalls[i].name == NULL /* || sh_syscalls[i].addr == 0UL */)
1123 break;
1124
1125#ifdef SH_USE_LKM
1126 if (sh_syscalls[i].addr != kinfo->kmem_call_table[i])
1127 {
1128 if (check_getdents == 0 &&
1129 0 == strcmp(_(sh_syscalls[i].name), _("sys_getdents")))
1130 {
1131 check_getdents = 1;
1132 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__,
1133 0, MSG_E_SUBGEN,
1134 _("Modified kernel syscall (expected)."),
1135 _(sh_syscalls[i].name) );
1136 copy_if_next = i;
1137 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1138 continue;
1139 }
1140 /* #ifdef __NR_getdents64 */
1141 else if (check_getdents64 == 0 &&
1142 0 == strcmp(_(sh_syscalls[i].name),
1143 _("sys_getdents64")))
1144 {
1145 check_getdents64 = 1;
1146 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__,
1147 0, MSG_E_SUBGEN,
1148 _("Modified kernel syscall (expected)."),
1149 _(sh_syscalls[i].name) );
1150 copy_if_next_64 = i;
1151 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1152 continue;
1153 }
1154 /* #endif */
1155 else
1156 {
1157 size_old = sh_syscalls[i].addr;
1158 size_new = kinfo->kmem_call_table[i];
1159 mod_syscall_addr = 1;
1160 }
1161 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1162 }
1163#else
1164 if (sh_syscalls[i].addr != kinfo->kmem_call_table[i])
1165 {
1166 size_old = sh_syscalls[i].addr;
1167 size_new = kinfo->kmem_call_table[i];
1168 mod_syscall_addr = 1;
1169 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1170 }
1171#endif
1172
1173
1174 /* -- Check the code at syscall address
1175 *
1176 * Stored(old) is sh_syscalls[]
1177 */
1178 if ( (mod_syscall_addr == 0) &&
1179 ((sh_syscalls[i].code[0] != kinfo->kmem_code_table[i][0]) ||
1180 (sh_syscalls[i].code[1] != kinfo->kmem_code_table[i][1]))
1181 )
1182 {
1183 mtime_old = sh_syscalls[i].code[0];
1184 mtime_new = kinfo->kmem_code_table[i][0];
1185 ctime_old = sh_syscalls[i].code[1];
1186 ctime_new = kinfo->kmem_code_table[i][1];
1187 mod_syscall_code = 1;
1188
1189#ifdef SH_USE_LKM
1190 if (i == copy_if_next)
1191 {
1192 mod_syscall_code = 0;
1193 copy_if_next = -1;
1194 }
1195 if (i == copy_if_next_64)
1196 {
1197 mod_syscall_code = 0;
1198 copy_if_next_64 = -1;
1199 }
1200#endif
1201
1202 sh_syscalls[i].code[0] = kinfo->kmem_code_table[i][0];
1203 sh_syscalls[i].code[1] = kinfo->kmem_code_table[i][1];
1204 }
1205
1206 /* Build the error message, if something has been
1207 * detected.
1208 */
1209 if ((mod_syscall_addr != 0) || (mod_syscall_code != 0))
1210 {
1211#ifdef SH_USE_XML
1212 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1213 _("syscall"), i);
1214#else
1215 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1216 _("syscall"), i);
1217#endif
1218 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1219
1220 if (mod_syscall_addr != 0)
1221 {
1222 sl_snprintf(tmp, 128, sh_hash_size_format(),
1223 size_old, size_new);
1224 sl_strlcat(msg, tmp, SH_BUFSIZE);
1225 }
1226 if (mod_syscall_code != 0)
1227 {
1228 (void) sh_unix_gmttime (ctime_old, timstr_o, sizeof(timstr_o));
1229 (void) sh_unix_gmttime (ctime_new, timstr_n, sizeof(timstr_n));
1230#ifdef SH_USE_XML
1231 sl_snprintf(tmp, 128,
1232 _("ctime_old=\"%s\" ctime_new=\"%s\" "),
1233 timstr_o, timstr_n);
1234#else
1235 sl_snprintf(tmp, 128,
1236 _("ctime_old=<%s>, ctime_new=<%s>, "),
1237 timstr_o, timstr_n);
1238#endif
1239 sl_strlcat(msg, tmp, SH_BUFSIZE);
1240 (void) sh_unix_gmttime (mtime_old, timstr_o, sizeof(timstr_o));
1241 (void) sh_unix_gmttime (mtime_new, timstr_n, sizeof(timstr_n));
1242#ifdef SH_USE_XML
1243 sl_snprintf(tmp, 128,
1244 _("mtime_old=\"%s\" mtime_new=\"%s\" "),
1245 timstr_o, timstr_n);
1246#else
1247 sl_snprintf(tmp, 128,
1248 _("mtime_old=<%s>, mtime_new=<%s> "),
1249 timstr_o, timstr_n);
1250#endif
1251 sl_strlcat(msg, tmp, SH_BUFSIZE);
1252 }
1253 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1254 0, MSG_KERN_SYSCALL,
1255 i, _(sh_syscalls[i].name), msg);
1256 mod_syscall_addr = 0;
1257 mod_syscall_code = 0;
1258 }
1259 }
1260 }
1261 return;
1262}
1263
1264int sh_kern_check_internal ()
1265{
1266 int kd;
1267 int is_init;
1268 pid_t mpid;
1269 int mpipe[2];
1270 int status = 0;
1271
1272 struct sh_kernel_info kinfo;
1273
1274
1275 SL_ENTER(_("sh_kern_check_internal"));
1276
1277 /* -- Check whether initialisation is required; if yes, initialize.
1278 */
1279
1280 if (0 != check_init(&is_init))
1281 {
1282 SL_RETURN( (-1), _("sh_kern_check_internal"));
1283 }
1284
1285
1286 /* -- Open /dev/kmem and fork subprocess to read from it.
1287 */
1288
1289 if (kaddr == (unsigned int) -1) /* kaddr = address of the sys_call_table */
1290 {
1291 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, MSG_E_SUBGEN,
1292 _("no address for sys_call_table - switching off"),
1293 _("kern_check_internal") );
1294 ShKernActive = S_FALSE;
1295 SL_RETURN( (-1), _("sh_kern_check_internal"));
1296 }
1297
1298 kd = aud_open(FIL__, __LINE__, SL_YESPRIV, _("/dev/kmem"), O_RDONLY, 0);
1299
1300 if (kd < 0)
1301 {
1302 status = errno;
1303 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1304 _("error opening /dev/kmem"),
1305 _("kern_check_internal") );
1306 SL_RETURN( (-1), _("sh_kern_check_internal"));
1307 }
1308
1309 status = aud_pipe(FIL__, __LINE__, mpipe);
1310
1311 if (status == 0)
1312 {
1313 mpid = aud_fork(FIL__, __LINE__);
1314
1315 switch (mpid)
1316 {
1317 case -1:
1318 status = -1;
1319 break;
1320 case 0:
1321
1322 /* -- Child process reads /dev/kmem and writes to pipe
1323 */
1324 run_child(kd, mpipe);
1325 break;
1326
1327 /* -- Parent process reads from child via pipe
1328 */
1329 default:
1330 close(kd);
1331 status = read_from_child(mpid, mpipe, &kinfo);
1332 break;
1333 }
1334 }
1335
1336 if ( status < 0)
1337 {
1338 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1339 _("error reading from /dev/kmem"),
1340 _("kern_check_internal") );
1341 SL_RETURN( (-1), _("sh_kern_check_internal"));
1342 }
1343
1344 /* -- Check the proc_root inode.
1345 *
1346 * This will detect adore-ng.
1347 */
1348 check_proc_root( &kinfo );
1349
1350
1351 /* -- Check the system_call syscall gate.
1352 *
1353 * Stored(old) is system_call_code[]
1354 */
1355 check_syscall_gate( is_init, &kinfo );
1356
1357 /* -- Check the individual syscalls
1358 *
1359 * Stored(old) is sh_syscalls[] array.
1360 */
1361 check_system_calls ( is_init, &kinfo );
1362
1363 /* -- Check the Interrupt Descriptor Table
1364 */
1365 check_idt_table(is_init);
1366
1367 /* -- Check PCI ROM
1368 */
1369 check_pci();
1370
1371 SL_RETURN( (0), _("sh_kern_check_internal"));
1372}
1373/* ifdef HOST_IS_LINUX */
1374#else
1375
1376/********************************************************
1377 *
1378 * --- BSD ---
1379 *
1380 ********************************************************/
1381
1382#include <err.h>
1383#include <kvm.h>
1384#include <nlist.h>
1385
1386/* not OpenBSD */
1387#if defined(HOST_IS_FREEBSD)
1388#include <sys/sysent.h>
1389#endif
1390
1391#include <sys/syscall.h>
1392#ifndef SYS_MAXSYSCALL
1393#define SYS_MAXSYSCALL 512
1394#endif
1395
1396#ifdef __OpenBSD__
1397struct proc;
1398struct sysent {
1399 short sy_narg;
1400 short sy_argsize;
1401 int (*sy_call)(struct proc *, void *, register_t *);
1402};
1403#endif
1404
1405int sh_kern_data_init ()
1406{
1407 unsigned long store0 = 0;
1408 unsigned int store1 = 0, store2 = 0;
1409 int datasize, i;
1410 char * databuf = NULL;
1411
1412 /* syscall address and code
1413 */
1414 for (i = 0; i < SH_MAXCALLS; ++i)
1415 {
1416 databuf = sh_kern_db_syscall (i, _("syscall"),
1417 NULL, &store0, &store1, &store2,
1418 &datasize, SH_KERN_DBPOP);
1419 sh_syscalls[i].addr = store0;
1420 if (databuf != NULL) { SH_FREE(databuf); }
1421 if (store0 == 0) {
1422 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1423 _("syscall address not found in database"),
1424 _("sh_kern_data_init"));
1425 return -1;
1426 }
1427
1428 sh_syscalls[i].code[0] = (unsigned int) store1;
1429 sh_syscalls[i].code[1] = (unsigned int) store2;
1430 if ((store1 == 0) || (store2 == 0)) {
1431 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1432 _("syscall code not found in database"),
1433 _("sh_kern_data_init"));
1434 return -1;
1435 }
1436
1437 }
1438
1439 return 0;
1440}
1441
1442int sh_kern_check_internal ()
1443{
1444 struct sysent sy;
1445 kvm_t * kd;
1446 int i;
1447 int status = -1;
1448 char errbuf[_POSIX2_LINE_MAX+1];
1449 struct nlist * sys_list;
1450 struct nlist list[2];
1451
1452 unsigned long offset = 0L;
1453 unsigned int syscall_code[2]; /* 8 bytes */
1454 unsigned long syscall_addr;
1455
1456 unsigned long store0 = 0;
1457 unsigned int store1 = 0, store2 = 0;
1458
1459 UINT64 size_old = 0, size_new = 0;
1460 UINT64 mtime_old = 0, mtime_new = 0;
1461 UINT64 ctime_old = 0, ctime_new = 0;
1462 char tmp[128];
1463 char msg[2*SH_BUFSIZE];
1464 char timstr_o[32];
1465 char timstr_n[32];
1466
1467 static int is_init = 0;
1468
1469 SL_ENTER(_("sh_kern_check_internal"));
1470
1471 if (is_init == 0)
1472 {
1473 if (sh.flag.checkSum != SH_CHECK_INIT && sh.flag.update != S_TRUE)
1474 {
1475 if (0 == sh_kern_data_init()) {
1476 is_init = 1;
1477 } else {
1478 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status,
1479 MSG_E_SUBGEN,
1480 _("could not initialize - switching off"),
1481 _("kern_check_internal") );
1482 ShKernActive = S_FALSE;
1483 SL_RETURN( (-1), _("sh_kern_check_internal"));
1484 }
1485 }
1486 else if ((sh.flag.checkSum == SH_CHECK_INIT ||
1487 sh.flag.checkSum == SH_CHECK_CHECK) &&
1488 (sh.flag.update == S_TRUE))
1489 {
1490 if (0 == sh_kern_data_init()) {
1491 is_init = 1;
1492 } else {
1493 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status,
1494 MSG_E_SUBGEN,
1495 _("no or incomplete data in baseline database"),
1496 _("kern_check_internal") );
1497 }
1498 }
1499 }
1500
1501 /* defined, but not used
1502 */
1503 ShKernDelay = 0;
1504
1505 list[0].n_name = "_sysent";
1506 list[1].n_name = NULL;
1507
1508 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
1509 if (!kd)
1510 {
1511 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1512 errbuf,
1513 _("kvm_openfiles") );
1514 SL_RETURN( (-1), _("sh_kern_check_internal"));
1515 }
1516
1517 i = kvm_nlist(kd, list);
1518 if (i == -1)
1519 {
1520 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1521 kvm_geterr(kd),
1522 _("kvm_nlist (_sysent)") );
1523 kvm_close(kd);
1524 SL_RETURN( (-1), _("sh_kern_check_internal"));
1525 }
1526
1527 sys_list = SH_ALLOC((SYS_MAXSYSCALL+1) * sizeof(struct nlist));
1528
1529 for (i = 0; i < SH_MAXCALLS; ++i)
1530 sys_list[i].n_name = sh_syscalls[i].name;
1531 sys_list[SH_MAXCALLS].n_name = NULL;
1532
1533 i = kvm_nlist(kd, sys_list);
1534 if (i == -1)
1535 {
1536 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1537 kvm_geterr(kd),
1538 _("kvm_nlist (syscalls)") );
1539 kvm_close(kd);
1540 SH_FREE(sys_list);
1541 SL_RETURN( (-1), _("sh_kern_check_internal"));
1542 }
1543 else if (i > 0)
1544 {
1545 sl_snprintf(tmp, 128,
1546 _("%d invalid syscalls"), i);
1547 /*
1548 for (i = 0; i < SH_MAXCALLS; ++i) {
1549 if (sys_list[i].n_type == 0 && sys_list[i].n_value == 0)
1550 fprintf(stderr, "invalid: [%3d] %s\n", i, sh_syscalls[i].name);
1551 }
1552 */
1553 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
1554 tmp,
1555 _("kvm_nlist (syscalls)") );
1556 }
1557
1558 /* Check the individual syscalls
1559 *
1560 * Stored(old) is sh_syscalls[] array.
1561 */
1562 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1563 {
1564 for (i = 0; i < SH_MAXCALLS; ++i)
1565 {
1566 if (sh_syscalls[i].name == NULL)
1567 {
1568 sl_snprintf(tmp, 128,
1569 _("too few entries in sh_syscalls[]: have %d, expect %d"),
1570 i, SH_MAXCALLS);
1571
1572 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1573 tmp,
1574 _("sh_kern_check_internal") );
1575 break;
1576 }
1577
1578 /* read address of syscall from sysent table
1579 */
1580 offset = list[0].n_value + (i*sizeof(struct sysent));
1581 if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
1582 {
1583 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1584 kvm_geterr(kd),
1585 _("kvm_read (syscall table)") );
1586 kvm_close(kd);
1587 SH_FREE(sys_list);
1588 SL_RETURN( (-1), _("sh_kern_check_internal"));
1589 }
1590 syscall_addr = (unsigned long) sy.sy_call;
1591 store0 = syscall_addr;
1592
1593 /* read the syscall code
1594 */
1595 if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]),
1596 2 * sizeof(unsigned int)) < 0)
1597 {
1598 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1599 kvm_geterr(kd),
1600 _("kvm_read (syscall code)") );
1601 kvm_close(kd);
1602 SH_FREE(sys_list);
1603 SL_RETURN( (-1), _("sh_kern_check_internal"));
1604 }
1605 store1 = syscall_code[0]; store2 = syscall_code[1];
1606
1607 sh_kern_db_syscall (i, _("syscall"),
1608 NULL, &store0, &store1, &store2,
1609 0, SH_KERN_DBPUSH);
1610 }
1611 }
1612
1613 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1614 (sh.flag.update == S_TRUE && is_init == 1))
1615 {
1616 for (i = 0; i < SH_MAXCALLS; ++i)
1617 {
1618 if (sh_syscalls[i].name == NULL)
1619 {
1620 sl_snprintf(tmp, 128,
1621 _("too few entries in sh_syscalls[]: have %d, expect %d"),
1622 i, SH_MAXCALLS);
1623
1624 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1625 tmp,
1626 _("sh_kern_check_internal") );
1627 break;
1628 }
1629
1630 /* read address of syscall from sysent table
1631 */
1632 offset = list[0].n_value + (i*sizeof(struct sysent));
1633 if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
1634 {
1635 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1636 kvm_geterr(kd),
1637 _("kvm_read (syscall table)") );
1638 kvm_close(kd);
1639 SH_FREE(sys_list);
1640 SL_RETURN( (-1), _("sh_kern_check_internal"));
1641 }
1642 syscall_addr = (unsigned long) sy.sy_call;
1643
1644 if (sh_syscalls[i].addr != syscall_addr)
1645 {
1646#ifdef SH_USE_XML
1647 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1648 _("syscall"), i);
1649#else
1650 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1651 _("syscall"), i);
1652#endif
1653 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1654
1655 size_old = sh_syscalls[i].addr;
1656 size_new = syscall_addr;
1657 sl_snprintf(tmp, 128, sh_hash_size_format(),
1658 size_old, size_new);
1659 sl_strlcat(msg, tmp, SH_BUFSIZE);
1660
1661 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1662 status, MSG_KERN_SYSCALL,
1663 i, _(sh_syscalls[i].name),
1664 msg);
1665 sh_syscalls[i].addr = syscall_addr;
1666 }
1667 else
1668 {
1669 /* read the syscall code
1670 */
1671 if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]),
1672 2 * sizeof(unsigned int)) < 0)
1673 {
1674 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1675 kvm_geterr(kd),
1676 _("kvm_read (syscall code)") );
1677 kvm_close(kd);
1678 SH_FREE(sys_list);
1679 SL_RETURN( (-1), _("sh_kern_check_internal"));
1680 }
1681
1682 if (sh_syscalls[i].code[0] != syscall_code[0] ||
1683 sh_syscalls[i].code[1] != syscall_code[1])
1684 {
1685 mtime_old = sh_syscalls[i].code[0];
1686 mtime_new = syscall_code[0];
1687 ctime_old = sh_syscalls[i].code[1];
1688 ctime_new = syscall_code[1];
1689
1690#ifdef SH_USE_XML
1691 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1692 _("syscall"), i);
1693#else
1694 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1695 _("syscall"), i);
1696#endif
1697 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1698
1699 (void) sh_unix_gmttime (ctime_old, timstr_o, sizeof(timstr_o));
1700 (void) sh_unix_gmttime (ctime_new, timstr_n, sizeof(timstr_n));
1701#ifdef SH_USE_XML
1702 sl_snprintf(tmp, 128,
1703 _("ctime_old=\"%s\" ctime_new=\"%s\" "),
1704 timstr_o, timstr_n);
1705#else
1706 sl_snprintf(tmp, 128,
1707 _("ctime_old=<%s>, ctime_new=<%s>, "),
1708 timstr_o, timstr_n);
1709#endif
1710 sl_strlcat(msg, tmp, SH_BUFSIZE);
1711 (void) sh_unix_gmttime (mtime_old, timstr_o, sizeof(timstr_o));
1712 (void) sh_unix_gmttime (mtime_new, timstr_n, sizeof(timstr_n));
1713#ifdef SH_USE_XML
1714 sl_snprintf(tmp, 128,
1715 _("mtime_old=\"%s\" mtime_new=\"%s\" "),
1716 timstr_o, timstr_n);
1717#else
1718 sl_snprintf(tmp, 128,
1719 _("mtime_old=<%s>, mtime_new=<%s> "),
1720 timstr_o, timstr_n);
1721#endif
1722 sl_strlcat(msg, tmp, SH_BUFSIZE);
1723
1724 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1725 status, MSG_KERN_SYSCALL,
1726 i, _(sh_syscalls[i].name),
1727 msg);
1728 sh_syscalls[i].code[0] = syscall_code[0];
1729 sh_syscalls[i].code[1] = syscall_code[1];
1730 }
1731 }
1732 }
1733 }
1734 SH_FREE(sys_list);
1735 if(kvm_close(kd) < 0)
1736 {
1737 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1738 kvm_geterr(kd),
1739 _("kvm_close") );
1740 exit(EXIT_FAILURE);
1741 }
1742
1743 SL_RETURN( (0), _("sh_kern_check_internal"));
1744}
1745
1746#endif
1747
1748/*************
1749 *
1750 * module init
1751 *
1752 *************/
1753#if defined(HOST_IS_LINUX)
1754#include <sys/utsname.h>
1755#endif
1756
1757static int AddressReconf = 0;
1758
1759int sh_kern_init (struct mod_type * arg)
1760{
1761#if defined(HOST_IS_LINUX)
1762 struct utsname buf;
1763 char * str;
1764#endif
1765 (void) arg;
1766
1767 SL_ENTER(_("sh_kern_init"));
1768 if (ShKernActive == S_FALSE)
1769 SL_RETURN( (-1), _("sh_kern_init"));
1770
1771#if defined(HOST_IS_LINUX)
1772 uname(&buf);
1773
1774 if ((AddressReconf < 5) && (0 != strcmp(SH_KERNEL_VERSION, buf.release)))
1775 {
1776 str = SH_ALLOC(256);
1777 sl_snprintf(str, 256,
1778 "Compiled for kernel %s, but current kernel is %s, and kernel addresses have not been re-configured",
1779 SH_KERNEL_VERSION, buf.release);
1780 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
1781 str,
1782 _("kern_check") );
1783 SH_FREE(str);
1784 ShKernActive = S_FALSE;
1785 SL_RETURN( (-1), _("sh_kern_init"));
1786 }
1787#endif
1788
1789 lastcheck = time (NULL);
1790 if (sh.flag.checkSum != SH_CHECK_INIT)
1791 {
1792 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1793 _("Checking kernel syscalls"),
1794 _("kern_check") );
1795 }
1796 sh_kern_check_internal ();
1797 SL_RETURN( (0), _("sh_kern_init"));
1798}
1799
1800/*************
1801 *
1802 * module cleanup
1803 *
1804 *************/
1805int sh_kern_end ()
1806{
1807 return (0);
1808}
1809
1810
1811/*************
1812 *
1813 * module timer
1814 *
1815 *************/
1816int sh_kern_timer (time_t tcurrent)
1817{
1818 if (ShKernActive == S_FALSE)
1819 return 0;
1820
1821 if ((int) (tcurrent - lastcheck) >= ShKernInterval)
1822 {
1823 lastcheck = tcurrent;
1824 return (-1);
1825 }
1826 return 0;
1827}
1828
1829/*************
1830 *
1831 * module check
1832 *
1833 *************/
1834int sh_kern_check ()
1835{
1836 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
1837 _("Checking kernel syscalls"),
1838 _("kern_check") );
1839 return (sh_kern_check_internal ());
1840}
1841
1842/*************
1843 *
1844 * module setup
1845 *
1846 *************/
1847
1848int sh_kern_set_severity (const char * c)
1849{
1850 char tmp[32];
1851 tmp[0] = '='; tmp[1] = '\0';
1852 sl_strlcat (tmp, c, 32);
1853 sh_error_set_level (tmp, &ShKernSeverity);
1854 return 0;
1855}
1856
1857int sh_kern_set_timer (const char * c)
1858{
1859 long val;
1860
1861 SL_ENTER(_("sh_kern_set_timer"));
1862
1863 val = strtol (c, (char **)NULL, 10);
1864 if (val <= 0)
1865 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
1866 _("kern timer"), c);
1867
1868 val = (val <= 0 ? 60 : val);
1869
1870 ShKernInterval = (time_t) val;
1871 SL_RETURN( 0, _("sh_kern_set_timer"));
1872}
1873
1874int sh_kern_set_activate (const char * c)
1875{
1876 int i;
1877 SL_ENTER(_("sh_kern_set_activate"));
1878 i = sh_util_flagval(c, &ShKernActive);
1879 SL_RETURN(i, _("sh_kern_set_activate"));
1880}
1881
1882int sh_kern_set_idt (const char * c)
1883{
1884 int i;
1885 SL_ENTER(_("sh_kern_set_idt"));
1886 i = sh_util_flagval(c, &ShKernIDT);
1887 SL_RETURN(i, _("sh_kern_set_idt"));
1888}
1889
1890int sh_kern_set_sc_addr (const char * c)
1891{
1892 char * endptr;
1893 unsigned long value;
1894
1895 SL_ENTER(_("sh_kern_set_sc_addr"));
1896 errno = 0;
1897 value = strtoul(c, &endptr, 16);
1898 if ((ULONG_MAX == value) && (errno == ERANGE))
1899 {
1900 SL_RETURN((-1), _("sh_kern_set_sc_addr"));
1901 }
1902 if ((*c == '\0') || (*endptr != '\0'))
1903 {
1904 SL_RETURN((-1), _("sh_kern_set_sc_addr"));
1905 }
1906 system_call_addr = value;
1907 ++AddressReconf;
1908 SL_RETURN((0), _("sh_kern_set_sc_addr"));
1909}
1910
1911int sh_kern_set_sct_addr (const char * c)
1912{
1913 char * endptr;
1914 unsigned long value;
1915
1916 SL_ENTER(_("sh_kern_set_sct_addr"));
1917 errno = 0;
1918 value = strtoul(c, &endptr, 16);
1919 if ((ULONG_MAX == value) && (errno == ERANGE))
1920 {
1921 SL_RETURN((-1), _("sh_kern_set_sct_addr"));
1922 }
1923 if ((*c == '\0') || (*endptr != '\0'))
1924 {
1925 SL_RETURN((-1), _("sh_kern_set_sct_addr"));
1926 }
1927 kaddr = (unsigned int) value;
1928 ++AddressReconf;
1929 SL_RETURN((0), _("sh_kern_set_sct_addr"));
1930}
1931
1932int sh_kern_set_proc_root (const char * c)
1933{
1934 char * endptr;
1935 unsigned long value;
1936
1937 SL_ENTER(_("sh_kern_set_proc_root"));
1938 errno = 0;
1939 value = strtoul(c, &endptr, 16);
1940 if ((ULONG_MAX == value) && (errno == ERANGE))
1941 {
1942 SL_RETURN((-1), _("sh_kern_set_proc_root"));
1943 }
1944 if ((*c == '\0') || (*endptr != '\0'))
1945 {
1946 SL_RETURN((-1), _("sh_kern_set_proc_root"));
1947 }
1948
1949 proc_root = value;
1950 ++AddressReconf;
1951 SL_RETURN((0), _("sh_kern_set_proc_root"));
1952}
1953
1954int sh_kern_set_proc_root_iops (const char * c)
1955{
1956 char * endptr;
1957 unsigned long value;
1958
1959 SL_ENTER(_("sh_kern_set_proc_root_iops"));
1960 errno = 0;
1961 value = strtoul(c, &endptr, 16);
1962 if ((ULONG_MAX == value) && (errno == ERANGE))
1963 {
1964 SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
1965 }
1966 if ((*c == '\0') || (*endptr != '\0'))
1967 {
1968 SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
1969 }
1970
1971 proc_root_iops = value;
1972 ++AddressReconf;
1973 SL_RETURN((0), _("sh_kern_set_proc_root_iops"));
1974}
1975
1976int sh_kern_set_proc_root_lookup (const char * c)
1977{
1978 char * endptr;
1979 unsigned long value;
1980
1981 SL_ENTER(_("sh_kern_set_proc_root_lookup"));
1982 errno = 0;
1983 value = strtoul(c, &endptr, 16);
1984 if ((ULONG_MAX == value) && (errno == ERANGE))
1985 {
1986 SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
1987 }
1988 if ((*c == '\0') || (*endptr != '\0'))
1989 {
1990 SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
1991 }
1992 proc_root_lookup = value;
1993 ++AddressReconf;
1994 SL_RETURN((0), _("sh_kern_set_proc_root_lookup"));
1995}
1996
1997#endif
1998
1999/* #ifdef SH_USE_KERN */
2000#endif
Note: See TracBrowser for help on using the repository browser.