+ -
当前位置:首页 → 问答吧 → 请问改写系统调用函数的时候能否嵌套调用别的系统调用?

请问改写系统调用函数的时候能否嵌套调用别的系统调用?

时间:2010-07-30

来源:互联网

我按照论坛上的方法修改new_open函数的时候,想先在文件之中写点东西,就找到sys_call_table之中write和open函数的指针赋给old_write和old_open。
然后在new_open里面先调用old_open获得fd,然后调用old_write,但是创建文件的时候debug...old_write返回-14,打开文件的时候old_write返回-9。
查询错误列表一个是bad address,一个是Bad file number。
请问各位大大,这代表什么意思?fd是打开文件返回的,参数貌似应该没错啊。

作者: cdwzt   发布时间: 2010-07-30

贴 一下你的代码

作者: 0vk0   发布时间: 2010-07-31

本帖最后由 cdwzt 于 2010-07-31 09:57 编辑

回复 0vk0


    哦!好的!我是在Godbach 大大发的帖子的基础上改的。主要是open调用里面添加了点内容,但是查看系统日志发现在open里面调用old_write没有写成功,实际上文件也还是原来的大小。
但是lseek返回了正确的值(定义的宏#define PAGE_RESERVED 1000)
  1. #include <linux/kernel.h>

  2. #include <linux/init.h>

  3. #include <linux/module.h>

  4. #include <linux/kprobes.h>

  5. #include <linux/kallsyms.h>

  6. #include <linux/sched.h>

  7. #include <linux/ptrace.h>

  8. #include <linux/mm.h>

  9. #include <linux/smp.h>

  10. #include <linux/user.h>

  11. #include <linux/errno.h>

  12. #include <linux/cpu.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/fs.h>           /*struct file*/
  15. #include <linux/file.h>         /*fget() fput()*/
  16. #include <linux/fdtable.h>      /*NR_OPEN_DEFAULT*/
  17. #include <linux/dcache.h>       /*d_path()*/
  18. #include <linux/random.h>
  19. #include <linux/syscalls.h>

  20. #include <asm/uaccess.h>

  21. #include <asm/fcntl.h>

  22. #include <asm/unistd.h>



  23. #define PAGE_RESERVED (4*1024)



  24. MODULE_DESCRIPTION("Intercept the system call table in Linux");

  25. MODULE_AUTHOR("alert7 ([email][email protected][/email]) \n\t\talbcamus <[email][email protected][/email]>");

  26. MODULE_LICENSE("GPL");





  27. /* comment the following line to shut me up */

  28. #define INTERCEPT_DEBUG



  29. #ifdef INTERCEPT_DEBUG

  30.     #define dbgprint(format,args...) \

  31.         printk("intercept: function:%s-L%d: "format, __FUNCTION__, __LINE__, ##args);

  32. #else

  33.     #define dbgprint(format,args...)  do {} while(0);

  34. #endif





  35. /**

  36. * the system call table

  37. */

  38. void **my_table;



  39. unsigned int orig_cr0;



  40. /**

  41. * the original syscall functions

  42. */

  43. asmlinkage long (*old_open) (char __user *filename, int flags, int mode);

  44. asmlinkage int (*old_read) (unsigned int fd, char __user *buf, size_t count);

  45. asmlinkage int (*old_write) (unsigned int fd, char __user *buf, size_t count);
  46. asmlinkage long (*old_lseek) (unsigned int fd, off_t offset, unsigned int origin);



  47. struct idtr {

  48.     unsigned short limit;

  49.     unsigned int base;

  50. } __attribute__ ((packed));





  51. struct idt {

  52.     unsigned short off1;

  53.     unsigned short sel;

  54.     unsigned char none, flags;

  55.     unsigned short off2;

  56. } __attribute__ ((packed));





  57. /**

  58. * clear WP bit of CR0, and return the original value

  59. */

  60. unsigned int clear_and_return_cr0(void)

  61. {

  62.     unsigned int cr0 = 0;

  63.     unsigned int ret;



  64.     asm volatile ("movl %%cr0, %%eax"

  65.               : "=a"(cr0)

  66.               );

  67.     ret = cr0;



  68.     /* clear the 16 bit of CR0, a.k.a WP bit */

  69.     cr0 &= 0xfffeffff;



  70.     asm volatile ("movl %%eax, %%cr0"

  71.               :

  72.               : "a"(cr0)

  73.               );

  74.     return ret;

  75. }



  76. /** set CR0 with new value

  77. *

  78. * @val : new value to set in cr0

  79. */

  80. void setback_cr0(unsigned int val)

  81. {

  82.     asm volatile ("movl %%eax, %%cr0"

  83.               :

  84.               : "a"(val)

  85.               );

  86. }





  87. /**

  88. * Return the first appearence of NEEDLE in HAYSTACK.  

  89. * */

  90. static void *memmem(const void *haystack, size_t haystack_len,

  91.             const void *needle, size_t needle_len)

  92. {/*{{{*/

  93.     const char *begin;

  94.     const char *const last_possible

  95.         = (const char *) haystack + haystack_len - needle_len;



  96.     if (needle_len == 0)

  97.         /* The first occurrence of the empty string is deemed to occur at

  98.            the beginning of the string.  */

  99.         return (void *) haystack;



  100.     /* Sanity check, otherwise the loop might search through the whole

  101.        memory.  */

  102.     if (__builtin_expect(haystack_len < needle_len, 0))

  103.         return NULL;



  104.     for (begin = (const char *) haystack; begin <= last_possible;

  105.          ++begin)

  106.         if (begin[0] == ((const char *) needle)[0]

  107.             && !memcmp((const void *) &begin[1],

  108.                    (const void *) ((const char *) needle + 1),

  109.                    needle_len - 1))

  110.             return (void *) begin;



  111.     return NULL;

  112. }/*}}}*/





  113. /**

  114. * Find the location of sys_call_table

  115. */

  116. static unsigned long get_sys_call_table(void)

  117. {/*{{{*/

  118. /* we'll read first 100 bytes of int $0x80 */

  119. #define OFFSET_SYSCALL 100        



  120.     struct idtr idtr;

  121.     struct idt idt;

  122.     unsigned sys_call_off;

  123.     unsigned retval;

  124.     char sc_asm[OFFSET_SYSCALL], *p;



  125.     /* well, let's read IDTR */

  126.     asm("sidt %0":"=m"(idtr)

  127.              :

  128.              :"memory" );



  129.     dbgprint("idtr base at 0x%X, limit at 0x%X\n", (unsigned int)idtr.base,(unsigned short)idtr.limit);



  130.     /* Read in IDT for vector 0x80 (syscall) */

  131.     memcpy(&idt, (char *) idtr.base + 8 * 0x80, sizeof(idt));



  132.     sys_call_off = (idt.off2 << 16) | idt.off1;



  133.     dbgprint("idt80: flags=%X sel=%X off=%X\n",

  134.                  (unsigned) idt.flags, (unsigned) idt.sel, sys_call_off);



  135.     /* we have syscall routine address now, look for syscall table

  136.        dispatch (indirect call) */

  137.     memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);



  138.     /**

  139.      * Search opcode of `call sys_call_table(,eax,4)'

  140.      */

  141.     p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\x85", 3);

  142.     if (p == NULL)

  143.         return 0;



  144.     retval = *(unsigned *) (p + 3);

  145.     if (p) {

  146.         dbgprint("sys_call_table at 0x%x, call dispatch at 0x%x\n",

  147.              retval, (unsigned int) p);

  148.     }

  149.     return retval;

  150. #undef OFFSET_SYSCALL

  151. }/*}}}*/







  152. /**

  153. * new_open - replace the original sys_open when initilazing,

  154. *           as well as be got rid of when removed

  155. */


  156. asmlinkage long new_open(char *filename, int flags, int mode)

  157. {

  158.             int i, write_return;
  159.         long fd;
  160.         unsigned char Flag, CNT[8];
  161.         char __user * CNT_user[8];
  162.         fd = old_open (filename, flags, mode);

  163.         //printk("open ...fd=%08X\n", fd);

  164.         i = 0;
  165.         while(((char*)(filename))[i++]!='\0');
  166.         if(i<=5)
  167.         {
  168.                 goto ret;
  169.         }
  170.         i -= 5;
  171.        
  172.         if(0 == (strcmp(&(((char*)(filename))[i]),".txt")))
  173.         {
  174.                 printk("Cteate(open) txt files...fd=%08X\n", fd);
  175.                 //get_random_bytes(CNT, 8);
  176.                 memset(CNT, 0xAA, 8);
  177.                 Flag = 1;
  178.                 printk("next write flags and counter...");
  179.                 old_write(fd, &Flag, 1);
  180.                 //copy_to_user(CNT_user, CNT, 8);
  181.                 write_return = old_write(fd, CNT, 8);
  182.                 printk("write return:%d\n", write_return);
  183.                 //i = old_lseek(fd, PAGE_RESERVED, SEEK_SET);
  184.                 //printk("lseek return:%x\n", i);
  185.         }



  186. ret:  

  187.     return fd;

  188. }



  189. asmlinkage int new_read(unsigned int fd, char *buf, size_t count)
  190. {
  191.         return old_read(fd, buf, count);
  192. }



  193. asmlinkage int new_write(unsigned int fd, char *buf, size_t count)
  194. {
  195.         return old_write(fd, buf, count);
  196. }

  197. asmlinkage long new_lseek(unsigned int fd, off_t offset, unsigned int origin)
  198. {
  199.         return old_lseek(fd, offset, origin);
  200. }


  201. static int intercept_init(void)

  202. {

  203.     my_table = (void **)get_sys_call_table();

  204.     if (my_table == NULL)

  205.         return -1;



  206.     dbgprint("sys call table address %p\n", (void *) my_table);



  207. #define REPLACE(x) old_##x = my_table[__NR_##x];\

  208.     my_table[__NR_##x] = new_##x



  209.    

  210.         REPLACE(open);
  211.         REPLACE(read);       
  212.         REPLACE(write);
  213.         REPLACE(lseek);

  214. #if 0

  215.     can_exec_fork = can_intercept_fork_exec();

  216.     if(can_exec_fork == 1)

  217.         REPLACE(execve);

  218. #endif



  219. #undef REPLACE

  220.     return 0;

  221. }











  222. static int __init this_init(void)

  223. {

  224.     int ret;

  225.     printk("syscall intercept: Hi, poor linux!\n");



  226.     orig_cr0 = clear_and_return_cr0();   

  227.     ret = intercept_init();

  228.     setback_cr0(orig_cr0);



  229.     return ret;

  230. }



  231. static void __exit this_fini(void)

  232. {

  233.     printk("syscall intercept: bye, poor linux!\n");



  234. #define RESTORE(x) my_table[__NR_##x] = old_##x



  235.     orig_cr0 = clear_and_return_cr0();   

  236.         RESTORE(open);
  237.         RESTORE(read);
  238.         RESTORE(write);
  239.         RESTORE(lseek);

  240. #if 0

  241.     if(can_exec_fork == 1)

  242.         RESTORE(execve);

  243. #endif

  244.     setback_cr0(orig_cr0);



  245. #undef RESTORE

  246. }



  247. module_init(this_init);

  248. module_exit(this_fini);
复制代码

作者: cdwzt   发布时间: 2010-07-31

热门下载

更多