如何在linux内核的memcpy函数中添加一个钩子?

时间:2022-01-08 16:56:01

The following are my steps,but it did not work as intended.

以下是我的步骤,但并没有达到预期的效果。

linux-3.16-rc2\arch\x86\lib\memcpy_64.S:

x86 linux - 3.16 - rc2 \拱\ \ lib \ memcpy_64.S:

changed

改变了

ENTRY(__memcpy)
ENTRY(memcpy)
...
CFI_ENDPROC
ENDPROC(memcpy)
ENDPROC(__memcpy)

to:

:

ENTRY(__memcpy)
ENTRY(x86_memcpy)
...
CFI_ENDPROC
ENDPROC(x86_memcpy)
ENDPROC(__memcpy)

linux-3.16-rc2\lib\string.c:

linux - 3.16 - rc2 \ lib \ string.c:

changed

改变了

#ifndef __HAVE_ARCH_MEMCPY
void *memcpy(void *dest, const void *src, size_t count)
{
    char *tmp = dest;
    const char *s = src;

    while (count--)
        *tmp++ = *s++;
    return dest;
}
#endif

to:

:

//#ifndef __HAVE_ARCH_MEMCPY
void *memcpy(void *dest, const void *src, size_t count)
{
    char *tmp = dest;
    const char *s = src;
    my_hook();
    while (count--)
            *tmp++ = *s++;
    return dest;
}
//#endif

delete EXPORT_SYMBOL(memcpy) in arch/x86/kernel/x8664_ksyms_64.c

删除EXPORT_SYMBOL(memcpy)拱/ x86 /内核/ x8664_ksyms_64.c

add test code in linux-3.16-rc2\mm\memcpy_test.c:

添加测试代码在linux-3.16-rc2\mm\memcpy_test.c:

#include <linux/mm.h>
#include <linux/kallsyms.h>
#include <linux/module.h>
int hook_value = -1;
int test_begin = 0;
void  my_test_begin(void)
{
    char src[128] = {0};
    char dst[128] = {1};
    test_begin = 1;
    mb();
    memcpy((char*)dst,(char*)src,50);
    test_begin = 0;
    mb();
    printk("hook value:%d\n",hook_value);
}

void  my_hook(void)
{
    if(test_begin)
            hook_value=1;
}

After invoke my_test_begin, I found hook_value remained -1, it seems that memcpy in my_test_begin has never reached hook_value=1 can anyone help? thx!

在调用my_test_begin之后,我发现hook_value仍然是-1,似乎my_test_begin中的memcpy从未达到hook_value=1,有人能帮忙吗?谢谢!

1 个解决方案

#1


0  

thanks to @Basile Starynkevitch I solve the problem, yes, 'Sometimes, the compiler is optimizing memcpy to __builtin_memcpy'

感谢@Basile Starynkevitch我解决了这个问题,是的,“有时,编译器正在优化memcpy以__builtin_memcpy”

#1


0  

thanks to @Basile Starynkevitch I solve the problem, yes, 'Sometimes, the compiler is optimizing memcpy to __builtin_memcpy'

感谢@Basile Starynkevitch我解决了这个问题,是的,“有时,编译器正在优化memcpy以__builtin_memcpy”