It depends on your distro/system.
Linux loadable modules are ko files located in /lib/modules/$(uname -r)/, sorted into sub-folders. A few files in this folder helps handling them all, especially modules.dep[.bin] and modules.order[.bin]. modprobe read those files to get full path of a module and its dependencies.
Usual computer Linux distros
On most Linux distributions, module files are installed by packages of your package manager. A core set of them is installed by a quite generic package named kernel-modules for example, and on some distributions extra less-used modules can be installed by specific packages (modules are split into several packages). You can find which packages provides unwanted modules and uninstall them.
Be warned:
- Even if you uninstall a specific extra module package, its name and path will still be listed in
module.order and modules.dep files. Those files are not a threat and you need them in order to use modprobe. They will be removed only if you uninstall core modules package.
- Uninstalling modules can break your system, by removing the key features of you kernel. You likely want to execute
lsmod to get a minimal list of modules you should not remove.
You may be tempted to edit text files to remove references of unwanted modules, but editing binary files counterparts is likely tedious and those files will be recovered every time their package gets updated.
Embedded systems
Small embedded systems can still rely on loadable modules, but may not provide any package managers.
You may need to rebuild your embedded system entirely to alter it since some embedded systems use read-only root file systems.
Rebuild your kernel or your embedded system entirely
Depending on your need, you may want to rebuild your kernel in order to
- remove support of unwanted features
- lower the use of loadable modules by building them inside the kernel itself whenever possible
- enable module signing so your kernel will reject patched or foreign modules
- and even remove kernel support for loadable modules so loading one will not be possible at all
Doing this requires some skills though.
Other kernel hardening techniques
A few other easy-to-use mechanisms can increase your kernel security against dynamically loaded modules:
- Writing 1 to sysctl kernel.modules_disabled special file (actually /proc/sys/kernel/modules_disabled) deny any further loadable module loading or unloading, freezing them.
- Doing the same with kernel.kexec_load_disabled deny anyone from hot-switching to another kernel binary (quite recent kernels feature used to apply kernel upgrades on servers without downtime)