Skip to content

fix(mp3): signed overflow (UB) in fixed-point mp3d_synth_pair on malformed input #4133

Closed

Description

@zackees

What happens

UBSan fires inside the fixed-point polyphase on the l3-nonstandard-big-iscf conformance vector:

minimp3.h:2699:29: runtime error: signed integer overflow: -838085126 + -1518500249 cannot be represented in type 'int32_t'
minimp3.h:2700:29: runtime error: signed integer overflow: 1326454660 - -838085126 cannot be represented in type 'int32_t'

Cause

mp3d_synth_pair, the fixed-point one:

a  = (int64_t)(z[14*64] - z[    0]) * 29;
a += (int64_t)(z[ 1*64] + z[13*64]) * 213;

The cast applies to the result of the subtraction, not to its operands. So z[14*64] - z[0] is evaluated in int32_t and only then widened. With large sample values the int32 arithmetic overflows, which is undefined behaviour -- the compiler is entitled to assume it cannot happen, so the generated code for a malformed stream is not merely wrong but unconstrained.

This is FastLED's code, not upstream's. The int32 variant is a + block in FASTLED.patch, added for the fixed-point port. Upstream's is float:

float a;
a  = (z[14*64] - z[    0]) * 29;      /* no integer overflow possible */

Fix

Widen the operands before the arithmetic, in the seven add/sub pairs of the first accumulator:

a  = ((int64_t)z[14*64] - (int64_t)z[    0]) * 29;

Widening rather than saturating is the right call here. The result feeds an int64_t accumulator, so there is headroom for the true difference, and preserving it matches the float path's semantics -- mp3d_add_sat/mp3d_sub_sat exist for values that must stay in int32, which these do not. The second accumulator multiplies single z[] terms with no add or subtract and needs no change.

Why nothing caught it

Third instance of the same pattern in this codebase, so it is worth naming rather than just fixing.

The differential fuzzer runs under ASan and UBSan and is clean -- over the vendored corpus. Normal audio never drives the polyphase inputs large enough to overflow int32; l3-nonstandard-big-iscf does, because it is a deliberately malformed intensity-stereo scalefactor stream that pushes the pipeline to 230767 where every other vector stays under 4.

The other two instances: #4127 (dequant clamped to 1.0 on a headroom measurement taken over 2 of 83 vectors) and the conformance gate itself, which measured PSNR but not decoded length and so scored 115.61 dB on a vector that dropped 27904 samples. In all three the measurement was sound and the scope it was taken over was too narrow.

Severity

Low likelihood, non-trivial consequence. It needs a malformed or hostile stream, not ordinary audio. But an MP3 decoder is exactly the kind of component that gets pointed at untrusted input, and UB is not a "wrong output" bug -- it is a "no guarantees at all" bug, and it lives in the hottest kernel in the decoder.

Acceptance criteria

  • UBSan clean on l3-nonstandard-big-iscf and the rest of the conformance suite.
  • Decoded PCM byte-identical on every vector that does not overflow, since widening changes nothing where int32 was already sufficient.
  • The conformance runner gains a sanitizer pass so this class of finding is caught by CI rather than by hand.

Found by running the conformance suite added in #4127 under ASan and UBSan.

Refs #4127

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions