系统相关
首页 > 系统相关> > Linux 监测内存访问的方法汇总【转】

Linux 监测内存访问的方法汇总【转】

作者:互联网

转自:https://blog.csdn.net/dianzichongchong/article/details/120133833

1. hw break point
它是 linux kernel 自带的,监测一段内存访问信息的方法。它可以检测一段内存是否被读或写。

具体可见 linux 自带例子:linux/samples/hw_breakpoint/data_breakpoint.c

static int __init hw_break_module_init(void)
{
int ret;
struct perf_event_attr attr;

hw_breakpoint_init(&attr);
attr.bp_addr = kallsyms_lookup_name(ksym_name);
attr.bp_len = HW_BREAKPOINT_LEN_4;
attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;

sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
if (IS_ERR((void __force *)sample_hbp)) {
ret = PTR_ERR((void __force *)sample_hbp);
goto fail;
}

printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);

return 0;

fail:
printk(KERN_INFO "Breakpoint registration failed\n");

return ret;
}

static void __exit hw_break_module_exit(void)
{
unregister_wide_hw_breakpoint(sample_hbp);
printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
}
register_wide_hw_breakpoint 用来向所有 CPU 注册监测的地址和大小以及访问类型,它们存放在 perf_event_attr 中。

地址是 kernel 虚拟地址,支持监测内存的大小和访问类型详见:linux/include/uapi/linux/hw_breakpoint.h

enum {
HW_BREAKPOINT_LEN_1 = 1,
HW_BREAKPOINT_LEN_2 = 2,
HW_BREAKPOINT_LEN_4 = 4,
HW_BREAKPOINT_LEN_8 = 8,
};

enum {
HW_BREAKPOINT_EMPTY = 0,
HW_BREAKPOINT_R = 1,
HW_BREAKPOINT_W = 2,
HW_BREAKPOINT_RW = HW_BREAKPOINT_R | HW_BREAKPOINT_W,
HW_BREAKPOINT_X = 4,
HW_BREAKPOINT_INVALID = HW_BREAKPOINT_RW | HW_BREAKPOINT_X,
};
当不需要监测该段内存时,调用 unregister_wide_hw_breakpoint 即可。

当监测到指定内存被访问时,会在 dmesg 中打印如下信息:

[ 2295.708755] Call trace:
[ 2295.711234] dump_backtrace+0x0/0x1a0
[ 2295.714929] show_stack+0x14/0x20
[ 2295.718279] dump_stack+0x94/0xb4
[ 2295.721633] sample_hbp_handler+0xc/0x18 [hbp_drv]
[ 2295.725853] __perf_event_overflow+0x54/0x100
[ 2295.730248] perf_swevent_overflow+0x54/0x88
[ 2295.734556] perf_swevent_event+0xc0/0xc8
2. mprotect
mpotect 是 linux 的 API,用来修改一块内存的访问权限。可以用于监测内存的非法访问。

可见如下例子:

#include <sys/mman.h>
#include <unistd.h>

#include <stdint.h>
#include <stdio.h>

static int protect_memory(void *addr, uint64_t len, uint32_t type) {
int ret = 0;

uint64_t start = (uint64_t) addr;
uint64_t end = start + len;

uint64_t page_size = getpagesize();

start = start- start % page_size;

while (start < end) {
ret = mprotect((void *) start, page_size, type);

printf("---0x%llx---0x%llx, type: %d, ret: %d\n",
start, page_size, type, ret);
start += page_size;
}

return ret;
}
调用 mprotect 传入虚拟地址、大小(不能超过PAGE_SIZE),以及新的属性。属性只能取如下4个,可以用 | 复合多个:

1)PROT_READ:表示内存段内的内容可读;

2)PROT_WRITE:表示内存段内的内容可写;

3)PROT_EXEC:表示内存段中的内容可执行;

4)PROT_NONE:表示内存段中的内容根本没法访问

需要监测内存被非法写入时,将内存属性设为只读,然后等待 log 即可,如下所示:

int main() {
int buffer[20] = { 0 };

protect_memory(buffer, 20 * sizeof(int), PROT_READ);
buffer[10] = 1;

return 0;
}
在内存被试图写入时,可以在 kernel log 中看到如下信息:

signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xffb94228
Cause: stack pointer is not in a rw map; likely due to stack overflow.
r0 00000000 r1 00001000 r2 00000001 r3 00000000
r4 00001000 r5 00000000 r6 ffb94000 r7 00000000
r8 ffb94288 r9 eed55124 r10 00000000 r11 00000001
ip 00000000 sp ffb94228 lr 00a4d477 pc 00a4d476
backtrace:
#00 pc 00001476 /system/bin/mprotect_demo (main+130) (BuildId: ...)
参考:C语言之 mprotect - Max_hhg - 博客园
————————————————
版权声明:本文为CSDN博主「孤独的小鱼&lt;---<」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dianzichongchong/article/details/120133833

标签:Linux,汇总,HW,ret,hw,start,内存,BREAKPOINT
来源: https://www.cnblogs.com/sky-heaven/p/16669166.html