code.ivysaur.me

mkkmod

Pack a binary into a self-contained, self-executing Linux kernel module using the usermode driver infrastructure.

Adapted from an example by Richard Weinberger.

License: GPL-2.0-only

Usage

  • Build sample binary to embed: ./build-sample.sh
  • Build kernel module: ./build-module.sh ./sample
    • To build for the non-running kernel: KERN_VER=6.9.8-200.fc40.x86_64 ./build-module.sh ./sample
  • Run: insmod ./eprog.ko

Requirements

  • The generated module must have a GPL-compatible license to use EXPORT_SYMBOL_GPL functions.
  • Requires kernel 5.9 or later.
  • Kernel version needs to export the umd functionality.

If you get errors of the form modpost: "fork_usermode_driver" [...] undefined! check if your kernel exposes the functions in /usr/src/linux-headers-$(uname -r)/Module.symvers and/or that the functions are present in /proc/kallsyms.

Compatibility tests:

|Distro|Kernel version|Notes |---|---|--- |Debian 12|6.1.0-22|❌ Missing symbols |Fedora 40|6.9.8-200|✅ Features are present

Reference

  • https://sigma-star.at/blog/2023/07/embedded-go-prog/
  • https://git.kernel.org/pub/scm/linux/kernel/git/rw/misc.git/tree/drivers/misc/embedded_prog?h=embedded_go_prog
  • https://medium.com/@adityapatnaik27/linux-kernel-module-in-tree-vs-out-of-tree-build-77596fc35891
  • https://github.com/torvalds/linux/blob/master/include/linux/usermode_driver.h
  • https://github.com/torvalds/linux/blob/master/kernel/usermode_driver.c