更改内核函数指针的地址

时间:2014-08-30 11:31:56

标签: c pointers linux-kernel

我写了一个模块来尝试更改导出符号的地址' do_fork'在调用原始do_fork地址之前先指向我的函数。到目前为止,我似乎无法更改地址,因为它给出了作为左操作数分配所需的错误'左值。'

我不确定如何将指针do_fork()更改为我的函数fake_fork();

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/sched.h>
#include <linux/module.h>

int c=0;

long fake_fork(unsigned long a, unsigned long b, unsigned long c, int __user *d, int __user *e)
{
    ++c;

    return do_fork(a, b, c, d, e);
}

EXPORT_SYMBOL(fake_fork);

static int fork_proc_show(struct seq_file *m, void *v)
{
    seq_printf(m, "System Call fork called: %d times.\n", c);
    return 0;
}

static int fork_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, fork_proc_show, NULL);
}

static const struct file_operations fork_proc_fops = {
    .open       = fork_proc_open,
    .read       = seq_read,
    .llseek     = seq_lseek,
    .release    = single_release,
};

static int __init proc_fork_init(void)
{
    do_fork = fake_fork;  // <-- Not working

    printk("init proc forkcounter\n");
    proc_create("forkcounter", 0, NULL, &fork_proc_fops);
    return 0;
}

static void __exit cleanup_fork_module(void)
{
    remove_proc_entry("forkcounter",NULL);

    printk("cleanup proc forkcounter\n");
}

module_init(proc_fork_init);
module_exit(cleanup_fork_module);

1 个答案:

答案 0 :(得分:3)

您无法更改do_fork,它在运行时是常量。它是do_fork()函数的地址。

您无法为功能指定任何内容。这是因为函数的名称不是变量。它是常量指针。

相同
5 = 2 + 2;

您会收到相同的错误消息。

我假设您希望每次调用do_fork()时调用您的函数。它实施起来会更复杂。我将以this link为例开始。

相关问题