Skip to content

audio: kpb: Fixing issues reported by CodeQL during PTL code scanning#10858

Open
tmleman wants to merge 2 commits into
thesofproject:mainfrom
tmleman:topic/upstream/pr/audio/kpb/fix/CodeQL_issues
Open

audio: kpb: Fixing issues reported by CodeQL during PTL code scanning#10858
tmleman wants to merge 2 commits into
thesofproject:mainfrom
tmleman:topic/upstream/pr/audio/kpb/fix/CodeQL_issues

Conversation

@tmleman

@tmleman tmleman commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

These two commits are addressing issues reported by CodeQL and confirmed by other tools with use of AI for verification.

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.

Pull request overview

This PR addresses CodeQL-reported integer width/overflow concerns in the KPB (Key Phrase Buffer) audio component by widening intermediate types and forcing safer integer promotion during size calculations.

Changes:

  • Widened ch loop variables from uint16_t to uint32_t in mic selection copy helpers.
  • Added (size_t) casts in several size/multiplication expressions to avoid unintended 32-bit overflow/truncation in arithmetic.

Comment thread src/audio/kpb.c
struct audio_stream *istream = &source->stream;
struct audio_stream *ostream = &sink->stream;
uint16_t ch;
uint32_t ch;
Comment thread src/audio/kpb.c
struct audio_stream *istream = &source->stream;
struct audio_stream *ostream = &sink->stream;
uint16_t ch;
uint32_t ch;
Comment thread src/audio/kpb.c
buffer_stream_invalidate(source, size);
size_t out_samples;
uint16_t ch;
uint32_t ch;
Comment thread src/audio/kpb.c
buffer_stream_invalidate(source, size);
size_t out_samples;
uint16_t ch;
uint32_t ch;
tmleman added 2 commits June 9, 2026 11:06
Six multiplications in the key-phrase buffer compute their product at
32-bit width and only then assign it to a wider size_t result. If the
operands are large the overflow has already occurred before widening.
The KPB sizing math is partly driven by externally-influenced values
(cli->drain_req, the configured channel count, sampling frequency and
container width), so this is a real overflow surface rather than a
purely theoretical one.

Cast the leading operand to size_t in each expression so the whole
product is evaluated at the destination width:

  - kpb_micselect_copy16/32(): loop bound samples_per_chan * in_channels
  - kpb_init_draining():       drain_req and bytes_per_ms
  - adjust_drain_interval():   pipeline_period
  - validate_host_params():    bytes_per_ms

No functional change on in-range inputs; only the intermediate
arithmetic width changes.

Found-by: CodeQL 2.24.2 (codeql/cpp-queries cpp-security-extended),
  rule cpp/integer-multiplication-cast-to-long. Run with
  database build-mode=none over sof/src (host clang cannot target the
  Xtensa production build), 867 files / 98 queries. Findings at
  kpb.c:1117,1148,1610,1619,1791,2397.
AI-triaged: findings manually cross-referenced against
  clang-tidy bugprone-implicit-widening-of-multiplication-result and
  semgrep raptor-integer-truncation on the same surface, and confirmed
  the operand types (uint32_t / macro constants) against
  struct sof_kpb_config and struct kpb_client before fixing.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
The four kpb_micselect_copy16/32() variants declare the channel loop
counter "ch" as uint16_t while iterating "for (ch = 0; ch < ...
micsel_channels; ch++)", where micsel_channels is uint32_t. The loop
condition promotes ch to a wider type for the comparison, so if
micsel_channels ever exceeds UINT16_MAX the counter wraps at 65536 and
the loop fails to terminate. The same narrow counter would also
truncate channel indexing.

Declare ch as uint32_t so its width matches the bound it is compared
against and the channel count it indexes. KPB_MAX_MICSEL_CHANNELS keeps
the real value small, so this is hardening rather than an observed
runaway, but the type mismatch is removed at the source.

CodeQL flagged the two non-HiFi variants (kpb.c:1112,1143); the two
KPB_HIFI3 variants (kpb.c:1050,1084) carry the identical pattern and
are fixed in the same change for consistency.

Found-by: CodeQL 2.24.2 (codeql/cpp-queries cpp-security-extended),
  rule cpp/comparison-with-wider-type. Run with database
  build-mode=none over sof/src, 867 files / 98 queries.
AI-triaged: traced ch and micsel_channels declarations across all four
  micselect copy functions and confirmed the bound is uint32_t before
  widening the counter; extended the fix to the HiFi3 variants the tool
  did not reach in this build configuration.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but can you resolve the copilot comments. I suspect this pattern/fix will apply in other places too.

Comment thread src/audio/kpb.c

const int16_t *in_data;
int16_t *out_data;
const uint32_t samples_per_chan = size / (sizeof(uint16_t) * micsel_channels);

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.

const size_t samples_per_chan

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