linux/lib/kunit
David Gow f2c6dbd220 kunit: Device wrappers should also manage driver name
kunit_driver_create() accepts a name for the driver, but does not copy
it, so if that name is either on the stack, or otherwise freed, we end
up with a use-after-free when the driver is cleaned up.

Instead, strdup() the name, and manage it as another KUnit allocation.
As there was no existing kunit_kstrdup(), we add one. Further, add a
kunit_ variant of strdup_const() and kfree_const(), so we don't need to
allocate and manage the string in the majority of cases where it's a
constant.

However, these are inline functions, and is_kernel_rodata() only works
for built-in code. This causes problems in two cases:
- If kunit is built as a module, __{start,end}_rodata is not defined.
- If a kunit test using these functions is built as a module, it will
  suffer the same fate.

This fixes a KASAN splat with overflow.overflow_allocation_test, when
built as a module.

Restrict the is_kernel_rodata() case to when KUnit is built as a module,
which fixes the first case, at the cost of losing the optimisation.

Also, make kunit_{kstrdup,kfree}_const non-inline, so that other modules
using them will not accidentally depend on is_kernel_rodata(). If KUnit
is built-in, they'll benefit from the optimisation, if KUnit is not,
they won't, but the string will be properly duplicated.

Fixes: d03c720e03 ("kunit: Add APIs for managing devices")
Reported-by: Nico Pache <npache@redhat.com>
Closes: https://groups.google.com/g/kunit-dev/c/81V9b9QYON0
Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Rae Moar <rmoar@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Tested-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2024-08-26 07:03:46 -06:00
..
.kunitconfig kunit: tool: make --kunitconfig accept dirs, add lib/kunit fragment 2021-04-02 14:14:36 -06:00
assert_test.c kunit: Cover 'assert.c' with tests 2024-05-30 12:53:47 -06:00
assert.c kunit: Cover 'assert.c' with tests 2024-05-30 12:53:47 -06:00
attributes.c kunit: add is_init test attribute 2023-12-18 13:21:15 -07:00
debugfs.c kunit: add ability to run tests after boot using debugfs 2023-12-18 13:25:49 -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 kunit: Device wrappers should also manage driver name 2024-08-26 07:03:46 -06:00
executor_test.c kunit: executor: Simplify string allocation handling 2024-07-12 10:11:48 -06:00
executor.c kunit: executor: Simplify string allocation handling 2024-07-12 10:11:48 -06: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
Kconfig kunit: test: Move fault tests behind KUNIT_FAULT_TEST Kconfig option 2024-05-06 14:22:02 -06:00
kunit-example-test.c kunit: add missing MODULE_DESCRIPTION() macros to core modules 2024-06-07 15:59:05 -06:00
kunit-test.c kunit: add missing MODULE_DESCRIPTION() macros to core modules 2024-06-07 15:59:05 -06:00
Makefile kunit: test: Add vm_mmap() allocation resource manager 2024-06-14 19:31:33 -06:00
resource.c kunit: Add kunit_add_action() to defer a call until test exit 2023-05-25 08:52:55 -06:00
static_stub.c kunit: Expose 'static stub' API to redirect functions 2023-02-08 14:28:17 -07:00
string-stream-test.c kunit: string-stream-test: use KUNIT_DEFINE_ACTION_WRAPPER 2024-05-06 14:22:02 -06:00
string-stream.c kunit: string-stream: Allow ERR_PTR to be passed to string_stream_destroy() 2023-12-18 13:21:14 -07:00
string-stream.h kunit: string-stream: Decouple string_stream from kunit 2023-09-18 10:45:40 -06:00
test.c kunit: Device wrappers should also manage driver name 2024-08-26 07:03:46 -06:00
try-catch-impl.h kunit: hide unexported try-catch interface in try-catch-impl.h 2020-01-09 16:42:09 -07:00
try-catch.c kunit: Fix race condition in try-catch completion 2024-05-06 14:22:02 -06:00
user_alloc.c kunit/usercopy: Disable testing on !CONFIG_MMU 2024-07-02 10:11:40 -06:00