求助!如何获得系统的sys_call_table?

内核编译和嵌入式产品的设计与开发
回复
jh31777
帖子: 4
注册时间: 2008-10-01 18:40

求助!如何获得系统的sys_call_table?

#1

帖子 jh31777 » 2009-04-27 18:19

我从网站上搜的,但是不知道怎么运行,
希望那位高手能给我详细介绍一下,谢谢
说一下我要的结果,我只要能把sys_call_table的地址就行。



linux中所有的syscall都是调用int 0x80, int 0x80的中断服务程序为system_call(arch/x86/kernel /traps_32.c:set_system_gate(SYSCALL_VECTOR,&system_call). system_call (arch/x86/entry_32.S)最终call *sys_call_table(,%eax,4)来完成一个syscall调用.

即 int 0x80 -> system_call -> sys_call_table, 这样我们只要首先获取int 0x80的中断服务地址system_call地址, 然后在system_call代码中直接搜索call指令即可找到sys_call_table地址了!

内核版本: ubuntu 2.6.24-19-server



/*
* Standard in kernel modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module, */
#include <linux/moduleparam.h> /* which will have params */
#include <linux/unistd.h> /* The list of system calls */

/*
* For the current (process) structure, we need
* this to know who the current user is.
*/
#include <linux/sched.h>
#include <asm/uaccess.h>


unsigned long *sys_call_table = 0;
//EXPORT_SYMBOL ( sys_call_table );
struct {
unsigned short limit;
unsigned int base;
} __attribute__ ( ( packed ) ) idtr;
struct {
unsigned short offset_low;
unsigned short segment_select;
unsigned char reserved, flags;
unsigned short offset_high;
} __attribute__ ( ( packed ) ) * idt;


unsigned long* find_sys_call_table(void)
{
unsigned long system_call = 0; // x80中断处理程序system_call 地址
char *call_hex = "\xff\x14\x85"; // call 指令
char *code_ptr = NULL;
char *p = NULL;
unsigned long sct = 0x0;
int i = 0;

// 获取中断描述符表寄存器的地址
__asm__ ( "sidt %0": "=m" ( idtr ) );
// 获取0x80中断处理程序的地址
idt = ( void * ) ( idtr.base + 8 * 0x80 );
system_call = ( idt->offset_high << 16 ) | idt->offset_low;

// 搜索system_call代码
code_ptr = (char *)system_call;
for(i = 0;i < ( 100 - 2 ); i++) {
//// 查找call指令
if(code_ptr == call_hex[0]
&& code_ptr[i+1] == call_hex[1]
&& code_ptr[i+2] == call_hex[2] ) {
//
p = &code_ptr + 3;
break;
}
}
if ( p ){
sct = *(unsigned long*)p;
}
return (unsigned long*)sct;
}

int init_module ( void ) {

if ( ( sys_call_table = find_sys_call_table() ) ) {
printk( "sys_call_table = %p\n", sys_call_table );
}
return 0;
}

void cleanup_module ( void ) {
}





Makefile文件:



obj-m = get_sct.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
回复