Re: [PATCH] tracing: fix memcpy size when copying stack entries

From: Steven Rostedt
Date: Tue Jun 13 2023 - 11:38:10 EST


On Tue, 13 Jun 2023 07:19:14 +0200
Sven Schnelle <svens@xxxxxxxxxxxxx> wrote:

> > Yes the above may be special, but your patch breaks it.
>
> Indeed, i'm feeling a bit stupid for sending that patch, should have
> used my brain during reading the source. Thanks for the explanation.

Does this quiet the fortifier?

-- Steve

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 64a4dde073ef..1bac7df1f4b6 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -3118,6 +3118,7 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,
struct ftrace_stack *fstack;
struct stack_entry *entry;
int stackidx;
+ void *stack;

/*
* Add one, for this function and the call to save_stack_trace()
@@ -3163,7 +3164,18 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,
goto out;
entry = ring_buffer_event_data(event);

- memcpy(&entry->caller, fstack->calls, size);
+ /*
+ * For backward compatibility reasons, the entry->caller is an
+ * array of 8 slots to store the stack. This is also exported
+ * to user space. The amount allocated on the ring buffer actually
+ * holds enough for the stack specified by nr_entries. This will
+ * go into the location of entry->caller. Due to string fortifiers
+ * checking the size of the destination of memcpy() it triggers
+ * when it detects that size is greater than 8. To hide this from
+ * the fortifiers, use a different pointer "stack".
+ */
+ stack = (void *)&entry->caller;
+ memcpy(stack, fstack->calls, size);
entry->size = nr_entries;

if (!call_filter_check_discard(call, entry, buffer, event))