linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kprobes: Fix possible warn in __arm_kprobe_ftrace()
@ 2024-04-07  3:59 Zheng Yejian
  2024-04-08  2:50 ` Masami Hiramatsu
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Zheng Yejian @ 2024-04-07  3:59 UTC (permalink / raw)
  To: naveen.n.rao, anil.s.keshavamurthy, davem, mhiramat
  Cc: linux-kernel, linux-trace-kernel, zhengyejian1

There is once warn in __arm_kprobe_ftrace() on:

 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
 if (WARN_ONCE(..., "Failed to arm kprobe-ftrace at %pS (error %d)\n", ...)
   return ret;

This warning is due to 'p->addr' is not a valid ftrace_location and
that invalid 'p->addr' was bypassed in check_kprobe_address_safe():
   check_kprobe_address_safe() {
     ...
     // 1. p->addr is in some module, this check passed
     is_module_text_address((unsigned long) p->addr)
     ...
     // 2. If the moudle is removed, the *probed_mod is NULL, but then
     //    the return value would still be 0 !!!
     *probed_mod = __module_text_address((unsigned long) p->addr);
     ...
   }

So adjust the module text check to fix it.

Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com>
---
 kernel/kprobes.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 9d9095e81792..e0612cc3e2a3 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1567,10 +1567,16 @@ static int check_kprobe_address_safe(struct kprobe *p,
 	jump_label_lock();
 	preempt_disable();
 
+	*probed_mod = NULL;
+	if (!core_kernel_text((unsigned long) p->addr)) {
+		*probed_mod = __module_text_address((unsigned long) p->addr);
+		if (!(*probed_mod)) {
+			ret = -EINVAL;
+			goto out;
+		}
+	}
 	/* Ensure it is not in reserved area nor out of text */
-	if (!(core_kernel_text((unsigned long) p->addr) ||
-	    is_module_text_address((unsigned long) p->addr)) ||
-	    in_gate_area_no_mm((unsigned long) p->addr) ||
+	if (in_gate_area_no_mm((unsigned long) p->addr) ||
 	    within_kprobe_blacklist((unsigned long) p->addr) ||
 	    jump_label_text_reserved(p->addr, p->addr) ||
 	    static_call_text_reserved(p->addr, p->addr) ||
@@ -1581,7 +1587,6 @@ static int check_kprobe_address_safe(struct kprobe *p,
 	}
 
 	/* Check if 'p' is probing a module. */
-	*probed_mod = __module_text_address((unsigned long) p->addr);
 	if (*probed_mod) {
 		/*
 		 * We must hold a refcount of the probed module while updating
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-04-10 14:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-07  3:59 [PATCH] kprobes: Fix possible warn in __arm_kprobe_ftrace() Zheng Yejian
2024-04-08  2:50 ` Masami Hiramatsu
2024-04-08  3:29   ` Zheng Yejian
2024-04-08  8:34 ` [PATCH v2] kprobes: Avoid " Zheng Yejian
2024-04-08 12:41   ` Masami Hiramatsu
2024-04-09  6:20     ` Zheng Yejian
2024-04-09 13:49       ` Masami Hiramatsu
2024-04-10  1:44         ` Zheng Yejian
2024-04-10  1:58 ` [PATCH v3] kprobes: Fix possible use-after-free issue on kprobe registration Zheng Yejian
2024-04-10 14:03   ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).