Skip to content

telemetry: performance_monitor: bound extended perf data reply - #11477

Open
Shrusti-pk wants to merge 2 commits into
thesofproject:mainfrom
Shrusti-pk:telemetry-ext-perf-bound
Open

Shrusti-pk wants to merge 2 commits into
thesofproject:mainfrom
Shrusti-pk:telemetry-ext-perf-bound

Conversation

@Shrusti-pk

@Shrusti-pk Shrusti-pk commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

get_extended_performance_data() fills the IPC4_EXTENDED_GLOBAL_PERF_DATA reply without knowing the size of the buffer it writes to:

  • the buffer is ipc->comp_data (4096 bytes) and every enabled core and occupied perf slot adds a 48-byte item, so 85 fit; a slot is taken for each component the host creates, kept after the component is freed while measurements are started, and there are 88 slots by default (170 without SOF_TELEMETRY_MODULE_RESOURCES)
  • trigger: 85 components on a single core, then a BaseFW LARGE_CONFIG_GET for IPC4_EXTENDED_GLOBAL_PERF_DATA. The 86th item lands past comp_data, up to 372 bytes past with 5 cores, about 4 KiB with 170 slots. The MAILBOX_DSPBOX_SIZE check in ipc4_get_large_config_module_instance() only runs after the write
  • through a VENDOR_CONFIG_PARAM get the same getter is reached with data pointing after earlier TLV replies inside comp_data, so the whole buffer size is not a safe bound there either
  • reproduced by running the verbatim function under ASan against a 4096-byte heap block: heap-buffer-overflow WRITE of size 8 for 85 components

The fix passes the buffer size down and refuses the request with -ENOBUFS (IPC4_OUT_OF_MEMORY, "not enough space in the IPC reply buffer") when the items do not all fit, rather than returning a report the host cannot tell apart from a complete one. The size comes from the in/out data_off_size, the host max reply length (what Linux sends on a GET and what the vendor config path passes as max_length), capped at SOF_IPC_MSG_MAX_SIZE. Reports that fit are byte-identical.

The first commit makes the vendor config get path account for its own TLV header: the DSPBOX bound checked produced_data + max_length but the header is written in front of the value, so a getter honouring max_length could still end 8 bytes past the box, and the first block of a multi-block get passed max_length on unbounded.

The global and I/O reports use 16-byte items and fit. I could not build the Zephyr image locally, so the change is compile- and run-tested through the standalone ASan harness.

@sofci

sofci commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

reply test this please to run this test once

@lgirdwood
lgirdwood requested a balanced review from Copilot November 17, 2026 16:06
@lgirdwood

Copy link
Copy Markdown
Member

test this please

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Vendor-config GETs can still overrun the buffer, and capped reports silently omit records without a way to retrieve them.

2 open issues
What changed in this PR

Adds size-aware generation of IPC4 extended performance telemetry replies to prevent oversized reports from overflowing the reply buffer.

Changes:

  • Validate destination capacity and cap the number of core and component records written.
  • Pass the IPC message-size limit from BaseFW and update the API declaration, disabled stub, and documentation.
File Description
src/​kernel/​debug/​telemetry/​performance_monitor.c Adds capacity validation and limits emitted performance records.
src/​kernel/​audio/​base_fw.c Supplies the IPC message-size limit to the telemetry function.
src/​include/​sof/​debug/​telemetry/​performance_monitor.h Updates the function signature, stub, and buffer-size documentation.

Review effort: Balanced

Comment thread src/kernel/audio/base_fw.c Outdated
struct extended_global_perf_data *perf_data = (struct extended_global_perf_data *)data;

ret = get_extended_performance_data(perf_data);
ret = get_extended_performance_data(perf_data, SOF_IPC_MSG_MAX_SIZE);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, through a vendor config get the data pointer sits after the earlier TLV replies, so the full comp_data size was the wrong bound there. The getter now takes its size from the in/out data_off_size, which is the host max length on a direct GET (what Linux sends) and max_length on the vendor path, capped at SOF_IPC_MSG_MAX_SIZE. The vendor path itself was also 8 bytes short: it bounded produced_data + max_length against the DSPBOX but writes the sof_tlv header in front of the value, so a getter honouring max_length could still run past the box. The first commit counts the header there and clamps the first-block max_length the same way.

Comment on lines +447 to +450
if (slots_count > max_slots) {
tr_warn(&ipc_tr, "%zu perf items do not fit, reporting %zu", slots_count,
max_slots);
slots_count = max_slots;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, a short report with a success code is worse than no report. Dropped the cap: the getter now counts the items up front and returns -ENOBUFS without writing anything when they don't all fit, which base_fw reports as IPC4_OUT_OF_MEMORY ("not enough space in the IPC reply buffer"). Reports that fit are unchanged. Multi-block continuation for this param would be the way to fetch the full set, but that's a protocol addition rather than a bounds fix, so I've left it out here.

@lgirdwood

Copy link
Copy Markdown
Member

@Shrusti-pk can you respond/respin to teh review comments. Thanks !

ipc4_get_vendor_config_module_instance() checks that produced_data
plus the host max_length fits in MAILBOX_DSPBOX_SIZE, but the
sof_tlv header is written at produced_data and the getter's output
starts after it. A getter that honours max_length can therefore end
8 bytes past the DSPBOX, and the header write itself goes past it
when produced_data is within 8 bytes of the end. For the first block
of a multi-block get max_length is passed on unbounded while the
output also starts after the header.

Count the header in the multi-TLV bound and clamp the first block
max_length to what is left after the header, so the in/out size a
getter sees is the room it really has.

Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
get_extended_performance_data() writes one 48-byte ext_perf_data_item
for every enabled core and every occupied performance slot, and never
looks at the size of the buffer it writes to. The IPC4 BaseFW
LARGE_CONFIG_GET for IPC4_EXTENDED_GLOBAL_PERF_DATA hands it
ipc->comp_data, a SOF_IPC_MSG_MAX_SIZE (4096 byte) heap block, which
holds 85 items after the count word.

A slot is taken for every component the host creates, and while
measurements are started it stays taken after the component is freed.
CONFIG_SOF_TELEMETRY_MAX_COMP_ENTRIES defaults to 88 (170 without
SOF_TELEMETRY_MODULE_RESOURCES), so 85 components on one core are
enough for the report to run past the end of comp_data: up to 372
bytes with 5 cores and 88 slots, about 4 KiB with 170 slots. The
reply size check in ipc4_get_large_config_module_instance() runs only
after the getter has written. Through a VENDOR_CONFIG_PARAM get the
same getter is reached with data pointing after earlier TLV replies
inside comp_data, so the whole buffer size is not a safe bound there.

Pass the buffer size to get_extended_performance_data() and refuse
the request with -ENOBUFS, reported as IPC4_OUT_OF_MEMORY, when the
items do not all fit, instead of returning a report the host cannot
tell apart from a complete one. The size is the in/out data_off_size,
the host max reply length that the vendor config path narrows to the
room left after its TLV header, capped at SOF_IPC_MSG_MAX_SIZE.
Reports that fit are unchanged. The 16-byte global and I/O perf
reports cannot exceed the buffer with the configured slot counts.

Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
@Shrusti-pk Shrusti-pk changed the title telemetry: performance_monitor: cap extended perf data reply telemetry: performance_monitor: bound extended perf data reply Nov 23, 2026
@Shrusti-pk
Shrusti-pk force-pushed the telemetry-ext-perf-bound branch from e033e3b to 91e7dff Compare November 23, 2026 06:52
@Shrusti-pk

Copy link
Copy Markdown
Contributor Author

Respun. The getter now refuses a report that doesn't fit (IPC4_OUT_OF_MEMORY) instead of truncating it, takes its bound from the in/out data_off_size so the vendor config get path is covered too, and a first commit makes that path leave room for its own TLV header. Both review threads are answered inline.

@kv2019i kv2019i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a rebase, otherwise looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants