r/linuxadmin • u/Vegetable-Escape7412 • May 22 '26
Your Linux system has +6,000 kernel modules which can be autoloaded. You use 80 of them. ModuleJail blacklist all of the unused ones. Server and desktop profiles and much more in a simple shell script.
Hey r/linuxadmin. I'm the author of this so I'm flagging that up front - this is a "would love feedback from people running real fleets" post.
The problem. Modern distro kernels ship with thousands of loadable modules. Almost all of them are attack surface that you're paying for in availability (autoload via udev, hotplug, dependency resolution) but not using. With AI-assisted kernel vulnerability discovery accelerating, every module a host can load but doesn't need to load is a problem you'd rather not have.
ModuleJail walks lsmod, treats whatever is loaded right now as "necessary," and writes a modprobe.d blacklist file for everything else. Optionally adds a --whitelist-file for modules you want preserved even if they're not currently loaded (think: rarely-used filesystem drivers you mount once a quarter).
What it isn't.
- Not a vulnerability scanner. The model is "unused, therefore blacklisted," not "vulnerable, therefore blacklisted."
- Not a defense against an attacker who already has root - they can rm the file. It's about reducing the unprivileged-trigger / autoload paths.
- Not initramfs-aware. Modules baked into the initrd are out of scope.
- Not a daemon, not a monitor. Single POSIX shell script, runs once, writes one file in /etc/modprobe.d/.
Revert.
rm /etc/modprobe.d/modulejail-blacklist.conf
and you're back. No reboot needed - the kernel reads modprobe.d at load time. Explicit sudo modprobe foo always wins over the blacklist, by design.
What I want feedback on. What does this need before you'd run it across a fleet? Things I've heard so far: an Ansible role, a --dry-run flag, JSON output for diff-friendly state tracking, kernel-version pinning in the generated file header. What else?
Repo: github.com/jnuyens/modulejail
License: GPL-3.0
Packaging: .deb and .rpm on the releases page; AUR package today.
16
u/wosmo May 22 '26
I've been wondering about the feasibility of just setting kernel.modules_disabled=1. Obviously it'd need to be done post-boot, if it's done too early it could be problematic. But from my understanding, it'd stop all module loading without affecting modules already loaded.
16
u/yadad May 23 '26
Once your system has booted with all required services running, there's no more need to load more modules. Also, your script doesn't stop any new modules, only blacklists known existing modules.
echo 1 > /proc/sys/kernel/modules_disabled
echo confidentiality > /sys/kernel/security/lockdown
Use this first line to do what you need - disable new modules entirely. The 2nd line is equally good.
https://linuxsecurity.com/howtos/learn-tips-and-tricks/lockdown-mode-kernel-self-protection
30
u/threar May 22 '26
If you're going this far why blacklist over just removing (or moving them elsewhere) the unused modules? Another option if this is "across a fleet" is to recompile and rebuild the RPM (or deb) and push a stripped down kernel and then you keep a closer watch on what's actually available.
5
u/michaelpaoli May 23 '26
Don't forget about proc/sys/kernel/modules_disabled
Set that, and no loading of additional modules, nor unloading of loaded modules, and that can't be changed, even by root, short of a reboot.
If one merely blacklists/whitelists, root can change/bypass that, so it's not so securely locked in.
Hmmm, I was under the impression, that, at least once-upon-a-time Linux had same or similar mechanism, but when activating, one could optionally set a password at that time, and then that password was the only way to revert that setting short of a reboot - and that this capability/idea had at least originally come from BSD (and that it might've been Linux that added the capability of setting a password at that time to allow it to later be reverted short of a reboot). But at least at present with very quick search I'm not finding references to such password capability.
3
u/ReachingForVega May 23 '26
This is so simple it's brilliant. I need to add this to all my homelab servers (Ubuntu) asap.
Have you tested it on Synology devices?Ā
2
u/lihaarp May 23 '26
Feels like just disabling autoloading would be more sensible.
Also this would need some form of notification system for attempted module loads, as otherwise you'll be very confused when your new USB gadget just won't work.
2
u/xiaodown May 23 '26
We have an automated vulnerability scanner bot that raises (and depending on configuration, optionally merges) security fixes.
Plus, our software gets built into docker images and run in containers anyway, and we deploy at least once per day. If thereās no merges to main, thereās an automated build that kicks off a fresh build and deploy.
Itās been forever since Iāve logged into a production system. I donāt even have access beyond dev environments (local -> dev -> merge to main -> staging -> soak -> prod progressive rollout).
4
u/archontwo May 23 '26
Ahem. Not to be that guy. But when initramd is built you can choose which modules are included. The default option is 'most' which includes some common ones plus whatever you are currently running.
Still nice you found an itch to scratch, so far be it to say it is wasted effort, but you might have learned the boot process a little better to use the tooling already available to you.Ā
11
u/barkwahlberg May 23 '26
Ok Claude, now that you made my Bash script, write a Reddit post for me
1
u/frymaster May 23 '26 edited May 23 '26
We run shared-node shell services for 5000+ users, on a variety of distros. Our use-case is going to be running the script, generating a deny-list file, and then pushing that out to our nodes, rather than installing anything
that "minimal" is the most-minimal profile is a problem for us. Not all of the modules in the list are loaded on all of our systems. Obviously the file is easy to edit, but it'd be nice if we didn't have to i.e. if there was a "none" profile.
EDIT: I'm not sure why, when logging, you use
exit 0when failing-true and/bin/falsewhen failing-false - you could just doexit 1in the latter case3
u/Kurgan_IT May 23 '26
Nice idea, not invasive, easy to revert, easy to disable, then do the actions that load needed new modules, and then re-run to get a new configuration. Nice.
2
u/ReachingForVega Aug 24 '26
Just came back to say I'm running this and great work continuing to maintain it /u/vegetable-escape7412
-3
u/Dolapevich May 23 '26
I build my own kernel with the required support, sorry.