简体   繁体   中英

How to add a hook in memcpy function of linux kernel?

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

linux-3.16-rc2\\arch\\x86\\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:

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

add test code in 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM