gpt4 book ai didi

linux - 应该如何调用 init_module(在用户空间中)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:58 25 4
gpt4 key购买 nike

我正在尝试找出如何从“C”应用程序加载内核模块,我不想使用 finit_module 因为在我的系统中没有用于此系统调用的 glibc 包装器.

这是我尝试过的:

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

int init_module(void *module_image, unsigned long len,
const char *param_values);

int main() {
int res = 0;
void *buf = 0;
struct stat sb;
int rc = 0;

int fd = open("/tmp/my-test.ko", O_RDONLY|O_CLOEXEC);
if (fd < 0) {
rc = -1;
goto EXIT;
}

res = fstat(fd, &sb);
if (res == -1) {
rc = -2;
goto EXIT_CLOSE;
}

buf = mmap(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
if (buf == 0) {
rc = -3;
goto EXIT_CLOSE;
}

res = init_module(buf, sb.st_size, 0);
if (res == -1) {
rc = -4;
goto EXIT_FREE;
}


EXIT_FREE:
munmap(buf, sb.st_size);

EXIT_CLOSE:
close(fd);

EXIT:
return rc;
}

这是我从 strace 得到的:

$ sudo strace ./a.out
execve("./a.out", ["./a.out"], [/* 28 vars */]) = 0
brk(0) = 0x7be000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f93015cd000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=116129, ...}) = 0
mmap(NULL, 116129, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f93015a8000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9300fe0000
mprotect(0x7f930119b000, 2097152, PROT_NONE) = 0
mmap(0x7f930139b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7f930139b000
mmap(0x7f93013a1000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f93013a1000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f93015c8000
arch_prctl(ARCH_SET_FS, 0x7f93015c8740) = 0
mprotect(0x7f930139b000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7f93015ca000, 4096, PROT_READ) = 0
munmap(0x7f93015a8000, 116129) = 0
open("/tmp/my-test.ko", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=11036, ...}) = 0
mmap(NULL, 11036, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x7f93015c0000
init_module(0x7f93015c0000, 11036, NULL) = -1 EFAULT (Bad address)
munmap(0x7f93015c0000, 11036) = 0
close(3) = 0
exit_group(-4) = ?
+++ exited with 252 +++

如果我使用 insmod 或创建我自己的 finit_module 包装器,模块可以正常加载。

应该怎么做?

最佳答案

init_module (const char *param_values) 的第三个参数是 NULL。你应该让它不为空......一个空字符串就足够了。

res = init_module(buf, sb.st_size, "");

关于linux - 应该如何调用 init_module(在用户空间中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com