Skip to content
systemcoding.dev
Kernels

Check a Module's vermagic Against the Running Kernel With modinfo

Compare a module's vermagic string with uname -r using modinfo to see why insmod says Invalid module format, and what the version magic and CRC checks protect.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, Rust and C logos.

The check that decides whether a module loads

Of all the ways loading a module fails, "Invalid module format" is the most common. It means the kernel compared the module's recorded build configuration with its own and found a difference. The recorded string is the version magic, vermagic, and modinfo prints it. Learning to read it turns a vague error into a one-line diagnosis.

This post shows how to read vermagic, what each token means, what the kernel checks beyond it (symbol checksums), how to compare a module with the running kernel and what to do about a mismatch. Everything is for Linux 6.12 LTS with the tools of kmod 33.

Read the vermagic

Shell
modinfo -F vermagic /lib/modules/"$(uname -r)"/kernel/net/dummy.ko
modinfo -F vermagic ./hello.ko
uname -r
Text
6.12.9-amd64 SMP preempt mod_unload modversions
6.12.9-amd64 SMP preempt mod_unload modversions
6.12.9-amd64

The -F option prints one field, here vermagic. The string begins with the kernel release the module was built for and continues with a list of configuration tokens. The module path and the values on your machine will differ.

What the tokens mean

The first token is UTS_RELEASE, the release string, which equals uname -r for a matching kernel. The rest are features compiled into the kernel whose binary interface a module depends on:

TokenCorresponding optionWhy a module cares
SMPCONFIG_SMPSpinlock and per-CPU data layouts differ on a uniprocessor build
preemptCONFIG_PREEMPTLocking and preempt_count code differ
preempt_rtCONFIG_PREEMPT_RTSpinlocks are sleeping locks
mod_unloadCONFIG_MODULE_UNLOADThe module has a reference counter and exit code
modversionsCONFIG_MODVERSIONSThe module carries symbol checksums
gcc-14 or clangBuild compiler (some configurations)Compiler-specific ABI features

Several other tokens appear on some architectures, such as MIPS32_R2 or ARMv7, and the exact list comes from include/linux/vermagic.h in the source tree of the kernel in question. The rule is simple: the whole string must be equal, not just the release.

What a mismatch looks like

Take a module built for a different kernel and try to load it:

Shell
sudo insmod ./hello.ko
Text
insmod: ERROR: could not insert module ./hello.ko: Invalid module format

The terminal message is generic. The kernel log says exactly what differed:

Shell
sudo dmesg | tail -n 1
Text
hello: version magic '6.8.0-45-generic SMP preempt mod_unload modversions ' should be '6.12.9-amd64 SMP preempt mod_unload modversions '

The first quoted string is the module's vermagic and the second is the running kernel's. Read them side by side. In this example the release differs, which is the commonest cause: the module was built for an older kernel, or the headers for the wrong one. When the release matches and a token differs, such as preempt versus none, the module was built against a kernel with a different configuration, for example a header tree from another flavor of the same release.

The second check: symbol versions

vermagic guards the build configuration. CONFIG_MODVERSIONS adds a finer check: each exported symbol has a checksum computed from its type signature, and a module records the checksum of every symbol it uses. At load time the kernel compares them. A mismatch gives a different error:

Text
hello: disagrees about version of symbol module_layout

or, for a specific function:

Text
hello: disagrees about version of symbol printk

This happens when the module was built against headers of a kernel with the same release string but a different source or configuration, so a structure changed. It is the check that catches a module built against a distribution's headers and loaded into a custom kernel with the same version number. The checksums live in Module.symvers of the build tree and in the module's __versions section, which modprobe --dump-modversions can print:

Shell
modprobe --dump-modversions ./hello.ko | head -n 3
Text
0x4f4a3b2c	module_layout
0x9d1e7b50	_printk
0xbdfb6dbb	__fentry__

The checksum values depend on the kernel the module was built against and will differ from these.

A script that checks before you load

You can compare the two strings in a script, to give a clear message instead of the generic error:

Shell
#!/bin/sh
mod="$1"
want="$(modinfo -F vermagic "$mod")"
have="$(cat /proc/sys/kernel/osrelease)"

case "$want" in
    "$have "*) echo "release matches: $have" ;;
    *) echo "module built for '${want%% *}', kernel is '$have'" >&2; exit 1 ;;
esac
Text
module built for '6.8.0-45-generic', kernel is '6.12.9-amd64'

This compares only the release, which is the usual mismatch. For a complete comparison, a tool that has the kernel's own full vermagic would be needed, and the practical way is to rebuild against the right headers rather than to reason about each token.

What to do about a mismatch

Rebuild against the running kernel. Install the matching headers (linux-headers-$(uname -r)) and run make again. This fixes the release and the tokens at once. It is the answer in nearly every case.

Use DKMS for third-party modules, so the rebuild happens automatically when a new kernel is installed. The next post covers it.

Do not force it. modprobe --force-vermagic and --force-modversion exist, and with CONFIG_MODULE_FORCE_LOAD the kernel can accept a mismatched module. They bypass the safety check. A module built for another configuration reads structures at wrong offsets, which corrupts memory and often crashes the machine without warning. Treat forcing as a last resort for debugging, never a workaround.

Reading vermagic from a binary without modinfo

On a system without kmod, the string is in the module's .modinfo section as plain text:

Shell
strings hello.ko | grep '^vermagic='
Text
vermagic=6.12.9-amd64 SMP preempt mod_unload modversions

The module's .modinfo section holds key=value strings, which is where modinfo reads the license, author and parameters from too.

Why the kernel is this strict

It is reasonable to ask why the kernel refuses a module whose release string matches but whose tokens differ. The answer is that a kernel module is not a program that talks to the kernel through a stable interface. It is machine code that is linked into the kernel's address space and reads and writes the kernel's own structures directly. The kernel project says plainly that it has no stable in-kernel ABI: a structure can gain a field, a lock can change type or a macro can expand differently, between any two builds. A module compiled against yesterday's layout and loaded into today's kernel would read the wrong offsets and write the wrong memory, with no error at the moment of the mistake.

Each token in the version magic stands for one such hazard. SMP changes how spinlocks and per-CPU variables are laid out, so a module built without it would use locks that do not lock. preempt and preempt_rt change what spin_lock means. modversions makes the kernel prove, symbol by symbol, that the function a module calls still has the prototype the module was compiled with. Together they approximate "this module was compiled against exactly this kernel", and that is the only guarantee the kernel can make, so it makes it strictly and refuses otherwise.

Modules from distributions and vendors

Vendor and distribution modules are built per kernel, which is why a driver package from a hardware vendor lists a long matrix of supported kernels, and why a binary module "for 6.12" often fails on 6.12.12 from another distribution. The vendor built one specific configuration. If your kernel differs in a single relevant option, the check fails, and the vendor's only fix is to rebuild. Source-based delivery, with the module compiled on your machine against your headers, avoids the problem by construction, which is the idea behind DKMS and the reason open drivers are easier to keep working than binary-only ones.

When you debug a loading failure, work down a short list in order. Read the dmesg line, compare the two version magic strings, check that modinfo shows the module you think it does and not a stale copy elsewhere on the module path, and confirm with uname -r that you rebooted into the kernel you built for. Those four checks resolve nearly every report of an invalid module format.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.