- 论坛徽章:
- 0
|
看下我的问题,我的代码试图替换read系统调用,但insmod之后,体统会回到login界面,然后一直提示错误的文件描述符,为什么?代码如下
#include <linux/fs.h>
#include <linux/unistd.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
int errno;
int orig_cr0;
static inline _syscall3(ssize_t,read,int,fd,void*,buf,size_t,count);
static inline _syscall3(ssize_t,write,int,fd,void*,buf,size_t,count);
int (*original_read)(int,void *,size_t);
int (*original_write)(int ,void *,size_t);
struct {
unsigned short limit;
unsigned int base;
}__attribute__((packed)) idtr;
struct {
unsigned short off1;
unsigned short sel;
unsigned char none,flagg;
unsigned short off2;
}__attribute__((packed)) idt;
unsigned int clear_and_return_cr0(void)
{
unsigned int cr0 = 0;
unsigned int ret;
asm volatile ("movl %%cr0, %%eax"
: "=a"(cr0)
);
ret = cr0;
/*clear the 20th bit of CR0,*/
cr0 &= 0xfffeffff;
asm volatile ("movl %%eax, %%cr0"
:
: "a"(cr0)
);
return ret;
}
void setback_cr0(unsigned int val)
{
asm volatile ("movl %%eax, %%cr0"
:
: "a"(val)
);
}
void ** get_sys_call_table(void){
unsigned int sys_call_off;
unsigned int sys_call_table;
unsigned char *p;
int i;
asm("sidt %0":"=m"(idtr));
printk("addr of idtr:%x\n",&idtr);
memcpy(&idt,(void *)(idtr.base+8*0x80),sizeof(idt));
sys_call_off = (idt.off2<< 16|idt.off1);
printk("addr of idt ox80:%x\n",sys_call_off);
p =(unsigned char *)sys_call_off;
for (i=0;i<100;i++){
if (p==0xff && p[i+1]==0x14 && p[i+2] == 0x85){
sys_call_table = *(unsigned int *)(p+i+3);
printk("i:[%d],addr of sys_call_table:%x\n",i,sys_call_table);
return sys_call_table;
}
}
return (void **)0;
}
int mystrncmp(char *str1,char *str2,int len){
int index = 0 ;
for (;index < len && *str1 && *str2;index++){
if (*(str1++) != *(str2++))
return -1;
}
if (index >=len)
return 0;
return -1;
}
int hacked_read(int fd,char *buf,size_t count){
int res = original_read(fd,buf,count); //与int res = (*original_read)(fd,buf,count);情况相同
return res;
}
int hacked_write(int fd,char *buf,size_t count){
int res = 0;
/* if (mystrncmp(buf,"zhzh",4)){
buf[0] = 'a';
buf[1] = 'b';
buf[2] = 'c';
buf[3] = 'd';
}
*/
res = original_write(fd,buf,count);
return res;
}
int init_module(void){
void **sys_call_table = get_sys_call_table();
original_read = sys_call_table[__NR_read];
original_write = sys_call_table[__NR_write];
orig_cr0 = clear_and_return_cr0();
sys_call_table[__NR_read]= hacked_read;
// sys_call_table[__NR_write]= hacked_write;
setback_cr0(orig_cr0);
return 0;
}
void cleanup_module(void)
{ void **sys_call_table = get_sys_call_table();
orig_cr0 = clear_and_return_cr0();
sys_call_table[__NR_read] = original_read;
// sys_call_table[__NR_write] = original_write;
setback_cr0(orig_cr0);
}
[ 本帖最后由 空灵静世 于 2009-10-28 10:18 编辑 ] |
|