Skip to contentSkip to conversation

module: Extend module_blacklist parameter to built-in modules

PATCH Superseded Aaron Tomlin 3 participants 5 messages Jun 22 – Aug 6

Subsystems (2)

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Aaron Tomlin

author reviewer
#0 lore raw

PATCH v2 1/1 module: Extend module_blacklist parameter to built-in modules

Currently, the module_blacklist= parameter only prevents the dynamic loading of external modules. It possesses no mechanism to intercept or prevent the initialisation of built-in modules, as their associated initcalls are invoked unconditionally during system boot.

This patch extends the blacklisting behaviour to encompass built-in modules. It introduces a dedicated ".initcall.modnames" section into the linker script, systematically mapping each initcall to its originating module name. During the boot sequence, do_one_initcall() interrogates this mapping; should the executing initcall belong to a blacklisted module, its execution is explicitly bypassed.

Furthermore, to preserve the efficacy of Kernel Address Space Layout Randomisation (KASLR) and prevent binary bloat, the mapping mechanism rigorously adheres to CONFIG_HAVE_ARCH_PREL32_RELOCATIONS. Rather than storing absolute function pointers, it employs 32-bit relative offsets, successfully avoiding the generation of thousands of absolute relocations.

--
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=" 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

- Linked to v1: https://lore.kernel.org/lkml/[email protected]/

---
 include/asm-generic/vmlinux.lds.h |  7 +++++++
 include/linux/init.h              | 31 +++++++++++++++++++++++++++++--
 include/linux/module.h            |  2 ++
 init/main.c                       | 27 +++++++++++++++++++++++++++
 kernel/module/main.c              |  4 ++--
 5 files changed, 67 insertions(+), 4 deletions(-)
include/asm-generic/vmlinux.lds.h +7 -0
index 5659f4b5a125..ac0e5f4f2893 100644
@@ -957,6 +957,12 @@
 #define CON_INITCALL							\
 	BOUNDED_SECTION_POST_LABEL(.con_initcall.init, __con_initcall, _start, _end)
 
+#define INITCALL_MODNAMES						\
+	. = ALIGN(8);							\
+	__start_initcall_modnames = .;					\
+	KEEP(*(.initcall.modnames))					\
+	__stop_initcall_modnames = .;
+
 #define NAMED_SECTION(name) \
 	. = ALIGN(8); \
 	name : AT(ADDR(name) - LOAD_OFFSET) \
@@ -1166,6 +1172,7 @@
 		INIT_SETUP(initsetup_align)				\
 		INIT_CALLS						\
 		CON_INITCALL						\
+		INITCALL_MODNAMES					\
 		INIT_RAM_FS						\
 	}
 
include/linux/init.h +29 -2
index 40331923b9f4..212f64a07c73 100644
@@ -125,6 +125,16 @@ static inline initcall_t initcall_from_entry(initcall_entry_t *entry)
 }
 #endif
 
+struct initcall_modname {
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	initcall_entry_t initcall_fn;
+	int modname_offset;
+#else
+	initcall_t initcall_fn;
+	const char *modname;
+#endif
+};
+
 extern initcall_entry_t __con_initcall_start[], __con_initcall_end[];
 
 /* Used for constructor calls. */
@@ -270,9 +280,26 @@ extern struct module __this_module;
 		__initcall_stub(fn, __iid, id),			\
 		__initcall_name(initcall, __iid, id),		\
 		__initcall_section(__sec, __iid))
-
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define ___define_initcall(fn, id, __sec)				\
+	__unique_initcall(fn, id, __sec, __initcall_id(fn));		\
+	asm(".pushsection \".initcall.modnames\", \"a\"\n"		\
+	    ".balign 4\n"						\
+	    ".long " #fn " - .\n"					\
+	    ".long __initcall_modstr_" #fn #id " - .\n"			\
+	    ".popsection\n"						\
+	    ".pushsection .init.rodata, \"a\"\n"			\
+	    "__initcall_modstr_" #fn #id ": .string \"" KBUILD_MODNAME "\"\n" \
+	    ".popsection\n");
+#else
 #define ___define_initcall(fn, id, __sec)			\
-	__unique_initcall(fn, id, __sec, __initcall_id(fn))
+	__unique_initcall(fn, id, __sec, __initcall_id(fn));	\
+	static struct initcall_modname __initcall_modname_##fn##id __used \
+		__section(".initcall.modnames") = {		\
+			.initcall_fn = fn,				\
+			.modname = KBUILD_MODNAME	\
+		};
+#endif
 
 #define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
 
include/linux/module.h +2 -0
index 7566815fabbe..7e25fcd61b50 100644
@@ -756,6 +756,8 @@ void *dereference_module_function_descriptor(struct module *mod, void *ptr);
 int register_module_notifier(struct notifier_block *nb);
 int unregister_module_notifier(struct notifier_block *nb);
 
+extern bool module_is_blacklisted(const char *module_name);
+
 extern void print_modules(void);
 
 static inline bool module_requested_async_probing(struct module *module)
init/main.c +27 -0
index e363232b428b..fbdc42859791 100644
@@ -1334,12 +1334,39 @@ static inline void do_trace_initcall_level(const char *level)
 }
 #endif /* !TRACEPOINTS_ENABLED */
 
+extern struct initcall_modname __start_initcall_modnames[];
+extern struct initcall_modname __stop_initcall_modnames[];
+
+static const char *initcall_get_modname(initcall_t fn)
+{
+	struct initcall_modname *p;
+
+	for (p = __start_initcall_modnames; p < __stop_initcall_modnames; p++) {
+		if (initcall_from_entry(&p->initcall_fn) == fn) {
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+			return (const char *)offset_to_ptr(&p->modname_offset);
+#else
+			return p->modname;
+#endif
+		}
+	}
+	return NULL;
+}
+
 int __init_or_module do_one_initcall(initcall_t fn)
 {
 	int count = preempt_count();
 	char msgbuf[64];
+	const char *modname;
 	int ret;
 
+	modname = initcall_get_modname(fn);
+	if (modname && module_is_blacklisted(modname)) {
+		pr_info("Skipping initcall for blacklisted built-in module %s\n",
+			modname);
+		return 0;
+	}
+
 	if (initcall_blacklisted(fn))
 		return -EPERM;
 
kernel/module/main.c +2 -2
index 46dd8d25a605..02deee5a7480 100644
@@ -2921,7 +2921,7 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
 
 /* module_blacklist is a comma-separated list of module names */
 static char *module_blacklist;
-static bool blacklisted(const char *module_name)
+bool module_is_blacklisted(const char *module_name)
 {
 	const char *p;
 	size_t len;
@@ -3391,7 +3391,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;
 	}
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Accessing the .initcall.modnames array during loadable module initialization reads freed .init…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Accessing the .initcall.modnames array during loadable module initialization reads freed .init memory, causing a Use-After-Free.
- [High] Built-in module blacklisting silently bypasses or fails to compile under CONFIG_LTO_CLANG / CONFIG_CFI_CLANG due to function pointer mismatch.
- [Low] Kernel build fails when CONFIG_MODULES is disabled due to missing module_is_blacklisted() definition.
--

commit 5c29fcaa43d56c5e3cb5426f55d85a536519732f
Author: Aaron Tomlin <[email protected]>

module: Extend module_blacklist parameter to built-in modules

This patch extends the module_blacklist parameter to intercept and prevent the initialization of built-in modules during system boot. It creates a .initcall.modnames section mapping initcalls to module names, using 32-bit relative offsets to preserve KASLR.

Quoted 4 lines
include/linux/init.h +0 -0
index 40331923b9f4a..212f64a07c739 100644

[ ... ]

include/linux/init.h extern struct module __this_module; · line 288

Quoted 10 lines
include/linux/init.h +6 -0
@@ -270,9 +280,26 @@ extern struct module __this_module;
 		__initcall_stub(fn, __iid, id),			\
 		__initcall_name(initcall, __iid, id),		\
 		__initcall_section(__sec, __iid))
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define ___define_initcall(fn, id, __sec)				\
+	__unique_initcall(fn, id, __sec, __initcall_id(fn));		\
+	asm(".pushsection \".initcall.modnames\", \"a\"\n"		\
+	    ".balign 4\n"						\
+	    ".long " #fn " - .\n"					\

[Severity: High]
When compiled with CONFIG_LTO_CLANG or CONFIG_CFI_CLANG, the kernel wraps initcalls in an __initcall_stub function, and stores the stub's address in the initcall array.

Since ___define_initcall explicitly stores the original function's address here, will initcall_get_modname() fail to match the runtime CFI/LTO stub address passed by do_one_initcall()?

Also, could using #fn in inline assembly for a static function renamed by LTO cause an undefined reference link error?

Quoted 5 lines
include/linux/init.h +5 -0
+	    ".long __initcall_modstr_" #fn #id " - .\n"			\
+	    ".popsection\n"						\
+	    ".pushsection .init.rodata, \"a\"\n"			\
+	    "__initcall_modstr_" #fn #id ": .string \"" KBUILD_MODNAME "\"\n" \
+	    ".popsection\n");

[ ... ]

include/linux/module.h void *dereference_module_function_descriptor(struct module *mod, void *ptr); · line 757

Quoted 9 lines
include/linux/module.h +1 -0
index d80c3ea574726..21668ef1430a5 100644
@@ -754,6 +754,8 @@ void *dereference_module_function_descriptor(struct module *mod, void *ptr);
 int register_module_notifier(struct notifier_block *nb);
 int unregister_module_notifier(struct notifier_block *nb);
 
+extern bool module_is_blacklisted(const char *module_name);

[Severity: Low]
Does this cause a build failure when CONFIG_MODULES is disabled?

The function module_is_blacklisted() is defined in kernel/module/main.c, but it is declared here unconditionally and called from do_one_initcall() in init/main.c. If CONFIG_MODULES is disabled, could the linker fail with an undefined reference?

Quoted 2 lines
include/linux/module.h +1 -0
+
 extern void print_modules(void);

[ ... ]

Quoted 25 lines
init/main.c +17 -0
index b84818ad9685f..ce6d769b99e17 100644
@@ -1365,12 +1365,39 @@ static inline void do_trace_initcall_level(const char *level)
 }
 #endif /* !TRACEPOINTS_ENABLED */
 
+extern struct initcall_modname __start_initcall_modnames[];
+extern struct initcall_modname __stop_initcall_modnames[];
+
+static const char *initcall_get_modname(initcall_t fn)
+{
+	struct initcall_modname *p;
+
+	for (p = __start_initcall_modnames; p < __stop_initcall_modnames; p++) {
+		if (initcall_from_entry(&p->initcall_fn) == fn) {
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+			return (const char *)offset_to_ptr(&p->modname_offset);
+#else
+			return p->modname;
+#endif
+		}
+	}
+	return NULL;

init/main.c static inline void do_trace_initcall_level(const char *level) · line 1394

Quoted 10 lines
init/main.c +4 -0
+}
+
 int __init_or_module do_one_initcall(initcall_t fn)
 {
 	int count = preempt_count();
 	char msgbuf[64];
+	const char *modname;
 	int ret;
 
+	modname = initcall_get_modname(fn);

[Severity: Critical]
Is there a use-after-free risk here when loading dynamic modules?

The .initcall.modnames section is placed in INIT_DATA_SECTION, mapping it to .init.data. Memory in the .init.* sections is freed by free_initmem() after the system boots.

Since do_one_initcall() is marked __init_or_module, it is also called by do_init_module() whenever a loadable module is initialized after boot:

do_init_module()
do_one_initcall()
initcall_get_modname()

Does this unconditionally iterate over the __start_initcall_modnames array residing in the freed .init.data section?

Quoted 8 lines
init/main.c +6 -0
+	if (modname && module_is_blacklisted(modname)) {
+		pr_info("Skipping initcall for blacklisted built-in module %s\n",
+			modname);
+		return 0;
+	}
+
 	if (initcall_blacklisted(fn))
 		return -EPERM;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw
In reply to Aaron Tomlin
Quoted 11 lines · Aaron Tomlin

Currently, the module_blacklist= parameter only prevents the dynamic loading of external modules. It possesses no mechanism to intercept or prevent the initialisation of built-in modules, as their associated initcalls are invoked unconditionally during system boot.

This patch extends the blacklisting behaviour to encompass built-in modules. It introduces a dedicated ".initcall.modnames" section into the linker script, systematically mapping each initcall to its originating module name. During the boot sequence, do_one_initcall() interrogates this mapping; should the executing initcall belong to a blacklisted module, its execution is explicitly bypassed.

Hi Arnd, Luis, Petr, Daniel, Sami,

Sashiko [1] correctly reported issues with this version. Consequently, in the next iteration, I will implement the following changes:

1. Abandon the raw assembly approach in favour of using standard C structures (i.e., struct initcall_modname). While this sacrifices the PREL32 relative offset optimisation for this specific table, it guarantees architectural safety and ensures native compiler support for both LTO and CFI.

2. Wrap the module_is_blacklisted declaration in the necessary #ifdef CONFIG_MODULES guards to prevent undefined reference errors when loadable module support is disabled.

3. Iterating over .initcall.modnames within the shared do_one_initcall() path introduces a severe UAF vulnerability when loadable modules are initialised post-boot. To rectify this, I will introduce a strict, read-only flag (initmem_freed) to explicitly bypass the built-in lookup once free_initmem() has reclaimed the .init sections. Furthermore, to maintain a clean separation of concerns, the blacklist check for loadable modules will be migrated directly into the module loader path (do_init_module()).

[1]: https://sashiko.dev/#/patchset/20260622140259.2974-1-atomlin%40atomlin.com

Kind regards,

Signature · 1 line
Aaron Tomlin
#3 lore raw
In reply to Aaron Tomlin
kernel test robot noticed the following build errors:

Hi Aaron,

kernel test robot noticed the following build errors:

[auto build test ERROR on soc/for-next]
[also build test ERROR on linus/master arnd-asm-generic/master v7.2-rc6 next-20260804] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Aaron-Tomlin/module-Extend-module_blacklist-parameter-to-built-in-modules/20260805-123559
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
patch link:    https://lore.kernel.org/r/20260622140259.2974-1-atomlin%40atomlin.com
patch subject: [PATCH v2] module: Extend module_blacklist parameter to built-in modules
config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20260805/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260805/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

init/main.c: In function 'do_one_initcall':

>> init/main.c:1364:24: error: implicit declaration of function 'module_is_blacklisted' [-Wimplicit-function-declaration]
    1364 |         if (modname && module_is_blacklisted(modname)) {
         |                        ^~~~~~~~~~~~~~~~~~~~~

vim +/module_is_blacklisted +1364 init/main.c

1355
1356 int __init_or_module do_one_initcall(initcall_t fn)
1357 {
1358 int count = preempt_count();
1359 char msgbuf[64];
1360 const char *modname;
1361 int ret;
1362
1363 modname = initcall_get_modname(fn);

> 1364		if (modname && module_is_blacklisted(modname)) {

1365 pr_info("Skipping initcall for blacklisted built-in module %s\n",
1366 modname);
1367 return 0;
1368 }
1369
1370 if (initcall_blacklisted(fn))
1371 return -EPERM;
1372
1373 do_trace_initcall_start(fn);
1374 ret = fn();
1375 do_trace_initcall_finish(fn, ret);
1376
1377 msgbuf[0] = 0;
1378
1379 if (preempt_count() != count) {
1380 sprintf(msgbuf, "preemption imbalance ");
1381 preempt_count_set(count);
1382 }
1383 if (irqs_disabled()) {
1384 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
1385 local_irq_enable();
1386 }
1387 WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
1388
1389 add_latent_entropy();
1390 return ret;
1391 }
1392

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#4 lore raw
In reply to Aaron Tomlin
kernel test robot noticed the following build errors:

Hi Aaron,

kernel test robot noticed the following build errors:

[auto build test ERROR on soc/for-next]
[also build test ERROR on linus/master arnd-asm-generic/master v7.2-rc6 next-20260805] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Aaron-Tomlin/module-Extend-module_blacklist-parameter-to-built-in-modules/20260805-123559
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
patch link:    https://lore.kernel.org/r/20260622140259.2974-1-atomlin%40atomlin.com
patch subject: [PATCH v2] module: Extend module_blacklist parameter to built-in modules
config: powerpc64-randconfig-002-20260806 (https://download.01.org/0day-ci/archive/20260806/[email protected]/config)
compiler: clang version 22.1.3 (https://github.com/llvm/llvm-project e9846648fd6183ee6d8cbdb4502213fcf902a211)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260806/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> init/main.c:1364:17: error: call to undeclared function 'module_is_blacklisted'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1364 |         if (modname && module_is_blacklisted(modname)) {
         |                        ^

1 error generated.

vim +/module_is_blacklisted +1364 init/main.c

1355
1356 int __init_or_module do_one_initcall(initcall_t fn)
1357 {
1358 int count = preempt_count();
1359 char msgbuf[64];
1360 const char *modname;
1361 int ret;
1362
1363 modname = initcall_get_modname(fn);

> 1364		if (modname && module_is_blacklisted(modname)) {

1365 pr_info("Skipping initcall for blacklisted built-in module %s\n",
1366 modname);
1367 return 0;
1368 }
1369
1370 if (initcall_blacklisted(fn))
1371 return -EPERM;
1372
1373 do_trace_initcall_start(fn);
1374 ret = fn();
1375 do_trace_initcall_finish(fn, ret);
1376
1377 msgbuf[0] = 0;
1378
1379 if (preempt_count() != count) {
1380 sprintf(msgbuf, "preemption imbalance ");
1381 preempt_count_set(count);
1382 }
1383 if (irqs_disabled()) {
1384 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
1385 local_irq_enable();
1386 }
1387 WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
1388
1389 add_latent_entropy();
1390 return ret;
1391 }
1392

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki