Linux内核中 如何动态替换系统调用函数

 

         通过修改内核源码修改系统调用,对一般的开发者来说太费时间,使用动态模块修改系统调用,省时省力,能够快速切换。

 

         先通过getSyscallTable(void)获得内存中的系统调用表的地址,然后就可以将自己的函数指针放在上面了。千万别忘了保存和恢复原来的系统调用指针!

 

 

 

看看源代码吧!!

 

 

Makefile:

obj-m := syscall.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module.symvers

 

 

 

syscall.c:

#ifndef __KERNEL__
# define __KERNEL__
#endif
                                                                                                              
#ifndef MODULE
# define MODULE
#endif
                                                                                                              
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <asm/unistd.h>

#ifdef CONFIG_SMP
#define __SMP__
#endif

 

/*
     系统调用号的定义在 /usr/include/asm/unistd.h 文件中
     printk结果存在/proc/kmsg中。
     可以用cat /boot/System.map-`uname -r` |grep sys_call_table 查看table的地址
*/
#define __NR_testsyscall 39

unsigned long **sys_call_table;

struct idt_tag
{
    unsigned short offset_low,segment_select;
    unsigned char reserved,flags;
    unsigned short offset_high;
};

unsigned long *  savedcall;

static unsigned long getSyscallTable(void)
{
    unsigned char idtr[6],*shell,*sort;
    struct idt_tag *idt;
    unsigned long system_call,sct;
    unsigned short offset_low,offset_high;
    char *p;
    int i;
    __asm__("sidt %0":"=m"(idtr));
    idt = (struct idt_tag*)((*(unsigned long*)&idtr[2]) + 8 * 0x80);
    offset_low = idt->offset_low;
    offset_high = idt->offset_high;
    system_call = (offset_high)<<16 | offset_low;   
    shell = (char*)system_call;
    sort = "/xff/x14/x85";

    for(i = 0;i < 100-2;i++)
        if(shell [ i ] == sort[0] && shell[i+1] == sort[1] && shell[i+2] == sort[2])
            break;
    p = &shell [ i ] + 3;
    sct = *(unsigned long*)p;
    return sct;   
}


asmlinkage long testsyscall(char *buf)
{
 printk("hello world/n");
 char* b="hello world/n";
 if(copy_to_user(buf,b,strlen(b))!=strlen(b))
 {
  printk("复制失败/n");
       return -1;
 }
 printk("复制成功/n");
 return 0;
}

int init_module(void)
{
 sys_call_table = (unsigned long**)getSyscallTable();
 printk("***系统调用表的首地址为%p/n",sys_call_table);
 savedcall=sys_call_table[__NR_testsyscall];//保存原来的调用
 printk("原来的%d系统调用为: %p/n",__NR_testsyscall,savedcall);
 printk("目的系统调用为%p/n",(unsigned long*)testsyscall);
 sys_call_table[__NR_testsyscall]=(unsigned long*)testsyscall;
 printk("更改后的%d系统调用为: %p/n",__NR_testsyscall,sys_call_table[__NR_testsyscall]);
 printk("loaded success /n");
 
      return 0;
}
   
void cleanup_module(void)
{
 printk("恢复前的%d系统调用为: %p/n",__NR_testsyscall,sys_call_table[__NR_testsyscall]);
 sys_call_table[__NR_testsyscall]=savedcall;
 printk("恢复后的%d系统调用为: %p/n",__NR_testsyscall,sys_call_table[__NR_testsyscall]);
 printk("unloaded success/n");
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("QCH");

 

 

 

 

测试程序:

#include <linux/unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define __NR_testsyscall 39

 int testsyscall(char *buf){
     return syscall(__NR_testsyscall,buf);
  }

int main()
{
 char buf[128];
 int i;
 for(i=0;i<128;i++)
 {
      buf[i]='/0';
 }
 long r=-10;
 r=testsyscall(buf);
 printf("%s/n",buf);
 
 
 return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值