tracing/probes: Fix various bugs in dynamic probe events
Masami Hiramatsu (Google)
maintainerv3 cover tracing/probes: Fix various bugs in dynamic probe events
Hi,
This is the 3rd version of the series fixing several bugs in dynamic probe events (kprobe, fprobe, uprobe, and eprobe) and BTF/fetch-argument handling.
Here is the previous version:
https://lore.kernel.org/all/179444679892.345731.5271591537517106679.stgit@devnote2/
This version updates [03/13] to fix BTF bitfield handling for kflag=0 bit offsets, non-power-of-2 container sizes, and big-endian bitfields pointed out by Sashiko.
- [01/13] Check whether the target event is enabled or busy before unlinking a sibling probe during multiprobe unregistration, and add the missing checks to trace_uprobe and trace_eprobe.
- [02/13] Enable newly appended fprobes and uprobes immediately when the target multiprobe event is already enabled, matching trace_kprobe behavior.
- [03/13] Fix BTF bitfield parsing for non-kflag bitfields (such as enum bitfields, non-byte-aligned offsets, and non-power-of-2 sizes), select a narrow container fetch type to avoid out-of-bounds buffer writes, and compute bitfield extraction parameters from the container width for both big- and little-endian bitfields.
- [04/13] Add missing BTF_KIND_PTR, NULL, and BTF_KIND_INT checks in btf_type_is_char_ptr() and btf_type_is_char_array().
- [05/13] Use explicit fixed-width integer types (s8/u8, s16/u16, s32/u32) in eprobe get_event_field() so 1-byte signed fields are properly sign-extended under -funsigned-char.
- [06/13] Disallow attaching eprobes to fprobe events to prevent potential recursion issues, matching kprobes and uprobes.
- [07/13] Check FETCH_OP_UDEREF before advancing the fetch instruction pointer in finalize_fetch_insn() so user-space dereference string arrays emit FETCH_OP_ST_USTRING instead of FETCH_OP_ST_STRING.
- [08/13] Reject array specifiers on non-dereference string sources (such as $comm or symstr) in finalize_fetch_insn().
- [09/13] Fix memory leak of tail in __trace_uprobe_create() when generating a default event name.
Minor updates/cleanups:
- [10/13] Allow the symstr type on function entry arguments ($argN) in return probes (FETCH_OP_EDATA).
- [11/13] Reject empty fetch expressions before :%pd and :%pD in traceprobe_expand_dentry_args().
- [12/13] Validate the command prefix in kprobe, uprobe, and eprobe creation when the colon separator is omitted so invalid prefixes are rejected.
- [13/13] Propagate actual error return codes in eprobe and fprobe creation paths instead of overwriting them with -EINVAL.
Changes in v3:
- [03/13] Use ctx->last_bitoffs as code->lshift on __BIG_ENDIAN_BITFIELD architectures in parse_btf_bitfield(), and fold BTF_INT_OFFSET(intdata) into bitoffs before checking alignment and !is_power_of_2(type->size) in get_bitoffset_of_field().
Changes in v2:
- [03/13] Check (bitoffs % 8) in get_bitoffset_of_field() so unaligned byte-sized kflag=0 bitfields also record ctx->last_bitsize.
- [08/13] Add Fixes and Cc: stable tags.
- [09/13] Newly added to fix tail memory leak in __trace_uprobe_create().
Notes:
- These bugs were identified and the fixes and cover letter were prepared with the assistance of an LLM coding assistant.
Thank you,
---
base-commit: be94710b1f6ba1bea33e73129c110af80e999b2d
Masami Hiramatsu (Google) (13):
tracing/probes: Check enabled and busy state before unlinking sibling probes tracing/probes: Enable newly appended probe when target event is enabled tracing/probes: Fix BTF bitfield parsing and out-of-bounds buffer write tracing/probes: Fix missing BTF kind checks in char pointer/array helpers tracing/eprobe: Fix 1-byte signed field fetch in get_event_field() tracing/eprobe: Disallow attaching eprobes to fprobe events tracing/probes: Fix user-space dereference string array store type tracing/probes: Reject array count on non-dereference string sources tracing/uprobes: Fix memory leak of tail in __trace_uprobe_create() tracing/probes: Allow symstr type for function entry arguments in return probes tracing/probes: Reject empty fetch expression for %pd and %pD tracing/probes: Validate command prefix in kprobe, uprobe, and eprobe tracing/probes: Propagate error codes in eprobe and fprobe creation
kernel/trace/trace_eprobe.c | 39 +++++++++++------- kernel/trace/trace_fprobe.c | 26 +++++------- kernel/trace/trace_kprobe.c | 11 +++-- kernel/trace/trace_probe.c | 94 ++++++++++++++++++++++++++++++++++--------- kernel/trace/trace_uprobe.c | 79 ++++++++++++++++++++++++------------ 5 files changed, 171 insertions(+), 78 deletions(-)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v3 1/13 tracing/probes: Check enabled and busy state before unlinking sibling probes
From: Masami Hiramatsu (Google) <[email protected]>
When a multiprobe event is enabled or referenced (for example, by an eprobe, trigger, or open tracefs file) and a user attempts to delete the event or one of its probes via dynamic_events, checking trace_probe_has_sibling() before the enabled and busy checks allows the first probe to be unlinked and freed while the event is still active, only returning -EBUSY on the last sibling probe. For uprobe events, unlinking and freeing an enabled sibling probe without unregistering its uprobe_consumer also causes a use-after-free.
Ensure all four probe types (kprobe, fprobe, uprobe, and eprobe) verify that the event is neither enabled nor busy before unlinking any sibling probe.
--- kernel/trace/trace_eprobe.c | 12 ++++++++---- kernel/trace/trace_fprobe.c | 8 ++++---- kernel/trace/trace_kprobe.c | 8 ++++---- kernel/trace/trace_uprobe.c | 8 ++++++-- 4 files changed, 22 insertions(+), 14 deletions(-)
kernel/trace/trace_eprobe.c +8 -4
@@ -94,14 +94,18 @@ static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)static int unregister_trace_eprobe (struct trace_eprobe *ep) { -/* If other probes are on the event, just unregister eprobe */ -if (trace_probe_has_sibling (&ep->tp)) - goto unreg; -/* Enabled event can not be unregistered */ if (trace_probe_is_enabled (&ep->tp))return -EBUSY ; +/* If there's a reference to the dynamic event */ +if (trace_event_dyn_busy (trace_probe_event_call (&ep->tp))) +return -EBUSY ; + +/* If other probes are on the event, just unregister eprobe */ +if (trace_probe_has_sibling (&ep->tp)) + goto unreg; +/* Will fail if probe is being used by ftrace or perf */ if (trace_probe_unregister_event_call (&ep->tp))return -EBUSY ;
kernel/trace/trace_fprobe.c +4 -4
@@ -853,10 +853,6 @@ static void __unregister_trace_fprobe(struct trace_fprobe *tf)/* Unregister a trace_probe and probe_event */ static int unregister_trace_fprobe (struct trace_fprobe *tf) { -/* If other probes are on the event, just unregister fprobe */ -if (trace_probe_has_sibling (&tf->tp)) - goto unreg; -/* Enabled event can not be unregistered */ if (trace_probe_is_enabled (&tf->tp))return -EBUSY ;
@@ -865,6 +861,10 @@ static int unregister_trace_fprobe(struct trace_fprobe *tf)if (trace_event_dyn_busy (trace_probe_event_call (&tf->tp)))return -EBUSY ; +/* If other probes are on the event, just unregister fprobe */ +if (trace_probe_has_sibling (&tf->tp)) + goto unreg; +/* Will fail if probe is being used by ftrace or perf */ if (unregister_fprobe_event (tf))return -EBUSY ;
kernel/trace/trace_kprobe.c +4 -4
@@ -548,10 +548,6 @@ static void __unregister_trace_kprobe(struct trace_kprobe *tk)/* Unregister a trace_probe and probe_event */ static int unregister_trace_kprobe (struct trace_kprobe *tk) { -/* If other probes are on the event, just unregister kprobe */ -if (trace_probe_has_sibling (&tk->tp)) - goto unreg; -/* Enabled event can not be unregistered */ if (trace_probe_is_enabled (&tk->tp))return -EBUSY ;
@@ -560,6 +556,10 @@ static int unregister_trace_kprobe(struct trace_kprobe *tk)if (trace_event_dyn_busy (trace_probe_event_call (&tk->tp)))return -EBUSY ; +/* If other probes are on the event, just unregister kprobe */ +if (trace_probe_has_sibling (&tk->tp)) + goto unreg; +/* Will fail if probe is being used by ftrace or perf */ if (unregister_kprobe_event (tk))return -EBUSY ;
kernel/trace/trace_uprobe.c +6 -2
@@ -396,13 +396,17 @@ static int unregister_trace_uprobe(struct trace_uprobe *tu) {int ret; -if (trace_probe_has_sibling (&tu->tp)) - goto unreg; +/* Enabled event can not be unregistered */ +if (trace_probe_is_enabled (&tu->tp)) +return -EBUSY ;/* If there's a reference to the dynamic event */ if (trace_event_dyn_busy (trace_probe_event_call (&tu->tp)))return -EBUSY ; +if (trace_probe_has_sibling (&tu->tp)) + goto unreg; + ret =unregister_uprobe_event (tu);if (ret)return ret;
Masami Hiramatsu (Google)
maintainerPATCH v3 2/13 tracing/probes: Enable newly appended probe when target event is enabled
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Unlike kprobe events, fprobe and uprobe events only register their underlying probes when the trace event is enabled. When a new probe is appended to an existing multiprobe fprobe or uprobe event that is already enabled, the appended probe is linked into the event's probe list without being registered.
As a result, the newly appended probe never fires until the event is disabled and re-enabled. Moreover, disabling an fprobe event calls unregister_fprobe() on the unregistered fprobe, and opening or closing a perf event on an enabled uprobe event dereferences a NULL tu->uprobe in uprobe_apply().
Register and enable the newly appended fprobe or uprobe immediately when the target event is already enabled.
--- kernel/trace/trace_fprobe.c | 14 +++++------ kernel/trace/trace_uprobe.c | 56 ++++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 27 deletions(-)
kernel/trace/trace_fprobe.c +6 -8
@@ -933,7 +933,10 @@ static int append_trace_fprobe_event(struct trace_fprobe *tf, struct trace_fprobif (ret)return ret; - ret =trace_fprobe_verify_target (tf); +if (trace_probe_is_enabled (&tf->tp)) + ret =__register_trace_fprobe (tf); +else + ret =trace_fprobe_verify_target (tf);if (ret)trace_probe_unlink (&tf->tp);else
@@ -1520,13 +1523,8 @@ static int disable_trace_fprobe(struct trace_event_call *call,trace_probe_clear_flag (tp,TP_FLAG_PROFILE );if (!trace_probe_is_enabled (tp)) { -list_for_each_entry (tf,trace_probe_probe_list (tp), tp.list) { -unregister_fprobe (&tf->fp); -if (tf->tuser) { -tracepoint_user_put (tf->tuser); - tf->tuser =NULL ; - } - } +list_for_each_entry (tf,trace_probe_probe_list (tp), tp.list) +__unregister_trace_fprobe (tf); } out:
kernel/trace/trace_uprobe.c +37 -19
75 lines not on this page · raw message
Masami Hiramatsu (Google)
maintainerPATCH v3 3/13 tracing/probes: Fix BTF bitfield parsing and out-of-bounds buffer write
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
BTF bitfield parsing in trace_probe.c assumes a 64-bit (8-byte) container when computing FETCH_OP_MOD_BF shift amounts and basesize, whereas the preceding store instruction only allocates and writes the field's fetch type size (which may be 1, 2, or 4 bytes when kflag=1). At runtime, fetch_apply_bitfield() performs an 8-byte read-modify-write on the smaller ring buffer slot, corrupting adjacent ring buffer data or writing past the end of the event record.
In addition, signed bitfields with bit widths other than 8/16/32/64 fail to resolve a fetch type, bitfields whose width is a multiple of 8 inside a larger integer or spanning a byte boundary fail to emit FETCH_OP_MOD_BF or select a container wide enough for bitoffs + bitsize, kflag=0 bitfield offsets >= 8 bits are not folded into the byte offset, and big-endian architectures use the little-endian left-shift formula in parse_btf_bitfield().
Compute the bitfield byte and bit offsets consistently during field lookup, select the smallest unsigned fetch type that covers the bit offset and bit width, and calculate the FETCH_OP_MOD_BF parameters from the selected fetch type size for both big- and little-endian bitfields.
---
Changes in v3:
- Use ctx->last_bitoffs as code->lshift on __BIG_ENDIAN_BITFIELD architectures in parse_btf_bitfield().
- Fold BTF_INT_OFFSET(intdata) into bitoffs before checking alignment and check !is_power_of_2(type->size) in get_bitoffset_of_field().
Changes in v2:
- Check (bitoffs % 8) in get_bitoffset_of_field() so unaligned byte-sized kflag=0 bitfields also record ctx->last_bitsize.
--- kernel/trace/trace_probe.c | 61 +++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 15 deletions(-)
kernel/trace/trace_probe.c +46 -15
@@ -486,6 +486,18 @@ static const char *fetch_type_from_btf_type(struct btf *btf, {u32 intdata; +if (ctx->last_bitsize) { +u32 bits = ctx->last_bitsize + ctx->last_bitoffs; + +if (bits <=8 ) +return "u8" ; +if (bits <=16 ) +return "u16" ; +if (bits <=32 ) +return "u32" ; +return "u64" ; + } +/* TODO: const char * could be converted as a string */ switch (BTF_INFO_KIND (type->info)) {case BTF_KIND_ENUM :
@@ -520,10 +532,6 @@ static const char *fetch_type_from_btf_type(struct btf *btf,case 64 :return "u64" ; } -/* bitfield, size is encoded in the type */ - ctx->last_bitsize =BTF_INT_BITS (intdata); - ctx->last_bitoffs +=BTF_INT_OFFSET (intdata); -return "u64" ; } }/* TODO: support other types */
@@ -653,6 +661,12 @@ static int get_bitoffset_of_field(char **pfieldname, const struct btf_type **pty/* Add anonymous structure/union offset */ bitoffs += anon_offs; + type =btf_type_skip_modifiers (btf, field->type,NULL ); +if (!type) { +trace_probe_log_err (ctx->offset,BAD_BTF_TID ); +return -EINVAL ; + } +/* Accumulate the bit-offsets of the dot-connected fields */ if (btf_type_kflag (container_type)) { bitoffs +=BTF_MEMBER_BIT_OFFSET (field->offset);
@@ -660,12 +674,15 @@ static int get_bitoffset_of_field(char **pfieldname, const struct btf_type **pty }else { bitoffs += field->offset; ctx->last_bitsize =0 ; - } - - type =btf_type_skip_modifiers (btf, field->type,NULL ); -if (!type) { -trace_probe_log_err (ctx->offset,BAD_BTF_TID ); -return -EINVAL ; +if (BTF_INFO_KIND (type->info) ==BTF_KIND_INT ) { +u32 intdata =btf_type_int (type); + + bitoffs +=BTF_INT_OFFSET (intdata); +if ((bitoffs %8 ) || +BTF_INT_BITS (intdata) !=BYTES_TO_BITS (type->size) || + !is_power_of_2 (type->size)) + ctx->last_bitsize =BTF_INT_BITS (intdata); + } }if (next)
@@ -858,13 +875,22 @@ static const struct fetch_type *find_fetch_type_from_btf_type( }static int parse_btf_bitfield (struct fetch_insn **pcode, +const struct fetch_type *t,struct traceprobe_parse_context *ctx) {struct fetch_insn *code = *pcode; -if ((ctx->last_bitsize %8 ==0 ) && ctx->last_bitoffs ==0 ) +if ((ctx->last_bitsize ==0 || + ctx->last_bitsize ==BYTES_TO_BITS (t->size)) && + ctx->last_bitoffs ==0 )return 0 ; +if (ctx->last_bitsize ==0 || +BYTES_TO_BITS (t->size) < (ctx->last_bitsize + ctx->last_bitoffs)) { +trace_probe_log_err (ctx->offset,BAD_BITFIELD ); +return -EINVAL ; + } + code++;if (code->op !=FETCH_OP_NOP ) {trace_probe_log_err (ctx->offset,TOO_MANY_OPS );
@@ -873,9 +899,13 @@ static int parse_btf_bitfield(struct fetch_insn **pcode, *pcode = code; code->op =FETCH_OP_MOD_BF ; - code->lshift =64 - (ctx->last_bitsize + ctx->last_bitoffs); - code->rshift =64 - ctx->last_bitsize; - code->basesize =64 /8 ; +#ifdef __BIG_ENDIAN_BITFIELD + code->lshift = ctx->last_bitoffs; +#else + code->lshift =BYTES_TO_BITS (t->size) - (ctx->last_bitsize + ctx->last_bitoffs); +#endif + code->rshift =BYTES_TO_BITS (t->size) - ctx->last_bitsize; + code->basesize = t->size;return 0 ; }
@@ -1085,6 +1115,7 @@ static int parse_btf_arg(char *varname, }static int parse_btf_bitfield (struct fetch_insn **pcode, +const struct fetch_type *t,struct traceprobe_parse_context *ctx) {trace_probe_log_err (ctx->offset,NOSUP_BTFARG );
@@ -1995,7 +2026,7 @@ static int finalize_fetch_insn(struct fetch_insn *code, }else if (IS_ENABLED (CONFIG_PROBE_EVENTS_BTF_ARGS ) && ctx->last_type) {/* If user not specified the type, try parsing BTF bitfield. */ - ret =parse_btf_bitfield (&code, ctx); + ret =parse_btf_bitfield (&code, parg->type, ctx);if (ret)return ret; }
Masami Hiramatsu (Google)
maintainerPATCH v3 4/13 tracing/probes: Fix missing BTF kind checks in char pointer/array helpers
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
In struct btf_type, the type ID and byte size share a union, and only BTF_KIND_INT types are followed by a 32-bit integer encoding word. Because btf_type_is_char_ptr() and btf_type_is_char_array() do not verify that the outer type is BTF_KIND_PTR or that the resolved element type is a non-NULL BTF_KIND_INT before inspecting it, non-pointer BTF types can have their byte size misinterpreted as a BTF type ID, and arrays of non-integer types read out-of-bounds or misinterpret trailing BTF metadata as BTF_INT_* encoding bits.
Validate the BTF type kinds and NULL pointers before reading inner type metadata in both helpers.
--- kernel/trace/trace_probe.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/trace/trace_probe.c +8 -0
@@ -411,6 +411,9 @@ static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)const struct btf_type *real_type;u32 intdata; +if (BTF_INFO_KIND (type->info) !=BTF_KIND_PTR ) +return false; + real_type =btf_type_skip_modifiers (btf, type->type,NULL );if (!real_type)return false;
@@ -435,6 +438,11 @@ static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type) array = (const struct btf_array *)(type +1 ); real_type =btf_type_skip_modifiers (btf, array->type,NULL ); +if (!real_type) +return false; + +if (BTF_INFO_KIND (real_type->info) !=BTF_KIND_INT ) +return false; intdata =btf_type_int (real_type);return !(BTF_INT_ENCODING (intdata) &BTF_INT_SIGNED )
Masami Hiramatsu (Google)
maintainerPATCH v3 5/13 tracing/eprobe: Fix 1-byte signed field fetch in get_event_field()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Because the kernel is built with -funsigned-char, plain 'char' is unsigned on all architectures. Reading 1-byte signed trace event fields via *(char *)addr in get_event_field() therefore zero-extends instead of sign-extending negative s8 values.
Use explicit fixed-width integer types (s8/u8, s16/u16, s32/u32) when reading event fields.
--- kernel/trace/trace_eprobe.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/trace/trace_eprobe.c +6 -6
@@ -334,21 +334,21 @@ get_event_field(struct fetch_insn *code, void *rec)switch (field->size) {case 1 :if (field->is_signed) - val = *(char *)addr; + val = *(s8 *)addr;else - val = *(unsigned char *)addr; + val = *(u8 *)addr;break ;case 2 :if (field->is_signed) - val = *(short *)addr; + val = *(s16 *)addr;else - val = *(unsigned short *)addr; + val = *(u16 *)addr;break ;case 4 :if (field->is_signed) - val = *(int *)addr; + val = *(s32 *)addr;else - val = *(unsigned int *)addr; + val = *(u32 *)addr;break ;default :if (field->size ==sizeof (long )) {
Masami Hiramatsu (Google)
maintainerPATCH v3 6/13 tracing/eprobe: Disallow attaching eprobes to fprobe events
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Event probes (eprobes) are not allowed to attach to other dynamic probe events (kprobes, uprobes, and eprobes) to avoid recursive probe chains and lifecycle dependencies. However, when fprobe events were introduced, TRACE_EVENT_FL_FPROBE was omitted from the exclusion mask in find_and_get_event(), allowing eprobes to attach to fprobe and tprobe events.
Exclude TRACE_EVENT_FL_FPROBE events in find_and_get_event().
--- kernel/trace/trace_eprobe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_eprobe.c +2 -1
@@ -762,7 +762,8 @@ find_and_get_event(const char *system, const char *event_name) (TRACE_EVENT_FL_IGNORE_ENABLE |TRACE_EVENT_FL_KPROBE |TRACE_EVENT_FL_UPROBE | - TRACE_EVENT_FL_EPROBE)) +TRACE_EVENT_FL_EPROBE | + TRACE_EVENT_FL_FPROBE)) continue;if (!tp_event->class->system ||strcmp (system, tp_event->class->system))
Masami Hiramatsu (Google)
maintainerPATCH v3 7/13 tracing/probes: Fix user-space dereference string array store type
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
When finalizing a string array argument (e.g. +0u(%reg):string[N]), finalize_fetch_insn() advances the instruction pointer to a new slot for the store operation before checking whether the preceding operation was FETCH_OP_UDEREF. As a result, user-space dereference string arrays with type "string" emit FETCH_OP_ST_STRING (kernel memory read) instead of FETCH_OP_ST_USTRING.
Check for FETCH_OP_UDEREF before advancing the instruction pointer.
--- kernel/trace/trace_probe.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/trace/trace_probe.c +4 -2
@@ -1956,6 +1956,8 @@ static int finalize_fetch_insn(struct fetch_insn *code,/* Store operation */ if (parg->type->is_string) { +bool is_uderef; +/* Check bad combination of the type and the last fetch_insn. */ if (!strcmp (parg->type->name,"symstr" )) { if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
@@ -1975,6 +1977,7 @@ static int finalize_fetch_insn(struct fetch_insn *code, } } + is_uderef = (code->op ==FETCH_OP_UDEREF ); if (!strcmp(parg->type->name, "symstr") || (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || code->op == FETCH_OP_IMMSTR) || code->op == FETCH_OP_TP_ARG ||
@@ -1995,8 +1998,7 @@ static int finalize_fetch_insn(struct fetch_insn *code, }/* If op == DEREF, replace it with STRING */ -if (!strcmp (parg->type->name,"ustring" ) || - code->op ==FETCH_OP_UDEREF ) +if (!strcmp (parg->type->name,"ustring" ) || is_uderef) code->op =FETCH_OP_ST_USTRING ;else if (!strcmp (parg->type->name,"symstr" )) code->op =FETCH_OP_ST_SYMSTR ;
Masami Hiramatsu (Google)
maintainerPATCH v3 8/13 tracing/probes: Reject array count on non-dereference string sources
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
The runtime array loop (FETCH_OP_LP_ARRAY) for string types requires a preceding memory dereference instruction so it can advance the pointer address on each iteration. Non-dereference string sources such as $comm, immediate strings, and eprobe event fields cannot be indexed as pointer arrays, but finalize_fetch_insn() still accepts an array count on them and emits an invalid FETCH_OP_LP_ARRAY sequence.
Reject string array counts when the preceding operation is not a memory dereference.
---
Changes in v2:
- Add Fixes and Cc: stable tags.
--- kernel/trace/trace_probe.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +6 -0
@@ -1990,6 +1990,12 @@ static int finalize_fetch_insn(struct fetch_insn *code,* For the symstr, it doesn't need to dereference, thus * it just get the value. */ +if (parg->count && (code->op !=FETCH_OP_DEREF && + code->op !=FETCH_OP_UDEREF )) { +trace_probe_log_err (ctx->offset + type_offset, +BAD_STRING ); +return -EINVAL ; + } code++;if (code->op !=FETCH_OP_NOP ) {trace_probe_log_err (ctx->offset,TOO_MANY_OPS );
Masami Hiramatsu (Google)
maintainerPATCH v3 9/13 tracing/uprobes: Fix memory leak of tail in __trace_uprobe_create()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
When generating a default event name in __trace_uprobe_create(), tail is allocated with kstrdup() without __free(kfree). If the subsequent kmalloc() for buf fails, the function returns -ENOMEM immediately and leaks tail.
Declare tail with __free(kfree) so it is automatically freed on all exit paths.
---
Changes in v2:
- Newly added.
--- kernel/trace/trace_uprobe.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/trace/trace_uprobe.c +1 -2
@@ -701,7 +701,7 @@ static int __trace_uprobe_create(int argc, const char **argv) }if (!event) { -char *tail; + char *tail __free(kfree) = NULL;char *ptr; tail =kstrdup (kbasename (filename),GFP_KERNEL );
@@ -717,7 +717,6 @@ static int __trace_uprobe_create(int argc, const char **argv)return -ENOMEM ;snprintf (buf,MAX_EVENT_NAME_LEN ,"%c_%s_0x%lx" ,'p' , tail, offset); event = buf; -kfree (tail); } argc -=2 ;
Masami Hiramatsu (Google)
maintainerPATCH v3 10/13 tracing/probes: Allow symstr type for function entry arguments in return probes
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Function entry arguments accessed in return probes ($argN or BTF entry arguments) are fetched from the entry data buffer via FETCH_OP_EDATA, which was omitted from the allowed operations for the "symstr" type and rejected with BAD_SYMSTRING.
Allow FETCH_OP_EDATA to be formatted as "symstr".
--- kernel/trace/trace_probe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_probe.c +2 -1
@@ -1962,7 +1962,8 @@ static int finalize_fetch_insn(struct fetch_insn *code,if (!strcmp (parg->type->name,"symstr" )) {if (code->op !=FETCH_OP_REG && code->op !=FETCH_OP_STACK && code->op !=FETCH_OP_RETVAL && code->op !=FETCH_OP_ARG && - code->op !=FETCH_OP_DEREF && code->op !=FETCH_OP_TP_ARG ) { + code->op !=FETCH_OP_DEREF && code->op !=FETCH_OP_TP_ARG && + code->op !=FETCH_OP_EDATA ) {trace_probe_log_err (ctx->offset + type_offset,BAD_SYMSTRING );return -EINVAL ;
[email protected]
AI reviewRe: [PATCH v3 10/13] tracing/probes: Allow symstr type for function entry arguments in return probes
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] tracing/probes: missing Fixes trailer for FETCH_OP_EDATA symstr fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] tracing/probes: missing Fixes trailer for FETCH_OP_EDATA symstr fix
--
commit 48b9066b7edfcc25acab7912c4c55cf34cf5c88d
Author: Masami Hiramatsu (Google) <[email protected]>
Quoted 10 lines
tracing/probes: Allow symstr type for function entry arguments in return probes
Function entry arguments accessed in return probes ($argN or BTF entry arguments) are fetched from the entry data buffer via
FETCH_OP_EDATA, which was omitted from the allowed operations for the "symstr" type and rejected withBAD_SYMSTRING.Allow
FETCH_OP_EDATAto be formatted as "symstr".Signed-off-by Masami Hiramatsu (Google) <[email protected]>
[Severity: Low]FETCH_OP_EDATA was introduced after the symstr type check in finalize_fetch_insn() already existed. Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179455847934.403392.15146485751358774349.stgit@devnote2?part=10
Masami Hiramatsu (Google)
maintainerRe: [PATCH v3 10/13] tracing/probes: Allow symstr type for function entry arguments in return probes
Quoted 4 lines · On Fri, 13 Nov 2026 08:39:42 +0000, [email protected]…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] tracing/probes: missing Fixes trailer forFETCH_OP_EDATAsymstr fix
Note that this is not a fix, but extending ability. Let me pick this to probes/core (and then for-next)
Thank you,
Quoted 25 lines
--
commit
48b9066b7edfcc25acab7912c4c55cf34cf5c88d
Author: Masami Hiramatsu (Google) <[email protected]>Quoted 10 lines
tracing/probes: Allow symstr type for function entry arguments in return probes
Function entry arguments accessed in return probes ($argN or BTF entry arguments) are fetched from the entry data buffer via
FETCH_OP_EDATA, which was omitted from the allowed operations for the "symstr" type and rejected withBAD_SYMSTRING.Allow
FETCH_OP_EDATAto be formatted as "symstr".Signed-off-by Masami Hiramatsu (Google) <[email protected]>[Severity: Low]
FETCH_OP_EDATAwas introduced after the symstr type check infinalize_fetch_insn()already existed. Should this include:Fixes 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)")Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179455847934.403392.15146485751358774349.stgit@devnote2?part=10
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v3 11/13 tracing/probes: Reject empty fetch expression for %pd and %pD
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
When an argument ends with :%pd or :%pD but has an empty target fetch expression (such as ":%pd" or "name=:%pd"), traceprobe_expand_dentry_args() still expands it into an invalid nested dereference expression. The subsequent parser failure then logs an error position pointing into the expanded internal string past the end of the user's command.
Reject empty %pd and %pD fetch expressions upfront before expansion.
--- kernel/trace/trace_probe.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/trace/trace_probe.c +8 -2
@@ -2438,11 +2438,17 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)if (!tmp)return -ENOMEM ; + arg_len =strlen (argv[i]); + tmp[arg_len -4 ] ='\0' ; equal =strchr (tmp,'=' );if (equal) *equal ='\0' ; - arg_len =strlen (argv[i]); - tmp[arg_len -4 ] ='\0' ; +if (*(equal ? equal +1 : tmp) =='\0' ) { +trace_probe_log_set_index (i +2 ); +trace_probe_log_err (equal ? equal +1 - tmp :0 , +BAD_FETCH_ARG ); +return -EINVAL ; + }if (argv[i][arg_len -1 ] =='d' ) ret = snprintf(tmpbuf + used, bufsize - used, "%s%s+0x0(+0x%zx(%s)):string",
Masami Hiramatsu (Google)
maintainerPATCH v3 12/13 tracing/probes: Validate command prefix in kprobe, uprobe, and eprobe
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
In kprobe, uprobe, and eprobe creation, trailing characters after the command prefix ('p', 'r', or 'e') without a ':' separator (or non-digit characters before ':') are silently ignored, creating a probe with the default event name instead of reporting a syntax error as fprobe does.
Reject invalid characters following the command prefix with BAD_MAXACT.
--- kernel/trace/trace_eprobe.c | 10 ++++++++-- kernel/trace/trace_kprobe.c | 3 +++ kernel/trace/trace_uprobe.c | 12 +++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-)
kernel/trace/trace_eprobe.c +8 -2
@@ -846,12 +846,18 @@ static int __trace_eprobe_create(int argc, const char *argv[]) trlog =trace_probe_log_init ("event_probe" , argc, argv); - event =strchr (&argv[0 ][1 ],':' ); +if (argv[0 ][1 ] !='\0' ) { +if (argv[0 ][1 ] !=':' ) { +trace_probe_log_set_index (0 ); +trace_probe_log_err (1 ,BAD_MAXACT ); +return -EINVAL ; + } + event = &argv[0 ][2 ]; + }if (event) { gbuf =kmalloc (MAX_EVENT_NAME_LEN ,GFP_KERNEL );if (!gbuf)return -ENOMEM ; - event++; ret =traceprobe_parse_event_name (&event, &group, gbuf, event - argv[0 ]);if (ret)
kernel/trace/trace_kprobe.c +3 -0
@@ -941,6 +941,9 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],trace_probe_log_err (1 ,MAXACT_TOO_BIG );return -EINVAL ; } + }else if (argv[0 ][1 ] !='\0' && argv[0 ][1 ] !=':' ) { +trace_probe_log_err (1 ,BAD_MAXACT ); +return -EINVAL ; }/* try to parse an address. if that fails, try to read the
kernel/trace/trace_uprobe.c +9 -3
@@ -616,9 +616,6 @@ static int __trace_uprobe_create(int argc, const char **argv)return -E2BIG ; } -if (argv[0 ][1 ] ==':' ) - event = &argv[0 ][2 ]; -if (!strchr (argv[1 ],'/' ))return -ECANCELED ;
@@ -631,6 +628,15 @@ static int __trace_uprobe_create(int argc, const char **argv)if (!arg || !isdigit (arg[1 ]))return -ECANCELED ; +if (argv[0 ][1 ] !='\0' ) { +if (argv[0 ][1 ] !=':' ) { +trace_probe_log_set_index (0 ); +trace_probe_log_err (1 ,BAD_MAXACT ); +return -EINVAL ; + } + event = &argv[0 ][2 ]; + } +trace_probe_log_set_index (1 );/* filename is the 2nd argument */ *arg++ ='\0' ;
Masami Hiramatsu (Google)
maintainerPATCH v3 13/13 tracing/probes: Propagate error codes in eprobe and fprobe creation
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
In __trace_eprobe_create() and trace_fprobe_create_internal(), error codes returned by helper and registration functions (such as -ENOMEM or -EEXIST) are overwritten with -EINVAL.
Propagate the actual error codes to the caller.
--- kernel/trace/trace_eprobe.c | 2 +- kernel/trace/trace_fprobe.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace_eprobe.c +1 -1
@@ -917,7 +917,7 @@ static int __trace_eprobe_create(int argc, const char *argv[])trace_probe_log_set_index (filter_idx); ret =trace_eprobe_parse_filter (ep, filter_cnt, argv + filter_idx);if (ret) -return -EINVAL ; +return ret; }else ep->filter_str =NULL ;
kernel/trace/trace_fprobe.c +2 -2
@@ -1272,7 +1272,7 @@ static int trace_fprobe_create_internal(int argc, const char *argv[],/* a symbol(or tracepoint) must be specified */ ret =parse_symbol_and_return (argc, argv, &symbol, &is_return, is_tracepoint);if (ret <0 ) -return -EINVAL ; +return ret;trace_probe_log_set_index (0 );if (event) {
@@ -1388,7 +1388,7 @@ static int trace_fprobe_create_internal(int argc, const char *argv[],trace_probe_log_err (0 ,BAD_PROBE_ADDR ); else if (ret != -ENOMEM && ret != -EEXIST)trace_probe_log_err (0 ,FAIL_REG_PROBE ); -return -EINVAL ; +return ret; }/* 'tf' is successfully registered. To avoid freeing, assign NULL. */