linux kernel module mock added

This commit is contained in:
Alireza moradi 2025-12-26 18:34:42 +03:30 committed by alish14
parent 91c558e43b
commit 1eb72d1621
5 changed files with 114 additions and 0 deletions

View file

@ -0,0 +1,10 @@
obj-m += mymodule.o
mymodule-objs := mock.o mem.o
PWD := $(CURDIR)
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

36
hyperdbg/linux/mock/mem.c Normal file
View file

@ -0,0 +1,36 @@
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "module_info.h"
#include "mem.h"
void* MemAllocKernel(size_t Size)
{
void* ptr = kzalloc(Size, GFP_KERNEL);
if (ptr)
printk(KERN_INFO "MemAllocKernel: Allocated %zu bytes at %px\n", Size, ptr);
else
printk(KERN_ERR "MemAllocKernel: failed to allocate %zu bytes\n", Size);
return ptr;
}
void MemFree(void* Ptr)
{
if (Ptr) {
printk(KERN_INFO "MemFree: Freeing memory at %px\n", Ptr);
kfree(Ptr);
}
}
void MemCopy(void* Destination, const void* Source, size_t Size)
{
memcpy(Destination, Source, Size);
}
void MemSet(void* Destination, int Value, size_t Size)
{
memset(Destination, Value, Size);
}

16
hyperdbg/linux/mock/mem.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef MEM_LINUX_H
#define MEM_LINUX_H
#if !defined(__linux__) || !defined(__KERNEL__)
#error "This code must be compiled for Linux kernel only"
#endif
#include <linux/types.h>
void* MemAllocKernel(size_t Size);
void MemFree(void* Ptr);
void MemCopy(void* Destination, const void* Source, size_t Size);
void MemSet(void* Destination, int Value, size_t Size);
#endif

View file

@ -0,0 +1,39 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "module_info.h"
#include "mem.h"
static void* g_AllocatedBuffer;
static size_t g_BufferSize = 4096;
static int mock_init(void)
{
char source_data[] = "This is a test string from Linux kernel module";
printk(KERN_INFO "Mock module loading\n");
g_AllocatedBuffer = MemAllocKernel(g_BufferSize);
if (!g_AllocatedBuffer)
return -ENOMEM;
MemCopy(g_AllocatedBuffer, source_data, sizeof(source_data));
printk(KERN_INFO "Copied data: %s\n", (char*)g_AllocatedBuffer);
MemSet(g_AllocatedBuffer, 0, g_BufferSize);
printk(KERN_INFO "Buffer zeroed\n");
return 0;
}
static void mock_exit(void)
{
printk(KERN_INFO "Mock module unloading\n");
MemFree(g_AllocatedBuffer);
}
module_init(mock_init);
module_exit(mock_exit);

View file

@ -0,0 +1,13 @@
// module_info.h
#ifndef MODULE_INFO_H
#define MODULE_INFO_H
#include <linux/module.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alish");
MODULE_DESCRIPTION("Linux Kernel module Mock");
MODULE_VERSION("0.1");
#endif // _MODULE_INFO_H_