x86/kprobes: Fix __recover_optprobed_insn check optimizing logic
authorYang Jihong <yangjihong1@huawei.com>
Mon, 20 Feb 2023 23:49:16 +0000 (08:49 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 10 Mar 2023 08:34:13 +0000 (09:34 +0100)
commit 868a6fc0ca2407622d2833adefe1c4d284766c4c upstream.

Since the following commit:

  commit f66c0447cca1 ("kprobes: Set unoptimized flag after unoptimizing code")

modified the update timing of the KPROBE_FLAG_OPTIMIZED, a optimized_kprobe
may be in the optimizing or unoptimizing state when op.kp->flags
has KPROBE_FLAG_OPTIMIZED and op->list is not empty.

The __recover_optprobed_insn check logic is incorrect, a kprobe in the
unoptimizing state may be incorrectly determined as unoptimizing.
As a result, incorrect instructions are copied.

The optprobe_queued_unopt function needs to be exported for invoking in
arch directory.

Link: https://lore.kernel.org/all/20230216034247.32348-2-yangjihong1@huawei.com/
Fixes: f66c0447cca1 ("kprobes: Set unoptimized flag after unoptimizing code")
Cc: stable@vger.kernel.org
Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kernel/kprobes/opt.c
include/linux/kprobes.h
kernel/kprobes.c

index e57e07b0edb64cf5b4d82d64adfc61de94c60424..f406bfa9a8cddc8a5ffeb8f3885f8aacaebca382 100644 (file)
@@ -46,8 +46,8 @@ unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
                /* This function only handles jump-optimized kprobe */
                if (kp && kprobe_optimized(kp)) {
                        op = container_of(kp, struct optimized_kprobe, kp);
-                       /* If op->list is not empty, op is under optimizing */
-                       if (list_empty(&op->list))
+                       /* If op is optimized or under unoptimizing */
+                       if (list_empty(&op->list) || optprobe_queued_unopt(op))
                                goto found;
                }
        }
index a0b92be98984ea58b3608b664d8f45d4af60ac32..ab39285f71a6c82c90248bf5af4e9f5e5b24cf51 100644 (file)
@@ -378,6 +378,7 @@ extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
 DEFINE_INSN_CACHE_OPS(optinsn);
 
 extern void wait_for_kprobe_optimizer(void);
+bool optprobe_queued_unopt(struct optimized_kprobe *op);
 #else /* !CONFIG_OPTPROBES */
 static inline void wait_for_kprobe_optimizer(void) { }
 #endif /* CONFIG_OPTPROBES */
index 1c18ecf9f98b10a10443bda16a54da648275560a..90b0fac6efa01c5e2e035ac887e66c517539c6d0 100644 (file)
@@ -662,7 +662,7 @@ void wait_for_kprobe_optimizer(void)
        mutex_unlock(&kprobe_mutex);
 }
 
-static bool optprobe_queued_unopt(struct optimized_kprobe *op)
+bool optprobe_queued_unopt(struct optimized_kprobe *op)
 {
        struct optimized_kprobe *_op;