[Add] 新增“内存访问越界”导致内核报错:Segmentation fault

This commit is contained in:
gaoyang3513
2024-10-14 15:53:47 +08:00
parent dbdc68d7ab
commit c4510ef554
2 changed files with 28 additions and 1 deletions

View File

@ -20,6 +20,31 @@ static int trigger_oops(void)
return 0; return 0;
} }
static int trigger_oob(void) {
char *buffer;
int *out_of_bounds;
printk(KERN_INFO "Out-of-bounds memory access test module loaded\n");
// Allocate a small buffer
// buffer = kmalloc(256, GFP_KERNEL);
buffer = vmalloc(PAGE_SIZE * 2);
if (!buffer) {
printk(KERN_ERR "Failed to allocate memory\n");
return -ENOMEM;
}
// Attempt to access memory out of bounds
out_of_bounds = (int *)(buffer + PAGE_SIZE * 3); // Move pointer to the end of the buffer
*out_of_bounds = 0; // This should cause an out-of-bounds write
printk(KERN_INFO "Out-of-bounds Ofst[%lu] memory access performed\n", 256 + PAGE_SIZE);
kfree(buffer);
return 0;
}
static int trigger_oom(void) static int trigger_oom(void)
{ {
void *memory = NULL; void *memory = NULL;
@ -122,6 +147,8 @@ static int trigger_init(void) {
trigger_rcu(); trigger_rcu();
else if (test_type == 3) else if (test_type == 3)
trigger_deadlock(); trigger_deadlock();
else if (test_type == 4)
trigger_oob();
return 0; // 加载模块不应该有返回值但这里返回0避免编译警告。 return 0; // 加载模块不应该有返回值但这里返回0避免编译警告。
} }

View File

@ -24,7 +24,7 @@ INSTALL_DIR ?= $(LOCAL_DIR)/__install
#******************************************************************************* #*******************************************************************************
MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo)
SUB_DIRS := 01-Debug/ SUB_DIRS := spi 01-Debug
#******************************************************************************* #*******************************************************************************
# Compile configure # Compile configure