Skip to content

fix out-of-bounds X underflow in SBR low-power QMF assembly - #225

Merged
fabiangreffrath merged 1 commit into
knik0:masterfrom
netliomax25-code:sbr-lowpower-qmf-kx-underflow
Jun 25, 2026
Merged

fix out-of-bounds X underflow in SBR low-power QMF assembly#225
fabiangreffrath merged 1 commit into
knik0:masterfrom
netliomax25-code:sbr-lowpower-qmf-kx-underflow

Conversation

@netliomax25-code

Copy link
Copy Markdown
Contributor
  1. In SBR_LOW_POWER builds, sbr_process_channel() (libfaad/sbr_dec.c) finishes each output time slot of the QMF matrix X with an overlap add:

    QMF_RE(X[l][kx_band - 1 + bsco_band]) +=
        QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][kx_band - 1 + bsco_band]);
  2. For the leading time slots (l < sbr->t_E[ch][0]) the band parameters are taken from the previous frame: kx_band = sbr->kx_prev and bsco_band = sbr->bsco_prev.

  3. On the very first decoded SBR frame both are still 0: sbr_info is zero-initialised, and sbr_save_prev_data() — the only writer of kx_prev/bsco_prev — runs after the frame is processed.

  4. A first frame whose grid is VARFIX or VARVAR with bs_abs_bord >= 1 sets abs_bord_lead > 0, so t_E[ch][0] = rate * abs_bord_lead > 0. The l = 0 slot then takes the kx_prev path, and kx_band - 1 + bsco_band evaluates to -1, so the statement reads and writes X[0][-1] — one real_t before the stack array X[MAX_NTSRHFG][64]. AddressSanitizer reports a stack-buffer-underflow at sbr_dec.c:447.

  5. The current-frame kx is always >= 5 (the qmf_start_channel() start tables and derived_frequency_table()), so the only way kx_band + bsco_band can be 0 is the first-frame kx_prev == 0 / bsco_prev == 0 case. This is a different site from the hf_assembly() upper-bound (m + kx + 1 == 64) overflow.

  6. Fix: guard the overlap add with if (kx_band + bsco_band > 0), skipping it when there is no band below 0 to add. Normal frames (kx_band >= 5) are unaffected and the high-quality (non-low-power) path does not contain this statement.

  7. Verified with a minimal crafted SBR payload driven through sbr_extension_data() + sbrDecodeSingleFrame() in an SBR_LOW_POWER + ASan build: stack-buffer-underflow at sbr_dec.c:447 before the change, clean afterwards (ret == 0, decode continues). The default and SBR_LOW_POWER configurations both build clean with -Wall.

In SBR_LOW_POWER builds, sbr_process_channel() ends each time slot with an
overlap add at X[l][kx_band - 1 + bsco_band]. For leading time slots
(l < t_E[ch][0]) kx_band/bsco_band are taken from the previous frame
(kx_prev/bsco_prev), which are still 0 on the first decoded frame. A first
frame with a VARFIX/VARVAR grid and bs_abs_bord >= 1 makes t_E[ch][0] > 0,
so l = 0 uses kx_prev == 0 and the index becomes -1, reading and writing
X[0][-1] one element before the stack array.

Skip the overlap add when kx_band + bsco_band == 0; there is no band below
0 to add. Normal frames have kx_band >= 5 and are unaffected.
@fabiangreffrath
fabiangreffrath merged commit e87c741 into knik0:master Jun 25, 2026
6 checks passed
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.

2 participants