Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] kprobes: test_optprobe_rearm_unoptimizing() probes ftrace entry - [Medium] kprobes: stack UAF if KUNIT_ASSERT_EQ() aborts with registered kprobe - [Low] kprobes: unexported wait_for_kprobe_optimizer() breaks module build --
commit 879cc55db87246da67610c427cb32cb35c372768 Author: Masami Hiramatsu (Google) <[email protected]> kprobes: Add KUnit test cases for kprobes regressions This commit adds four KUnit test cases to lib/tests/test_kprobes.c to verify fixes for regressions in kretprobe unregistration, kretprobe re-registration, ftrace ops switching, and optprobe re-arming. > diff --git a/lib/tests/test_kprobes.c b/lib/tests/test_kprobes.c > index 06e729e4de051..c20f70cc63d73 100644 > --- a/lib/tests/test_kprobes.c > +++ b/lib/tests/test_kprobes.c [ ... ] > +#if defined(CONFIG_KPROBES_ON_FTRACE) > +/* Test for: kprobes: Switch ftrace ops when adding probe with post_handler > */ > +static void test_kprobe_ftrace_post_handler_switch(struct kunit *test) > +{ > + struct kprobe kp_entry = { > + .symbol_name = "kprobe_target", > + .pre_handler = kp_pre_handler, > + }; > + struct kprobe kp_post = { > + .symbol_name = "kprobe_target", > + .post_handler = kp_post_handler, > + }; > + > + current_test = test; > + > + /* 1. Register first probe without post_handler (armed with > kprobe_ftrace_ops) */ > + KUNIT_ASSERT_EQ(test, 0, register_kprobe(&kp_entry)); > + > + /* 2. Register second probe with post_handler at same IP (must switch > ops) */ > + KUNIT_ASSERT_EQ(test, 0, register_kprobe(&kp_post)); [Severity: Medium] In test_kprobe_ftrace_post_handler_switch(), kp_entry is allocated on the test kthread stack. If the second register_kprobe(&kp_post) call fails (for example, if alloc_aggr_kprobe() or ftrace ops switching fails), the assertion macro aborts the test immediately via: __kunit_abort() -> kunit_try_catch_throw() -> kthread_exit(0) Would this exit the kthread and free its stack while kp_entry is still registered in kprobe_table, leaving a dangling stack pointer that causes a stack use-after-free? The same question applies to test_optprobe_rearm_unoptimizing() below, where opt_kp is on the stack and registered before the assertions on disable_kprobe(&opt_kp) and enable_kprobe(&opt_kp). > + > + /* 3. Execute target function to trigger both */ > + preh_val = 0; > + posth_val = 0; > + target(rand1); > + KUNIT_EXPECT_NE(test, 0, preh_val); > + KUNIT_EXPECT_NE(test, 0, posth_val); > + > + /* 4. Unregistering must successfully disarm without -ENOENT */ > + unregister_kprobe(&kp_post); > + unregister_kprobe(&kp_entry); > +} > +#endif > + > +#if defined(CONFIG_OPTPROBES) > +/* Test for: kprobes: Do not overwrite jump instruction when re-arming > unoptimizing probe */ > +static void test_optprobe_rearm_unoptimizing(struct kunit *test) > +{ > + struct kprobe opt_kp = { > + .symbol_name = "kprobe_target", > + .pre_handler = kp_pre_handler, > + }; > + > + current_test = test; > + KUNIT_ASSERT_EQ(test, 0, register_kprobe(&opt_kp)); [Severity: Medium] Does test_optprobe_rearm_unoptimizing() actually exercise jump optimization when ftrace-based kprobes are enabled? Because opt_kp is registered at kprobe_target with offset 0, on kernels with ftrace-based kprobes enabled, register_kprobe() marks the probe as an ftrace kprobe: register_kprobe() -> check_kprobe_address_safe() -> check_ftrace_location() if (ftrace_location(addr) == addr) p->flags |= KPROBE_FLAG_FTRACE; Then __register_kprobe() calls try_to_optimize_kprobe(), which bails out immediately: try_to_optimize_kprobe() if (kprobe_ftrace(p)) return; As a result, disable_kprobe() and enable_kprobe() take the ftrace disarm/arm paths instead of queuing opt_kp on unoptimizing_list and re-arming an optimized probe. Should opt_kp be placed at a non-zero instruction offset within kprobe_target, or target a notrace function, so that jump optimization is actually used? > + > + /* Allow background optimizer thread to jump-optimize the probe */ > + wait_for_kprobe_optimizer(); [Severity: Low] Can this break the build when the test is compiled as a loadable module? In lib/Kconfig.debug, this test suite is tristate: config KPROBES_SANITY_TEST tristate "Kprobes sanity tests" if !KUNIT_ALL_TESTS However, wait_for_kprobe_optimizer() in kernel/kprobes.c is not exported to modules, causing modpost to fail with an undefined symbol when the test is built as a module with optprobes enabled. Should wait_for_kprobe_optimizer() be exported, or should this test be guarded like the stack_trace_save_regs() callers earlier in lib/tests/test_kprobes.c? -- Sashiko AI review ยท https://sashiko.dev/#/patchset/179375029807.247640.13024754173032838618.stgit@devnote2?part=8
