【问题标题】:Kernel probe not inserted for system_call function未为 system_call 函数插入内核探针
【发布时间】:2013-12-01 10:04:46
【问题描述】:

我可以使用kprobe 机制通过以下示例代码附加处理程序:

#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/kallsyms.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/kprobes.h> 

static struct kprobe kp; 

int Pre_Handler(struct kprobe *p, struct pt_regs *regs){
    printk("pre_handler\n");
    return 0;
}

void Post_Handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags) {
    printk("post_handler\n");
} 

int __init init (void) {
    kp.pre_handler = Pre_Handler;
    kp.post_handler = Post_Handler;
    kp.addr = (kprobe_opcode_t *)kallsyms_lookup_name("sys_fork"); 
    printk("%d\n", register_kprobe(&kp)); 
    return 0;
}

void __exit cleanup(void) {
    unregister_kprobe(&kp); 
}

MODULE_LICENSE("GPL");   
module_init(init);
module_exit(cleanup); 

但是,似乎并非所有内核例程都可以通过这种方式进行跟踪。我尝试将处理程序附加到 system_call 以通过任何系统调用执行来调用它们,并进行以下更改:

kp.addr = (kprobe_opcode_t *)kallsyms_lookup_name("system_call"); 

并且没有插入探针。 dmesg 表明 register_kprobe 返回 -22,即 -EINVAL。为什么这个函数无法追踪?是否可以在调度任何系统调用之前附加 kprobe 处理程序?

$ uname -r
3.8.0-29-generic

【问题讨论】:

    标签: linux linux-kernel kprobe


    【解决方案1】:

    system_call 受到 kprobes 保护,无法探测 system_call 函数。 我认为在调用任何实际系统调用之前,我们没有任何有用的信息可供您获取。例如,如果您看到函数 system_call:

        RING0_INT_FRAME                 # can't unwind into user space anyway
        ASM_CLAC
        pushl_cfi %eax                  # save orig_eax
        SAVE_ALL
        GET_THREAD_INFO(%ebp)
                                        # system call tracing in operation / emulation
        testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
        jnz syscall_trace_entry
        cmpl $(NR_syscalls), %eax
        jae syscall_badsys
        syscall_call:
        call *sys_call_table(,%eax,4)
    

    在调用您的实际系统调用之前有一些说明。是的,我不确定您是否需要这些说明中的任何信息。

    【讨论】:

    • 您知道在任何系统调用之前执行某些代码的任何有效的非侵入性方法吗?
    • 查看github.com/alexandernst/procmon 的procmon 项目。这可能会有所帮助。抄送:@alexandernst
    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多