Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
Make modinfo work properly (fixes #80)
Browse files Browse the repository at this point in the history
  • Loading branch information
geofft committed Aug 10, 2020
1 parent 1e8ca1d commit 86a8004
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 37 deletions.
6 changes: 3 additions & 3 deletions hello-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Drop for HelloWorldModule {

linux_kernel_module::kernel_module!(
HelloWorldModule,
author: "Fish in a Barrel Contributors",
description: "An extremely simple kernel module",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"An extremely simple kernel module",
license: b"GPL"
);
48 changes: 39 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub use crate::types::{CStr, Mode};
///
/// linux_kernel_module::kernel_module!(
/// MyKernelModule,
/// author: "Fish in a Barrel Contributors",
/// description: "My very own kernel module!",
/// license: "GPL"
/// author: b"Fish in a Barrel Contributors",
/// description: b"My very own kernel module!",
/// license: b"GPL"
/// );
#[macro_export]
macro_rules! kernel_module {
Expand Down Expand Up @@ -72,13 +72,43 @@ macro_rules! kernel_module {
)*
};

(@attribute $name:ident, $value:expr) => {
// TODO: The modinfo attributes below depend on the compiler placing
// the variables in order in the .modinfo section, so that you end up
// with b"key=value\0" in order in the section. This is a reasonably
// standard trick in C, but I'm not sure that rustc guarantees it.
//
// Ideally we'd be able to use concat_bytes! + stringify_bytes! +
// some way of turning a string literal (or at least a string
// literal token) into a bytes literal, and get a single static
// [u8; * N] with the whole thing, but those don't really exist yet.
// Most of the alternatives (e.g. .as_bytes() as a const fn) give
// you a pointer, not an array, which isn't right.

(@attribute author, $value:expr) => {
#[link_section = ".modinfo"]
pub static AUTHOR_KEY: [u8; 7] = *b"author=";
#[link_section = ".modinfo"]
pub static AUTHOR_VALUE: [u8; $value.len()] = *$value;
#[link_section = ".modinfo"]
pub static AUTHOR_NUL: [u8; 1] = *b"\0";
};

(@attribute description, $value:expr) => {
#[link_section = ".modinfo"]
pub static DESCRIPTION_KEY: [u8; 12] = *b"description=";
#[link_section = ".modinfo"]
pub static DESCRIPTION_VALUE: [u8; $value.len()] = *$value;
#[link_section = ".modinfo"]
pub static DESCRIPTION_NUL: [u8; 1] = *b"\0";
};

(@attribute license, $value:expr) => {
#[link_section = ".modinfo"]
pub static LICENSE_KEY: [u8; 8] = *b"license=";
#[link_section = ".modinfo"]
pub static LICENSE_VALUE: [u8; $value.len()] = *$value;
#[link_section = ".modinfo"]
#[allow(non_upper_case_globals)]
// TODO: Generate a name the same way the kernel's `__MODULE_INFO` does.
// TODO: This needs to be a `[u8; _]`, since the kernel defines this as a `const char []`.
// See https://github.com/rust-lang/rfcs/pull/2545
pub static $name: &'static [u8] = concat!(stringify!($name), "=", $value, '\0').as_bytes();
pub static LICENSE_NUL: [u8; 1] = *b"\0";
};
}

Expand Down
6 changes: 3 additions & 3 deletions tests/chrdev-region-allocation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl linux_kernel_module::KernelModule for ChrdevRegionAllocationTestModule {

linux_kernel_module::kernel_module!(
ChrdevRegionAllocationTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing character device region allocation",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing character device region allocation",
license: b"GPL"
);
6 changes: 3 additions & 3 deletions tests/chrdev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl linux_kernel_module::KernelModule for ChrdevTestModule {

linux_kernel_module::kernel_module!(
ChrdevTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing character devices",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing character devices",
license: b"GPL"
);
6 changes: 3 additions & 3 deletions tests/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl linux_kernel_module::KernelModule for TestFSModule {

linux_kernel_module::kernel_module!(
TestFSModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing filesystem::register",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing filesystem::register",
license: b"GPL"
);
18 changes: 18 additions & 0 deletions tests/modinfo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "modinfo-tests"
version = "0.1.0"
authors = ["Alex Gaynor <alex.gaynor@gmail.com>", "Geoffrey Thomas <geofft@ldpreload.com>"]
edition = "2018"

[lib]
crate-type = ["staticlib"]
test = false

[features]
default = ["linux-kernel-module"]

[dependencies]
linux-kernel-module = { path = "../..", optional = true }

[dev-dependencies]
kernel-module-testlib = { path = "../../testlib" }
18 changes: 18 additions & 0 deletions tests/modinfo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![no_std]

use linux_kernel_module;

struct ModinfoTestModule;

impl linux_kernel_module::KernelModule for ModinfoTestModule {
fn init() -> linux_kernel_module::KernelResult<Self> {
Ok(ModinfoTestModule)
}
}

linux_kernel_module::kernel_module!(
ModinfoTestModule,
author: b"Fish in a Barrel Contributors",
description: b"Empty module for testing modinfo",
license: b"GPL"
);
38 changes: 38 additions & 0 deletions tests/modinfo/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;

use kernel_module_testlib::with_kernel_module;

#[test]
fn test_modinfo() {
let module = env::var("KERNEL_MODULE").unwrap();

for (key, value) in &[
("author", "Fish in a Barrel Contributors"),
("description", "Empty module for testing modinfo"),
("license", "GPL"),
] {
let modinfo = Command::new("modinfo")
.arg("-F")
.arg(key)
.arg(&module)
.output()
.unwrap();
assert!(modinfo.status.success());
assert_eq!(&std::str::from_utf8(&modinfo.stdout).unwrap().trim(), value);
}
}

#[test]
fn test_no_proprietary_taint() {
let module = env::var("KERNEL_MODULE").unwrap();
let module_name = Path::new(&module).file_stem().unwrap();
let sysfs_path = Path::new("/sys/module").join(module_name).join("taint");

with_kernel_module(|| {
let taints = fs::read_to_string(&sysfs_path).unwrap();
assert!(!taints.contains("P"));
});
}
6 changes: 3 additions & 3 deletions tests/printk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl linux_kernel_module::KernelModule for PrintkTestModule {

linux_kernel_module::kernel_module!(
PrintkTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing println!()",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing println!()",
license: b"GPL"
);
6 changes: 3 additions & 3 deletions tests/random/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl linux_kernel_module::KernelModule for RandomTestModule {

linux_kernel_module::kernel_module!(
RandomTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing the CSPRNG",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing the CSPRNG",
license: b"GPL"
);
2 changes: 1 addition & 1 deletion tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(*args, **kwargs):


def main(argv):
for path in os.listdir(BASE_DIR):
for path in argv[1:] or os.listdir(BASE_DIR):
if (
not os.path.isdir(os.path.join(BASE_DIR, path)) or
not os.path.exists(os.path.join(BASE_DIR, path, "tests"))
Expand Down
6 changes: 3 additions & 3 deletions tests/sysctl-get/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Drop for SysctlGetTestModule {

linux_kernel_module::kernel_module!(
SysctlGetTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing sysctls",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing sysctls",
license: b"GPL"
);
6 changes: 3 additions & 3 deletions tests/sysctl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl linux_kernel_module::KernelModule for SysctlTestModule {

linux_kernel_module::kernel_module!(
SysctlTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing sysctls",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing sysctls",
license: b"GPL"
);
6 changes: 3 additions & 3 deletions tests/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl linux_kernel_module::KernelModule for UtilsTestModule {

linux_kernel_module::kernel_module!(
UtilsTestModule,
author: "Fish in a Barrel Contributors",
description: "A module for testing various utilities",
license: "GPL"
author: b"Fish in a Barrel Contributors",
description: b"A module for testing various utilities",
license: b"GPL"
);

0 comments on commit 86a8004

Please sign in to comment.