tracing/histograms: Reject non-stacktrace fields for synthetic stacktrace fields
Subsystems (2)
PATCH v2 1/1 tracing/histograms: Reject non-stacktrace fields for synthetic stacktrace fields
In check_synth_field(), when a synthetic event field is a stacktrace (field->is_stack) and the histogram field is not a "long[" stacktrace array, validation falls through to the generic size and signedness comparison.
Because parse_synth_field() sets a stacktrace field's type to "__data_loc unsigned long[]", its size is sizeof(u64) and synth_field_signed() marks it as signed. Consequently, any 8-byte signed scalar histogram field passes the fallback check, and its scalar value is subsequently dereferenced as a kernel stacktrace pointer in get_field_size() and trace_stack().
Furthermore, checking strstr(hist_field->type, "long[") is insufficient because tracepoint fields defined with __bitmask() or __rel_bitmask() also have type "__data_loc unsigned long[]" or "__rel_loc unsigned long[]", whilst create_hist_field() treats them as 4-byte scalar values without HIST_FIELD_FL_STACKTRACE.
Propagate HIST_FIELD_FL_STACKTRACE to variable references and field variables (whilst excluding variable aliases from per-element stacktrace capture), and reject any mismatch between HIST_FIELD_FL_STACKTRACE and field->is_stack in check_synth_field().
---
v1 -> v2:
- Check HIST_FIELD_FL_STACKTRACE instead of strstr(hist_field->type, "long[") so __bitmask() and __rel_bitmask() fields are also rejected.
- Propagate HIST_FIELD_FL_STACKTRACE to variable references and field variables, whilst excluding variable aliases from per-element stacktrace capture.
kernel/trace/trace_events_hist.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
kernel/trace/trace_events_hist.c +11 -4
@@ -2121,7 +2121,8 @@ static int init_var_ref(struct hist_field *ref_field, ref_field->size = var_field->size; ref_field->is_signed = var_field->is_signed; ref_field->flags |= var_field->flags & - (HIST_FIELD_FL_TIMESTAMP |HIST_FIELD_FL_TIMESTAMP_USECS ); + (HIST_FIELD_FL_TIMESTAMP |HIST_FIELD_FL_TIMESTAMP_USECS | +HIST_FIELD_FL_STACKTRACE );if (system) { ref_field->system =kstrdup (system,GFP_KERNEL );
@@ -3318,6 +3319,7 @@ static struct field_var *create_field_var(struct hist_trigger_data *hist_data, ret =PTR_ERR (var); goto err; } + var->flags |= val->flags &HIST_FIELD_FL_STACKTRACE ; field_var = kzalloc_obj(struct field_var);if (!field_var) {
@@ -3946,7 +3948,10 @@ static int check_synth_field(struct synth_event *event, && field->is_dynamic)return 0 ; -if (strstr (hist_field->type,"long[" ) && field->is_stack) +if (!!(hist_field->flags &HIST_FIELD_FL_STACKTRACE ) != field->is_stack) +return -EINVAL ; + +if (field->is_stack)return 0 ;if (strcmp (field->type, hist_field->type) !=0 ) {
@@ -4487,7 +4492,8 @@ static int create_var_field(struct hist_trigger_data *hist_data,if (!ret && hist_data->fields[val_idx]->flags &HIST_FIELD_FL_EXECNAME )update_var_execname (hist_data->fields[val_idx]); -if (!ret && hist_data->fields[val_idx]->flags & +if (!ret && !(hist_data->fields[val_idx]->flags &HIST_FIELD_FL_ALIAS ) && + hist_data->fields[val_idx]->flags & (HIST_FIELD_FL_STRING |HIST_FIELD_FL_STACKTRACE )) hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
@@ -5228,7 +5234,8 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,if (hist_field->flags &HIST_FIELD_FL_VAR ) { var_idx = hist_field->var.idx; -if (hist_field->flags & +if (!(hist_field->flags &HIST_FIELD_FL_ALIAS ) && + hist_field->flags & (HIST_FIELD_FL_STRING |HIST_FIELD_FL_STACKTRACE )) {unsigned int str_start, var_str_idx, idx;char *str, *val_str;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Quoted 18 lines · Lee Jones
In
check_synth_field(), when a synthetic event field is a stacktrace (field->is_stack) and the histogram field is not a "long[" stacktrace array, validation falls through to the generic size and signedness comparison.Because
parse_synth_field()sets a stacktrace field's type to "__data_locunsigned long[]", its size is sizeof(u64) andsynth_field_signed()marks it as signed. Consequently, any 8-byte signed scalar histogram field passes the fallback check, and its scalar value is subsequently dereferenced as a kernel stacktrace pointer inget_field_size()andtrace_stack().Furthermore, checking
strstr(hist_field->type, "long[")is insufficient because tracepoint fields defined with__bitmask()or__rel_bitmask()also have type "__data_locunsigned long[]" or "__rel_locunsigned long[]", whilstcreate_hist_field()treats them as 4-byte scalar values withoutHIST_FIELD_FL_STACKTRACE.
Quoted 4 lines
Propagate
HIST_FIELD_FL_STACKTRACEto variable references and field variables (whilst excluding variable aliases from per-element stacktrace capture), and reject any mismatch betweenHIST_FIELD_FL_STACKTRACEandfield->is_stackincheck_synth_field().
Good catch! Looks good to me.
Thanks
Quoted 12 lines
Fixes 00cf3d672a9d ("tracing: Allow synthetic events to pass around stacktraces")Signed-off-by Lee Jones <[email protected]>---
v1 -> v2:
- CheckHIST_FIELD_FL_STACKTRACEinstead ofstrstr(hist_field->type, "long[")so__bitmask()and__rel_bitmask()fields are also rejected.
- PropagateHIST_FIELD_FL_STACKTRACEto variable references and field variables, whilst excluding variable aliases from per-element stacktrace capture.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 18 lines · Lee Jones
In
check_synth_field(), when a synthetic event field is a stacktrace (field->is_stack) and the histogram field is not a "long[" stacktrace array, validation falls through to the generic size and signedness comparison.Because
parse_synth_field()sets a stacktrace field's type to "__data_locunsigned long[]", its size is sizeof(u64) andsynth_field_signed()marks it as signed. Consequently, any 8-byte signed scalar histogram field passes the fallback check, and its scalar value is subsequently dereferenced as a kernel stacktrace pointer inget_field_size()andtrace_stack().Furthermore, checking
strstr(hist_field->type, "long[")is insufficient because tracepoint fields defined with__bitmask()or__rel_bitmask()also have type "__data_locunsigned long[]" or "__rel_locunsigned long[]", whilstcreate_hist_field()treats them as 4-byte scalar values withoutHIST_FIELD_FL_STACKTRACE.
Quoted 8 lines
Propagate
HIST_FIELD_FL_STACKTRACEto variable references and field variables (whilst excluding variable aliases from per-element stacktrace capture), and reject any mismatch betweenHIST_FIELD_FL_STACKTRACEandfield->is_stackincheck_synth_field().Fixes 00cf3d672a9d ("tracing: Allow synthetic events to pass around stacktraces")Signed-off-by Lee Jones <[email protected]>
Good catch! Looks good to me.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>