1. http://bbs.chinaunix.net/thread-2143235-1-1.html
  2. Linux System Call Table

    http://bluemaster.iu.hio.no/edu/dark/lin-asm/syscalls.html
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/fcntl.h>
  8. #include <asm/unistd.h>
  9. #include <asm/ia32_unistd.h>
  10. #include <asm/msr.h>



  11. #define THIS_DESCRIPTION "Dynamically get sys_call_table/ia32_sys_call_table in kernel module.\n\t\tI don't think there would be anyone who won't enable CONFIG_IA32_EMULATION these days.\n\t\tso you can add the unessary check if you really want to."
  12. MODULE_DESCRIPTION( THIS_DESCRIPTION );
  13. MODULE_AUTHOR("albcamus <albcamus@gmail.com>");
  14. MODULE_LICENSE("GPL");


  15. /**
  16. * TODO: if you want shut this up, comment the following line.
  17. */
  18. #define BY_IDT_DEBUG

  19. #ifdef BY_IDT_DEBUG
  20. #define dbgprint(format,args...) \
  21.                  printk("get_syscall: function:%s-L%d: "format, __FUNCTION__, __LINE__, ## args);
  22. #else
  23. #define dbgprint(format,args...)  do {} while(0);
  24. #endif


  25. /**
  26. * sys call table
  27. */
  28. void ** my_ia32_syscall_table;
  29. void ** my_syscall_table;



  30. /**
  31. * 10 bytes  -- please refer to AMD64 Architecture Programmer's 
  32. *         Manuals for more information.
  33. */
  34. struct idtr {
  35.     unsigned short limit;
  36.     unsigned long base; //in 64bit mode, base address is 8 bytes
  37. } __attribute__ ((packed));


  38. /**
  39. * in long mode -- 64bit mode and compatity mode, 
  40. * every IDT entry has a 16-byte size
  41. */
  42. struct idt {
  43.     u16 offset_low;
  44.     u16 segment;
  45.     unsigned ist : 3, zero0 : 5, type : 5, dpl :2, p : 1;
  46.     u16 offset_middle;
  47.     u32 offset_high;
  48.     u32 zero1;
  49. } __attribute__ ((packed));




  50. /**
  51. * Return the first appearence of NEEDLE in HAYSTACK.  -- copied from PHRACK
  52. * */
  53. static void *memmem(const void *haystack, size_t haystack_len,
  54.             const void *needle, size_t needle_len)
  55. {/*{{{*/
  56.     const char *begin;
  57.     const char *const last_possible
  58.         = (const char *) haystack + haystack_len - needle_len;

  59.     if (needle_len == 0)
  60.         /* The first occurrence of the empty string is deemed to occur at
  61.            the beginning of the string.  */
  62.         return (void *) haystack;

  63.     /* Sanity check, otherwise the loop might search through the whole
  64.        memory.  */
  65.     if (__builtin_expect(haystack_len < needle_len, 0))
  66.         return NULL;

  67.     for (begin = (const char *) haystack; begin <= last_possible;
  68.          ++begin)
  69.         if (begin[0] == ((const char *) needle)[0]
  70.             && !memcmp((const void *) &begin[1],
  71.                    (const void *) ((const char *) needle + 1),
  72.                    needle_len - 1))
  73.             return (void *) begin;

  74.     return NULL;
  75. }/*}}}*/


  76. /**
  77. * Find the location of ia32_sys_call_table
  78. */
  79. static unsigned long get_syscall_table_ia32(void)
  80. {/*{{{*/
  81. #define OFFSET_SYSCALL 100    /* from system_call/ia32_syscall, we'll read first 150 bytes */

  82.     struct idtr idtr;
  83.     struct idt idt;

  84.     unsigned long sys_call_off;
  85.     unsigned long retval;

  86.     char sc_asm[OFFSET_SYSCALL], *p;




  87.     /* well, let's read IDTR */
  88.     asm("sidt %0"
  89.         :"=m"(idtr)
  90.         : );

  91.     dbgprint("idtr base at %p\n", (void *)idtr.base);

  92.     /**
  93.      * Read in IDT for vector 0x80 (syscall) 
  94.      */
  95.     memcpy(&idt, (char *) idtr.base + 16 * 0x80, sizeof(idt)); 
  96.     sys_call_off =  ( ( (unsigned long)idt.offset_high ) << 32 ) | 
  97.         ( ((idt.offset_middle << 16 ) | idt.offset_low) & 0x00000000ffffffff ); 
  98.     dbgprint("sys_call_off at %p\n", (void *)sys_call_off);

  99.     /* we have syscall routine address now, look for syscall table
  100.        dispatch (indirect call) */
  101.     memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);
  102.     /**
  103.      * ia32_call > ia32_tracesys > ia32_do_syscall > 'call *ia32_sys_call_table(,%rax,8)'
  104.      * Find callq *ia32_sys_call_table(,%rax,8)
  105.      *
  106.      * (gdb) disassemble ia32_syscall
  107.      * Dump of assembler code for function ia32_syscall:
  108.      * 0xffffffff81066b98 <ia32_syscall+0>:    swapgs
  109.      * 0xffffffff81066b9b <ia32_syscall+3>:    sti
  110.      * 0xffffffff81066b9c <ia32_syscall+4>:    mov    %eax,%eax
  111.      * 0xffffffff81066b9e <ia32_syscall+6>:    push   %rax
  112.      * 0xffffffff81066b9f <ia32_syscall+7>:    cld
  113.      * 0xffffffff81066ba0 <ia32_syscall+8>:    sub    $0x48,%rsp
  114.      * 0xffffffff81066ba4 <ia32_syscall+12>:   mov    %rdi,0x40(%rsp)
  115.      * 0xffffffff81066ba9 <ia32_syscall+17>:   mov    %rsi,0x38(%rsp)
  116.      * 0xffffffff81066bae <ia32_syscall+22>:   mov    %rdx,0x30(%rsp)
  117.      * 0xffffffff81066bb3 <ia32_syscall+27>:   mov    %rcx,0x28(%rsp)
  118.      * 0xffffffff81066bb8 <ia32_syscall+32>:   mov    %rax,0x20(%rsp)
  119.      * 0xffffffff81066bbd <ia32_syscall+37>:   mov    %gs:0x10,%r10
  120.      * 0xffffffff81066bc6 <ia32_syscall+46>:   sub    $0x1fd8,%r10
  121.      * 0xffffffff81066bcd <ia32_syscall+53>:   orl    $0x2,0x14(%r10)
  122.      * 0xffffffff81066bd2 <ia32_syscall+58>:   testl  $0x181,0x10(%r10)
  123.      * 0xffffffff81066bda <ia32_syscall+66>:   jne    0xffffffff81066c04 <ia32_tracesys>
  124.      * End of assembler dump.
  125.      * (gdb) disassemble ia32_tracesys
  126.      * Dump of assembler code for function ia32_tracesys:
  127.      * 0xffffffff81066c04 <ia32_tracesys+0>:   sub    $0x30,%rsp
  128.      * 0xffffffff81066c08 <ia32_tracesys+4>:   mov    %rbx,0x28(%rsp)
  129.      * 0xffffffff81066c0d <ia32_tracesys+9>:   mov    %rbp,0x20(%rsp)
  130.      * 0xffffffff81066c12 <ia32_tracesys+14>:  mov    %r12,0x18(%rsp)
  131.      * 0xffffffff81066c17 <ia32_tracesys+19>:  mov    %r13,0x10(%rsp)
  132.      * 0xffffffff81066c1c <ia32_tracesys+24>:  mov    %r14,0x8(%rsp)
  133.      * 0xffffffff81066c21 <ia32_tracesys+29>:  mov    %r15,(%rsp)
  134.      * 0xffffffff81066c25 <ia32_tracesys+33>:  movq   $0xffffffffffffffda,0x50(%rsp)
  135.      * 0xffffffff81066c2e <ia32_tracesys+42>:  mov    %rsp,%rdi
  136.      * 0xffffffff81066c31 <ia32_tracesys+45>:  callq  0xffffffff81073a02 <syscall_trace_enter>
  137.      * 0xffffffff81066c36 <ia32_tracesys+50>:  mov    0x30(%rsp),%r11
  138.      * 0xffffffff81066c3b <ia32_tracesys+55>:  mov    0x38(%rsp),%r10
  139.      * 0xffffffff81066c40 <ia32_tracesys+60>:  mov    0x40(%rsp),%r9
  140.      * 0xffffffff81066c45 <ia32_tracesys+65>:  mov    0x48(%rsp),%r8
  141.      * 0xffffffff81066c4a <ia32_tracesys+70>:  mov    0x58(%rsp),%rcx
  142.      * 0xffffffff81066c4f <ia32_tracesys+75>:  mov    0x60(%rsp),%rdx
  143.      * 0xffffffff81066c54 <ia32_tracesys+80>:  mov    0x68(%rsp),%rsi
  144.      * 0xffffffff81066c59 <ia32_tracesys+85>:  mov    0x70(%rsp),%rdi
  145.      * 0xffffffff81066c5e <ia32_tracesys+90>:  mov    0x78(%rsp),%rax
  146.      * 0xffffffff81066c63 <ia32_tracesys+95>:  mov    (%rsp),%r15
  147.      * 0xffffffff81066c67 <ia32_tracesys+99>:  mov    0x8(%rsp),%r14
  148.      * 0xffffffff81066c6c <ia32_tracesys+104>: mov    0x10(%rsp),%r13
  149.      * 0xffffffff81066c71 <ia32_tracesys+109>: mov    0x18(%rsp),%r12
  150.      * 0xffffffff81066c76 <ia32_tracesys+114>: mov    0x20(%rsp),%rbp
  151.      * 0xffffffff81066c7b <ia32_tracesys+119>: mov    0x28(%rsp),%rbx
  152.      * 0xffffffff81066c80 <ia32_tracesys+124>: add    $0x30,%rsp
  153.      * 0xffffffff81066c84 <ia32_tracesys+128>: jmpq   0xffffffff81066bdc <ia32_do_syscall>
  154.      * End of assembler dump.
  155.      * (gdb) disassemble ia32_do_syscall
  156.      * Dump of assembler code for function ia32_do_syscall:
  157.      * 0xffffffff81066bdc <ia32_do_syscall+0>: cmp    $0x13d,%eax
  158.      * 0xffffffff81066be1 <ia32_do_syscall+5>: ja     0xffffffff81066c89 <ia32_badsys>
  159.      * 0xffffffff81066be7 <ia32_do_syscall+11>:        mov    %edi,%r8d
  160.      * 0xffffffff81066bea <ia32_do_syscall+14>:        mov    %ebp,%r9d
  161.      * 0xffffffff81066bed <ia32_do_syscall+17>:        xchg   %ecx,%esi
  162.      * 0xffffffff81066bef <ia32_do_syscall+19>:        mov    %ebx,%edi
  163.      * 0xffffffff81066bf1 <ia32_do_syscall+21>:        mov    %edx,%edx
  164.      * 0xffffffff81066bf3 <ia32_do_syscall+23>:        callq  *0xffffffff812e7c70(,%rax,8)
  165.      * End of assembler dump.
  166.      * (gdb) x/xw ia32_do_syscall+23
  167.      * 0xffffffff81066bf3 <ia32_do_syscall+23>:        0x70c514ff
  168.      * (gdb)
  169.      *
  170.      */
  171.     p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\xc5", 3);
  172.     if (p == NULL) {
  173.         printk("opcode not found, meats that we cannot find sys_call_table.\n");
  174.         return 0;
  175.     }

  176.     retval = *(unsigned long *) (p + 3);
  177.     return retval;
  178. #undef OFFSET_SYSCALL
  179. }/*}}}*/


  180. /**
  181. * Find the location of long-mode sys_call_table
  182. */
  183. static unsigned long get_syscall_table_long(void)
  184. {/*{{{*/
  185. #define OFFSET_SYSCALL 150
  186.     unsigned long syscall_long, retval;
  187.     char sc_asm[OFFSET_SYSCALL];

  188.     rdmsrl(MSR_LSTAR, syscall_long);
  189.     dbgprint("long mode: system_call is at %p\n", (void *)syscall_long);

  190.     memcpy(sc_asm, (char *)syscall_long, OFFSET_SYSCALL);


  191.     /**
  192.      * Find callq *sys_call_table(,%rax,8) 
  193.      * -------------------------------------
  194.      * (gdb) disassemble system_call
  195.      * Dump of assembler code for function system_call:
  196.      * 0xffffffff81063750 <system_call+0>:     swapgs
  197.      * 0xffffffff81063753 <system_call+3>:     mov    %rsp,%gs:0x18
  198.      * 0xffffffff8106375c <system_call+12>:    mov    %gs:0x10,%rsp
  199.      * 0xffffffff81063765 <system_call+21>:    sti
  200.      * 0xffffffff81063766 <system_call+22>:    sub    $0x50,%rsp
  201.      * 0xffffffff8106376a <system_call+26>:    mov    %rdi,0x40(%rsp)
  202.      * 0xffffffff8106376f <system_call+31>:    mov    %rsi,0x38(%rsp)
  203.      * 0xffffffff81063774 <system_call+36>:    mov    %rdx,0x30(%rsp)
  204.      * 0xffffffff81063779 <system_call+41>:    mov    %rax,0x20(%rsp)
  205.      * 0xffffffff8106377e <system_call+46>:    mov    %r8,0x18(%rsp)
  206.      * 0xffffffff81063783 <system_call+51>:    mov    %r9,0x10(%rsp)
  207.      * 0xffffffff81063788 <system_call+56>:    mov    %r10,0x8(%rsp)
  208.      * 0xffffffff8106378d <system_call+61>:    mov    %r11,(%rsp)
  209.      * 0xffffffff81063791 <system_call+65>:    mov    %rax,0x48(%rsp)
  210.      * 0xffffffff81063796 <system_call+70>:    mov    %rcx,0x50(%rsp)
  211.      * 0xffffffff8106379b <system_call+75>:    mov    %gs:0x10,%rcx
  212.      * 0xffffffff810637a4 <system_call+84>:    sub    $0x1fd8,%rcx
  213.      * 0xffffffff810637ab <system_call+91>:    testl  $0x181,0x10(%rcx)
  214.      * 0xffffffff810637b2 <system_call+98>:    jne    0xffffffff81063889 <tracesys>
  215.      * 0xffffffff810637b8 <system_call+104>:   cmp    $0x117,%rax
  216.      * 0xffffffff810637be <system_call+110>:   ja     0xffffffff8106387b <badsys>
  217.      * 0xffffffff810637c4 <system_call+116>:   mov    %r10,%rcx
  218.      * 0xffffffff810637c7 <system_call+119>:   callq  *0xffffffff812e6d60(,%rax,8)
  219.      * 0xffffffff810637ce <system_call+126>:   mov    %rax,0x20(%rsp)
  220.      * End of assembler dump.
  221.      * (gdb) x/xw system_call+119
  222.      * 0xffffffff810637c7 <system_call+119>:   0x60c514ff
  223.      * (gdb)
  224.      */
  225.     retval = (unsigned long) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\xc5", 3);
  226.     if ( retval != 0 ) {
  227.         dbgprint("long mode : sys_call_table is at %p\n", 
  228.                 (void *) (* (unsigned long *)(retval+3)) ) ;
  229.         retval = (unsigned long) ( * (unsigned long *)(retval+3) );
  230.     } else {
  231.         printk("long mode : memmem found nothing, returning NULL:( \n");
  232.         retval = 0;
  233.     } 

  234. #undef OFFSET_SYSCALL
  235.     return retval;
  236. }/*}}}*/




  237. static int get_syscall_init_module(void)
  238. {/*{{{*/
  239.     printk(KERN_DEBUG "get_syscall: Hi, you fucking linux!\n");

  240.     my_ia32_syscall_table = (void **) get_syscall_table_ia32();
  241.     if (my_ia32_syscall_table == 0)
  242.         return -1;

  243.     my_syscall_table = (void **) get_syscall_table_long();
  244.     if (my_syscall_table == 0)
  245.         return -1;

  246.     dbgprint("ia32_sys_call_table address : %p\n", (void *) my_ia32_syscall_table);
  247.     dbgprint("long mode : sys_call_table address : %p\n", (void *) my_syscall_table);

  248. #define REPLACE_IA32(x) o_sys_##x = my_ia32_syscall_table[__NR_ia32_##x];\
  249.     my_ia32_syscall_table[__NR_ia32_##x] = my_sys_##x

  250. #define GET_OLD_IA32_SYSCALL_ADDR(x) my_ia32_syscall_table[__NR_ia32_##x]
  251. //    dbgprint("sys_read at %p, my_read at %p\n", (void *)GET_OLD_IA32_SYSCALL_ADDR(read), (void *)my_sys_read);

  252. //    REPLACE_IA32(read);
  253. #undef REPLACE_IA32


  254.     return 0;
  255. }/*}}}*/


  256. static void get_syscall_exit_module(void)
  257. {/*{{{*/
  258.     printk(KERN_DEBUG "get_syscall: bye, you fucking linux!\n");

  259. #define RESTORE_IA32(x) my_ia32_syscall_table[__NR_ia32_##x] = o_sys_##x

  260. //    RESTORE_IA32(read);

  261. #undef RESTORE_IA32
  262. }/*}}}*/

  263. module_init(get_syscall_init_module);
  264. module_exit(get_syscall_exit_module);
复制代码




Reference :
http://phrack.org/archives/58/p58-0x07
 posted on 2011-08-26 16:56  chingliuyu  阅读(890)  评论(0编辑  收藏  举报