source: trunk/src/sh_kern.c@ 141

Last change on this file since 141 was 140, checked in by rainer, 17 years ago

Utility function for threaded modules.

File size: 48.1 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 SH_MUTEX_LOCK(mutex_readdir);
971
972 while (NULL != (entry = readdir(df)))
973 {
974 if (0 == strcmp(entry->d_name, ".") &&
975 0 == strcmp(entry->d_name, ".."))
976 continue;
977
978 pcipath = sh_util_strconcat(pci_dir, "/",
979 entry->d_name, "/rom", NULL);
980 check_pci_rom(pcipath, entry->d_name);
981 SH_FREE(pcipath);
982 }
983
984 SH_MUTEX_UNLOCK(mutex_readdir);
985
986 closedir(df);
987 }
988}
989
990/* -- Check the proc_root inode.
991 *
992 * This will detect adore-ng.
993 */
994static void check_proc_root (struct sh_kernel_info * kinfo)
995{
996 struct proc_dir_entry proc_root_dir;
997
998/* 2.6.21 (((2) << 16) + ((6) << 8) + (21)) */
999#if SH_KERNEL_NUMBER < 132629
1000 struct inode_operations proc_root_inode;
1001
1002 memcpy (&proc_root_inode, &(kinfo->proc_root_inode), sizeof(struct inode_operations));
1003
1004 /* Seems that the info does not relate anymore to proc_root_lookup(?)
1005 */
1006 if ( (unsigned int) *proc_root_inode.lookup != proc_root_lookup)
1007 {
1008 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
1009 _("proc_root_inode_operations.lookup != proc_root_lookup"));
1010 }
1011#endif
1012
1013 memcpy (&proc_root_dir, &(kinfo->proc_root_dir), sizeof(struct proc_dir_entry));
1014 if ( (((unsigned int) * &proc_root_dir.proc_iops) != proc_root_iops)
1015 && (proc_root_dir.size != proc_root_iops)
1016 && (((unsigned int) * &proc_root_dir.proc_fops) != proc_root_iops)
1017 )
1018 {
1019 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
1020 _("proc_root.proc_iops != proc_root_inode_operations"));
1021 }
1022
1023 return;
1024}
1025
1026/* -- Check the system_call syscall gate.
1027 *
1028 * Stored(old) is system_call_code[]
1029 */
1030static void check_syscall_gate(int is_init, struct sh_kernel_info * kinfo)
1031{
1032 int i, j;
1033 unsigned long store0;
1034 unsigned int store1, store2;
1035 int datasize;
1036 int max_system_call = (SYS_CALL_LOC < 128) ? 128 : SYS_CALL_LOC;
1037 char msg[2*SH_BUFSIZE];
1038
1039 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1040 {
1041 store0 = 0; store1 = 0; store2 = 0;
1042 datasize = SH_KERN_SCC;
1043 sh_kern_db_syscall (0, _("system_call"),
1044 &(kinfo->new_system_call_code), &store0, &store1, &store2,
1045 &datasize, SH_KERN_DBPUSH);
1046 }
1047
1048 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1049 (sh.flag.update == S_TRUE && is_init == 1))
1050 {
1051 for (i = 0; i < (max_system_call + 4); ++i)
1052 {
1053 if (system_call_code[i] != kinfo->new_system_call_code[i])
1054 {
1055
1056 sh_kern_pathmsg (msg, sizeof(msg),
1057 0, _("system_call"),
1058 system_call_code, SH_KERN_SCC,
1059 kinfo->new_system_call_code, SH_KERN_SCC);
1060
1061 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1062 0, MSG_KERN_GATE,
1063 kinfo->new_system_call_code[i], 0,
1064 system_call_code[i], 0,
1065 0, _("system_call (interrupt handler)"),
1066 msg);
1067
1068 for (j = 0; j < (max_system_call + 4); ++j)
1069 system_call_code[j] = kinfo->new_system_call_code[j];
1070 break;
1071 }
1072 }
1073 }
1074 return;
1075}
1076
1077static void check_system_calls (int is_init, struct sh_kernel_info * kinfo)
1078{
1079 int i;
1080
1081#ifdef SH_USE_LKM
1082 static int check_getdents = 0;
1083 /* #ifdef __NR_getdents64 */
1084 static int check_getdents64 = 0;
1085 /* #endif */
1086 static int copy_if_next = -1;
1087 static int copy_if_next_64 = -1;
1088#endif
1089
1090 unsigned long store0;
1091 unsigned int store1, store2;
1092 int mod_syscall_addr = 0;
1093 int mod_syscall_code = 0;
1094 UINT64 size_old = 0, size_new = 0;
1095 UINT64 mtime_old = 0, mtime_new = 0;
1096 UINT64 ctime_old = 0, ctime_new = 0;
1097 char tmp[128];
1098 char msg[2*SH_BUFSIZE];
1099 char timstr_o[32];
1100 char timstr_n[32];
1101
1102 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1103 {
1104 for (i = 0; i < SH_MAXCALLS; ++i)
1105 {
1106 store0 = kinfo->kmem_call_table[i];
1107 store1 = kinfo->kmem_code_table[i][0]; store2 = kinfo->kmem_code_table[i][1];
1108 sh_kern_db_syscall (i, _("syscall"),
1109 NULL, &store0, &store1, &store2,
1110 0, SH_KERN_DBPUSH);
1111 }
1112 }
1113
1114 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1115 (sh.flag.update == S_TRUE && is_init == 1))
1116 {
1117 for (i = 0; i < SH_MAXCALLS; ++i)
1118 {
1119 if (sh_syscalls[i].name == NULL /* || sh_syscalls[i].addr == 0UL */)
1120 break;
1121
1122#ifdef SH_USE_LKM
1123 if (sh_syscalls[i].addr != kinfo->kmem_call_table[i])
1124 {
1125 if (check_getdents == 0 &&
1126 0 == strcmp(_(sh_syscalls[i].name), _("sys_getdents")))
1127 {
1128 check_getdents = 1;
1129 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__,
1130 0, MSG_E_SUBGEN,
1131 _("Modified kernel syscall (expected)."),
1132 _(sh_syscalls[i].name) );
1133 copy_if_next = i;
1134 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1135 continue;
1136 }
1137 /* #ifdef __NR_getdents64 */
1138 else if (check_getdents64 == 0 &&
1139 0 == strcmp(_(sh_syscalls[i].name),
1140 _("sys_getdents64")))
1141 {
1142 check_getdents64 = 1;
1143 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__,
1144 0, MSG_E_SUBGEN,
1145 _("Modified kernel syscall (expected)."),
1146 _(sh_syscalls[i].name) );
1147 copy_if_next_64 = i;
1148 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1149 continue;
1150 }
1151 /* #endif */
1152 else
1153 {
1154 size_old = sh_syscalls[i].addr;
1155 size_new = kinfo->kmem_call_table[i];
1156 mod_syscall_addr = 1;
1157 }
1158 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1159 }
1160#else
1161 if (sh_syscalls[i].addr != kinfo->kmem_call_table[i])
1162 {
1163 size_old = sh_syscalls[i].addr;
1164 size_new = kinfo->kmem_call_table[i];
1165 mod_syscall_addr = 1;
1166 sh_syscalls[i].addr = kinfo->kmem_call_table[i];
1167 }
1168#endif
1169
1170
1171 /* -- Check the code at syscall address
1172 *
1173 * Stored(old) is sh_syscalls[]
1174 */
1175 if ( (mod_syscall_addr == 0) &&
1176 ((sh_syscalls[i].code[0] != kinfo->kmem_code_table[i][0]) ||
1177 (sh_syscalls[i].code[1] != kinfo->kmem_code_table[i][1]))
1178 )
1179 {
1180 mtime_old = sh_syscalls[i].code[0];
1181 mtime_new = kinfo->kmem_code_table[i][0];
1182 ctime_old = sh_syscalls[i].code[1];
1183 ctime_new = kinfo->kmem_code_table[i][1];
1184 mod_syscall_code = 1;
1185
1186#ifdef SH_USE_LKM
1187 if (i == copy_if_next)
1188 {
1189 mod_syscall_code = 0;
1190 copy_if_next = -1;
1191 }
1192 if (i == copy_if_next_64)
1193 {
1194 mod_syscall_code = 0;
1195 copy_if_next_64 = -1;
1196 }
1197#endif
1198
1199 sh_syscalls[i].code[0] = kinfo->kmem_code_table[i][0];
1200 sh_syscalls[i].code[1] = kinfo->kmem_code_table[i][1];
1201 }
1202
1203 /* Build the error message, if something has been
1204 * detected.
1205 */
1206 if ((mod_syscall_addr != 0) || (mod_syscall_code != 0))
1207 {
1208#ifdef SH_USE_XML
1209 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1210 _("syscall"), i);
1211#else
1212 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1213 _("syscall"), i);
1214#endif
1215 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1216
1217 if (mod_syscall_addr != 0)
1218 {
1219 sl_snprintf(tmp, 128, sh_hash_size_format(),
1220 size_old, size_new);
1221 sl_strlcat(msg, tmp, SH_BUFSIZE);
1222 }
1223 if (mod_syscall_code != 0)
1224 {
1225 (void) sh_unix_gmttime (ctime_old, timstr_o, sizeof(timstr_o));
1226 (void) sh_unix_gmttime (ctime_new, timstr_n, sizeof(timstr_n));
1227#ifdef SH_USE_XML
1228 sl_snprintf(tmp, 128,
1229 _("ctime_old=\"%s\" ctime_new=\"%s\" "),
1230 timstr_o, timstr_n);
1231#else
1232 sl_snprintf(tmp, 128,
1233 _("ctime_old=<%s>, ctime_new=<%s>, "),
1234 timstr_o, timstr_n);
1235#endif
1236 sl_strlcat(msg, tmp, SH_BUFSIZE);
1237 (void) sh_unix_gmttime (mtime_old, timstr_o, sizeof(timstr_o));
1238 (void) sh_unix_gmttime (mtime_new, timstr_n, sizeof(timstr_n));
1239#ifdef SH_USE_XML
1240 sl_snprintf(tmp, 128,
1241 _("mtime_old=\"%s\" mtime_new=\"%s\" "),
1242 timstr_o, timstr_n);
1243#else
1244 sl_snprintf(tmp, 128,
1245 _("mtime_old=<%s>, mtime_new=<%s> "),
1246 timstr_o, timstr_n);
1247#endif
1248 sl_strlcat(msg, tmp, SH_BUFSIZE);
1249 }
1250 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1251 0, MSG_KERN_SYSCALL,
1252 i, _(sh_syscalls[i].name), msg);
1253 mod_syscall_addr = 0;
1254 mod_syscall_code = 0;
1255 }
1256 }
1257 }
1258 return;
1259}
1260
1261int sh_kern_check_internal ()
1262{
1263 int kd;
1264 int is_init;
1265 pid_t mpid;
1266 int mpipe[2];
1267 int status = 0;
1268
1269 struct sh_kernel_info kinfo;
1270
1271
1272 SL_ENTER(_("sh_kern_check_internal"));
1273
1274 /* -- Check whether initialisation is required; if yes, initialize.
1275 */
1276
1277 if (0 != check_init(&is_init))
1278 {
1279 SL_RETURN( (-1), _("sh_kern_check_internal"));
1280 }
1281
1282
1283 /* -- Open /dev/kmem and fork subprocess to read from it.
1284 */
1285
1286 if (kaddr == (unsigned int) -1) /* kaddr = address of the sys_call_table */
1287 {
1288 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, MSG_E_SUBGEN,
1289 _("no address for sys_call_table - switching off"),
1290 _("kern_check_internal") );
1291 ShKernActive = S_FALSE;
1292 SL_RETURN( (-1), _("sh_kern_check_internal"));
1293 }
1294
1295 kd = aud_open(FIL__, __LINE__, SL_YESPRIV, _("/dev/kmem"), O_RDONLY, 0);
1296
1297 if (kd < 0)
1298 {
1299 status = errno;
1300 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1301 _("error opening /dev/kmem"),
1302 _("kern_check_internal") );
1303 SL_RETURN( (-1), _("sh_kern_check_internal"));
1304 }
1305
1306 status = aud_pipe(FIL__, __LINE__, mpipe);
1307
1308 if (status == 0)
1309 {
1310 mpid = aud_fork(FIL__, __LINE__);
1311
1312 switch (mpid)
1313 {
1314 case -1:
1315 status = -1;
1316 break;
1317 case 0:
1318
1319 /* -- Child process reads /dev/kmem and writes to pipe
1320 */
1321 run_child(kd, mpipe);
1322 break;
1323
1324 /* -- Parent process reads from child via pipe
1325 */
1326 default:
1327 close(kd);
1328 status = read_from_child(mpid, mpipe, &kinfo);
1329 break;
1330 }
1331 }
1332
1333 if ( status < 0)
1334 {
1335 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1336 _("error reading from /dev/kmem"),
1337 _("kern_check_internal") );
1338 SL_RETURN( (-1), _("sh_kern_check_internal"));
1339 }
1340
1341 /* -- Check the proc_root inode.
1342 *
1343 * This will detect adore-ng.
1344 */
1345 check_proc_root( &kinfo );
1346
1347
1348 /* -- Check the system_call syscall gate.
1349 *
1350 * Stored(old) is system_call_code[]
1351 */
1352 check_syscall_gate( is_init, &kinfo );
1353
1354 /* -- Check the individual syscalls
1355 *
1356 * Stored(old) is sh_syscalls[] array.
1357 */
1358 check_system_calls ( is_init, &kinfo );
1359
1360 /* -- Check the Interrupt Descriptor Table
1361 */
1362 check_idt_table(is_init);
1363
1364 /* -- Check PCI ROM
1365 */
1366 check_pci();
1367
1368 SL_RETURN( (0), _("sh_kern_check_internal"));
1369}
1370/* ifdef HOST_IS_LINUX */
1371#else
1372
1373/********************************************************
1374 *
1375 * --- BSD ---
1376 *
1377 ********************************************************/
1378
1379#include <err.h>
1380#include <kvm.h>
1381#include <nlist.h>
1382
1383/* not OpenBSD */
1384#if defined(HOST_IS_FREEBSD)
1385#include <sys/sysent.h>
1386#endif
1387
1388#include <sys/syscall.h>
1389#ifndef SYS_MAXSYSCALL
1390#define SYS_MAXSYSCALL 512
1391#endif
1392
1393#ifdef __OpenBSD__
1394struct proc;
1395struct sysent {
1396 short sy_narg;
1397 short sy_argsize;
1398 int (*sy_call)(struct proc *, void *, register_t *);
1399};
1400#endif
1401
1402int sh_kern_data_init ()
1403{
1404 unsigned long store0 = 0;
1405 unsigned int store1 = 0, store2 = 0;
1406 int datasize, i;
1407 char * databuf = NULL;
1408
1409 /* syscall address and code
1410 */
1411 for (i = 0; i < SH_MAXCALLS; ++i)
1412 {
1413 databuf = sh_kern_db_syscall (i, _("syscall"),
1414 NULL, &store0, &store1, &store2,
1415 &datasize, SH_KERN_DBPOP);
1416 sh_syscalls[i].addr = store0;
1417 if (databuf != NULL) { SH_FREE(databuf); }
1418 if (store0 == 0) {
1419 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1420 _("syscall address not found in database"),
1421 _("sh_kern_data_init"));
1422 return -1;
1423 }
1424
1425 sh_syscalls[i].code[0] = (unsigned int) store1;
1426 sh_syscalls[i].code[1] = (unsigned int) store2;
1427 if ((store1 == 0) || (store2 == 0)) {
1428 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1429 _("syscall code not found in database"),
1430 _("sh_kern_data_init"));
1431 return -1;
1432 }
1433
1434 }
1435
1436 return 0;
1437}
1438
1439int sh_kern_check_internal ()
1440{
1441 struct sysent sy;
1442 kvm_t * kd;
1443 int i;
1444 int status = -1;
1445 char errbuf[_POSIX2_LINE_MAX+1];
1446 struct nlist * sys_list;
1447 struct nlist list[2];
1448
1449 unsigned long offset = 0L;
1450 unsigned int syscall_code[2]; /* 8 bytes */
1451 unsigned long syscall_addr;
1452
1453 unsigned long store0 = 0;
1454 unsigned int store1 = 0, store2 = 0;
1455
1456 UINT64 size_old = 0, size_new = 0;
1457 UINT64 mtime_old = 0, mtime_new = 0;
1458 UINT64 ctime_old = 0, ctime_new = 0;
1459 char tmp[128];
1460 char msg[2*SH_BUFSIZE];
1461 char timstr_o[32];
1462 char timstr_n[32];
1463
1464 static int is_init = 0;
1465
1466 SL_ENTER(_("sh_kern_check_internal"));
1467
1468 if (is_init == 0)
1469 {
1470 if (sh.flag.checkSum != SH_CHECK_INIT && sh.flag.update != S_TRUE)
1471 {
1472 if (0 == sh_kern_data_init()) {
1473 is_init = 1;
1474 } else {
1475 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status,
1476 MSG_E_SUBGEN,
1477 _("could not initialize - switching off"),
1478 _("kern_check_internal") );
1479 ShKernActive = S_FALSE;
1480 SL_RETURN( (-1), _("sh_kern_check_internal"));
1481 }
1482 }
1483 else if ((sh.flag.checkSum == SH_CHECK_INIT ||
1484 sh.flag.checkSum == SH_CHECK_CHECK) &&
1485 (sh.flag.update == S_TRUE))
1486 {
1487 if (0 == sh_kern_data_init()) {
1488 is_init = 1;
1489 } else {
1490 sh_error_handle (ShKernSeverity, FIL__, __LINE__, status,
1491 MSG_E_SUBGEN,
1492 _("no or incomplete data in baseline database"),
1493 _("kern_check_internal") );
1494 }
1495 }
1496 }
1497
1498 /* defined, but not used
1499 */
1500 ShKernDelay = 0;
1501
1502 list[0].n_name = "_sysent";
1503 list[1].n_name = NULL;
1504
1505 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
1506 if (!kd)
1507 {
1508 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1509 errbuf,
1510 _("kvm_openfiles") );
1511 SL_RETURN( (-1), _("sh_kern_check_internal"));
1512 }
1513
1514 i = kvm_nlist(kd, list);
1515 if (i == -1)
1516 {
1517 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1518 kvm_geterr(kd),
1519 _("kvm_nlist (_sysent)") );
1520 kvm_close(kd);
1521 SL_RETURN( (-1), _("sh_kern_check_internal"));
1522 }
1523
1524 sys_list = SH_ALLOC((SYS_MAXSYSCALL+1) * sizeof(struct nlist));
1525
1526 for (i = 0; i < SH_MAXCALLS; ++i)
1527 sys_list[i].n_name = sh_syscalls[i].name;
1528 sys_list[SH_MAXCALLS].n_name = NULL;
1529
1530 i = kvm_nlist(kd, sys_list);
1531 if (i == -1)
1532 {
1533 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1534 kvm_geterr(kd),
1535 _("kvm_nlist (syscalls)") );
1536 kvm_close(kd);
1537 SH_FREE(sys_list);
1538 SL_RETURN( (-1), _("sh_kern_check_internal"));
1539 }
1540 else if (i > 0)
1541 {
1542 sl_snprintf(tmp, 128,
1543 _("%d invalid syscalls"), i);
1544 /*
1545 for (i = 0; i < SH_MAXCALLS; ++i) {
1546 if (sys_list[i].n_type == 0 && sys_list[i].n_value == 0)
1547 fprintf(stderr, "invalid: [%3d] %s\n", i, sh_syscalls[i].name);
1548 }
1549 */
1550 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
1551 tmp,
1552 _("kvm_nlist (syscalls)") );
1553 }
1554
1555 /* Check the individual syscalls
1556 *
1557 * Stored(old) is sh_syscalls[] array.
1558 */
1559 if (sh.flag.checkSum == SH_CHECK_INIT || sh.flag.update == S_TRUE)
1560 {
1561 for (i = 0; i < SH_MAXCALLS; ++i)
1562 {
1563 if (sh_syscalls[i].name == NULL)
1564 {
1565 sl_snprintf(tmp, 128,
1566 _("too few entries in sh_syscalls[]: have %d, expect %d"),
1567 i, SH_MAXCALLS);
1568
1569 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1570 tmp,
1571 _("sh_kern_check_internal") );
1572 break;
1573 }
1574
1575 /* read address of syscall from sysent table
1576 */
1577 offset = list[0].n_value + (i*sizeof(struct sysent));
1578 if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
1579 {
1580 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1581 kvm_geterr(kd),
1582 _("kvm_read (syscall table)") );
1583 kvm_close(kd);
1584 SH_FREE(sys_list);
1585 SL_RETURN( (-1), _("sh_kern_check_internal"));
1586 }
1587 syscall_addr = (unsigned long) sy.sy_call;
1588 store0 = syscall_addr;
1589
1590 /* read the syscall code
1591 */
1592 if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]),
1593 2 * sizeof(unsigned int)) < 0)
1594 {
1595 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1596 kvm_geterr(kd),
1597 _("kvm_read (syscall code)") );
1598 kvm_close(kd);
1599 SH_FREE(sys_list);
1600 SL_RETURN( (-1), _("sh_kern_check_internal"));
1601 }
1602 store1 = syscall_code[0]; store2 = syscall_code[1];
1603
1604 sh_kern_db_syscall (i, _("syscall"),
1605 NULL, &store0, &store1, &store2,
1606 0, SH_KERN_DBPUSH);
1607 }
1608 }
1609
1610 if ((sh.flag.checkSum != SH_CHECK_INIT) ||
1611 (sh.flag.update == S_TRUE && is_init == 1))
1612 {
1613 for (i = 0; i < SH_MAXCALLS; ++i)
1614 {
1615 if (sh_syscalls[i].name == NULL)
1616 {
1617 sl_snprintf(tmp, 128,
1618 _("too few entries in sh_syscalls[]: have %d, expect %d"),
1619 i, SH_MAXCALLS);
1620
1621 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1622 tmp,
1623 _("sh_kern_check_internal") );
1624 break;
1625 }
1626
1627 /* read address of syscall from sysent table
1628 */
1629 offset = list[0].n_value + (i*sizeof(struct sysent));
1630 if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
1631 {
1632 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1633 kvm_geterr(kd),
1634 _("kvm_read (syscall table)") );
1635 kvm_close(kd);
1636 SH_FREE(sys_list);
1637 SL_RETURN( (-1), _("sh_kern_check_internal"));
1638 }
1639 syscall_addr = (unsigned long) sy.sy_call;
1640
1641 if (sh_syscalls[i].addr != syscall_addr)
1642 {
1643#ifdef SH_USE_XML
1644 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1645 _("syscall"), i);
1646#else
1647 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1648 _("syscall"), i);
1649#endif
1650 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1651
1652 size_old = sh_syscalls[i].addr;
1653 size_new = syscall_addr;
1654 sl_snprintf(tmp, 128, sh_hash_size_format(),
1655 size_old, size_new);
1656 sl_strlcat(msg, tmp, SH_BUFSIZE);
1657
1658 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1659 status, MSG_KERN_SYSCALL,
1660 i, _(sh_syscalls[i].name),
1661 msg);
1662 sh_syscalls[i].addr = syscall_addr;
1663 }
1664 else
1665 {
1666 /* read the syscall code
1667 */
1668 if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]),
1669 2 * sizeof(unsigned int)) < 0)
1670 {
1671 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1672 kvm_geterr(kd),
1673 _("kvm_read (syscall code)") );
1674 kvm_close(kd);
1675 SH_FREE(sys_list);
1676 SL_RETURN( (-1), _("sh_kern_check_internal"));
1677 }
1678
1679 if (sh_syscalls[i].code[0] != syscall_code[0] ||
1680 sh_syscalls[i].code[1] != syscall_code[1])
1681 {
1682 mtime_old = sh_syscalls[i].code[0];
1683 mtime_new = syscall_code[0];
1684 ctime_old = sh_syscalls[i].code[1];
1685 ctime_new = syscall_code[1];
1686
1687#ifdef SH_USE_XML
1688 sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ",
1689 _("syscall"), i);
1690#else
1691 sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ",
1692 _("syscall"), i);
1693#endif
1694 sl_strlcpy(msg, tmp, SH_BUFSIZE);
1695
1696 (void) sh_unix_gmttime (ctime_old, timstr_o, sizeof(timstr_o));
1697 (void) sh_unix_gmttime (ctime_new, timstr_n, sizeof(timstr_n));
1698#ifdef SH_USE_XML
1699 sl_snprintf(tmp, 128,
1700 _("ctime_old=\"%s\" ctime_new=\"%s\" "),
1701 timstr_o, timstr_n);
1702#else
1703 sl_snprintf(tmp, 128,
1704 _("ctime_old=<%s>, ctime_new=<%s>, "),
1705 timstr_o, timstr_n);
1706#endif
1707 sl_strlcat(msg, tmp, SH_BUFSIZE);
1708 (void) sh_unix_gmttime (mtime_old, timstr_o, sizeof(timstr_o));
1709 (void) sh_unix_gmttime (mtime_new, timstr_n, sizeof(timstr_n));
1710#ifdef SH_USE_XML
1711 sl_snprintf(tmp, 128,
1712 _("mtime_old=\"%s\" mtime_new=\"%s\" "),
1713 timstr_o, timstr_n);
1714#else
1715 sl_snprintf(tmp, 128,
1716 _("mtime_old=<%s>, mtime_new=<%s> "),
1717 timstr_o, timstr_n);
1718#endif
1719 sl_strlcat(msg, tmp, SH_BUFSIZE);
1720
1721 sh_error_handle (ShKernSeverity, FIL__, __LINE__,
1722 status, MSG_KERN_SYSCALL,
1723 i, _(sh_syscalls[i].name),
1724 msg);
1725 sh_syscalls[i].code[0] = syscall_code[0];
1726 sh_syscalls[i].code[1] = syscall_code[1];
1727 }
1728 }
1729 }
1730 }
1731 SH_FREE(sys_list);
1732 if(kvm_close(kd) < 0)
1733 {
1734 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
1735 kvm_geterr(kd),
1736 _("kvm_close") );
1737 exit(EXIT_FAILURE);
1738 }
1739
1740 SL_RETURN( (0), _("sh_kern_check_internal"));
1741}
1742
1743#endif
1744
1745/*************
1746 *
1747 * module init
1748 *
1749 *************/
1750#if defined(HOST_IS_LINUX)
1751#include <sys/utsname.h>
1752#endif
1753
1754static int AddressReconf = 0;
1755
1756int sh_kern_init (struct mod_type * arg)
1757{
1758#if defined(HOST_IS_LINUX)
1759 struct utsname buf;
1760 char * str;
1761#endif
1762 (void) arg;
1763
1764 SL_ENTER(_("sh_kern_init"));
1765 if (ShKernActive == S_FALSE)
1766 SL_RETURN( (-1), _("sh_kern_init"));
1767
1768#if defined(HOST_IS_LINUX)
1769 uname(&buf);
1770
1771 if ((AddressReconf < 5) && (0 != strcmp(SH_KERNEL_VERSION, buf.release)))
1772 {
1773 str = SH_ALLOC(256);
1774 sl_snprintf(str, 256,
1775 "Compiled for kernel %s, but current kernel is %s, and kernel addresses have not been re-configured",
1776 SH_KERNEL_VERSION, buf.release);
1777 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
1778 str,
1779 _("kern_check") );
1780 SH_FREE(str);
1781 ShKernActive = S_FALSE;
1782 SL_RETURN( (-1), _("sh_kern_init"));
1783 }
1784#endif
1785
1786 lastcheck = time (NULL);
1787 if (sh.flag.checkSum != SH_CHECK_INIT)
1788 {
1789 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1790 _("Checking kernel syscalls"),
1791 _("kern_check") );
1792 }
1793 sh_kern_check_internal ();
1794 SL_RETURN( (0), _("sh_kern_init"));
1795}
1796
1797/*************
1798 *
1799 * module cleanup
1800 *
1801 *************/
1802int sh_kern_end ()
1803{
1804 return (0);
1805}
1806
1807
1808/*************
1809 *
1810 * module timer
1811 *
1812 *************/
1813int sh_kern_timer (time_t tcurrent)
1814{
1815 if (ShKernActive == S_FALSE)
1816 return 0;
1817
1818 if ((int) (tcurrent - lastcheck) >= ShKernInterval)
1819 {
1820 lastcheck = tcurrent;
1821 return (-1);
1822 }
1823 return 0;
1824}
1825
1826/*************
1827 *
1828 * module check
1829 *
1830 *************/
1831int sh_kern_check ()
1832{
1833 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
1834 _("Checking kernel syscalls"),
1835 _("kern_check") );
1836 return (sh_kern_check_internal ());
1837}
1838
1839/*************
1840 *
1841 * module setup
1842 *
1843 *************/
1844
1845int sh_kern_set_severity (const char * c)
1846{
1847 char tmp[32];
1848 tmp[0] = '='; tmp[1] = '\0';
1849 sl_strlcat (tmp, c, 32);
1850 sh_error_set_level (tmp, &ShKernSeverity);
1851 return 0;
1852}
1853
1854int sh_kern_set_timer (const char * c)
1855{
1856 long val;
1857
1858 SL_ENTER(_("sh_kern_set_timer"));
1859
1860 val = strtol (c, (char **)NULL, 10);
1861 if (val <= 0)
1862 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
1863 _("kern timer"), c);
1864
1865 val = (val <= 0 ? 60 : val);
1866
1867 ShKernInterval = (time_t) val;
1868 SL_RETURN( 0, _("sh_kern_set_timer"));
1869}
1870
1871int sh_kern_set_activate (const char * c)
1872{
1873 int i;
1874 SL_ENTER(_("sh_kern_set_activate"));
1875 i = sh_util_flagval(c, &ShKernActive);
1876 SL_RETURN(i, _("sh_kern_set_activate"));
1877}
1878
1879int sh_kern_set_idt (const char * c)
1880{
1881 int i;
1882 SL_ENTER(_("sh_kern_set_idt"));
1883 i = sh_util_flagval(c, &ShKernIDT);
1884 SL_RETURN(i, _("sh_kern_set_idt"));
1885}
1886
1887int sh_kern_set_sc_addr (const char * c)
1888{
1889 char * endptr;
1890 unsigned long value;
1891
1892 SL_ENTER(_("sh_kern_set_sc_addr"));
1893 errno = 0;
1894 value = strtoul(c, &endptr, 16);
1895 if ((ULONG_MAX == value) && (errno == ERANGE))
1896 {
1897 SL_RETURN((-1), _("sh_kern_set_sc_addr"));
1898 }
1899 if ((*c == '\0') || (*endptr != '\0'))
1900 {
1901 SL_RETURN((-1), _("sh_kern_set_sc_addr"));
1902 }
1903 system_call_addr = value;
1904 ++AddressReconf;
1905 SL_RETURN((0), _("sh_kern_set_sc_addr"));
1906}
1907
1908int sh_kern_set_sct_addr (const char * c)
1909{
1910 char * endptr;
1911 unsigned long value;
1912
1913 SL_ENTER(_("sh_kern_set_sct_addr"));
1914 errno = 0;
1915 value = strtoul(c, &endptr, 16);
1916 if ((ULONG_MAX == value) && (errno == ERANGE))
1917 {
1918 SL_RETURN((-1), _("sh_kern_set_sct_addr"));
1919 }
1920 if ((*c == '\0') || (*endptr != '\0'))
1921 {
1922 SL_RETURN((-1), _("sh_kern_set_sct_addr"));
1923 }
1924 kaddr = (unsigned int) value;
1925 ++AddressReconf;
1926 SL_RETURN((0), _("sh_kern_set_sct_addr"));
1927}
1928
1929int sh_kern_set_proc_root (const char * c)
1930{
1931 char * endptr;
1932 unsigned long value;
1933
1934 SL_ENTER(_("sh_kern_set_proc_root"));
1935 errno = 0;
1936 value = strtoul(c, &endptr, 16);
1937 if ((ULONG_MAX == value) && (errno == ERANGE))
1938 {
1939 SL_RETURN((-1), _("sh_kern_set_proc_root"));
1940 }
1941 if ((*c == '\0') || (*endptr != '\0'))
1942 {
1943 SL_RETURN((-1), _("sh_kern_set_proc_root"));
1944 }
1945
1946 proc_root = value;
1947 ++AddressReconf;
1948 SL_RETURN((0), _("sh_kern_set_proc_root"));
1949}
1950
1951int sh_kern_set_proc_root_iops (const char * c)
1952{
1953 char * endptr;
1954 unsigned long value;
1955
1956 SL_ENTER(_("sh_kern_set_proc_root_iops"));
1957 errno = 0;
1958 value = strtoul(c, &endptr, 16);
1959 if ((ULONG_MAX == value) && (errno == ERANGE))
1960 {
1961 SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
1962 }
1963 if ((*c == '\0') || (*endptr != '\0'))
1964 {
1965 SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
1966 }
1967
1968 proc_root_iops = value;
1969 ++AddressReconf;
1970 SL_RETURN((0), _("sh_kern_set_proc_root_iops"));
1971}
1972
1973int sh_kern_set_proc_root_lookup (const char * c)
1974{
1975 char * endptr;
1976 unsigned long value;
1977
1978 SL_ENTER(_("sh_kern_set_proc_root_lookup"));
1979 errno = 0;
1980 value = strtoul(c, &endptr, 16);
1981 if ((ULONG_MAX == value) && (errno == ERANGE))
1982 {
1983 SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
1984 }
1985 if ((*c == '\0') || (*endptr != '\0'))
1986 {
1987 SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
1988 }
1989 proc_root_lookup = value;
1990 ++AddressReconf;
1991 SL_RETURN((0), _("sh_kern_set_proc_root_lookup"));
1992}
1993
1994#endif
1995
1996/* #ifdef SH_USE_KERN */
1997#endif
Note: See TracBrowser for help on using the repository browser.