summaryrefslogtreecommitdiff
path: root/test/test_kmod/mod.rs
blob: fb7260ba9c9d9fcfb3e1371fd09cf86c6f3548ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::fs::copy;
use std::path::PathBuf;
use std::process::Command;
use tempfile::{tempdir, TempDir};
use crate::*;

fn compile_kernel_module() -> (PathBuf, String, TempDir) {
    let _m = crate::FORK_MTX
        .lock()
        .expect("Mutex got poisoned by another test");

    let tmp_dir = tempdir().expect("unable to create temporary build directory");

    copy(
        "test/test_kmod/hello_mod/hello.c",
        &tmp_dir.path().join("hello.c"),
    ).expect("unable to copy hello.c to temporary build directory");
    copy(
        "test/test_kmod/hello_mod/Makefile",
        &tmp_dir.path().join("Makefile"),
    ).expect("unable to copy Makefile to temporary build directory");

    let status = Command::new("make")
        .current_dir(tmp_dir.path())
        .status()
        .expect("failed to run make");

    assert!(status.success());

    // Return the relative path of the build kernel module
    (tmp_dir.path().join("hello.ko"), "hello".to_owned(), tmp_dir)
}

use nix::errno::Errno;
use nix::kmod::{delete_module, DeleteModuleFlags};
use nix::kmod::{finit_module, init_module, ModuleInitFlags};
use nix::Error;
use std::ffi::CString;
use std::fs::File;
use std::io::Read;

#[test]
fn test_finit_and_delete_module() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

    let f = File::open(kmod_path).expect("unable to open kernel module");
    finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
        .expect("unable to load kernel module");

    delete_module(
        &CString::new(kmod_name).unwrap(),
        DeleteModuleFlags::empty(),
    ).expect("unable to unload kernel module");
}

#[test]
fn test_finit_and_delete_modul_with_params() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

    let f = File::open(kmod_path).expect("unable to open kernel module");
    finit_module(
        &f,
        &CString::new("who=Rust number=2018").unwrap(),
        ModuleInitFlags::empty(),
    ).expect("unable to load kernel module");

    delete_module(
        &CString::new(kmod_name).unwrap(),
        DeleteModuleFlags::empty(),
    ).expect("unable to unload kernel module");
}

#[test]
fn test_init_and_delete_module() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

    let mut f = File::open(kmod_path).expect("unable to open kernel module");
    let mut contents: Vec<u8> = Vec::new();
    f.read_to_end(&mut contents)
        .expect("unable to read kernel module content to buffer");
    init_module(&mut contents, &CString::new("").unwrap()).expect("unable to load kernel module");

    delete_module(
        &CString::new(kmod_name).unwrap(),
        DeleteModuleFlags::empty(),
    ).expect("unable to unload kernel module");
}

#[test]
fn test_init_and_delete_module_with_params() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

    let mut f = File::open(kmod_path).expect("unable to open kernel module");
    let mut contents: Vec<u8> = Vec::new();
    f.read_to_end(&mut contents)
        .expect("unable to read kernel module content to buffer");
    init_module(&mut contents, &CString::new("who=Nix number=2015").unwrap())
        .expect("unable to load kernel module");

    delete_module(
        &CString::new(kmod_name).unwrap(),
        DeleteModuleFlags::empty(),
    ).expect("unable to unload kernel module");
}

#[test]
fn test_finit_module_invalid() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let kmod_path = "/dev/zero";

    let f = File::open(kmod_path).expect("unable to open kernel module");
    let result = finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty());

    assert_eq!(result.unwrap_err(), Error::Sys(Errno::EINVAL));
}

#[test]
fn test_finit_module_twice_and_delete_module() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

    let f = File::open(kmod_path).expect("unable to open kernel module");
    finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
        .expect("unable to load kernel module");

    let result = finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty());

    assert_eq!(result.unwrap_err(), Error::Sys(Errno::EEXIST));

    delete_module(
        &CString::new(kmod_name).unwrap(),
        DeleteModuleFlags::empty(),
    ).expect("unable to unload kernel module");
}

#[test]
fn test_delete_module_not_loaded() {
    require_capability!(CAP_SYS_MODULE);
    let _m0 = crate::KMOD_MTX.lock().expect("Mutex got poisoned by another test");
    let _m1 = crate::CWD_LOCK.read().expect("Mutex got poisoned by another test");

    let result = delete_module(&CString::new("hello").unwrap(), DeleteModuleFlags::empty());

    assert_eq!(result.unwrap_err(), Error::Sys(Errno::ENOENT));
}