我试图编写一些简单的测试代码,作为挂钩系统调用表的演示.

"sys_call_table"在2.6中不再导出,所以我只是从系统中获取地址.映射文件,我可以看到它是正确的(查看内存中我找到的地址,我可以看到指向系统调用的指针).

然而,当我试图修改这个表时,内核给出了一个带有"无法处理虚拟地址c061e4f4处的内核分页请求"的"Oops",并且机器重新启动.

这里是CentOS 5.4,运行2.6.18-164.10.1.el5.有什么保护措施吗,还是我只是有个小虫子?我知道它与SELinux一起提供,我也try 过将其设置为允许模式,但这没有什么区别

这是我的代码:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>

void **sys_call_table;

asmlinkage int (*original_call) (const char*, int, int);

asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
   printk("A file was opened\n");
   return original_call(file, flags, mode);
}

int init_module()
{
    // sys_call_table address in System.map
    sys_call_table = (void*)0xc061e4e0;
    original_call = sys_call_table[__NR_open];

    // Hook: Crashes here
    sys_call_table[__NR_open] = our_sys_open;
}

void cleanup_module()
{
   // Restore the original call
   sys_call_table[__NR_open] = original_call;
}

推荐答案

我终于自己找到了答案.

http://www.linuxforums.org/forum/linux-kernel/133982-cannot-modify-sys_call_table.html

内核在某个时刻被更改,因此系统调用表是只读的.

cypherpunk:

即使已经晚了,但解决方案

.section .rodata,"a"
#include "syscall_table_32.S"

sys_call_table -> ReadOnly You have to compile the Kernel new if you want to "hack" around with sys_call_table...

该链接还具有将存储器更改为可写的示例.

nasekomoe:

大家好.感谢您的回复.我 很久以前就通过以下方式解决了这个问题 修改对存储器页的访问.我 我已经实现了两个函数,它们可以 适用于我的上级代码:

#include <asm/cacheflush.h>
#ifdef KERN_2_6_24
#include <asm/semaphore.h>
int set_page_rw(long unsigned int _addr)
{
    struct page *pg;
    pgprot_t prot;
    pg = virt_to_page(_addr);
    prot.pgprot = VM_READ | VM_WRITE;
    return change_page_attr(pg, 1, prot);
}

int set_page_ro(long unsigned int _addr)
{
    struct page *pg;
    pgprot_t prot;
    pg = virt_to_page(_addr);
    prot.pgprot = VM_READ;
    return change_page_attr(pg, 1, prot);
}

#else
#include <linux/semaphore.h>
int set_page_rw(long unsigned int _addr)
{
    return set_memory_rw(_addr, 1);
}

int set_page_ro(long unsigned int _addr)
{
    return set_memory_ro(_addr, 1);
}

#endif // KERN_2_6_24

以下是原始代码的修改版本,适用于我.

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>
#include <asm/semaphore.h>
#include <asm/cacheflush.h>

void **sys_call_table;

asmlinkage int (*original_call) (const char*, int, int);

asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
   printk("A file was opened\n");
   return original_call(file, flags, mode);
}

int set_page_rw(long unsigned int _addr)
{
   struct page *pg;
   pgprot_t prot;
   pg = virt_to_page(_addr);
   prot.pgprot = VM_READ | VM_WRITE;
   return change_page_attr(pg, 1, prot);
}

int init_module()
{
    // sys_call_table address in System.map
    sys_call_table = (void*)0xc061e4e0;
    original_call = sys_call_table[__NR_open];

    set_page_rw(sys_call_table);
    sys_call_table[__NR_open] = our_sys_open;
}

void cleanup_module()
{
   // Restore the original call
   sys_call_table[__NR_open] = original_call;
}

C++相关问答推荐

在CIL中运行时,sscanf返回0

CC crate 示例不会与C函数链接

修改pGM使用指针填充2-D数组但不起作用

ISO_C_BINDING,从Fortran调用C

如何解决C中的严格别名?

Malloc(sizeof(char[Length]))是否不正确?

空指针的运行时强制转换

如何将字符串argv[]赋给C中的整型数组?

如何在C宏中确定 struct 中元素的类型?

如果我释放其他内容,返回值就会出错

为什么即使在强制转换时,此代码也会溢出?

自定义变参数函数的C预处置宏和警告 suppress ?

Linux不想运行编译后的文件

S在本文中的价值观到底出了什么问题?

Printf()在C中打印终止字符之后的字符,我该如何解决这个问题?

从BIOS(8086)中读取刻度需要多少?

为什么我的旧式&q;函数在传递浮点数时会打印2?

STM32 FATFS用户手册(Um1721)中的代码正确吗?

为什么写入关闭管道会返回成功

UEFI 应用程序中的计时器回调仅在 AMI BIOS 中挂起