Skip to content

tools: ctl: bound csv data write against abi header size#10977

Open
jmestwa-coder wants to merge 1 commit into
thesofproject:mainfrom
jmestwa-coder:ctl-csv-abi-bound
Open

tools: ctl: bound csv data write against abi header size#10977
jmestwa-coder wants to merge 1 commit into
thesofproject:mainfrom
jmestwa-coder:ctl-csv-abi-bound

Conversation

@jmestwa-coder

Copy link
Copy Markdown
Contributor

ascii csv input in read_setup() overflows the tlv buffer with -r:

  • with -r (no_abi) the data write index starts past the 32-byte sof_abi_hdr
  • the per-value guard still checks n < n_max, where n_max is the unpadded ctrl_size
  • the binary branch reads only n_max - abi_size, the csv branch does not match it
  • a csv file with ctrl_size/4 values writes sizeof(struct sof_abi_hdr) bytes past the heap buffer

Bounded the csv write with n < n_max - abi_size like the binary branch.

@jmestwa-coder jmestwa-coder requested a review from singalsu as a code owner July 3, 2026 18:58
@sofci

sofci commented Jul 3, 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 review from lyakh July 9, 2026 09:49

@lyakh lyakh 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.

that loop could benefit from some clarity improvements - mixing size in bytes and index into a 32-bit array makes it a bit hard to follow

Comment thread tools/ctl/ctl.c Outdated
data_int_index = data_start_int_index;
while (fscanf(fh, "%u", &x) != EOF) {
if (n < n_max)
if (n < n_max - abi_size)

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.

is alignment guaranteed? E.g. if the buffer size is initialised from the file size in like

ctrl_size = get_file_size(ctl_data->in_fd);
it isn't guaranteed to be a multiple of 4? We're about to write 4 bytes to the buffer. Maybe either align it up to 4 bytes when allocating or change the check here to n <= n_max - abi_size - sizeof(uint32_t)?

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.

good catch, alignment isn't guaranteed and that's an overflow on its own. with ctrl_size = 257 the old bound let the last store write 3 bytes past the 265-byte buffer.

  • went with counting values rather than n <= n_max - abi_size - sizeof(uint32_t), because sizeof makes the rhs size_t, so a bytes control with count < 36 wraps it to a huge unsigned and the guard always passes
  • the loop now walks a uint32_t index bounded by val_max = (n_max - abi_size) / sizeof(uint32_t), which floors off the unaligned tail
  • n is computed once after the loop instead of being carried alongside the index

that also drops the bytes vs u32 index mixing you pointed at. the binary branch already lands exactly on the buffer end, so it's unchanged. pushed.

The ascii csv branch of read_setup() advances the write index past the
32-byte abi header for -r (no_abi) input but still bounds each write with
n < n_max, while the binary branch stops at n_max - abi_size. A csv tuning
file with ctrl_size/4 values then writes sizeof(struct sof_abi_hdr) bytes
past the end of the tlv buffer.

ctrl_size is not guaranteed to be a multiple of sizeof(uint32_t) either,
so bounding the byte count alone still lets the final 4-byte store run up
to 3 bytes past the buffer. Count the csv values and stop after the last
whole uint32_t that fits in the data area, which also keeps the loop from
mixing a size in bytes with an index into a uint32_t array.

Signed-off-by: Syed Mohammed Nayyar <jmestwa@gmail.com>
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.

4 participants