[v2,1/2] kprobes: Prohibit probing on CFI preamble symbol

Message ID 168904024679.116016.18089228029322008512.stgit@devnote2
State New
Headers
Series x86: kprobes: Fix CFI_CLANG related issues |

Commit Message

Masami Hiramatsu (Google) July 11, 2023, 1:50 a.m. UTC
  From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Do not allow to probe on "__cfi_" or "__pfx_" started symbol, because those
are used for CFI and not executed. Probing it will break the CFI.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 Changes in v2:
  - Check "__pfx_" prefix functions too.
  - Make the check unconditional.
---
 kernel/kprobes.c |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
  

Comments

Steven Rostedt July 28, 2023, 10:49 p.m. UTC | #1
On Tue, 11 Jul 2023 10:50:47 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Do not allow to probe on "__cfi_" or "__pfx_" started symbol, because those
> are used for CFI and not executed. Probing it will break the CFI.
> 
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

-- Steve

> ---
>  Changes in v2:
>   - Check "__pfx_" prefix functions too.
>   - Make the check unconditional.
> ---
>  kernel/kprobes.c |   14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
  
Masami Hiramatsu (Google) July 29, 2023, 12:57 p.m. UTC | #2
On Fri, 28 Jul 2023 18:49:13 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Tue, 11 Jul 2023 10:50:47 +0900
> "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
> 
> > From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > 
> > Do not allow to probe on "__cfi_" or "__pfx_" started symbol, because those
> > are used for CFI and not executed. Probing it will break the CFI.
> > 
> > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Thanks! I will pick this for probes/fixes.


> 
> -- Steve
> 
> > ---
> >  Changes in v2:
> >   - Check "__pfx_" prefix functions too.
> >   - Make the check unconditional.
> > ---
> >  kernel/kprobes.c |   14 +++++++++++++-
> >  1 file changed, 13 insertions(+), 1 deletion(-)
> >
  

Patch

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 00e177de91cc..3da9726232ff 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1545,6 +1545,17 @@  static int check_ftrace_location(struct kprobe *p)
 	return 0;
 }
 
+static bool is_cfi_preamble_symbol(unsigned long addr)
+{
+	char symbuf[KSYM_NAME_LEN];
+
+	if (lookup_symbol_name(addr, symbuf))
+		return false;
+
+	return str_has_prefix("__cfi_", symbuf) ||
+		str_has_prefix("__pfx_", symbuf);
+}
+
 static int check_kprobe_address_safe(struct kprobe *p,
 				     struct module **probed_mod)
 {
@@ -1563,7 +1574,8 @@  static int check_kprobe_address_safe(struct kprobe *p,
 	    within_kprobe_blacklist((unsigned long) p->addr) ||
 	    jump_label_text_reserved(p->addr, p->addr) ||
 	    static_call_text_reserved(p->addr, p->addr) ||
-	    find_bug((unsigned long)p->addr)) {
+	    find_bug((unsigned long)p->addr) ||
+	    is_cfi_preamble_symbol((unsigned long)p->addr)) {
 		ret = -EINVAL;
 		goto out;
 	}