Restrict Kernel Modules
Kernel Modules
In Linux, the kernel handles core system functions and manages hardware. To keep the kernel compact, additional features can be added dynamically through kernel modules.
Kernel modules are pieces of code that extend the kernel’s capabilities without needing a system reboot.
Use Cases
-
Hardware Support
- Device drivers for different hardware components.
- Provide support for virtualized hardware, e.g. in the cloud
-
Filesystem Support
- Modules for supporting various file system formats.
- Support for specialized filesystems like ZFS, Btrfs, or NTFS.
-
Network Protocols
- TCP/IP stack and other network protocol modules.
-
Security Modules
- Enhancing security with modules like SELinux and AppArmor.
Security Considerations
-
Unauthorized Modules
- Loaded unauthorized modules can create security risks.
- Restrict and control module loading to mitigate these risks.
-
Regular Updates
- Keep modules, especially third-party ones, updated.
-
Monitoring
- Regularly check for unexpected or unauthorized changes.
- se monitoring tools to alert on new module loads or changes
Example Commands
-
Load a module:
sudo modprobe <module-name>
-
Unload a module:
sudo rmmod <module-name>
-
List loaded modules:
lsmod
-
View module information:
modinfo <module-name>
Restricting Kernel Modules
To enhance security, restrict kernel module loading to prevent unauthorized modules from being loaded.
Methods:
-
sysctl Configuration
Usesysctl
to disable module loading.Edit
/etc/sysctl.conf
:kernel.modules_disabled = 1
Apply changes:
sudo sysctl -p
-
modprobe Configuration
Configuremodprobe
to restrict specific modules.Example
modprobe.conf
:install usb-storage /bin/true
Apply changes:
sudo modprobe -r <module-name>
-
Blacklisting Modules
Prevent specific modules from loading by editing/etc/modprobe.d/blacklist.conf
.Example blacklist.conf:
blacklist <module-name>
Apply changes:
sudo update-initramfs -u
Reboot and check:
shutdown -r now
lsmod | grep <module>
Caution:
- Modifying kernel module loading can affect system functionality.
- Review and update module restrictions regularly.
- Document changes and test before applying to production.
Disable Loading of USB Storage Modules
-
Edit
/etc/sysctl.conf
or create a new file in/etc/sysctl.d/
:# Disable loading of kernel modules
kernel.modules_disabled = 1Apply changes:
sudo sysctl -p
-
Create or edit
/etc/modprobe.d/blacklist-usb-storage.conf
:# Blacklist USB storage modules
blacklist usb-storageApply changes:
sudo update-initramfs -u
Disable Loading of DCCP Module
Another module that we can disabled is the Datagram Congestion Control Protocol (DCCP) Module.
-
Edit
/etc/modprobe.d/blacklist.conf
:blacklist dccp
-
Reboot and verify:
shutdown -r now
lsmod | grep dccp