免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2175 | 回复: 9
打印 上一主题 下一主题

[内核模块] 劫持read系统调用出现的bug [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-01-11 20:23 |只看该作者 |倒序浏览
本帖最后由 wugj03 于 2015-01-13 10:47 编辑

自定义的read函数如下:
  1. asmlinkage long new_read(unsigned int fd, char __user *buf, size_t count)
  2. {
  3.         long ret;
  4.         struct file *file;
  5.         char *pathname;

  6.         ret = old_read(fd, buf, count);
  7.         /*0 represent EOF
  8.         * We just collect a 'read' when finished
  9.         */
  10.         if(ret == 0){
  11.                 file = fget(fd);
  12.                 if(file){
  13.                         pathname = kmalloc(256, GFP_KERNEL);
  14.                         if(!pathname)
  15.                               return ret;
  16.                         strcpy(pathname, file->f_dentry->d_iname);
  17.                         pr_info("[%s:%d] READ '%s '\n", current->comm, current->pid, pathname);
  18.                         kfree(pathname);
  19.                         fput(file);
  20.                 }
  21.         }
  22.         return ret;
  23. }
复制代码
1.bug重现的情况:
#insmod prov.ko (涉及的模块)
...read some files...
#rmmod prov
#insmod prov.ko
点击另一个tty终端上,即获得焦点,系统就崩溃了。
PS:a.在第一次insmod时,点击另外一个tty终端,是不会出错的。
b. 去掉if(ret ==0){...}, 也是不会出现panic的
c.不是每次都会出现,但基本都出现,否则就要多一次rmmod,insmod,才出现panic

2. kdump的结果log信息如下:


3. crash工具上的信息



被这个bug折磨几天了,求大神拯救

论坛徽章:
0
2 [报告]
发表于 2015-01-11 20:24 |只看该作者
同样的记录信息的办法,劫持write系统调用,是没有问题的

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
3 [报告]
发表于 2015-01-12 09:08 |只看该作者
                        pathname = kmalloc(256, GFP_KERNEL);
                        strcpy(pathname, file->f_dentry->d_iname);
                        pr_info("[%s:%d] READ '%s '\n", current->comm, current->pid, pathname);
                        kfree(prec.p_pathname);
这里的kmalloc和kfree看似不匹配,kfree为何是prec.p_pathname?

论坛徽章:
0
4 [报告]
发表于 2015-01-12 09:43 |只看该作者
回复 3# humjb_1983

不好意思,忘记改过来就帖上去了,代码是kfree(pathname)的
就是在if(ret == 0){...} 只加fget和fput,都会出现同样的错误
   

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
5 [报告]
发表于 2015-01-12 10:57 |只看该作者
wugj03 发表于 2015-01-12 09:43
回复 3# humjb_1983

不好意思,忘记改过来就帖上去了,代码是kfree(pathname)的

如果是这样的话,那应该是其它代码的问题吧,把其它修改的代码也贴来看看?

论坛徽章:
0
6 [报告]
发表于 2015-01-12 15:47 |只看该作者
回复 5# humjb_1983


   获取系统调用表是用 http://bbs.chinaunix.net/thread-1946913-1-1.html 介绍的方法来获取的。

如果不劫持其他系统调用,只劫持了read系统调用,就不行。
请问是要看具体到那一块的代码呢

论坛徽章:
0
7 [报告]
发表于 2015-01-12 16:04 |只看该作者
回复 5# humjb_1983

全部代码如下
  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/fs.h>
  10. #include <linux/file.h>
  11. #include <linux/smp.h>
  12. #include <linux/user.h>
  13. #include <linux/errno.h>
  14. #include <linux/cpu.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/fcntl.h>
  17. #include <asm/unistd.h>

  18. MODULE_DESCRIPTION("Intercept the system call table in Linux");
  19. MODULE_LICENSE("GPL");


  20. /* comment the following line to shut me up */
  21. #define INTERCEPT_DEBUG

  22. #ifdef INTERCEPT_DEBUG
  23.     #define dbgprint(format,args...) \
  24.         printk("intercept: function:%s-L%d: "format, __FUNCTION__, __LINE__, ##args);
  25. #else
  26.     #define dbgprint(format,args...)  do {} while(0);
  27. #endif


  28. /**
  29. * * the system call table
  30. * */
  31. void **my_table;

  32. unsigned int orig_cr0;

  33. /**
  34. * * the original syscall functions
  35. * */
  36. asmlinkage long (*old_read) (unsigned int fd, char __user *buf, size_t count);


  37. struct idtr {
  38.     unsigned short limit;
  39.     unsigned int base;
  40. } __attribute__ ((packed));


  41. struct idt {
  42.     unsigned short off1;
  43.     unsigned short sel;
  44.     unsigned char none, flags;
  45.     unsigned short off2;
  46. } __attribute__ ((packed));



  47. /**
  48. * * clear WP bit of CR0, and return the original value
  49. * */
  50. unsigned int clear_and_return_cr0(void)
  51. {
  52.     unsigned int cr0 = 0;
  53.     unsigned int ret;

  54.     asm volatile ("movl %%cr0, %%eax"
  55.               : "=a"(cr0)
  56.               );
  57.     ret = cr0;

  58.     /* clear the 20 bit of CR0, a.k.a WP bit */
  59.     cr0 &= 0xfffeffff;

  60.     asm volatile ("movl %%eax, %%cr0"
  61.               :
  62.               : "a"(cr0)
  63.               );
  64.     return ret;
  65. }

  66. /** set CR0 with new value
  67. * *
  68. * * @val : new value to set in cr0
  69. * */
  70. void setback_cr0(unsigned int val)
  71. {
  72.     asm volatile ("movl %%eax, %%cr0"
  73.               :
  74.               : "a"(val)
  75.               );
  76. }


  77. /**
  78. * * Return the first appearence of NEEDLE in HAYSTACK.  
  79. * * */
  80. static void *memmem(const void *haystack, size_t haystack_len,
  81.             const void *needle, size_t needle_len)
  82. {/*{{{*/
  83.     const char *begin;
  84.     const char *const last_possible
  85.         = (const char *) haystack + haystack_len - needle_len;

  86.     if (needle_len == 0)
  87.         /* The first occurrence of the empty string is deemed to occur at
  88. *            the beginning of the string.  */
  89.         return (void *) haystack;

  90.     /* Sanity check, otherwise the loop might search through the whole
  91. *        memory.  */
  92.     if (__builtin_expect(haystack_len < needle_len, 0))
  93.         return NULL;

  94.     for (begin = (const char *) haystack; begin <= last_possible;
  95.          ++begin)
  96.         if (begin[0] == ((const char *) needle)[0]
  97.             && !memcmp((const void *) &begin[1],
  98.                    (const void *) ((const char *) needle + 1),
  99.                    needle_len - 1))
  100.             return (void *) begin;

  101.     return NULL;
  102. }/*}}}*/


  103. /**
  104. * * Find the location of sys_call_table
  105. * */
  106. static unsigned long get_sys_call_table(void)
  107. {/*{{{*/
  108. /* we'll read first 100 bytes of int $0x80 */
  109. #define OFFSET_SYSCALL 100        

  110.     struct idtr idtr;
  111.     struct idt idt;
  112.     unsigned sys_call_off;
  113.     unsigned retval;
  114.     char sc_asm[OFFSET_SYSCALL], *p;

  115.     /* well, let's read IDTR */
  116.     asm("sidt %0":"=m"(idtr)
  117.              :
  118.              :"memory" );

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

  120.     /* Read in IDT for vector 0x80 (syscall) */
  121.     memcpy(&idt, (char *) idtr.base + 8 * 0x80, sizeof(idt));

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

  123.     dbgprint("idt80: flags=%X sel=%X off=%X\n",
  124.                  (unsigned) idt.flags, (unsigned) idt.sel, sys_call_off);

  125.     /* we have syscall routine address now, look for syscall table
  126. *        dispatch (indirect call) */
  127.     memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);

  128.     /**
  129. *      * Search opcode of `call sys_call_table(,eax,4)'
  130. *           */
  131.     p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\x85", 3);
  132.     if (p == NULL)
  133.         return 0;

  134.     retval = *(unsigned *) (p + 3);
  135.     if (p) {
  136.         dbgprint("sys_call_table at 0x%x, call dispatch at 0x%x\n",
  137.              retval, (unsigned int) p);
  138.     }
  139.     return retval;
  140. #undef OFFSET_SYSCALL
  141. }/*}}}*/

  142. asmlinkage long new_read(unsigned int fd, char __user *buf, size_t count)
  143. {
  144.         long ret;
  145.         struct file *file;
  146.         ret = old_read(fd, buf, count);
  147.         /*0 represent EOF
  148.         * We just collect a 'read' when finished
  149.         */
  150.         if(ret == 0){
  151.                 file = fget(fd);
  152.                 if(file){
  153.                         fput(file);
  154.                 }
  155.         }
  156.         return ret;
  157. }

  158. static int intercept_init(void)
  159. {
  160.     my_table = (void **)get_sys_call_table();
  161.     if (my_table == NULL)
  162.         return -1;

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

  164. #define REPLACE(x) old_##x = my_table[__NR_##x];\
  165.     my_table[__NR_##x] = new_##x

  166.    
  167.         REPLACE(read);

  168. #undef REPLACE
  169.     return 0;
  170. }


  171. static int __init this_init(void)
  172. {
  173.     int ret;
  174.     printk("syscall intercept: Hi, poor linux!\n");

  175.     orig_cr0 = clear_and_return_cr0();   
  176.     ret = intercept_init();
  177.     setback_cr0(orig_cr0);

  178.     return ret;
  179. }

  180. static void __exit this_fini(void)
  181. {
  182.     printk("syscall intercept: bye, poor linux!\n");

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

  184.     orig_cr0 = clear_and_return_cr0();   
  185.         RESTORE(read);
  186.     setback_cr0(orig_cr0);

  187. #undef RESTORE
  188. }

  189. module_init(this_init);
  190. module_exit(this_fini);
复制代码

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
8 [报告]
发表于 2015-01-15 09:11 |只看该作者
没有明显的疑点。代码看似没有大问题。如下一个小问题:
181行long ret;
old_read函数返回值应该是ssize_t类型的,在32位系统中为int型的,建议改改。

从故障最终的表现看,应该是堆栈被**了,EIP指针都不对了,导致堆栈解析不了。不太好直接看出问题。可能是内存越界之类的~
要定位的话,估计得再仔细分析下vmcore,或者进一步打点看看了。

论坛徽章:
0
9 [报告]
发表于 2015-01-15 14:46 |只看该作者
回复 8# humjb_1983

我是以include/linux/syscalls.h的函数原型来定义的,那里就是

asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);
这个会有影响吗?
   

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
10 [报告]
发表于 2015-01-15 15:52 |只看该作者
wugj03 发表于 2015-01-15 14:46
回复 8# humjb_1983

我是以include/linux/syscalls.h的函数原型来定义的,那里就是

呵呵,我看我的环境(3.10版本)跟你的可能不一样。
看起来影响也不大,只是暂时没有其它想法了。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP