[PATCH] lkdtm/core: fix resource leaks on module init error path
From: Jiangshan Yi
Date: Wed Jul 01 2026 - 11:19:04 EST
When lkdtm_register_cpoint() fails during lkdtm_module_init(), the code
jumps to out_err:, which only calls debugfs_remove_recursive() and
returns the error code. Since module_init() returned an error, the
kernel does not load the module and never invokes module_exit(), so
lkdtm_module_exit() is not called.
As a result, everything allocated earlier in the init path is leaked:
- the lkdtm_kernel_info string allocated by kasprintf(),
- the four kmem_caches created by lkdtm_usercopy_init() and
lkdtm_heap_init().
The corresponding cleanup — lkdtm_heap_exit(), lkdtm_usercopy_exit()
and kfree(lkdtm_kernel_info) — lives only in lkdtm_module_exit() and
is therefore never executed on this failure path.
Free these resources on the error path before returning, mirroring the
cleanup done in lkdtm_module_exit().
Signed-off-by: Jiangshan Yi <yijiangshan@xxxxxxxxxx>
---
drivers/misc/lkdtm/core.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c
index ededa32d6744..d741c6051408 100644
--- a/drivers/misc/lkdtm/core.c
+++ b/drivers/misc/lkdtm/core.c
@@ -468,6 +468,9 @@ static int __init lkdtm_module_init(void)
out_err:
debugfs_remove_recursive(lkdtm_debugfs_root);
+ lkdtm_heap_exit();
+ lkdtm_usercopy_exit();
+ kfree(lkdtm_kernel_info);
return ret;
}
--
2.25.1