module: Extend module_blacklist parameter to built-in modules
Subsystems (4)
Aaron Tomlin
reviewerv9 cover module: Extend module_blacklist parameter to built-in modules
Currently, the "module_blacklist=" command-line parameter only applies to loadable modules. If a module is built-in, the parameter is silently ignored. This patch series extends the blacklisting functionality to built-in modules by intercepting their initialisation routines during early boot.
Following review feedback, the implementation has been split into two separate changes to decouple the introduction of the new feature from the terminology renaming:
1. The first patch extends the "module_blacklist=" parameter to built-in modules using the original blacklist terminology. It introduces the ".initcall.modnames" section to map initcall function pointers to their associated KBUILD_MODNAME strings (restricted only to module_init() invocations to save memory and avoid matching core kernel subsystems). It also restricts the check to a boot-time __init wrapper to eliminate Use-After-Free (UAF) and Spectre v1 vulnerability risks when loading dynamic modules at runtime, and adds a fast-path check to eliminate lookup overhead when the parameter is not in use
2. The second patch renames the variables and helper functions to adopt the preferred "module_denylist=" and module_is_denylisted() terminology in the codebase. To preserve the existing user-space ABI, "module_blacklist=" is kept as a legacy alias pointing to the same module_denylist variable
Changes since v8:
- Extended Rust procedural macro support in rust/macros/module.rs to generate .initcall.modnames metadata for built-in Rust modules, maintaining feature parity with C built-in modules when evaluating "module_blacklist=" and "module_denylist=" (Petr Pavlu)
- Merged the intermediate ___define_initcall_modname macro directly into __define_initcall_modname in include/linux/init.h to clean up macro expansion (Petr Pavlu)
- Reverted the parameter name in module_init(x) back to 'x' in include/linux/module.h to remain consistent with surrounding comments (Petr Pavlu)
- Cleaned up whitespace formatting in kernel/module/main.c (Petr Pavlu)
- Link to v8: https://lore.kernel.org/lkml/[email protected]/
Changes since v7:
- Fixed a double evaluation of __initcall_id(fn) in the built-in module initcall macro expansion
- Link to v7: https://lore.kernel.org/lkml/[email protected]/
Changes since v6:
- Grouped the __initcall_fn_ptr() macro definition inside the existing CONFIG_HAVE_ARCH_PREL32_RELOCATIONS block in include/linux/init.h (Petr Pavlu)
- Localised the built-in module initcall level and section naming strictly to include/linux/init.h by introducing the macros __define_initcall_modname() and __builtin_module_initcall(), keeping include/linux/module.h clean (Petr Pavlu)
- Removed the unnecessary dereference_function_descriptor() lookup wrapper in get_builtin_modname() in favour of a direct pointer comparison (Petr Pavlu)
- Cleaned up whitespace formatting in kernel/module/main.c (Petr Pavlu)
- Link to v6: https://lore.kernel.org/lkml/[email protected]/
Changes since v5:
- Resolved a modpost cross-section mismatch warning by introducing do_one_initcall_builtin() as a strict __init wrapper function, rather than performing the built-in module checks inside the __init_or_module do_one_initcall() function
- Addressed a UAF race condition with concurrent dynamic module loading by strictly bounding the blacklist evaluation to early boot via the new __init wrapper, removing temporal check
- Mitigated a potential Spectre v1 speculative execution vulnerability by ensuring get_builtin_modname() is exclusively called by __init code, preventing unprivileged runtime module loading from speculatively jumping into reclaimed ".init.text" instructions
- Updated Documentation/admin-guide/kernel-parameters.txt to explicitly mark "module_blacklist=" as deprecated and document "module_denylist="
- Link to v5: https://lore.kernel.org/lkml/[email protected]/
Changes since v4:
- Split the monolithic patch into two distinct commits. One to extend the functionality to built-in modules, and a second to safely transition the internal terminology to "denylist" (Arnd Bergmann)
- Preserved "module_blacklist=" as a legacy core_param alias in the second commit to ensure backwards compatibility with existing userspace configurations
- Restricted the population of the ".initcall.modnames" section strictly to module_init() rather than all ___define_initcall() invocations. This prevents non-module core initcalls from being redundantly mapped, saving memory and avoiding false-positive matches (Petr Pavlu)
- Introduced a fast-path evaluation to check if the blacklist/denylist is actually populated before invoking get_builtin_modname(), avoiding unnecessary lookups during boot (Petr Pavlu)
- Link to v4: https://lore.kernel.org/lkml/[email protected]/
Changes since v3:
- Renamed the external function prototype and internal helper to module_is_denylisted(), while updating the backing variable in main.c to module_denylist. To preserve user-space compatibility while adopting modern terminology, separate core_param entries have been introduced, allowing both the preferred module_denylist= parameter and the legacy module_blacklist= parameter to resolve to the same underlying variable (Andrew Morton)
- I introduced the __initcall_fn_ptr() macro helper to dynamically resolve the initcall pointer configuration:
- For architectures with relative 32-bit relocations (CONFIG_HAVE_ARCH_PREL32_RELOCATIONS=y), it resolves to the relocation stub pointer __initcall_stub(fn, __iid, id)
- For architectures without PREL32 relocations, it resolves directly to the function pointer fn
- Decoupled the module_denylist parameter parsing and the module_is_denylisted() function from CONFIG_MODULES, moving the logic to init/main.c. This ensures the denylist works for built-in modules even on monolithic kernels built without loadable module support (CONFIG_MODULES=n)
- Removed the conditional stub implementation of module_is_denylisted() in module.h and replaced it with a single, unconditional declaration outside of the #ifdef CONFIG_MODULES block. This prevents compiler warnings about missing prototypes and ensures visibility under a monolithic configuration
- Replaced the initmem_freed state variable and its synchronisation logic in kernel_init() with race-free spatial boundary checks using is_kernel_text() and is_kernel_inittext() in initcall_get_modname()
- Aligned the .initcall_modnames table with relocations by assigning .initcall_fn using the __initcall_stub() helper in ___define_initcall(). This ensures the lookup matches the actual stub pointer passed to do_one_initcall() when CONFIG_HAVE_ARCH_PREL32_RELOCATIONS is enabled. Passed the preprocessor __iid argument to ____define_initcall_modname once to avoid double evaluation of __COUNTER__ (which caused build failures with LTO)
- Updated initcall_get_modname() in main.c to resolve the function pointer fn using dereference_function_descriptor(fn) prior to checking the .text and .init.text boundaries, and dereference both fn and p->initcall_fn in the comparison loop to support descriptor-based architectures (e.g., PPC64)
- Link to v3: https://lore.kernel.org/lkml/[email protected]/
Changes since v2:
- Avoided relative 32-bit offsets (PREL32) with inline assembly, opting instead for standard C structures with absolute pointers. This fixes LTO and CFI compatibility issues (e.g., under Clang) where raw inline assembly fails to track compiler-generated symbols and CFI stubs
- Placed module name strings into the ".init.rodata" section via a dedicated static array to ensure they are freed from memory after boot
- Avoided Use-After-Free (UAF) bugs post-boot when loading dynamic modules:
- Added an 'initmem_freed' flag, marked as '__ro_after_init', set after free_initmem() to skip table lookups for dynamically loaded modules
- Added a blacklist check in do_init_module() for dynamic modules
- Simplified the linker script using the BOUNDED_SECTION_PRE_LABEL() macro to define the ".initcall.modnames" section boundary
- Added a dummy/stub implementation of module_is_blacklisted() when CONFIG_MODULES is disabled to avoid build errors
- Link to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Pivoted entirely from exposing built-in initcalls and their blacklist status via a debugfs interface to directly extending the existing "module_blacklist=" and new "module_blacklist=" to intercept built-in modules at boot (Petr Pavlu)
- Implemented 32-bit relative offsets (CONFIG_HAVE_ARCH_PREL32_RELOCATIONS) to store the mappings, preventing binary bloat and preserving KASLR efficacy
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (2):
module: Extend module_blacklist parameter to built-in modules
module: Rename module_blacklist to module_denylist
.../admin-guide/kernel-parameters.txt | 6 +- include/asm-generic/vmlinux.lds.h | 4 +- include/linux/init.h | 24 +++++++- include/linux/module.h | 4 +- init/main.c | 55 ++++++++++++++++++- kernel/module/main.c | 25 +-------- rust/macros/module.rs | 16 ++++++ 7 files changed, 105 insertions(+), 29 deletions(-)
Signature · 1 line
2.55.0
Aaron Tomlin
reviewerPATCH v9 1/2 module: Extend module_blacklist parameter to built-in modules
Currently, the "module_blacklist=" command-line parameter only applies to loadable modules. If a module is built-in, the parameter is silently ignored. This patch extends the blacklisting functionality to built-in modules by intercepting their initialisation routines during early boot.
To achieve this, we introduce a new ".initcall.modnames" memory section. For each built-in module, we use a standard C structure (i.e., struct initcall_modname) to map its initcall function pointer to its associated KBUILD_MODNAME string. This mapping is restricted only to files implementing built-in modules via module_init() to avoid mapping core kernel subsystems and save memory.
During boot, built-in initcalls are executed sequentially via do_initcall_level() and do_pre_smp_initcalls(). We introduce a new wrapper function, do_one_initcall_builtin(), to cross-reference the initcall function pointer against the ".initcall.modnames" table. If a match is found and the module is present in the blacklist, the initcall is skipped.
To make the blacklist functional on monolithic kernels, the command-line parameter parsing and the module_is_blacklisted() lookup function are decoupled from the loadable module subsystem and moved to init/main.c. This enables "module_blacklist=" to intercept built-in modules even on kernels built with CONFIG_MODULES=n.
---
Design Considerations and Trade-offs:
1. LTO and CFI Compatibility vs. PREL32
Previous iterations of this patch attempted to use top-level inline assembly to generate 32-bit relative offsets (PREL32) to save memory. However, raw inline assembly operates blindly outside of the C compiler's visibility. When compiled with CONFIG_LTO_CLANG or CONFIG_CFI_CLANG, the compiler applies symbol renaming and generates Control Flow Integrity stubs. The raw assembly string-matching fails to track these changes, resulting in undefined references or runtime address mismatches.
To resolve this, we strictly use standard C structures to hold the function pointers. This natively allows the compiler to resolve LTO renaming and map CFI stubs correctly. We trade the minor spatial optimisation of PREL32 (using absolute 64-bit pointers instead) to guarantee architectural safety under modern compiler protections. Because this metadata is placed in an ".init" section and freed entirely after boot, the temporary memory overhead is negligible.
2. Architectural Safety and Elimination of Runtime Vulnerabilities:
By embedding the boot-time blacklist check inside the do_one_initcall_builtin() __init wrapper function, we ensure the metadata lookup logic is exclusively invoked during early boot. This approach provides strict structural guarantees:
- It inherently eliminates Use-After-Free (UAF) and race conditions since loadable modules (which execute post-boot and invoke do_one_initcall() directly) bypass this __init wrapper entirely.
- It prevents modpost section mismatch warnings since the __init metadata is strictly accessed by other __init functions.
- It mitigates Spectre v1 speculative execution vulnerabilities by guaranteeing the unprivileged runtime module loading path cannot speculatively branch into reclaimed .init.text instructions.
--- include/asm-generic/vmlinux.lds.h | 4 ++- include/linux/init.h | 24 +++++++++++++- include/linux/module.h | 4 ++- init/main.c | 54 +++++++++++++++++++++++++++++-- kernel/module/main.c | 23 +------------ rust/macros/module.rs | 16 +++++++++ 6 files changed, 98 insertions(+), 27 deletions(-)
include/asm-generic/vmlinux.lds.h +3 -1
@@ -734,7 +734,9 @@ EARLYCON_TABLE() \ LSM_TABLE() \ EARLY_LSM_TABLE() \ - KUNIT_INIT_TABLE() + KUNIT_INIT_TABLE() \ +STRUCT_ALIGN (); \ + BOUNDED_SECTION_BY(.initcall.modnames, _initcall_modnames)#define INIT_TEXT \ *(.init.text .init.text.*) \
include/linux/init.h +23 -1
@@ -252,6 +252,7 @@ extern struct module __this_module;#endif #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define __initcall_fn_ptr (fn, __iid, id) __initcall_stub(fn, __iid, id)#define ____define_initcall (fn, __stub, __name, __sec) \ __define_initcall_stub(__stub, fn) \ asm(".section \"" __sec "\", \"a\" \n" \
@@ -260,6 +261,7 @@ extern struct module __this_module; ".previous \n"); \static_assert (__same_type (initcall_t, &fn));#else +#define __initcall_fn_ptr (fn, __iid, id) fn#define ____define_initcall (fn, __unused, __name, __sec) \ static initcall_t __name __used \ __attribute__((__section__(__sec))) = fn;
@@ -271,7 +273,27 @@ extern struct module __this_module; __initcall_name(initcall, __iid, id), \ __initcall_section(__sec, __iid)) -#define ___define_initcall (fn, id, __sec) \ +struct initcall_modname { +initcall_t initcall_fn; +const char *modname; +}; + +#define ____define_initcall_modname (fn, id, __sec, __iid) \ + __unique_initcall(fn, id, __sec, __iid) \ + static const char __initstr_##fn[] __used __aligned(1) \ + __section(".init.rodata") = KBUILD_MODNAME; \ + static const struct initcall_modname __modname_##fn __used \ + __section(".initcall.modnames") = { \ + .initcall_fn = __initcall_fn_ptr(fn, __iid, id),\ + .modname = __initstr_##fn \ + }; + +#define __define_initcall_modname (fn, id) \ + ____define_initcall_modname(fn, id, .initcall##id, __initcall_id(fn)) + +#define __builtin_module_initcall (fn) __define_initcall_modname(fn, 6) + +#define ___define_initcall (fn, id, __sec) \ __unique_initcall(fn, id, __sec, __initcall_id(fn))#define __define_initcall (fn, id) ___define_initcall(fn, id, .initcall##id)
include/linux/module.h +3 -1
@@ -86,7 +86,7 @@ extern void cleanup_module(void);* builtin) or at module insertion time (if a module). There can only * be one per module. */ -#define module_init (x) __initcall(x); +#define module_init (x) __builtin_module_initcall(x);/** * module_exit() - driver exit entry point
@@ -883,6 +883,8 @@ static inline void module_for_each_mod(int(*func)(struct module *mod, void *data }#endif /* CONFIG_MODULES */ +bool module_is_blacklisted (const char *module_name); +#ifdef CONFIG_SYSFS extern struct kset *module_kset;extern const struct kobj_type module_ktype;
init/main.c +52 -2
72 lines not on this page · raw message
kernel/module/main.c +1 -22
@@ -2919,27 +2919,6 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr,return 0 ; } -/* module_blacklist is a comma-separated list of module names */ -static char *module_blacklist; -static bool blacklisted (const char *module_name) -{ -const char *p; -size_t len; - -if (!module_blacklist) -return false; - -for (p = module_blacklist; *p; p += len) { - len =strcspn (p,"," ); -if (strlen (module_name) == len && !memcmp (module_name, p, len)) -return true; -if (p[len] ==',' ) - len++; - } -return false; -} -core_param (module_blacklist, module_blacklist, charp,0400 ); -static struct module *layout_and_allocate (struct load_info *info,int flags) {struct module *mod;
@@ -3391,7 +3370,7 @@ static int early_mod_check(struct load_info *info, int flags)* Now that we know we have the correct module name, check * if it's blacklisted. */ -if (blacklisted (info->name)) { +if (module_is_blacklisted (info->name)) {pr_err ("Module %s is blacklisted\n" , info->name);return -EPERM ; }
rust/macros/module.rs +16 -0
@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(
@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm); +#[cfg(not( MODULE ))] +#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname { + initcall_fn: #ident_init, + modname: #name_cstr.as_ptr().cast(), + }; +#[cfg(not( MODULE ))] #[no_mangle] pub extern "C" fn #ident_init() -> ::kernel::ffi::c_int {
Signature · 1 line
2.55.0
Petr Pavlu
maintainerQuoted 5 lines · Aaron Tomlin
include/asm-generic/vmlinux.lds.h +0
-0
include/asm-generic/vmlinux.lds.h line 742
Quoted 10 lines
include/asm-generic/vmlinux.lds.h +3
-1@@ -734,7 +734,9 @@ EARLYCON_TABLE() \ LSM_TABLE() \ EARLY_LSM_TABLE() \ - KUNIT_INIT_TABLE() + KUNIT_INIT_TABLE() \ +STRUCT_ALIGN (); \ + BOUNDED_SECTION_BY(.initcall.modnames, _initcall_modnames)#define INIT_TEXT \ *(.init.text .init.text.*) \
I realized that instead of using STRUCT_ALIGN(), a cleaner and less wasteful approach is to prevent any potential over-alignment of individual .initcall.modnames sections by having ____define_initcall_modname() define each struct initcall_modname with __aligned(__alignof__(struct initcall_modname)).
Quoted 17 lines
rust/macros/module.rs +2
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm); +#[cfg(not( MODULE ))]
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 10 lines
rust/macros/module.rs +10
-0+#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {
Can Rust directly use the C definition of initcall_modname via ::kernel::bindings::initcall_modname?
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 606
Quoted 2 lines
rust/macros/module.rs +2
-0+ initcall_fn: #ident_init, + modname: #name_cstr.as_ptr().cast(),
Can the modname string be placed in .init.rodata to match the behavior on the C side?
Quoted 5 lines
rust/macros/module.rs +2
-0+ }; +#[cfg(not( MODULE ))] #[no_mangle] pub extern "C" fn #ident_init() -> ::kernel::ffi::c_int {
Signature · 2 lines
Thanks, Petr
Gary Guo
reviewerQuoted 54 lines · Petr Pavlu
Quoted 5 lines · Aaron Tomlin
include/asm-generic/vmlinux.lds.h +0
-0include/asm-generic/vmlinux.lds.h line 742
Quoted 10 lines
include/asm-generic/vmlinux.lds.h +3
-1@@ -734,7 +734,9 @@ EARLYCON_TABLE() \ LSM_TABLE() \ EARLY_LSM_TABLE() \ - KUNIT_INIT_TABLE() + KUNIT_INIT_TABLE() \ +STRUCT_ALIGN (); \ + BOUNDED_SECTION_BY(.initcall.modnames, _initcall_modnames)#define INIT_TEXT \ *(.init.text .init.text.*) \I realized that instead of using
STRUCT_ALIGN(), a cleaner and less wasteful approach is to prevent any potential over-alignment of individual .initcall.modnames sections by having____define_initcall_modname()define eachstruct initcall_modnamewith__aligned(__alignof__(struct initcall_modname)).Quoted 17 lines
rust/macros/module.rs +2
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm); +#[cfg(not( MODULE ))] rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 10 lines
rust/macros/module.rs +10
-0+#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {Can Rust directly use the C definition of
initcall_modnamevia ::kernel::bindings::initcall_modname?
Quoted 5 lines
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 606
Quoted 2 lines
rust/macros/module.rs +2
-0+ initcall_fn: #ident_init, + modname: #name_cstr.as_ptr().cast(),Can the modname string be placed in .init.rodata to match the behavior on the C side?
Putting strings in .init.rodata is more likely to grow the size of kernel because it cannot be deduplicated with other strings; the names are very likely to be in .rodata already due to it being added to sysfs when registering with a bus.
Best,
Gary
Quoted 6 lines
Quoted 6 lines
rust/macros/module.rs +2
-0+ }; +#[cfg(not( MODULE ))] #[no_mangle] pub extern "C" fn #ident_init() -> ::kernel::ffi::c_int {
Petr Pavlu
maintainerQuoted 36 lines · Gary Guo
Quoted 35 lines · Petr Pavlu
Quoted 17 lines · Aaron Tomlin
rust/macros/module.rs +2
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm); +#[cfg(not( MODULE ))] rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 10 lines
rust/macros/module.rs +10
-0+#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {Can Rust directly use the C definition of
initcall_modnamevia ::kernel::bindings::initcall_modname?Quoted 2 lines
rust/macros/module.rs +2
-0+ initcall_fn: #ident_init, + modname: #name_cstr.as_ptr().cast(),
Quoted 7 lines
Quoted 2 lines
Can the modname string be placed in .init.rodata to match the behavior on the C side?
Putting strings in .init.rodata is more likely to grow the size of kernel because it cannot be deduplicated with other strings; the names are very likely to be in .rodata already due to it being added to sysfs when registering with a bus.
On the other hand, if these module name strings are not placed in .init.rodata and don't get merged with an existing string in vmlinux, some memory will be wasted after initialization completes. Built-in drivers with device_driver::mod_name should have their names in vmlinux but the same is not necessarily true for other modules.
For instance, my system is running openSUSE Tumbleweed with the stable 7.1.8 kernel. It has 227 built-in modules, about half of which are drivers.
$ wc -l "/usr/lib/modules/$(uname -r)/modules.builtin" 227 /usr/lib/modules/7.1.8-1-default/modules.builtin $ grep ^kernel/drivers "/usr/lib/modules/$(uname -r)/modules.builtin" | wc -l 125
Looking deeper, the script below runs the strings utility on vmlinux and checks whether the name of each built-in module is already present in the binary, at least as a suffix of another string.
On my system, the script shows that 156 modules have their names present in vmlinux, while 71 names are missing. The total size of the present module names is 1436 bytes, while the size of the missing module names is 911 bytes.
This means that if .initcall.modnames places its strings in .init.rodata, the size of the on-disk and initial kernel image should increase by 1436+911 bytes. On the other hand, if the strings are not placed in .init.rodata, 911 bytes will be wasted after initialization completes.
So there is a trade-off.
Signature · 29 lines
Cheers,
Petr
#!/bin/bash
vmlinux=$(xzcat "/usr/lib/modules/$(uname -r)/vmlinux.xz" | strings)
matched=0 unmatched_modules=0 extra_bytes=0 lost_bytes=0
for file in $(cat "/usr/lib/modules/$(uname -r)/modules.builtin"); do
base=$(basename --suffix=.ko "$file" | tr '-' '_')
echo "$vmlinux" | grep -q "$base$"
ret=$?
if [ "$ret" -eq 0 ]; then
matched_modules=$((matched_modules + 1))
extra_bytes=$((extra_bytes + ${#base} + 1))
else
unmatched_modules=$((unmatched_modules + 1))
lost_bytes=$((lost_bytes + ${#base} + 1))
fi
echo $ret $base
done
echo
echo "Matched modules: $matched_modules"
echo "Extra bytes: $extra_bytes"
echo
echo "Unmatched modules: $unmatched_modules"
echo "Lost bytes: $lost_bytes"Aaron Tomlin
reviewerQuoted 14 lines · Petr Pavlu
Quoted 13 lines · Aaron Tomlin
include/asm-generic/vmlinux.lds.h +3
-1@@ -734,7 +734,9 @@ EARLYCON_TABLE() \ LSM_TABLE() \ EARLY_LSM_TABLE() \ -KUNIT_INIT_TABLE () + KUNIT_INIT_TABLE() \ +STRUCT_ALIGN (); \ + BOUNDED_SECTION_BY(.initcall.modnames, _initcall_modnames)
Quoted 8 lines
include/asm-generic/vmlinux.lds.h line 742
Quoted 2 lines
include/asm-generic/vmlinux.lds.h +0
-0#define INIT_TEXT \ *(.init.text .init.text.*) \I realized that instead of using
STRUCT_ALIGN(), a cleaner and less wasteful approach is to prevent any potential over-alignment of individual .initcall.modnames sections by having____define_initcall_modname()define eachstruct initcall_modnamewith__aligned(__alignof__(struct initcall_modname)).
Hi Petr,
Nice, I see. So, adding __aligned(__alignof__(struct initcall_modname)) forces the compiler to use the structure's natural alignment (8 bytes on 64-bit), ensuring entries are packed contiguously with zero inter-element padding.
Quoted 16 lines
Quoted 16 lines
rust/macros/module.rs +1
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm);
Quoted 14 lines
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 11 lines
rust/macros/module.rs +11
-0+#[cfg(not( MODULE ))] +#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {Can Rust directly use the C definition of
initcall_modnamevia ::kernel::bindings::initcall_modname?
Well, defining the local '#[repr(C)] struct InitcallModname' follows the existing precedents and avoids expanding the bindgen surface with <linux/init.h>. I think this is preferred, no? Gary, any thoughts?
In rust/macros/module.rs the existing built-in initcall pointers are defined directly within the macro rather than pulling in initcall_t from bindings, keeping module.rs self-contained.
Quoted 5 lines
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 606
Quoted 2 lines
rust/macros/module.rs +2
-0+ initcall_fn: #ident_init, + modname: #name_cstr.as_ptr().cast(),Can the modname string be placed in .init.rodata to match the behavior on the C side?
This is a very good suggestion and definitely worth adopting! Thank you.
Kind regards,
Signature · 1 line
Aaron Tomlin
Aaron Tomlin
reviewerQuoted 31 lines · Aaron Tomlin
Quoted 16 lines
Quoted 16 lines
rust/macros/module.rs +1
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm);Quoted 14 lines
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 11 lines
rust/macros/module.rs +11
-0+#[cfg(not( MODULE ))] +#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {Can Rust directly use the C definition of
initcall_modnamevia ::kernel::bindings::initcall_modname?
Quoted 7 lines
Well, defining the local '#[repr(C)] struct InitcallModname' follows the existing precedents and avoids expanding the bindgen surface with <
linux/init.h>. I think this is preferred, no? Gary, any thoughts?In
rust/macros/module.rsthe existing built-in initcall pointers are defined directly within the macro rather than pulling ininitcall_tfrom bindings, keepingmodule.rsself-contained.
Hi Gary,
A gentle ping on this, to avoid holding up the series, shall we stick with this approach (i.e., keeping the local struct definition in the macro rather than expanding bindgen bindings)?
If there are no objections, I'll proceed with this for the next revision.
Kind regards,
Signature · 1 line
Aaron Tomlin
Gary Guo
reviewerQuoted 41 lines · Aaron Tomlin
Quoted 31 lines · Aaron Tomlin
Quoted 16 lines
Quoted 16 lines
rust/macros/module.rs +1
-0@@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {let ident_init =format_ident! ("__{ident}_init" );let ident_exit =format_ident! ("__{ident}_exit" );let ident_initcall =format_ident! ("__{ident}_initcall" ); +let ident_modname =format_ident! ("__{ident}_modname" );let initcall_section =".initcall6.init" ; let global_asm = format!(@@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {#[cfg( CONFIG_HAVE_ARCH_PREL32_RELOCATIONS )] ::core::arch::global_asm!(#global_asm);Quoted 14 lines
rust/macros/module.rs pub extern "C" fn cleanup_module() { · line 604
Quoted 11 lines
rust/macros/module.rs +11
-0+#[cfg(not( MODULE ))] +#[repr( C )] +struct InitcallModname { + initcall_fn:extern "C" fn () -> ::kernel::ffi::c_int , + modname: *const ::kernel::ffi::c_char , + } + +#[cfg(not( MODULE ))] +#[used(compiler)] +#[link_section = ".initcall.modnames" ] + static #ident_modname: InitcallModname = InitcallModname {Can Rust directly use the C definition of
initcall_modnamevia ::kernel::bindings::initcall_modname?Quoted 7 lines
Well, defining the local '#[repr(C)] struct InitcallModname' follows the existing precedents and avoids expanding the bindgen surface with <
linux/init.h>. I think this is preferred, no? Gary, any thoughts?In
rust/macros/module.rsthe existing built-in initcall pointers are defined directly within the macro rather than pulling ininitcall_tfrom bindings, keepingmodule.rsself-contained.
Quoted 7 lines
Hi Gary,
A gentle ping on this, to avoid holding up the series, shall we stick with this approach (i.e., keeping the local struct definition in the macro rather than expanding bindgen bindings)?
If there are no objections, I'll proceed with this for the next revision.
Adding linux/init.h to the list of headers exported by bindgen is fine. Given that this is a struct, I think Petr's suggestion to use generated bindings makes sense.
Best,
Gary
Aaron Tomlin
reviewerPATCH v9 2/2 module: Rename module_blacklist to module_denylist
In reply to Aaron TomlinTo preserve the existing user-space ABI, "module_blacklist=" is kept as a legacy alias pointing to the same module_denylist variable.
This patch addresses the documentation by marking "module_blacklist=" as deprecated in admin-guide/kernel-parameters.txt, and documents the new "module_denylist=" parameter. All internal symbols, such as module_is_blacklisted(), have been renamed to use "denylist" and all log messages now use "denylisted".
--- .../admin-guide/kernel-parameters.txt | 6 +++++- include/linux/module.h | 2 +- init/main.c | 19 ++++++++++--------- kernel/module/main.c | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -1
@@ -4179,7 +4179,11 @@ Kernel parameters Note that if CONFIG_MODULE_SIG_FORCE is set, that is always true, so this option does nothing. - module_blacklist= [KNL] Do not load a comma-separated list of + module_blacklist= [KNL] (deprecated) + This parameter has been renamed to module_denylist=. + Please use module_denylist= instead. + + module_denylist= [KNL] Do not load a comma-separated list of modules. Useful for debugging problem modules. mousedev.tap_time=
include/linux/module.h +1 -1
@@ -883,7 +883,7 @@ static inline void module_for_each_mod(int(*func)(struct module *mod, void *data }#endif /* CONFIG_MODULES */ -bool module_is_blacklisted (const char *module_name); +bool module_is_denylisted (const char *module_name);#ifdef CONFIG_SYSFS extern struct kset *module_kset;
init/main.c +10 -9
@@ -1337,17 +1337,17 @@ static inline void do_trace_initcall_level(const char *level)extern struct initcall_modname __start_initcall_modnames[];extern struct initcall_modname __stop_initcall_modnames[]; -/* module_blacklist is a comma-separated list of module names */ -static char *module_blacklist; -bool __init_or_module module_is_blacklisted(const char *module_name) +/* module_denylist is a comma-separated list of module names */ +static char *module_denylist; +bool __init_or_module module_is_denylisted(const char *module_name) {const char *p;size_t len; -if (!module_blacklist) +if (!module_denylist)return false; -for (p = module_blacklist; *p; p += len) { +for (p = module_denylist; *p; p += len) { len =strcspn (p,"," );if (strlen (module_name) == len && !memcmp (module_name, p, len))return true;
@@ -1356,7 +1356,8 @@ bool __init_or_module module_is_blacklisted(const char *module_name) }return false; } -core_param (module_blacklist, module_blacklist, charp,0400 ); +core_param (module_denylist, module_denylist, charp,0400 ); +core_param (module_blacklist, module_denylist, charp,0400 );static const char *__init get_builtin_modname (initcall_t fn) {
@@ -1373,10 +1374,10 @@ static void __init do_one_initcall_builtin(initcall_t fn) {const char *modname; -if (module_blacklist) { +if (module_denylist) { modname =get_builtin_modname (fn); -if (modname &&module_is_blacklisted (modname)) { -pr_info ("Skipping initcall for blacklisted built-in module %s\n" , +if (modname &&module_is_denylisted (modname)) { +pr_info ("Skipping initcall for denylisted built-in module %s\n" , modname);return ; }
kernel/module/main.c +2 -2
@@ -3370,8 +3370,8 @@ static int early_mod_check(struct load_info *info, int flags)* Now that we know we have the correct module name, check * if it's blacklisted. */ -if (module_is_blacklisted (info->name)) { -pr_err ("Module %s is blacklisted\n" , info->name); +if (module_is_denylisted (info->name)) { +pr_err ("Module %s is denylisted\n" , info->name);return -EPERM ; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v9 2/2] module: Rename module_blacklist to module_denylist
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The comment preceding the module_is_denylisted() call was not updated to reflect the new…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The comment preceding the module_is_denylisted() call was not updated to reflect the new terminology. --
--- Patch [2]: [PATCH v9 2/2] module: Rename module_blacklist to module_denylist --- commit ac79bb8334a6a0d6e2353ff853444d535ab50178
Author: Aaron Tomlin <[email protected]>
module: Rename module_blacklist to module_denylist
This commit renames the kernel parameter and internal symbols from "blacklist" to "denylist" while keeping the old parameter as a deprecated alias. Log messages and documentation are also updated to reflect the new terminology.
[ ... ]
kernel/module/main.c static int early_mod_check(struct load_info *info, int flags) · line 3382
Quoted 7 lines
kernel/module/main.c +0
-0@@ -3381,8 +3381,8 @@ static int early_mod_check(struct load_info *info, int flags)* Now that we know we have the correct module name, check * if it's blacklisted.
[Severity: Low]
This isn't a bug, but should this comment also be updated to say "denylisted" to match the rest of the terminology changes in this patch?
Quoted 7 lines
kernel/module/main.c +2
-2*/ -if (module_is_blacklisted (info->name)) { -pr_err ("Module %s is blacklisted\n" , info->name); +if (module_is_denylisted (info->name)) { +pr_err ("Module %s is denylisted\n" , info->name);return -EPERM ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Gary Guo
reviewerQuoted 27 lines · Aaron Tomlin
Currently, the "module_blacklist=" command-line parameter only applies to loadable modules. If a module is built-in, the parameter is silently ignored. This patch series extends the blacklisting functionality to built-in modules by intercepting their initialisation routines during early boot.
Following review feedback, the implementation has been split into two separate changes to decouple the introduction of the new feature from the terminology renaming:
1. The first patch extends the "module_blacklist=" parameter to built-in modules using the original blacklist terminology. It introduces the ".initcall.modnames" section to map initcall function pointers to their associated
KBUILD_MODNAMEstrings (restricted only tomodule_init()invocations to save memory and avoid matching core kernel subsystems). It also restricts the check to a boot-time__initwrapper to eliminate Use-After-Free (UAF) and Spectre v1 vulnerability risks when loading dynamic modules at runtime, and adds a fast-path check to eliminate lookup overhead when the parameter is not in use2. The second patch renames the variables and helper functions to adopt the preferred "module_denylist=" and
module_is_denylisted()terminology in the codebase. To preserve the existing user-space ABI, "module_blacklist=" is kept as a legacy alias pointing to the samemodule_denylistvariable
Quoted 3 lines
Aaron Tomlin (2):
module: Extendmodule_blacklistparameter to built-in modules
module: Renamemodule_blacklisttomodule_denylist
I feel with
https://lore.kernel.org/driver-core/[email protected]/
and this we're really making loadable module and builtin modules less different.
I wonder if we should just somewhat unify these completly, so built-in modules just behave identically to loadable modules, just without runtime relocations and ability to unload.
Of course, that's quite a big change... And mostly likely people won't care because almost everything is built as loadable modules in distros anyway.
Best,
Gary
Quoted 9 lines
.../admin-guide/kernel-parameters.txt | 6 +- include/asm-generic/vmlinux.lds.h | 4 +- include/linux/init.h | 24 +++++++- include/linux/module.h | 4 +- init/main.c | 55 ++++++++++++++++++- kernel/module/main.c | 25 +-------- rust/macros/module.rs | 16 ++++++ 7 files changed, 105 insertions(+), 29 deletions(-)
Petr Pavlu
maintainerQuoted 32 lines · Gary Guo
Quoted 27 lines · Aaron Tomlin
Currently, the "module_blacklist=" command-line parameter only applies to loadable modules. If a module is built-in, the parameter is silently ignored. This patch series extends the blacklisting functionality to built-in modules by intercepting their initialisation routines during early boot.
Following review feedback, the implementation has been split into two separate changes to decouple the introduction of the new feature from the terminology renaming:
1. The first patch extends the "module_blacklist=" parameter to built-in modules using the original blacklist terminology. It introduces the ".initcall.modnames" section to map initcall function pointers to their associated
KBUILD_MODNAMEstrings (restricted only tomodule_init()invocations to save memory and avoid matching core kernel subsystems). It also restricts the check to a boot-time__initwrapper to eliminate Use-After-Free (UAF) and Spectre v1 vulnerability risks when loading dynamic modules at runtime, and adds a fast-path check to eliminate lookup overhead when the parameter is not in use2. The second patch renames the variables and helper functions to adopt the preferred "module_denylist=" and
module_is_denylisted()terminology in the codebase. To preserve the existing user-space ABI, "module_blacklist=" is kept as a legacy alias pointing to the samemodule_denylistvariableQuoted 3 lines
Aaron Tomlin (2):
module: Extendmodule_blacklistparameter to built-in modules
module: Renamemodule_blacklisttomodule_denylist
Quoted 7 lines
I feel with
https://lore.kernel.org/driver-core/[email protected]/
and this we're really making loadable module and builtin modules less different.I wonder if we should just somewhat unify these completly, so built-in modules just behave identically to loadable modules, just without runtime relocations and ability to unload.
I can imagine this being possible and potentially useful. For instance, a minimal struct module could be used for each built-in and loadable module. For the latter, it could be then extended to something like struct loadable_module containing all the fields currently needed for loadable modules.
It could also help improve some C APIs. Functions currently cannot determine whether a NULL value passed as a module parameter indicates an invalid pointer or a built-in module [1].
Quoted 3 lines
Of course, that's quite a big change... And mostly likely people won't care because almost everything is built as loadable modules in distros anyway.
I agree this looks non-trivial. It would require proper investigation to see how it might actually turn out.
[1] https://lore.kernel.org/linux-modules/[email protected]/
Signature · 2 lines
Cheers, Petr
Aaron Tomlin
reviewerQuoted 52 lines · Petr Pavlu
Quoted 32 lines · Gary Guo
Quoted 27 lines · Aaron Tomlin
Currently, the "module_blacklist=" command-line parameter only applies to loadable modules. If a module is built-in, the parameter is silently ignored. This patch series extends the blacklisting functionality to built-in modules by intercepting their initialisation routines during early boot.
Following review feedback, the implementation has been split into two separate changes to decouple the introduction of the new feature from the terminology renaming:
1. The first patch extends the "module_blacklist=" parameter to built-in modules using the original blacklist terminology. It introduces the ".initcall.modnames" section to map initcall function pointers to their associated
KBUILD_MODNAMEstrings (restricted only tomodule_init()invocations to save memory and avoid matching core kernel subsystems). It also restricts the check to a boot-time__initwrapper to eliminate Use-After-Free (UAF) and Spectre v1 vulnerability risks when loading dynamic modules at runtime, and adds a fast-path check to eliminate lookup overhead when the parameter is not in use2. The second patch renames the variables and helper functions to adopt the preferred "module_denylist=" and
module_is_denylisted()terminology in the codebase. To preserve the existing user-space ABI, "module_blacklist=" is kept as a legacy alias pointing to the samemodule_denylistvariableQuoted 3 lines
Aaron Tomlin (2):
module: Extendmodule_blacklistparameter to built-in modules
module: Renamemodule_blacklisttomodule_denylistQuoted 7 lines
I feel with
https://lore.kernel.org/driver-core/[email protected]/
and this we're really making loadable module and builtin modules less different.I wonder if we should just somewhat unify these completly, so built-in modules just behave identically to loadable modules, just without runtime relocations and ability to unload.
I can imagine this being possible and potentially useful. For instance, a minimal
struct modulecould be used for each built-in and loadable module. For the latter, it could be then extended to something likestruct loadable_modulecontaining all the fields currently needed for loadable modules.It could also help improve some C APIs. Functions currently cannot determine whether a NULL value passed as a module parameter indicates an invalid pointer or a built-in module [1].
Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Of course, that's quite a big change... And mostly likely people won't care because almost everything is built as loadable modules in distros anyway.
I agree this looks non-trivial. It would require proper investigation to see how it might actually turn out.
Hi Gary, Petr,
Thanks for the interesting discussion and ideas.
I agree that unifying built-in and loadable modules under a common, minimal representation is an attractive long-term architectural concept. It would certainly bring cleaner semantics to APIs.
However, as noted, such a unification is a non-trivial, kernel-wide overhaul affecting core infrastructure etc.
In contrast, this series is focused on solving a specific, practical debugging and boot configuration issue with minimal intrusion, using metadata that is reclaimed entirely after boot.
Given the substantial difference in scope, I believe it would be best to keep this series focused on the immediate blacklist/denylist functionality and defer the broader module representation investigation for future work.
Kind regards,
Signature · 1 line
Aaron Tomlin