Allan
Allan

Reputation: 4710

How should init_module (in user-space) be called

I'm trying to figure out how to load a kernel module from a "C" application, I do not want to use finit_module as there are no glibc wrapper for this system call at my system.

Here is what I have tried:

#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;
}

Here is what I get from 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 +++

The module loads fine if I uses insmod or create my own finit_module wrapper.

How should this be done?

Upvotes: 1

Views: 1930

Answers (2)

Aaron Fields
Aaron Fields

Reputation: 41

Your 3rd argument to init_module (const char *param_values) is NULL. You should make it not-null... an empty string should suffice.

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

Upvotes: 4

Venkatesh
Venkatesh

Reputation: 157

Use system() library function in C

system("insmod ~/hello_world.ko");

Upvotes: 0

Related Questions