Skip to content

Shorten: signed integer overflow in getRiceGolombCode (attacker-controlled input) #1495

Open

Description

@Leythertag

Summary

The Shorten decoder (taglib/shorten/shortenfile.cpp, VariableLengthInput::getRiceGolombCode) accumulates the Rice-Golomb quotient in a signed int32_t and then shifts it left:

result = (result << k) | static_cast<int32_t>(...);      // line 135

result is built from the unary prefix of the bitstream, which is attacker-controlled: a long run of zero bits makes result large. For a large result, result << k (and the result << bitsAvailable branch just below) overflows a signed 32-bit int, which is undefined behaviour in C++.

It is reachable from the public FileRef API on an untrusted file — content detection sees the ajkg magic and routes into the Shorten parser — so any application that opens untrusted audio through TagLib can trigger it.

Affected code

taglib/shorten/shortenfile.cpp, in VariableLengthInput::getRiceGolombCode (around line 135):

int32_t result;
for(result = 0; !(bitBuffer & (1L << --bitsAvailable)); ++result) {   // unary prefix -> result
  ...
}

while(k != 0) {
  if(bitsAvailable >= k) {
    result = (result << k) | static_cast<int32_t>(       // <-- signed shift overflow (UB)
      (bitBuffer >> (bitsAvailable - k)) & sMaskTable[k]);
    ...
  }
  else {
    result = (result << bitsAvailable) | static_cast<int32_t>(   // <-- same here
      bitBuffer & sMaskTable[bitsAvailable]);
    ...
  }
}

Sanitizer report

Built with -fsanitize=address,undefined:

taglib/shorten/shortenfile.cpp:135:26: runtime error: left shift of 486539264 by 4 places cannot be represented in type 'int32_t' (aka 'int')
    #0 VariableLengthInput::getRiceGolombCode(int&, int)  shorten/shortenfile.cpp:135:26
    #1 VariableLengthInput::getUInt(unsigned int&, int, int)  shorten/shortenfile.cpp:159:9
    #2 TagLib::Shorten::File::read(TagLib::AudioProperties::ReadStyle)  shorten/shortenfile.cpp:334:17
    #3 TagLib::Shorten::File::File(...)  shorten/shortenfile.cpp:219:5
    #4 (anonymous namespace)::detectByContent(...)  fileref.cpp:365:18
    #5 TagLib::FileRef::parse(...)  fileref.cpp:689:13
    #6 TagLib::FileRef::FileRef(...)  fileref.cpp:437:3

Reproduction

A 13-byte input is enough (a minimal Shorten ajkg stream). Base64:

YWprZwIBAAAA/6TR+w==
printf '%s' 'YWprZwIBAAAA/6TR+w==' | base64 -d > repro.shn   # sha256: 0ecb7b404d3bc1bcf0cd7f23ea35080ab7e934b2cb550a12852c02a2e3f513a5

Then open it through FileRef (or feed it to taglib_fileref_fuzzer):

#include <taglib/fileref.h>
int main() { TagLib::FileRef f("repro.shn"); }   // UBSan aborts in getRiceGolombCode

Version

Reproduces on current master HEAD 58499cb157c38b8d07b262b15c4ecba9f36aeb9a (confirmed with a fresh clone + sanitizer build).

Impact (honest assessment)

  • Definite: undefined behaviour (signed-integer overflow). Under UBSan it aborts; in a normal build the result is compiler-dependent.
  • Not demonstrated: I have not shown this leads to an out-of-bounds access or memory corruption in a normal build. I'm reporting it as a robustness / undefined-behaviour defect in a parser that handles untrusted input, not as a proven memory-safety vulnerability — please assess severity on your side.

Suggested fix

Do the accumulation/shift in an unsigned type (e.g. compute result as uint32_t and cast back on return), so the shift can no longer overflow a signed int. It may also be worth bounding the unary-prefix length, since a very long prefix yields a meaningless quotient regardless. Happy to send a PR if that direction looks right.

Found via local libFuzzer + ASan/UBSan fuzzing.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions