mirror-linux/lib/kunit
Florian Schmaus fb6988b83b kunit: fix use-after-free in debugfs when using kunit.filter
When the kernel is booted with a kunit filter (e.g.,
kunit.filter="speed!=slow"), the kunit executor dynamically allocates
copies of the filtered test suites using kmalloc/kmemdup.

During the initial boot execution, kunit_debugfs_create_suite() creates
debugfs files (such as /sys/kernel/debug/kunit/<suite>/run) and
permanently stores a pointer to the dynamically allocated suite in the
inode's i_private field.

Previously, the executor freed this dynamically allocated suite_set
immediately after executing the boot-time tests. Because the debugfs
nodes were not destroyed, any subsequent interaction with the debugfs
`run` file from userspace triggered a use-after-free (UAF). On systems
with architectural capabilities, like CHERI RISC-V, this resulted in
an immediate fatal hardware exception due to the invalidation of the
capability tags on the reclaimed memory. On other architectures, it
resulted in silent memory corruption.

Fix this UAF by properly coupling the lifetime of the filtered suite
memory allocation to the lifetime of the kunit subsystem and its
associated VFS nodes. Ownership of the boot-time suite_set is now
transferred to a global tracker ('kunit_boot_suites'), and the memory
is cleanly released in kunit_exit() during module teardown.

Link: https://lore.kernel.org/r/20260507084854.233984-1-florian.schmaus@codasip.com
Fixes: e2219db280 ("kunit: add debugfs /sys/kernel/debug/kunit/<suite>/results display")
Signed-off-by: Florian Schmaus <florian.schmaus@codasip.com>
Reviewed-by: Martin Kaiser <martin@kaiser.cx>
Reviewed-by: David Gow <david@davidgow.net>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2026-05-21 10:45:49 -06:00
..
.kunitconfig kunit: tool: make --kunitconfig accept dirs, add lib/kunit fragment 2021-04-02 14:14:36 -06:00
Kconfig kunit: config: KUNIT_DEBUGFS should depend on DEBUG_FS 2026-05-04 12:10:40 -06:00
Makefile kunit: Always descend into kunit directory during build 2025-08-15 11:39:46 -06:00
assert.c kunit: fix up const mis-match in many assert functions 2026-01-05 15:32:03 -07:00
assert_test.c kunit: Cover 'assert.c' with tests 2024-05-30 12:53:47 -06:00
attributes.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
debugfs.c kunit: Introduce autorun option 2025-01-15 09:04:06 -07:00
debugfs.h kunit: add debugfs /sys/kernel/debug/kunit/<suite>/results display 2020-03-26 14:07:18 -06:00
device-impl.h kunit: device: Unregister the kunit_bus on shutdown 2024-02-06 17:07:37 -07:00
device.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
executor.c kunit: fix use-after-free in debugfs when using kunit.filter 2026-05-21 10:45:49 -06:00
executor_test.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
hooks-impl.h kunit: Add printf attribute to fail_current_test_impl 2023-02-08 18:08:14 -07:00
hooks.c kunit: Add "hooks" to call into KUnit when it's built as a module 2023-02-08 14:26:25 -07:00
kunit-example-test.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
kunit-test.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
platform-test.c platform: Add test managed platform_device/driver APIs 2024-07-29 15:33:12 -07:00
platform.c platform: Add test managed platform_device/driver APIs 2024-07-29 15:33:12 -07:00
resource.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
static_stub.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
string-stream-test.c kunit: string-stream-test: Include <linux/prandom.h> 2024-10-03 18:20:14 +02:00
string-stream.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
string-stream.h kunit: string-stream: Decouple string_stream from kunit 2023-09-18 10:45:40 -06:00
test.c kunit: fix use-after-free in debugfs when using kunit.filter 2026-05-21 10:45:49 -06:00
try-catch-impl.h kunit: Adjust kunit_test timeout based on test_{suite,case} speed 2025-06-24 20:47:39 -06:00
try-catch.c kunit: Adjust kunit_test timeout based on test_{suite,case} speed 2025-06-24 20:47:39 -06:00
user_alloc.c kunit: test: Export kunit_attach_mm() 2025-07-16 14:11:58 +02:00