A Linux kernel module (LKM) with a user-space controller that extracts detailed process information including memory layout and ELF sections.
This project implements a Linux Kernel Module that provides access to process information through the /proc filesystem. The module exposes process details such as:
- Process ID (PID) and Process Name
- CPU Usage statistics
- Memory Layout: Code, Data, and BSS sections
- ELF Binary information
- Start/End addresses for code and data segments
The project consists of two main components:
- Kernel Module (
elf_det.c) - Runs in kernel space and gathers process information - User Program (
proc_elf_ctrl.c) - User-space controller to interact with the module
- Process Memory Inspection: View detailed memory layout of any running process
- CPU Usage Tracking: Real-time CPU usage percentage calculation
- ELF Section Analysis: Extract ELF binary sections (code, data, BSS)
- Proc Filesystem Interface: Easy interaction through
/proc/elf_det/ - Sequential File Operations: Efficient data reading using kernel seq_file API
- User-Friendly CLI: Simple command-line interface for querying process information
kernel_module/
├── .devcontainer/ # Development container configuration
│ ├── Dockerfile # Pre-installs: clang-format, sparse, cppcheck
│ └── devcontainer.json # Auto-configures Git hooks on startup
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI/CD with static analysis
├── scripts/ # Testing and utility scripts
│ ├── qemu-setup.sh # Setup QEMU testing environment
│ ├── qemu-run.sh # Run QEMU VM
│ ├── qemu-test.sh # Automated module testing in QEMU
│ ├── pre-commit.sh # Pre-commit hook script (auto-installed in container)
│ └── README.md # Scripts documentation
├── src/
│ ├── elf_det.c # Kernel module source code
│ ├── proc_elf_ctrl.c # User-space controller program
│ ├── elf_det_tests.c # Unit tests for elf_det functions
│ ├── proc_elf_ctrl_tests.c # Unit tests for proc_elf_ctrl helpers
│ ├── elf_helpers.h # Helper functions for CPU usage and BSS range
│ ├── user_helpers.h # Helper functions for path building
│ └── Kbuild # Kernel build configuration
├── .clang-format # Code formatting configuration
├── .cppcheck-suppressions # Static analysis suppressions
├── .editorconfig # Editor configuration
├── Makefile # Build system with static analysis targets
├── .gitignore # Git ignore rules
└── README.md # This file
Generated after build:
└── build/ # Build artifacts (created by make)
├── elf_det.ko # Compiled kernel module
├── proc_elf_ctrl # Compiled user program
├── elf_det_tests # Compiled unit tests for elf_det
└── proc_elf_ctrl_tests # Compiled unit tests for proc_elf_ctrl
This project uses a Dev Container for development to ensure a consistent, fully-configured environment.
Required:
- Docker installed and running
- VS Code with Remote - Containers extension
- Internet connection for initial container build
Included in container:
- Ubuntu 24.04 with Kernel 6.8+ headers
- All build tools (gcc, make, kernel headers)
- Static analysis tools (clang-format, sparse, cppcheck) pre-installed
- Git pre-commit hooks automatically configured
- Zero manual configuration required
- Open the project in VS Code
- Click "Reopen in Container" when prompted (or use Command Palette: "Remote-Containers: Reopen in Container")
- Wait for the container to build and initialize (first time only)
- Build the project:
make allThis will build:
build/elf_det.ko- The kernel modulebuild/proc_elf_ctrl- The user program
All dependencies, tools, and hooks are pre-configured automatically!
sudo make installThis loads the module into the kernel. Verify it's loaded:
lsmod | grep elf_det./build/proc_elf_ctrlThe program will prompt you to enter a process ID (PID). You can find PIDs using:
ps aux | grep <process_name>***************************************************************************
******Navid user program for gathering memory info on desired process******
***************************************************************************
***************************************************************************
************enter the process id: 1234
the process info is here:
PID NAME CPU START_CODE END_CODE START_DATA END_DATA BSS_START BSS_END ELF
01234 bash 0.5 0x0000563a1234 0x0000563a5678 0x0000563a9abc 0x0000563adef0 0x00007ffc1234 0x00007ffc5678 0x0000000000000040
sudo make uninstallFor maximum safety, test the kernel module in an isolated QEMU virtual machine that won't affect your host system.
# One-time setup
./scripts/qemu-setup.sh
# Start VM
./scripts/qemu-run.sh
# In another terminal, run automated tests
./scripts/qemu-test.sh- Downloads Ubuntu 24.04 VM image
- Configures VM with kernel headers
- Provides SSH access on port 2222
- Completely isolates module testing from your host
- Automated build, install, test, and uninstall cycle
See scripts/README.md for detailed QEMU testing documentation.
| Target | Description |
|---|---|
make all |
Build both kernel module and user program (default) |
make module |
Build kernel module only |
make user |
Build user program only |
make install |
Install kernel module (requires root) |
make uninstall |
Remove kernel module (requires root) |
make test |
Install module and run user program |
make unit |
Build and run function-level unit tests (no kernel required) |
make clean |
Remove all build artifacts |
make help |
Display help message |
| Target | Description |
|---|---|
make check |
Run all static analysis checks (checkpatch + sparse + cppcheck) |
make checkpatch |
Check kernel coding style with checkpatch.pl |
make sparse |
Run sparse static analyzer for kernel code |
make cppcheck |
Run cppcheck static analyzer for C/C++ |
make format |
Format all source files with clang-format |
make format-check |
Check if code is properly formatted (CI-friendly) |
The kernel module creates entries in /proc/elf_det/:
/proc/elf_det/pid- Write-only file to specify target PID/proc/elf_det/det- Read-only file to retrieve process information
Key Functions:
elfdet_show()- Main function to gather and format process informationprocfile_write()- Handles PID input from user spaceprocfile_read()- Returns formatted process data
Kernel APIs Used:
proc_fs.h- Proc filesystem operationsseq_file.h- Sequential file interfacesched.h- Task/process structuresmm_types.h- Memory management structures
Simple C program that supports two modes:
- Interactive: prompts for a PID, writes to
/proc/elf_det/pid, then reads/proc/elf_det/detand prints two lines - Argument mode: run
./build/proc_elf_ctrl <PID>to write the PID and print exactly two lines non-interactively
You can override the proc directory for testing with the environment variable ELF_DET_PROC_DIR.
Example:
ELF_DET_PROC_DIR=/tmp/fakeproc ./build/proc_elf_ctrl 12345Internally, path construction is handled via helper build_proc_path().
Helper headers used:
src/user_helpers.h– path building with env overridesrc/elf_helpers.h– pure functions for CPU usage and BSS range
This project includes comprehensive static analysis and code formatting tools to ensure high code quality and compliance with Linux kernel coding standards.
Run the installation script to install all required tools:
./scripts/install-analysis-tools.shThis installs:
- clang-format: Code formatting according to kernel style
- sparse: Semantic parser for C, designed for kernel code
- cppcheck: General C/C++ static analysis
- checkpatch.pl: Official kernel coding style checker (requires kernel sources)
Run all checks at once:
make checkOr run individual checks:
# Check kernel coding style
make checkpatch
# Run sparse static analyzer
make sparse
# Run cppcheck analyzer
make cppcheckFormat all source files automatically:
make formatCheck if code is properly formatted (useful for CI/CD):
make format-check.clang-format- clang-format configuration (Linux kernel style).cppcheck-suppressions- Suppression list for false positives.editorconfig- Editor configuration for consistent coding style
Official Linux kernel coding style checker. Enforces kernel coding standards including:
- Indentation and spacing rules
- Line length limits
- Function declaration style
- Comment formatting
- Macro usage patterns
Semantic parser specifically designed for kernel code. Detects:
- Type confusion errors
- Endianness issues
- Lock context imbalances
- Address space mismatches
- Null pointer dereferences
General-purpose C/C++ static analyzer. Finds:
- Memory leaks
- Buffer overflows
- Uninitialized variables
- Dead code
- Logic errors
Code formatter that ensures consistent style:
- 8-space tabs (kernel standard)
- 80-column limit
- Linux brace style
- Proper spacing and alignment
The module has been tested on:
- Ubuntu 20.04 LTS (Kernel 5.15+)
- Ubuntu 22.04 LTS (Kernel 5.19+)
- Ubuntu 24.04 LTS (Kernel 6.8+)
Kernel Compatibility Notes:
- Kernel 5.6+ required (proc_ops API)
- Kernel 6.8+ recommended (VMA iterator API)
- The code has been updated to use modern kernel APIs including VMA iterators and proc_ops
Safe Testing Options:
Run pure function tests (no kernel required):
make unitThis builds and runs:
src/elf_det_tests.c– verifiescompute_usage_permyriad()andcompute_bss_range()src/proc_elf_ctrl_tests.c– verifiesbuild_proc_path()with and withoutELF_DET_PROC_DIR
Artifacts are created under build/.
- Dev Container (current setup) - Isolated from host
- QEMU VM (recommended for extra safety from kernel) - See scripts/README.md
- Cloud VM - Disposable testing environment
- Use the dev container for development - it provides a consistent, fully-configured environment
- Root privileges are required to load/unload kernel modules
- The container includes Ubuntu 24.04 with Kernel 6.8+ (VMA iterator API support)
- Accessing invalid PIDs may cause undefined behavior
- Always unload the module before rebuilding
- Static analysis tools and Git hooks are automatically configured on container startup
This project includes comprehensive static analysis and code formatting tools to ensure high code quality and compliance with Linux kernel coding standards.
All tools and hooks are automatically installed and configured in the dev container. No manual setup required!
The following tools are included:
Purpose: Automatic code formatting
Standard: Linux kernel coding style
Configuration: .clang-format
Features:
- 8-space tabs (kernel standard)
- 80-column line limit
- Linux brace style
- Consistent spacing and alignment
Usage:
make format # Format all files
make format-check # Check formatting (CI-friendly)Purpose: Kernel coding style compliance
Source: Official Linux kernel scripts
Checks:
- Indentation and spacing rules
- Line length limits (80 columns preferred)
- Function declaration style
- Comment formatting (/* */ style)
- Macro usage patterns
- Variable naming conventions
Usage:
make checkpatchPurpose: Semantic analysis for C code
Specialty: Kernel-specific checks
Detects:
- Type confusion errors
- Endianness issues (
__be32,__le32) - Lock context imbalances
- Address space mismatches (
__user,__kernel) - Null pointer dereferences
Usage:
make sparsePurpose: General C/C++ static analysis
Configuration: .cppcheck-suppressions
Detects:
- Memory leaks
- Buffer overflows
- Uninitialized variables
- Dead code
- Logic errors
Usage:
make cppcheckRun all checks at once:
make checkOr run individual checks:
make checkpatch # Kernel coding style
make sparse # Kernel static analysis
make cppcheck # General C/C++ analysis
make format # Format all code.clang-format- Code formatting rules (Linux kernel style).cppcheck-suppressions- Suppression list for false positives.editorconfig- Editor configuration for consistent coding style
Pre-commit hooks are automatically installed on container startup.
The pre-commit hook runs:
- Code formatting checks
- Cppcheck static analysis
- Checkpatch coding style validation
To bypass hooks (use sparingly):
git commit --no-verify -m "message"Follow Linux Kernel Coding Style:
- Use tabs (8 spaces), not spaces for indentation
- 80 column limit for code
- Opening brace on same line (except functions)
- Space after keywords:
if (,while (,for ( - No space after function names:
function(arg) - Use C89-style comments:
/* comment */
Example:
int example_function(int param)
{
if (param > 0) {
/* Comment style: C89 */
return param * 2;
}
return 0;
}Development Workflow:
# Everything is ready - just start coding!
# Format code before committing
make format
# Run all checks
make check
# Commit (hooks run automatically)
git commit -m "Your message"# Check kernel logs
dmesg | tail -n 20
# Verify kernel headers are installed
ls /lib/modules/$(uname -r)/build# Install missing dependencies
sudo apt-get install -y build-essential linux-headers-$(uname -r)
# Clean and rebuild
make clean
make all# Ensure module is loaded
lsmod | grep elf_det
# Check proc entries exist
ls -la /proc/elf_det/This project is licensed under Dual BSD/GPL license.
Navid Malekghaini
Contributions, issues, and feature requests are welcome!
- Integrated static analysis tools (clang-format, sparse, cppcheck, checkpatch)
- Automated Git pre-commit hooks for code quality
- Dev container with zero-configuration setup
- Enhanced CI/CD pipeline with static analysis checks
- Comprehensive code quality documentation
- Initial release with basic process information extraction
- Support for CPU usage, memory layout, and ELF sections
- User-space controller program
- Function-level unit tests for core functionality
- Dev container support for isolated development
- CI/CD pipeline with GitHub Actions
- QEMU testing environment for safe kernel module testing
- Comprehensive documentation and quick reference guides
- Dual BSD/GPL license
Note: This is an educational project demonstrating Linux kernel module development. Use responsibly and at your own risk.