source: trunk/src/kern_head.c@ 355

Last change on this file since 355 was 346, checked in by katerina, 14 years ago

Fixes for ticket #254: kernel check not working on Ubuntu 8.04 x86_64

File size: 24.1 KB
Line 
1/*
2 * need to #define SH_USE_KERN
3 *
4 */
5#define SH_SYSCALL_CODE
6
7#include "config.h"
8
9#if defined(HOST_IS_I86LINUX) || defined(HOST_IS_64LINUX)
10#define SH_IDT_TABLE
11#endif
12
13#include <stdio.h>
14#include <stdlib.h>
15
16#if defined(SH_USE_KERN)
17
18#undef _
19#define _(string) string
20#undef N_
21#define N_(string) string
22
23void usage(int flag)
24{
25 printf("\n");
26 printf("Usage: kern_head [-v | --verbose]\n");
27 printf(" kern_head [-h | --help]\n");
28 printf("\n");
29 /*
30 * printf(" You need superuser privileges to use this program,\n");
31 * printf(" because only the superuser can read from /dev/kmem.\n");
32 * printf("\n");
33 */
34 exit(flag);
35}
36
37#if defined(HOST_IS_LINUX)
38
39
40#include <string.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44#include <unistd.h>
45#include <errno.h>
46#include <limits.h>
47#include <sys/utsname.h>
48#include <sys/mman.h>
49
50/* number of system calls */
51#define SH_MAXCALLS 512
52
53#include "kern_head.h"
54
55static int verbose = 0;
56
57typedef struct _smap_entry {
58#ifdef SH_SYSCALL_CODE
59 unsigned int code[2]; /* 8 bytes */
60#endif
61 unsigned long addr;
62 char name[64];
63} smap_entry;
64
65union {
66 unsigned long addr_sys_call_table;
67 unsigned char str_sys_call_table[sizeof(unsigned long)];
68} sh_sys_call;
69
70#define SYS_CODE_SIZE 1024
71
72static unsigned long addr_system_call;
73static unsigned char system_call_code[SYS_CODE_SIZE];
74
75#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
76
77static int kmem_read (int fd, unsigned long addr, unsigned char * buf, int len)
78{
79 if (lseek(fd, addr, SEEK_SET) == (off_t) (-1))
80 {
81 if (verbose)
82 perror("kmem_read: lseek");
83 return -1;
84 }
85 if (read(fd, buf, len) < 0)
86 {
87 if (verbose)
88 perror("kmem_read: read");
89 return -1;
90 }
91 return 0;
92}
93
94static int kmem_mmap (int fd, unsigned long addr, unsigned char * buf, int len)
95{
96 size_t moff, roff;
97 size_t sz;
98 char * kmap;
99
100 sz = getpagesize(); /* unistd.h */
101
102 moff = ((size_t)(addr/sz)) * sz; /* lower page boundary */
103 roff = addr - moff; /* off relative to lower address of mmapped area */
104 kmap = mmap(0, len+sz, PROT_READ, MAP_PRIVATE, fd, moff);/* sys/mman.h */
105
106 if (kmap == MAP_FAILED)
107 {
108 /* then, try read()
109 */
110 if (verbose)
111 fprintf(stderr, "kmem_mmap: mmap() failed, now trying read()\n");
112
113 if (0 == kmem_read (fd, addr, buf, len))
114 return 0;
115
116 perror("kmem_mmap: mmap");
117 return -1;
118 }
119
120 memcpy (buf, &kmap[roff], len);
121
122 if (munmap(kmap, len+sz) != 0)
123 {
124 perror("kmem_mmap: munmap");
125 return -1;
126 }
127
128 return 0;
129}
130
131int read_kcode (unsigned long addr, unsigned char * buf, int len)
132{
133 int fd;
134
135 if (addr == 0UL)
136 {
137 perror("read_kcode: invalid input");
138 return -1;
139 }
140
141 fd = open ("/dev/kmem", O_RDONLY);
142
143 if (fd < 0)
144 {
145 if (verbose)
146 fprintf(stderr, "read_kcode: /dev/kmem failed, now trying /proc/kmem\n");
147
148 if (0 != access("/proc/kmem", R_OK))
149 {
150 perror("read_kcode: access /proc/kmem");
151
152 fprintf(stderr, "\n");
153
154 fprintf(stderr, "NOTE: kern_head: apparently you have no /dev/kmem, and the\n");
155 fprintf(stderr, " samhain_kmem module is not loaded\n");
156 fprintf(stderr, " If you get this message, please proceed as follows:\n");
157 fprintf(stderr, " $ make samhain_kmem.ko\n");
158 fprintf(stderr, " $ sudo /sbin/insmod samhain_kmem.ko; sudo ./kern_head > sh_ks.h; sudo /sbin/rmmod samhain_kmem\n");
159 fprintf(stderr, " $ make\n\n");
160 exit (EXIT_FAILURE);
161 }
162 fd = open ("/proc/kmem", O_RDONLY);
163 }
164
165 if (fd < 0)
166 {
167 perror("read_kcode: open /dev/kmem and /proc/kmem");
168 return -1;
169 }
170
171 if (kmem_mmap(fd, addr, buf, len) < 0)
172 {
173 close (fd);
174 return -1;
175 }
176
177 close (fd);
178
179 return 0;
180}
181
182int get_dispatch (int * qq)
183{
184 int i;
185
186 if (addr_system_call == 0L || sh_sys_call.addr_sys_call_table == 0L)
187 {
188 fprintf(stderr, "get_dispatch: invalid data\n");
189 return -1;
190 }
191
192 if (0 != read_kcode (addr_system_call, system_call_code, SYS_CODE_SIZE))
193 {
194 fprintf(stderr, "get_dispatch: could not read system_call code\n");
195 return -1;
196 }
197
198 for (i = 0; i < (SYS_CODE_SIZE - 4); ++i)
199 {
200 if (system_call_code[i] == sh_sys_call.str_sys_call_table[0] &&
201 system_call_code[i+1] == sh_sys_call.str_sys_call_table[1] &&
202 system_call_code[i+2] == sh_sys_call.str_sys_call_table[2] &&
203 system_call_code[i+3] == sh_sys_call.str_sys_call_table[3])
204 {
205 /*
206 fprintf(stderr, "INFO: get_dispatch: found sys_call_table in "\
207 "system_call code at %d\n", i);
208 */
209 *qq = i;
210 return 0;
211 }
212 }
213 fprintf(stderr,
214 "get_dispatch: did not find sys_call_table in system_call code\n");
215 fprintf(stderr,
216 "** This indicates that either your System.map does not match\n");
217 fprintf(stderr,
218 "** the currently running kernel, or that your System.map does\n");
219 fprintf(stderr,
220 "** not provide the required information, and thus use of\n");
221 fprintf(stderr,
222 "** the --with-kcheck option is not possible\n");
223 return -1;
224}
225
226unsigned long get_symbol_from_systemmap (char * systemmap,
227 char * symbol, char flag)
228{
229 FILE * fp;
230 char buf[512], addr[32], * p;
231 unsigned long retval = 0;
232#if defined(__x86_64__) || defined(__amd64__)
233 int off = 8;
234#else
235 int off = 0;
236#endif
237
238 fp = fopen (systemmap, "r");
239
240 if (!fp)
241 {
242 fprintf(stderr, "error opening <%s>\n", systemmap);
243 perror("get_symbol_from_systemmap: fopen");
244 return -1;
245 }
246 while (fgets(buf, 512, fp) != NULL)
247 {
248 if (buf[9+off] != flag)
249 continue;
250
251 p = strchr(buf, '\n');
252 if (p != NULL)
253 *p = '\0';
254
255 if (0 != strcmp(&buf[11+off], symbol))
256 continue;
257
258 addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
259 strncat(&addr[2], buf, 8+off);
260
261 retval = strtoul(addr, NULL, 0);
262 if (retval == ULONG_MAX)
263 {
264 perror("get_symbol_from_systemmap");
265 return -1;
266 }
267 }
268 fclose(fp);
269 return retval;
270}
271
272
273/* returns the number N of entries in syscall table
274 * (0 .. N-1) with valid syscalls
275 */
276int fill_smap(smap_entry * sh_smap, int num)
277{
278 FILE * fp;
279 char buf[512], addr[32], name[128];
280 int i, j, count = 0, maxcall = 0;
281#if defined(__x86_64__) || defined(__amd64__)
282 int off = 8;
283#else
284 int off = 0;
285#endif
286
287 fp = fopen (SYSTEMMAP, "r");
288
289 if (!fp)
290 {
291 perror("fill_smap: fopen");
292 fprintf(stderr, "fill_smap: error opening <%s>\n", SYSTEMMAP);
293 return -1;
294 }
295
296 /* initialize
297 */
298 sh_sys_call.addr_sys_call_table = 0L;
299
300 while (fgets(buf, 512, fp) != NULL)
301 {
302
303 if ( ( (buf[9+off] == 'D') || (buf[9+off] == 'd') ||
304 (buf[9+off] == 'R') || (buf[9+off] == 'r')) &&
305 0 == strncmp("sys_call_table", &buf[11+off], 14))
306 {
307 printf("/* found sys_call_table */\n");
308 /* --- copy symbol address ---
309 */
310 addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
311 strncat(&addr[2], buf, 8+off);
312 addr[10+off] = '\0';
313
314 sh_sys_call.addr_sys_call_table = strtoul(addr, NULL, 0);
315 if (sh_sys_call.addr_sys_call_table == ULONG_MAX)
316 {
317 perror("fill_smap");
318 return -1;
319 }
320 else
321 {
322 printf("#define SH_SYS_CALL_TABLE %s\n", addr);
323 }
324 }
325
326 if (buf[9+off] != 'T')
327 continue;
328
329 if (0 == strncmp("system_call", &buf[11+off], 11))
330 {
331 printf("/* found system_call */\n");
332 /* --- copy symbol address ---
333 */
334 addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
335 strncat(&addr[2], buf, 8+off);
336 addr[10+off] = '\0';
337 addr_system_call = strtoul(addr, NULL, 0);
338 if (addr_system_call == ULONG_MAX)
339 {
340 perror("fill_smap");
341 return -1;
342 }
343 }
344
345
346 if ( (buf[11+off]!='s' || buf[12+off]!='y' ||
347 buf[13+off]!='s' || buf[14+off]!='_') &&
348 (buf[11+off]!='o' || buf[12+off]!='l' ||
349 buf[13+off]!='d' || buf[14+off]!='_'))
350 continue;
351
352 for (i = 0; i < num; ++i)
353 {
354 for (j = 0; j < 127; ++j)
355 {
356 if (buf[11+off+j] == '\n' || buf[11+off+j] == '\0')
357 {
358 name[j] = '\0';
359 break;
360 }
361 name[j] = buf[11+off+j];
362 }
363
364
365 if (0 == strcmp(name, sh_smap[i].name))
366 {
367
368 /* --- copy symbol address ---
369 */
370 addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
371 strncat(&addr[2], buf, 8+off);
372 addr[10+off] = '\0';
373 sh_smap[i].addr = strtoul(addr, NULL, 0);
374 if (sh_smap[i].addr == ULONG_MAX)
375 {
376 perror("fill_smap");
377 return -1;
378 }
379 ++count;
380 if (i > maxcall) maxcall = i;
381 /* printf("maxcall = %d\n", maxcall); */
382 /* break; */
383 }
384 }
385 }
386 fclose(fp);
387
388 if ((count > 0) && (maxcall > 0))
389 return maxcall+1;
390 else
391 return count;
392}
393
394
395int main(int argc, char * argv[])
396{
397 int i, count, maxcall, qq;
398 smap_entry sh_smap[SH_MAXCALLS];
399 struct utsname utbuf;
400 char *p = NULL;
401
402 unsigned long proc_root;
403 unsigned long proc_root_iops;
404 unsigned long proc_root_lookup;
405
406 unsigned long addr_ni_syscall = 0;
407
408 int major, minor, micro, is64 = 0;
409
410 if (argc > 1)
411 {
412 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
413 usage(EXIT_SUCCESS);
414 else if (strcmp(argv[1], "-v") == 0 ||
415 strcmp(argv[1], "--verbose") == 0)
416 verbose = 1;
417 }
418
419 if (0 != uname(&utbuf))
420 {
421 perror("kern_head: uname");
422 exit (EXIT_FAILURE);
423 }
424
425 if (strncmp(utbuf.release, SH_KERNEL_VERSION, 3) != 0)
426 {
427 fprintf(stderr, "kern_head: current kernel version %s does not match\n",
428 utbuf.release);
429 fprintf(stderr, "kern_head: %s from config.h\n", SH_KERNEL_VERSION);
430 fprintf(stderr, "kern_head: continuing with %s\n", SH_KERNEL_VERSION);
431
432 p = SH_KERNEL_VERSION;
433 } else {
434 p = utbuf.release;
435 }
436
437 if (3 != sscanf(p, "%d.%d.%d", &major, &minor, &micro))
438 {
439 perror("kern_head: sscanf");
440 exit (EXIT_FAILURE);
441 }
442
443 if (minor != 4 && minor != 6)
444 {
445 fprintf(stderr, "kern_head: kernel %s not supported\n", p);
446 exit (EXIT_FAILURE);
447 }
448
449
450 if (utbuf.machine[0] != 'i' || utbuf.machine[2] != '8' ||
451 utbuf.machine[3] != '6')
452 {
453 if (0 != strcmp(utbuf.machine, "x86_64"))
454 {
455 fprintf(stderr, "kern_head: machine %s not supported\n", utbuf.machine);
456 exit (EXIT_FAILURE);
457 }
458 else
459 {
460 is64 = 1;
461 }
462 }
463
464 if (0 != getuid())
465 {
466 fprintf(stderr, "\n");
467
468 fprintf(stderr, "NOTE: kern_head: must run as 'root' (need to read from /dev/kmem)\n");
469 fprintf(stderr, " If you get this message, then proceed as follows:\n");
470 fprintf(stderr, " $ sudo ./kern_head > sh_ks.h\n");
471 fprintf(stderr, " $ make\n\n");
472 exit (EXIT_FAILURE);
473 }
474
475 printf("#ifndef SH_KERN_CALLS_H\n");
476 printf("#define SH_KERN_CALLS_H\n\n");
477
478 printf("\n/* Kernel %s, machine %s, %d bit -- use table callz_2p4 */\n\n",
479 p, utbuf.machine,
480 (is64 == 0) ? 32 : 64,
481 (is64 == 0) ? "syscalls_32" : "syscalls_64");
482
483 /* initiate the system call table
484 */
485 if (is64 == 0)
486 {
487 for (i = 0; i < SH_MAXCALLS; ++i)
488 {
489 if (syscalls_32[i] == NULL)
490 break;
491 strcpy(sh_smap[i].name, syscalls_32[i]);
492 sh_smap[i].addr = 0UL;
493 }
494 if (minor == 6) /* fix syscall map for 2.6 */
495 {
496 strcpy(sh_smap[0].name, "sys_restart_syscall");
497 strcpy(sh_smap[180].name, "sys_pread64");
498 strcpy(sh_smap[181].name, "sys_pwrite64");
499 }
500 }
501 else /* x86_64 */
502 {
503 for (i = 0; i < SH_MAXCALLS; ++i)
504 {
505 if (syscalls_64[i] == NULL)
506 break;
507 strcpy(sh_smap[i].name, syscalls_64[i]);
508 sh_smap[i].addr = 0UL;
509 }
510 }
511
512 count = i;
513
514 /* get the actual number of the highest syscall and use no more.
515 * get sys_call_table and system_call
516 */
517 maxcall = fill_smap(sh_smap, count);
518 if ( maxcall < 0)
519 {
520 printf("#endif\n");
521 fprintf(stderr, "kern_head: fill_smap failed\n");
522 exit (EXIT_FAILURE);
523 }
524
525 if (addr_system_call == 0L)
526 {
527 printf("#endif\n");
528 fprintf(stderr,
529 "kern_head: address of system_call not found in System.map\n");
530 fprintf(stderr,
531 "** This indicates that your System.map does not provide\n");
532 fprintf(stderr,
533 "** the required information, and thus use of the\n");
534 fprintf(stderr,
535 "** --with-kcheck option is not possible\n");
536 exit (EXIT_FAILURE);
537 }
538
539 for (i = 0; i < maxcall; ++i)
540 {
541 if (0 == strcmp(sh_smap[i].name, "sys_ni_syscall"))
542 {
543 addr_ni_syscall = sh_smap[i].addr;
544 break;
545 }
546 }
547
548 if (minor < 6)
549 {
550 maxcall = (maxcall > 256) ? 256 : maxcall;
551 }
552
553 for (i = 0; i < maxcall; ++i)
554 {
555 if (sh_smap[i].addr == 0UL)
556 {
557 if (verbose > 0)
558 fprintf(stderr, "** unknown syscall **: [%s]\n", sh_smap[i].name);
559 strcpy(sh_smap[i].name, "sys_ni_syscall");
560 sh_smap[i].addr = addr_ni_syscall;
561 }
562 }
563
564
565 /* get the location of the syscall table address within system_call
566 */
567 if ( get_dispatch (&qq) < 0)
568 {
569 printf("#endif\n");
570 fprintf(stderr, "kern_head: get_dispatch failed\n");
571 exit (EXIT_FAILURE);
572 }
573
574 if (qq <= 252)
575 printf("#define SYS_CALL_LOC %d\n", qq);
576 else
577 {
578 printf("#endif\n");
579 fprintf(stderr, "kern_head: SYS_CALL_LOC (%d) too large\n", qq);
580 exit(EXIT_FAILURE);
581 }
582 printf("#define SH_SYS_CALL_ADDR %#lx\n\n", addr_system_call);
583
584 printf("static unsigned char system_call_code[256] = { 0 };\n");
585
586 printf("#define SH_MAXCALLS %d\n\n", maxcall);
587
588#ifdef SH_IDT_TABLE
589 printf("static unsigned char idt_table[2048] = { 0 };\n");
590#endif
591
592 printf("typedef struct _sh_syscall_t {\n");
593#ifdef SH_SYSCALL_CODE
594 printf(" unsigned int code[2]; /* 8 bytes */\n");
595#endif
596 printf(" unsigned long addr;\n");
597 printf(" char * name;\n");
598 printf("} sh_syscall_t;\n\n");
599
600 printf("static sh_syscall_t sh_syscalls[] = {\n");
601
602 for (i = 0; i < maxcall; ++i)
603 {
604#ifdef SH_SYSCALL_CODE
605 printf(" /* %03d */ { { 0, 0 }, 0, N_(%c%s%c) },\n",
606 i, '"', sh_smap[i].name, '"');
607#else
608 printf(" /* %03d */ { 0, N_(%c%s%c) },\n",
609 i, '"', sh_smap[i].name, '"');
610#endif
611 }
612#ifdef SH_SYSCALL_CODE
613 printf(" /* eof */ { { 0x00000000, 0x00000000 }, 0x00000000, NULL }\n");
614#else
615 printf(" /* eof */ { 0x00000000, NULL }\n");
616#endif
617 printf("};\n\n");
618
619
620 /* get proc addresses
621 */
622 proc_root = get_symbol_from_systemmap (SYSTEMMAP,
623 "proc_root", 'D');
624 if (proc_root == 0)
625 {
626 proc_root = get_symbol_from_systemmap (SYSTEMMAP,
627 "proc_root", 'd');
628 }
629 if (proc_root == 0)
630 {
631 proc_root = get_symbol_from_systemmap (SYSTEMMAP,
632 "proc_root", 'R');
633 }
634 if (proc_root != 0) {
635 printf("#define PROC_ROOT_LOC %#lx\n\n", proc_root);
636 }
637
638 proc_root_lookup = get_symbol_from_systemmap (SYSTEMMAP,
639 "proc_root_lookup", 't');
640 if (proc_root_lookup == 0)
641 {
642 proc_root_lookup = get_symbol_from_systemmap (SYSTEMMAP,
643 "proc_root_lookup", 'T');
644 }
645 if (proc_root_lookup != 0) {
646 printf("#define PROC_ROOT_LOOKUP_LOC %#lx\n\n", proc_root_lookup);
647 }
648
649 proc_root_iops = get_symbol_from_systemmap (SYSTEMMAP,
650 "proc_root_inode_operations",
651 'd');
652 if (proc_root_iops == 0)
653 {
654 proc_root_iops = get_symbol_from_systemmap (SYSTEMMAP,
655 "proc_root_inode_operations",
656 'D');
657 }
658 if (proc_root_iops == 0)
659 {
660 proc_root_iops = get_symbol_from_systemmap (SYSTEMMAP,
661 "proc_root_inode_operations",
662 'R');
663 }
664 if (proc_root_iops != 0) {
665 printf("#define PROC_ROOT_IOPS_LOC %#lx\n\n", proc_root_iops);
666 }
667
668 if (KERNEL_VERSION(major,minor,micro) >= KERNEL_VERSION(2,6,17))
669 {
670 printf("#define TWO_SIX_SEVENTEEN_PLUS 1\n\n");
671 }
672
673 printf("#endif\n");
674
675 exit (EXIT_SUCCESS);
676}
677
678/* if defined(HOST_IS_LINUX) */
679#endif
680
681/************************************************************
682 *
683 *
684 * FreeBSD Implementation
685 *
686 ************************************************************/
687
688#if defined(HOST_IS_FREEBSD) || defined(__OpenBSD__)
689
690#include <stdlib.h>
691#include <unistd.h>
692#include <string.h>
693#include <err.h>
694#include <kvm.h>
695#include <fcntl.h>
696#include <nlist.h>
697#include <limits.h>
698#include <sys/types.h>
699#include <sys/utsname.h>
700
701#ifdef __FreeBSD__
702#include <sys/sysent.h>
703#endif
704
705#include <sys/syscall.h>
706
707#ifndef SYS_MAXSYSCALL
708#define SYS_MAXSYSCALL 512
709#endif
710
711/* number of system calls */
712#define SH_MAXCALLS 512
713#include "kern_head.h"
714static int verbose = 0;
715
716#ifdef __OpenBSD__
717struct proc;
718struct sysent {
719 short sy_narg;
720 short sy_argsize;
721 int (*sy_call)(struct proc *, void *, register_t *);
722};
723#endif
724
725typedef struct _smap_entry {
726 unsigned int code[2]; /* 8 bytes */
727 unsigned long addr;
728 char name[64];
729} smap_entry;
730
731union {
732 unsigned long addr_sys_call_table;
733 unsigned char str_sys_call_table[sizeof(unsigned long)];
734} sh_sys_call;
735
736struct nlist sys_list[SYS_MAXSYSCALL+1];
737
738struct nlist list[2];
739
740
741int main(int argc, char * argv[])
742{
743 int i, count, which;
744 smap_entry sh_smap[SYS_MAXSYSCALL];
745 struct utsname utbuf;
746 char errbuf[_POSIX2_LINE_MAX];
747
748 struct sysent sy;
749 unsigned long offset = 0L;
750 kvm_t *kd;
751
752 list[0].n_name = "_sysent";
753 list[1].n_name = NULL;
754
755 if (argc > 1)
756 {
757 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
758 usage(EXIT_SUCCESS);
759 else if (strcmp(argv[1], "-v") == 0 ||
760 strcmp(argv[1], "--verbose") == 0)
761 verbose = 1;
762 }
763
764 if (0 != uname(&utbuf))
765 {
766 perror("kern_head: uname");
767 exit (EXIT_FAILURE);
768 }
769
770#ifdef __OpenBSD__
771 if (utbuf.release[0] == '3')
772 which = 38;
773 else if (utbuf.release[0] == '4')
774 which = 40;
775#else
776 if (utbuf.release[0] == '4')
777 which = 4;
778 else if (utbuf.release[0] == '5')
779 which = 5;
780 else if (utbuf.release[0] == '6')
781 which = 5;
782#endif
783 else
784 {
785 fprintf(stderr, "kern_head: kernel %s not supported\n", utbuf.release);
786 exit (EXIT_FAILURE);
787 }
788
789 if (utbuf.machine[0] != 'i' || utbuf.machine[2] != '8' ||
790 utbuf.machine[3] != '6')
791 {
792 fprintf(stderr, "kern_head: machine %s not supported\n", utbuf.machine);
793 exit (EXIT_FAILURE);
794 }
795
796 if (0 != getuid())
797 {
798 fprintf(stderr, "\n");
799 fprintf(stderr, "NOTE: kern_head: must run as 'root' ");
800 fprintf(stderr, "(need to read from kernel)\n");
801 fprintf(stderr, " If you get this message, then proceed ");
802 fprintf(stderr, "as follows:\n");
803 fprintf(stderr, " $ su\n");
804 fprintf(stderr, " $ ./kern_head > sh_ks.h\n");
805 fprintf(stderr, " $ exit\n");
806 fprintf(stderr, " $ make\n\n");
807 exit (EXIT_FAILURE);
808 }
809
810 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
811 if (!kd)
812 {
813 fprintf(stderr, "check_sysent: kvm_openfiles: %s\n", errbuf);
814 exit(EXIT_FAILURE);
815 }
816
817 i = kvm_nlist(kd, list);
818 if (i == -1)
819 {
820 fprintf(stderr, "check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
821 exit(EXIT_FAILURE);
822 }
823 else if (i == 1)
824 {
825 fprintf(stderr, "check_sysent: kvm_nlist: _sysent not found\n");
826 exit(EXIT_FAILURE);
827 }
828 else if (list[0].n_value == 0)
829 {
830 fprintf(stderr, "check_sysent: kvm_nlist: zero address for _sysent\n");
831 exit(EXIT_FAILURE);
832 }
833
834 if (which == 4)
835 printf("\n/* Kernel %s, machine %s -- use table %s */\n\n",
836 utbuf.release, utbuf.machine, "callz_fbsd");
837 else if (which == 5 || which == 6)
838 printf("\n/* Kernel %s, machine %s -- use table %s */\n\n",
839 utbuf.release, utbuf.machine, "callz_fbsd5");
840 else if (which == 38 || which == 40)
841 printf("\n/* Kernel %s, machine %s -- use table %s */\n\n",
842 utbuf.release, utbuf.machine, "callz_obsd");
843
844
845 i = 0;
846 if (which == 4) {
847 while ((callz_fbsd[i] != NULL) && (i < SYS_MAXSYSCALL))
848 {
849 sys_list[i].n_name = callz_fbsd[i];
850 /* fprintf(stderr, "sys_list[%d] = %s\n", i, sys_list[i].n_name); */
851 ++i;
852 }
853 if ((utbuf.release[1] == '.') && (utbuf.release[2] == '1') &&
854 (utbuf.release[3] == '0'))
855 {
856 sys_list[336].n_name = callz_fbsd[151]; /* sendfile -> nosys */
857 }
858 } else if (which == 5 || which == 6) {
859 while ((callz_fbsd5[i] != NULL) && (i < SYS_MAXSYSCALL))
860 {
861 sys_list[i].n_name = callz_fbsd5[i];
862 /* fprintf(stderr, "sys_list[%d] = %s\n", i, sys_list[i].n_name); */
863 ++i;
864 }
865 }
866 else if (which == 38 || which == 40) {
867 while ((callz_obsd[i] != NULL) && (i < SYS_MAXSYSCALL))
868 {
869 sys_list[i].n_name = callz_obsd[i];
870 /* fprintf(stderr, "sys_list[%d] = %s\n", i, sys_list[i].n_name); */
871 ++i;
872 }
873 }
874
875 count = i;
876 sys_list[i].n_name = NULL;
877
878 i = kvm_nlist(kd, sys_list);
879 if (i == -1)
880 {
881 fprintf(stderr, "check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
882 /* exit(EXIT_FAILURE); */
883 }
884 else if (i != 0 && verbose != 0)
885 {
886 fprintf(stderr, "check_sysent: kvm_nlist: %d out of %d invalid.\n",
887 i, count);
888 fprintf(stderr, " Probably the table in kern_head.h\n");
889 fprintf(stderr, " is not for your kernel version.\n");
890 fprintf(stderr, " (No reason to worry, kcheck will "\
891 "work anyway)\n\n");
892 }
893
894 for (i = 0; i < count /* SYS_MAXSYSCALL */; i++)
895 {
896 if (NULL == sys_list[i].n_name)
897 break;
898 if (!sys_list[i].n_value && 0 != strcmp(sys_list[i].n_name, "_nosys")
899 && verbose != 0)
900 {
901 fprintf(stderr,"check_sysent: not found: slot %03d [%s]\n",
902 i, sys_list[i].n_name);
903 /* exit(EXIT_FAILURE); */
904 }
905 offset = list[0].n_value + (i*sizeof(struct sysent));
906 if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
907 {
908 fprintf(stderr,"check_sysent: kvm_read: %s\n", kvm_geterr(kd));
909 exit(EXIT_FAILURE);
910 }
911
912 if (verbose > 0)
913 fprintf(stderr, "(kvm_nlist) %#lx %#lx (sysent[%03d]) %03d [%s]\n",
914 (unsigned long) sys_list[i].n_value,
915 (unsigned long) sy.sy_call,
916 i, i, sys_list[i].n_name);
917
918 if((unsigned long)sy.sy_call != sys_list[i].n_value &&
919 sys_list[i].n_value != 0 &&
920 0 != strcmp(sys_list[i].n_name, "_nosys") &&
921 (unsigned long)sy.sy_call != sys_list[151].n_value)
922 {
923 fprintf(stderr,
924 "WARNING: (kvm_nlist) %#lx != %#lx (sysent[%03d]) %03d [%s]\n",
925 (unsigned long) sys_list[i].n_value,
926 (unsigned long) sy.sy_call,
927 i, i, sys_list[i].n_name);
928 }
929 sh_smap[i].addr = (unsigned long) sy.sy_call;
930 strncpy(sh_smap[i].name, sys_list[i].n_name, 64);
931 if(kvm_read(kd, (unsigned int) sy.sy_call, &(sh_smap[i].code[0]),
932 2 * sizeof(unsigned int)) < 0)
933 {
934 fprintf(stderr,"check_sysent: kvm_read: %s\n", kvm_geterr(kd));
935 exit(EXIT_FAILURE);
936 }
937 }
938
939 if(kvm_close(kd) < 0)
940 {
941 fprintf(stderr,"check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
942 exit(EXIT_FAILURE);
943 }
944
945 printf("#ifndef SH_KERN_CALLS_H\n");
946 printf("#define SH_KERN_CALLS_H\n\n");
947
948 printf("#define SH_MAXCALLS %d\n\n", count);
949
950 printf("typedef struct _sh_syscall_t {\n");
951 printf(" unsigned int code[2]; /* 8 bytes */\n");
952 printf(" unsigned long addr;\n");
953 printf(" char * name;\n");
954 printf("} sh_syscall_t;\n\n");
955
956 printf("static sh_syscall_t sh_syscalls[] = {\n");
957 for (i = 0; i < count; ++i) {
958 printf(" /* %03d */ {{ 0x%-8.8x, 0x%-8.8x }, 0x%-8.8lx, N_(%c%s%c) },\n",
959 i, sh_smap[i].code[0], sh_smap[i].code[1],
960 sh_smap[i].addr, '"', sh_smap[i].name, '"');
961 }
962 printf(" /* eof */ { { 0x00000000, 0x00000000 }, 0x00000000, NULL }\n");
963 printf("};\n\n");
964 printf("#endif\n");
965 return 0;
966}
967/* if defined(HOST_IS_FREEBSD) */
968#endif
969
970/* #if defined(SH_USE_KERN) */
971#else
972
973#include <stdio.h>
974#include <stdlib.h>
975
976int main()
977{
978 printf("#ifndef SH_KERN_CALLS_H\n");
979 printf("#define SH_KERN_CALLS_H\n\n");
980
981 printf("/* Dummy header. */\n\n");
982
983 printf("typedef struct _sh_syscall_t {\n");
984 printf(" unsigned long addr;\n");
985 printf(" char * name;\n");
986 printf("} sh_syscall_t;\n\n");
987
988 printf("#endif\n");
989
990 return (EXIT_SUCCESS);
991}
992
993/* #ifdef SH_USE_KERN */
994#endif
Note: See TracBrowser for help on using the repository browser.