Skip to contentSkip to conversation

ftrace: fix function type mismatches

PATCH In review Sami Tolvanen 3 participants 7 messages Oct 7, 2019 – Oct 17, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ftrace: fix function type mismatches

This change fixes indirect call mismatches with function and function graph tracing, which trip Control-Flow Integrity (CFI) checking.

---
 kernel/trace/fgraph.c | 9 ++++++---
 kernel/trace/ftrace.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
kernel/trace/fgraph.c +6 -3
index 7950a0356042..ecfd4a4a106a 100644
@@ -327,14 +327,17 @@ void ftrace_graph_sleep_time_control(bool enable)
 	fgraph_sleep_time = enable;
 }
 
+void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
+{
+}
+
 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
 {
 	return 0;
 }
 
 /* The callbacks that hook a function */
-trace_func_graph_ret_t ftrace_graph_return =
-			(trace_func_graph_ret_t)ftrace_stub;
+trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
 
@@ -614,7 +617,7 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		goto out;
 
 	ftrace_graph_active--;
-	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
+	ftrace_graph_return = ftrace_graph_return_stub;
 	ftrace_graph_entry = ftrace_graph_entry_stub;
 	__ftrace_graph_entry = ftrace_graph_entry_stub;
 	ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
kernel/trace/ftrace.c +5 -3
index 62a50bf399d6..b68ee130d4a2 100644
@@ -125,8 +125,9 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 				 struct ftrace_ops *op, struct pt_regs *regs);
 #else
 /* See comment below, where ftrace_ops_list_func is defined */
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
-#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs);
+#define ftrace_ops_list_func ftrace_ops_no_ops
 #endif
 
 static inline void ftrace_ops_init(struct ftrace_ops *ops)
@@ -6325,7 +6326,8 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 }
 NOKPROBE_SYMBOL(ftrace_ops_list_func);
 #else
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs)
 {
 	__ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
 }
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Sami Tolvanen

This change fixes indirect call mismatches with function and function graph tracing, which trip Control-Flow Integrity (CFI) checking.

Thanks for sending this! We're getting pretty close to having all the various CFI issues cleaned up now. :)

-Kees

Quoted 67 lines
---
 kernel/trace/fgraph.c | 9 ++++++---
 kernel/trace/ftrace.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
kernel/trace/fgraph.c +6 -3
index 7950a0356042..ecfd4a4a106a 100644
@@ -327,14 +327,17 @@ void ftrace_graph_sleep_time_control(bool enable)
 	fgraph_sleep_time = enable;
 }
 
+void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
+{
+}
+
 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
 {
 	return 0;
 }
 
 /* The callbacks that hook a function */
-trace_func_graph_ret_t ftrace_graph_return =
-			(trace_func_graph_ret_t)ftrace_stub;
+trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
 
@@ -614,7 +617,7 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		goto out;
 
 	ftrace_graph_active--;
-	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
+	ftrace_graph_return = ftrace_graph_return_stub;
 	ftrace_graph_entry = ftrace_graph_entry_stub;
 	__ftrace_graph_entry = ftrace_graph_entry_stub;
 	ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
kernel/trace/ftrace.c +5 -3
index 62a50bf399d6..b68ee130d4a2 100644
@@ -125,8 +125,9 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 				 struct ftrace_ops *op, struct pt_regs *regs);
 #else
 /* See comment below, where ftrace_ops_list_func is defined */
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
-#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs);
+#define ftrace_ops_list_func ftrace_ops_no_ops
 #endif
 
 static inline void ftrace_ops_init(struct ftrace_ops *ops)
@@ -6325,7 +6326,8 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 }
 NOKPROBE_SYMBOL(ftrace_ops_list_func);
 #else
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs)
 {
 	__ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
 }
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Signature · 1 line
Kees Cook
maintainer
#2 lore raw
Quoted 8 lines · On Thu, 10 Oct 2019 14:12:24 -0700 Kees Cook <[email protected]> wrote:
Quoted 4 lines · Sami Tolvanen

This change fixes indirect call mismatches with function and function graph tracing, which trip Control-Flow Integrity (CFI) checking.

Thanks for sending this! We're getting pretty close to having all the various CFI issues cleaned up now. :)

Unfortunately, this breaks function graph tracing. There's lots of arch code that tests if the function graph tracer is set to point to the ftrace_stub. Which by the way is a nop function written in assembly.

Is there a way to do an alias or something that can fix whatever you are trying to fix?

Quoted 57 lines

-Kees

Quoted 53 lines
---
 kernel/trace/fgraph.c | 9 ++++++---
 kernel/trace/ftrace.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
kernel/trace/fgraph.c +6 -3
index 7950a0356042..ecfd4a4a106a 100644
@@ -327,14 +327,17 @@ void ftrace_graph_sleep_time_control(bool enable)
 	fgraph_sleep_time = enable;
 }
 
+void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
+{
+}
+
 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
 {
 	return 0;
 }
 
 /* The callbacks that hook a function */
-trace_func_graph_ret_t ftrace_graph_return =
-			(trace_func_graph_ret_t)ftrace_stub;
+trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
 
@@ -614,7 +617,7 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		goto out;
 
 	ftrace_graph_active--;
-	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
+	ftrace_graph_return = ftrace_graph_return_stub;
 	ftrace_graph_entry = ftrace_graph_entry_stub;
 	__ftrace_graph_entry = ftrace_graph_entry_stub;
 	ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
kernel/trace/ftrace.c +3 -2
index 62a50bf399d6..b68ee130d4a2 100644
@@ -125,8 +125,9 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 				 struct ftrace_ops *op, struct pt_regs *regs);
 #else
 /* See comment below, where ftrace_ops_list_func is defined */
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
-#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs);
+#define ftrace_ops_list_func ftrace_ops_no_ops
 #endif
 
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, · line 6330

Quoted 8 lines
kernel/trace/ftrace.c +2 -1
 static inline void ftrace_ops_init(struct ftrace_ops *ops)
@@ -6325,7 +6326,8 @@ static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
 }
 NOKPROBE_SYMBOL(ftrace_ops_list_func);
 #else
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
+			      struct ftrace_ops *op, struct pt_regs *regs)

Also, this happens to be done because the arch only passes in the first two parameters. That's why this is called when ARCH_SUPPORTS_FTRACE_OPS is net set.

The Linux world is not x86 only!

-- Steve

Quoted 7 lines
Quoted 6 lines
kernel/trace/ftrace.c +0 -0
 {
 	__ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
 }
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
#3 lore raw
Quoted 1 line · Steven Rostedt

Unfortunately, this breaks function graph tracing.

Thank you for pointing this out. I should have realized this solution wouldn't work.

Quoted 2 lines

Is there a way to do an alias or something that can fix whatever you are trying to fix?

Unfortunately, an alias doesn't work in this case either, because the compiler still looks at the type of the target function. I'll find another way to solve the issue.

Sami

maintainer
#4 lore raw
Quoted 6 lines · On Mon, 14 Oct 2019 13:00:09 -0700 Sami Tolvanen <[email protected]>…
Quoted 1 line · Steven Rostedt

Unfortunately, this breaks function graph tracing.

Thank you for pointing this out. I should have realized this solution wouldn't work.

Quoted 7 lines
Quoted 2 lines

Is there a way to do an alias or something that can fix whatever you are trying to fix?

Unfortunately, an alias doesn't work in this case either, because the compiler still looks at the type of the target function. I'll find another way to solve the issue.

Would this work for you?

-- Steve

include/asm-generic/vmlinux.lds.h +9 -2
index dae64600ccbf..33b36b34721b 100644
@@ -111,15 +111,22 @@
 
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
+/*
+ * Need to also make ftrace_graph_stub point to ftrace_stub
+ * so that the same stub location may have different protocols
+ * and not mess up with C verifiers.
+ */
 #define MCOUNT_REC()	. = ALIGN(8);				\
 			__start_mcount_loc = .;			\
 			KEEP(*(__patchable_function_entries))	\
-			__stop_mcount_loc = .;
+			__stop_mcount_loc = .;			\
+			ftrace_graph_stub = ftrace_stub;
 #else
 #define MCOUNT_REC()	. = ALIGN(8);				\
 			__start_mcount_loc = .;			\
 			KEEP(*(__mcount_loc))			\
-			__stop_mcount_loc = .;
+			__stop_mcount_loc = .;			\
+			ftrace_graph_stub = ftrace_stub;
 #endif
 #else
 #define MCOUNT_REC()
kernel/trace/fgraph.c +8 -3
index 7950a0356042..fa3ce10d0405 100644
@@ -332,9 +332,14 @@ int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
 	return 0;
 }
 
+/*
+ * Simply points to ftrace_stub, but with the proper protocol.
+ * Defined by the linker script in linux/vmlinux.lds.h
+ */
+extern void ftrace_graph_stub(struct ftrace_graph_ret *);
+
 /* The callbacks that hook a function */
-trace_func_graph_ret_t ftrace_graph_return =
-			(trace_func_graph_ret_t)ftrace_stub;
+trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_stub;
 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
 
@@ -614,7 +619,7 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		goto out;
 
 	ftrace_graph_active--;
-	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
+	ftrace_graph_return = ftrace_graph_stub;
 	ftrace_graph_entry = ftrace_graph_entry_stub;
 	__ftrace_graph_entry = ftrace_graph_entry_stub;
 	ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
maintainer
#5 lore raw
Quoted 17 lines · On Tue, 15 Oct 2019 09:00:55 -0400 Steven Rostedt <[email protected]> wrote:

+++ b/include/asm-generic/vmlinux.lds.h

include/asm-generic/vmlinux.lds.h +5 -0
@@ -111,15 +111,22 @@
 
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
+/*
+ * Need to also make ftrace_graph_stub point to ftrace_stub
+ * so that the same stub location may have different protocols
+ * and not mess up with C verifiers.
+ */
 
 
kernel/trace/fgraph.c +0 -0
index 7950a0356042..fa3ce10d0405 100644

kernel/trace/fgraph.c int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) · line 340

Quoted 9 lines
kernel/trace/fgraph.c +6 -0
@@ -332,9 +332,14 @@ int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
 	return 0;
 }
 
+/*
+ * Simply points to ftrace_stub, but with the proper protocol.
+ * Defined by the linker script in linux/vmlinux.lds.h
+ */
+extern void ftrace_graph_stub(struct ftrace_graph_ret *);
+

s/protocol/prototype/g

Will update.

-- Steve

#6 lore raw
In reply to Steven Rostedt

include/asm-generic/vmlinux.lds.h

Quoted 10 lines · Steven Rostedt

Would this work for you?

 #define MCOUNT_REC()   . = ALIGN(8);                           \
                        __start_mcount_loc = .;                 \
                        KEEP(*(__patchable_function_entries))   \
include/asm-generic/vmlinux.lds.h +3 -1
-                       __stop_mcount_loc = .;
+                       __stop_mcount_loc = .;                  \
+                       ftrace_graph_stub = ftrace_stub;
 
+extern void ftrace_graph_stub(struct ftrace_graph_ret *);

Yes, it looks like the compiler is actually happy with this approach. Thank you for the patch!

Sami