Skip to content

Commit 85f4a56

Browse files
rostedtgregkh
authored andcommitted
tracing: Add length protection to histogram string copies
[ Upstream commit 938aa33 ] The string copies to the histogram storage has a max size of 256 bytes (defined by MAX_FILTER_STR_VAL). Only the string size of the event field needs to be copied to the event storage, but no more than what is in the event storage. Although nothing should be bigger than 256 bytes, there's no protection against overwriting of the storage if one day there is. Copy no more than the destination size, and enforce it. Also had to turn MAX_FILTER_STR_VAL into an unsigned int, to keep the min() comparison of the string sizes of comparable types. Link: https://lore.kernel.org/all/CAHk-=wjREUihCGrtRBwfX47y_KrLCGjiq3t6QtoNJpmVrAEb1w@mail.gmail.com/ Link: https://lkml.kernel.org/r/20211114132834.183429a4@rorschach.local.home Cc: Ingo Molnar <mingo@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Tom Zanussi <zanussi@kernel.org> Reported-by: Linus Torvalds <torvalds@linux-foundation.org> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org> Fixes: 63f84ae ("tracing/histogram: Do not copy the fixed-size char array field over the field size") Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 0efd57d commit 85f4a56

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎include/linux/trace_events.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ struct trace_event_file {
673673

674674
#define PERF_MAX_TRACE_SIZE 2048
675675

676-
#define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
676+
#define MAX_FILTER_STR_VAL 256U /* Should handle KSYM_SYMBOL_LEN */
677677

678678
enum event_trigger_type {
679679
ETT_NONE = (0),

‎kernel/trace/trace_events_hist.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,8 +2691,10 @@ static inline void __update_field_vars(struct tracing_map_elt *elt,
26912691
if (val->flags & HIST_FIELD_FL_STRING) {
26922692
char *str = elt_data->field_var_str[j++];
26932693
char *val_str = (char *)(uintptr_t)var_val;
2694+
unsigned int size;
26942695

2695-
strscpy(str, val_str, val->size);
2696+
size = min(val->size, STR_VAR_LEN_MAX);
2697+
strscpy(str, val_str, size);
26962698
var_val = (u64)(uintptr_t)str;
26972699
}
26982700
tracing_map_set_var(elt, var_idx, var_val);
@@ -4579,6 +4581,7 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
45794581
if (hist_field->flags & HIST_FIELD_FL_STRING) {
45804582
unsigned int str_start, var_str_idx, idx;
45814583
char *str, *val_str;
4584+
unsigned int size;
45824585

45834586
str_start = hist_data->n_field_var_str +
45844587
hist_data->n_save_var_str;
@@ -4587,7 +4590,9 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
45874590

45884591
str = elt_data->field_var_str[idx];
45894592
val_str = (char *)(uintptr_t)hist_val;
4590-
strscpy(str, val_str, hist_field->size);
4593+
4594+
size = min(hist_field->size, STR_VAR_LEN_MAX);
4595+
strscpy(str, val_str, size);
45914596

45924597
hist_val = (u64)(uintptr_t)str;
45934598
}

0 commit comments

Comments
 (0)