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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Interface to Dynamically Loaded Linux Kernel Modules.
//!
//! # Examples
//!
//! Print all currently loaded system modules
//!
//! ```rust
//! # use linapi::modules::*;
//!
//! let mods = LoadedModule::from_loaded();
//!
//! for m in mods {
//!     println!("Module: {}", m.name());
//! }
//! ```
//!
//! Load a module
//!
//! ```rust,no_run
//! # use linapi::modules::*;
//!
//! let m = ModuleFile::from_name("MyModule");
//! let loaded = m.load("my_param=1");
//! println!("Loaded module {}. my_param={}", loaded.name(), std::str::from_utf8(&loaded.parameters()["my_param"]).unwrap());
//! ```
//!
//! # Implementation
//!
//! This uses the sysfs interface, documented [here][1] and [here][2], and
//! various undocumented interfaces where noted.
//!
//! [1]: https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-module
//! [2]: https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-module
use crate::types::{
    util::{read_uevent, write_uevent},
    UEvent,
    UEventAction,
    MODULE_PATH,
    SYSFS_PATH,
};
use goblin::elf::{section_header::SHT_PROGBITS, Elf};
use nix::{
    kmod::{delete_module, finit_module, init_module, DeleteModuleFlags, ModuleInitFlags},
    sys::utsname::uname,
};
use std::{
    collections::HashMap,
    ffi::CString,
    fs,
    fs::DirEntry,
    io::BufRead,
    mem::size_of,
    path::{Path, PathBuf},
};
use walkdir::WalkDir;

const SIGNATURE_MAGIC: &[u8] = b"~Module signature appended~\n";

/// Kernel modules can be "tainted", which serve as a marker for debugging
/// purposes.
#[derive(Debug, Clone, Copy)]
pub enum Taint {
    /// Proprietary Module.
    Proprietary,

    /// Out of tree, third party Module.
    OutOfTree,

    /// Module was force loaded.
    Forced,

    /// Unstable Staging Module.
    Staging,

    /// Unsigned Module.
    Unsigned,
}

/// Module type
#[derive(Debug, Clone, Copy)]
pub enum Type {
    /// Built in to the kernel.
    ///
    /// These only show up if they have a version or one run-time parameter, and
    /// are missing most values.
    ///
    /// # Note
    ///
    /// The fact these show up isn't intentional and technically may change, or
    /// so claim the kernel docs.
    BuiltIn,

    /// Dynamically loaded.
    Dynamic,
}

/// Module Init Status
#[derive(Debug, Clone, Copy)]
pub enum Status {
    /// Normal state, fully loaded.
    Live,

    /// Running module init
    Coming,

    /// Going away, running module exit?
    Going,
}

/// Describes a loaded Linux kernel Module
#[derive(Debug)]
pub struct LoadedModule {
    /// The name of the Module
    name: String,

    /// Type of module
    module_type: Type,

    /// Path to the module
    path: PathBuf,
}

impl LoadedModule {
    /// Create from module directory
    ///
    /// # Note
    ///
    /// Built-in modules may appear in `/sys/modules` and they are ill-formed,
    /// missing required files.
    ///
    /// In this case `refcnt` is [`None`], `coresize` is 0, and `taint` is
    /// [`None`]
    fn from_dir(path: &Path) -> Self {
        let module_type = if path.join("coresize").exists() {
            Type::Dynamic
        } else {
            Type::BuiltIn
        };
        Self {
            name: path.file_stem().unwrap().to_str().unwrap().trim().into(),
            module_type,
            path: path.into(),
        }
    }

    /// Get an already loaded module by name
    ///
    /// # Panics
    ///
    /// - If no such module exists
    pub fn from_name(name: &str) -> Self {
        Self::from_dir(&Path::new(SYSFS_PATH).join("module").join(name))
    }

    /// Get currently loaded dynamic kernel modules
    ///
    /// # Note
    ///
    /// Modules can be unloaded, and if that happens methods on [`LoadedModule`]
    /// will panic
    pub fn from_loaded() -> Vec<Self> {
        let dir = Path::new(SYSFS_PATH).join("module");
        let mut mods = Vec::new();
        //
        for module in fs::read_dir(dir).unwrap() {
            let module: DirEntry = module.unwrap();
            let m = Self::from_dir(&module.path());
            if let Type::BuiltIn = m.module_type() {
                continue;
            }
            mods.push(m);
        }
        mods
    }

    /// Unload the module.
    ///
    /// # Panics
    ///
    /// - On failure
    pub fn unload(self) {
        delete_module(
            &CString::new(self.name).unwrap(),
            DeleteModuleFlags::O_NONBLOCK,
        )
        .unwrap();
    }

    /// Forcefully unload a kernel module.
    ///
    /// # Safety
    ///
    /// Force unloading is wildly dangerous and will taint your kernel.
    ///
    /// It can cause modules to be unloaded while still in use, or unload
    /// modules not designed to be unloaded.
    ///
    /// # Panics
    ///
    /// - On failure
    pub unsafe fn force_unload(self) {
        delete_module(
            &CString::new(self.name).unwrap(),
            DeleteModuleFlags::O_NONBLOCK | DeleteModuleFlags::O_TRUNC,
        )
        .unwrap();
    }

    /// Name of the module
    pub fn name(&self) -> &str {
        self.path.file_stem().unwrap().to_str().unwrap()
    }

    /// Module type, Builtin or Dynamic
    pub fn module_type(&self) -> Type {
        self.module_type
    }

    /// Module parameters.
    ///
    /// The kernel exposes these as files in a directory, and their contents are
    /// entirely module specific, hence `Vec<(String, Vec<u8>)>`, which can be
    /// [`std::io::Read`].
    ///
    /// # Stability
    ///
    /// The stability of parameters depends entirely on the specific module.
    pub fn parameters(&self) -> HashMap<String, Vec<u8>> {
        todo!()
    }

    /// Module reference count.
    ///
    /// If the module is built-in, or if the kernel was not built with
    /// `CONFIG_MODULE_UNLOAD`, this will be [`None`]
    pub fn ref_count(&self) -> Option<u32> {
        fs::read_to_string(self.path.join("refcnt"))
            .map(|s| s.trim().parse().unwrap())
            .ok()
    }

    /// Module size in bytes
    pub fn size(&self) -> u64 {
        fs::read_to_string(self.path.join("coresize"))
            .map(|s| s.trim().parse().unwrap())
            .unwrap()
    }

    /// Module taint, or [`None`] if untainted.
    ///
    /// See [`Taint`] for details.
    pub fn taint(&self) -> Option<Taint> {
        match fs::read_to_string(self.path.join("taint")).unwrap().trim() {
            "P" => Some(Taint::Proprietary),
            "O" => Some(Taint::OutOfTree),
            "F" => Some(Taint::Forced),
            "C" => Some(Taint::Staging),
            "E" => Some(Taint::Unsigned),
            _ => None,
        }
    }

    /// List of other modules that use/reference this one.
    ///
    /// # Note
    ///
    /// This uses the `holders` sysfs folder, which is completely undocumented
    /// by the kernel, beware.
    pub fn holders(&self) -> Vec<Self> {
        let mut v = Vec::new();
        for re in fs::read_dir(self.path.join("holders")).unwrap() {
            let re: DirEntry = re.unwrap();
            v.push(Self::from_dir(&re.path()))
        }
        v
    }

    /// Path to the module file.
    ///
    /// # Note
    ///
    /// There is no guarantee the returned path is the same module. The file may
    /// have changed on disk.
    ///
    /// This is equivalent to `find_module_file(&module.name())`
    pub fn file_path(&self) -> PathBuf {
        // find_module_file(&self.name())
        todo!()
    }

    /// Module status.
    ///
    /// # Note
    ///
    /// This uses the undocumented `initstate` file, which is probably
    /// `module_state` from `linux/module.h`.
    pub fn status(&self) -> Status {
        match fs::read_to_string(self.path.join("initstate"))
            .unwrap()
            .trim()
        {
            "live" => Status::Live,
            "coming" => Status::Coming,
            "going" => Status::Going,
            _ => panic!("Unknown module state"),
        }
    }
}

impl UEvent for LoadedModule {
    fn write(&self, action: UEventAction, uuid: Option<String>, args: HashMap<String, String>) {
        write_uevent(&self.path.join("uevent"), action, uuid, args)
    }
    fn read(&self) -> HashMap<String, String> {
        read_uevent(&self.path.join("uevent"))
    }
}

/// A Linux Kernel Module file on disk.
#[derive(Debug)]
pub struct ModuleFile {
    name: String,
    path: PathBuf,
}

impl ModuleFile {
    /// Search `/lib/modules/(uname -r)` for the module `name`.
    ///
    /// # Panics
    ///
    /// - If the module couldn't be found
    pub fn from_name(name: &str) -> Self {
        let path = Path::new(MODULE_PATH)
            .join(uname().release())
            .join("kernel");
        for entry in WalkDir::new(path) {
            let entry = entry.unwrap();
            if !entry.file_type().is_file() {
                continue;
            }
            // Compressed modules can have two? file extensions
            let m = if entry.path().extension().unwrap() == "ko" {
                entry.path().file_stem().unwrap()
            } else {
                Path::new(entry.path().file_stem().unwrap())
                    .file_stem()
                    .unwrap()
            };
            if m == name {
                return Self {
                    name: name.into(),
                    path: entry.into_path(),
                };
            }
        }
        panic!("Couldn't find module {}", name);
    }

    /// Use the file at `path` as a module.
    ///
    /// # Panics
    ///
    /// - If the module couldn't be found
    pub fn from_path(path: &Path) -> Self {
        assert!(path.exists());
        Self {
            name: path.file_stem().unwrap().to_str().unwrap().into(),
            path: path.into(),
        }
    }

    /// Load this kernel module, and return the [`LoadedModule`] describing it.
    ///
    /// # Arguments
    ///
    /// - `param`eters for the kernel module. See module documentation for
    ///   details, and `init_module(2)` for details on formatting.
    ///
    /// # Panics
    ///
    /// - On failure
    ///
    /// # Note
    ///
    /// Kernel modules may be compressed, and depending on crate features this
    /// function may automatically decompress it.
    pub fn load(&self, param: &str) -> LoadedModule {
        let img = fs::read(&self.path).unwrap();
        init_module(&img, &CString::new(param).unwrap()).unwrap();
        LoadedModule::from_dir(&Path::new(SYSFS_PATH).join("module").join(&self.name))
    }

    /// Force load this kernel module, and return the [`LoadedModule`]
    /// describing it.
    ///
    /// # Arguments
    ///
    /// - `param`eters for the kernel module. See module documentation for
    ///   details, and `init_module(2)` for details on formatting.
    ///
    /// # Safety
    ///
    /// Force loading a kernel module is dangerous, it skips important safety
    /// checks that help ensure module compatibility with your kernel.
    pub unsafe fn force_load(&self, param: &str) -> LoadedModule {
        let file = fs::File::open(&self.path).unwrap();
        finit_module(
            &file,
            &CString::new(param).unwrap(),
            ModuleInitFlags::MODULE_INIT_IGNORE_MODVERSIONS
                | ModuleInitFlags::MODULE_INIT_IGNORE_VERMAGIC,
        )
        .unwrap();
        LoadedModule::from_dir(&Path::new(SYSFS_PATH).join("module").join(&self.name))
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get information embedded in the module file.
    ///
    /// # Note
    ///
    /// This uses the `.modinfo` ELF section, which seems to be entirely
    /// undocumented.
    pub fn info(&self) -> ModInfo {
        let f = fs::read(&self.path).unwrap();
        let elf = Elf::parse(&f).unwrap();
        for header in elf.section_headers {
            if header.sh_type != SHT_PROGBITS {
                continue;
            }
            let name = elf.shdr_strtab.get(header.sh_name).unwrap().unwrap();
            if name == ".modinfo" {
                let mut map = HashMap::new();
                for kv in BufRead::split(&f[header.file_range()], b'\0') {
                    let kv: Vec<u8> = kv.unwrap();
                    let s = String::from_utf8(kv).unwrap();
                    let mut s = s.splitn(2, '=');
                    let key = s.next().unwrap().to_string();
                    let value = s.next().unwrap().to_string();
                    let vec = map.entry(key).or_insert(Vec::new());
                    if !value.is_empty() {
                        vec.push(value);
                    }
                }
                fn y_n(s: &str) -> bool {
                    if s == "Y" {
                        true
                    } else {
                        false
                    }
                }
                fn one(map: &mut HashMap<String, Vec<String>>, key: &str) -> String {
                    map.remove(key).map(|mut v| v.remove(0)).unwrap_or_default()
                }
                fn more(map: &mut HashMap<String, Vec<String>>, key: &str) -> Vec<String> {
                    map.remove(key).unwrap_or_default()
                }
                let mut parameters = Vec::new();
                // FIXME: Are parameters and their types guaranteed to be the same order?
                // Sort first?
                for ((name, description), type_) in map
                    .remove("parm")
                    .unwrap()
                    .into_iter()
                    .map(|s| {
                        let mut i = s.splitn(2, ':');
                        let name = i.next().unwrap();
                        let desc = i.next().unwrap();
                        (name.to_string(), desc.to_string())
                    })
                    .zip(map.remove("parmtype").unwrap().into_iter().map(|s| {
                        let mut i = s.splitn(2, ':');
                        i.next().unwrap();
                        let typ = i.next().unwrap();
                        typ.to_string()
                    }))
                {
                    parameters.push(ModParam {
                        name,
                        description,
                        type_,
                    })
                }
                return ModInfo {
                    alias: more(&mut map, "alias"),
                    soft_dependencies: more(&mut map, "softdep"),
                    license: one(&mut map, "license"),
                    authors: more(&mut map, "author"),
                    description: one(&mut map, "description"),
                    version: one(&mut map, "version"),
                    firmware: more(&mut map, "firmware"),
                    version_magic: one(&mut map, "vermagic"),
                    name: one(&mut map, "name"),
                    in_tree: y_n(&one(&mut map, "intree")),
                    retpoline: y_n(&one(&mut map, "retpoline")),
                    staging: y_n(&one(&mut map, "staging")),
                    dependencies: more(&mut map, "depends"),
                    source_checksum: one(&mut map, "srcversion"),
                    parameters,
                };
            }
        }
        panic!("Missing .modinfo")
    }

    /// Whether the module has a signature.
    ///
    /// This does not check if it's valid.
    ///
    /// # Note
    ///
    /// This is a temporary API.
    pub fn has_signature(&self) -> bool {
        let f = fs::read(&self.path).unwrap();
        f.ends_with(SIGNATURE_MAGIC)
    }

    /// Module Signature info, if any.
    // FIXME: rust-openssl does not expose the APIs we need, so this isn't possible.
    fn _signature(&self) -> Option<ModSig> {
        let f = fs::read(&self.path).unwrap();
        if f.ends_with(SIGNATURE_MAGIC) {
            // Length of file, minus the signature structure, minus the magic
            let len = f.len() - size_of::<RawModSig>() - SIGNATURE_MAGIC.len();
            //
            let sig: &[u8] = &f[len..];
            let mut sig = unsafe { (sig.as_ptr() as *const RawModSig).read_unaligned() };
            sig.signature_length = u32::from_be(sig.signature_length);
            dbg!(sig);
            //
            let data_start = len - sig.signature_length as usize;
            let _sig_data: &[u8] = &f[data_start..][..sig.signature_length as usize];
            //
            todo!()
        } else {
            None
        }
    }
}

/// Information on the signature added to the end of the module
///
/// See `linux/include/linux/module_signature.h` for some details.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
struct RawModSig {
    /// Public-key crypto algorithm
    algorithm: u8,

    // Digest hash
    hash: u8,

    // Key type
    id_type: u8,

    // Length of signer name
    signer_length: u8,

    // Length of key
    key_id_length: u8,

    _pad: [u8; 3],

    // Length of signature IN BIG ENDIAN
    signature_length: u32,
}

#[derive(Debug)]
struct ModSig {
    signer: String,
}

#[derive(Debug)]
pub struct ModParam {
    pub name: String,
    pub description: String,
    pub type_: String,
}

/// Information on a [`ModuleFile`]
///
/// # Notes
///
/// This uses the `.modinfo` ELF section, which is semi-documented in
/// `linux/modules.h` and `MODULE_INFO`.
#[derive(Debug)]
pub struct ModInfo {
    /// Module Aliases. Alternative names for this module.
    pub alias: Vec<String>,

    /// Soft Dependencies. Not required, but may provide additional features.
    pub soft_dependencies: Vec<String>,

    /// Module License
    ///
    /// See `MODULE_LICENSE` for details on this value.
    pub license: String,

    /// Module Author and email
    pub authors: Vec<String>,

    /// What the module does
    pub description: String,

    /// Module version
    pub version: String,

    /// Optional firmware file(s) needed by the module
    pub firmware: Vec<String>,

    /// Version magic string, used by the kernel for compatibility checking.
    pub version_magic: String,

    /// Module name, self-reported.
    pub name: String,

    /// Whether the module is from the kernel source tree.
    pub in_tree: bool,

    /// The retpoline security feature
    pub retpoline: bool,

    /// If the module is staging
    pub staging: bool,

    /// Other modules this one depends on
    pub dependencies: Vec<String>,

    /// Source Checksum.
    pub source_checksum: String,

    /// Module Parameters
    pub parameters: Vec<ModParam>,
}