Repository navigation
DSF: do not convert an out of range length to unsigned int - #1416
Conversation
DSF::Properties::read computes
d->length = d->samplingFrequency > 0
? static_cast<unsigned int>(static_cast<double>(d->sampleCount)
* 1000.0 / d->samplingFrequency + 0.5)
: 0;
sampleCount is a long long taken straight from the file and
samplingFrequency is an unsigned int from the file, so the millisecond
count can land well outside unsigned int. Converting a floating point
value the destination type cannot represent is undefined:
taglib/dsf/dsfproperties.cpp:132:35: runtime error: 8.41595e+09 is
outside the range of representable values of type 'unsigned int'
#0 TagLib::DSF::Properties::read(TagLib::ByteVector const&)
taglib#1 TagLib::DSF::Properties::Properties(...)
taglib#10 TagLib::FileRef::FileRef(char const*, bool, ...)
A negative sampleCount converts just as badly, so guard that too.
Report an unknown length instead of converting, matching what a zero
sampling frequency already does. Nothing changes for a valid file:
tests/data/empty10ms.dsf reads lengthMs=10 bitrate=5645 rate=2822400
ch=2 both before and after.
Found by mutating the files in tests/data and running them through a
parse, read properties and save round trip under UBSan.
Assisted-By: Claude Code (Claude Opus 5)
Same shape as the DSF change: sampleCount comes from a chunk size in the
file and sampleRate is read from it, so
static_cast<int>(static_cast<double>(d->sampleCount) * 1000.0
/ d->sampleRate + 0.5)
can be handed a value int cannot represent, which is undefined.
In the PR I said I had not demonstrated this one, because chunkFits
bounds the sample count by the file length so a few mutated bytes will
not do it. Constructing the file it does need - a 700 kB DSD chunk with
a sample rate of 1 - reaches it:
taglib/dsdiff/dsdiffproperties.cpp:61:24: runtime error: 2.8e+09 is
outside the range of representable values of type 'int'
Two reports before the change, none after. tests/data/empty10ms.dff is
unchanged at lengthMs=10 bitrate=5644 rate=2822400 ch=2.
Assisted-By: Claude Code (Claude Opus 5)
Third instance of the same shape as the DSF and DSDIFF changes:
static_cast<int>(static_cast<double>(duration) / 10000.0
- static_cast<double>(preroll) + 0.5)
duration and preroll are both long long values read from the file
properties object, so the result can land outside int, and converting a
double the destination type cannot represent is undefined:
taglib/asf/asffile.cpp:240:22: runtime error: -2.80375e+14 is outside
the range of representable values of type 'int'
Only skip the conversion when the value will not fit. Values that do
fit, negative ones included, are set exactly as before, so the three
.wma files in tests/data still report lengthMs 3549, 96502 and 3712.
Found by the same fuzzing that turned up the DSF case.
Assisted-By: Claude Code (Claude Opus 5)
|
Continued fuzzing turned up a third instance of the same shape, so I have added it here rather than opening another PR — static_cast<int>(static_cast<double>(duration) / 10000.0 - static_cast<double>(preroll) + 0.5)with The guard only skips the conversion when the value will not fit, so values that do fit — negative ones included — are set exactly as before. The three That makes three commits, each a self-contained instance of "a length computed from file-supplied numbers is converted to a type that cannot represent it": DSF, DSDIFF, ASF. Happy to split them if you would rather take them separately. Separately, and not in this PR: the same fuzzing found that the ID3v2 frame parsers cast the text encoding byte straight out of the file, d->textEncoding = static_cast<String::Type>(data[0]);with no check that the byte is one of the five values There are eleven of these across nine files, plus a twelfth of the same kind for I have deliberately not opened it, because where the helper belongs ( |
|
Two more of the same shape turned up, so this is now five commits. Both verified the same way — reverting only the hunk brings the report back, and the affected files in AIFF ( All seven AIFF files in MPC ( This one showed up independently from two different seed files, So the five are DSF, DSDIFF, ASF, AIFF and MPC — one commit each, all "a length or rate computed from file-supplied numbers is converted to a type that cannot represent it". Happy to split, squash or drop any of them. On the enum question I raised above, I have finished the survey and it is wider than the eleven ID3v2 sites I mentioned. Three different enums are built by casting a file-supplied value without checking it:
Observed values were 4294967269 for the first, and 3321888768 for Still not opening anything for this — it is fifteen sites and the fallback policy is a design decision. Just wanted the survey to be complete rather than leave you with a partial number. |
Fourth instance of the same shape. The sample rate is an 80 bit float read from the file and the frame count is a 32 bit count from it, so all three conversions here can be handed a value int cannot represent: taglib/riff/aiff/aiffproperties.cpp:145:38: runtime error: 6.29416e+49 is outside the range of representable values of type 'int' taglib/riff/aiff/aiffproperties.cpp:150:35: runtime error: 1.00707e+48 is outside the range of representable values of type 'int' Line 149 is the same shape and reachable the other way round, with a large sampleFrames over a small sample rate, so it is guarded too. Leave the field at its default rather than converting. All seven AIFF files in tests/data report identical channels, sample rate, bitrate and length before and after. Two reports before the change, none after. Assisted-By: Claude Code (Claude Opus 5)
Fifth instance of the same shape, in readSV8:
const auto length = static_cast<double>(frameCount) * 1000.0 / d->sampleRate;
d->length = static_cast<int>(length + 0.5);
d->bitrate = static_cast<int>(static_cast<double>(streamLength) * 8.0 / length + 0.5);
frameCount is sampleFrames minus begSilence, both read from the file, so
the millisecond figure can land outside int:
taglib/mpc/mpcproperties.cpp:244:39: runtime error: 4.18294e+17 is
outside the range of representable values of type 'int'
The bitrate on the next line goes the same way when length is small, so
it is guarded too. One report before, none after, and the .mpc files in
tests/data report identical properties.
Turned up independently from two different seed files, sv8_header.mpc
and zerodiv.mpc.
Assisted-By: Claude Code (Claude Opus 5)
5049fec to
d1c0327
Compare
|
Thanks for your contribution! |
What
DSF::Properties::readcomputes the length assampleCountis along longread straight from the file andsamplingFrequencyis an unsigned int from the file, so the millisecond count can land well outsideunsigned int. Converting a floating point value the destination type cannot represent is undefined:It is on the plain parse path — constructing a
FileRefover the file is enough. A negativesampleCountconverts just as badly, so the guard covers that too.The change
Report an unknown length rather than converting, which is what a zero sampling frequency already does.
Nothing changes for a valid file —
tests/data/empty10ms.dsfreadslengthMs=10 bitrate=5645 rate=2822400 ch=2both before and after. Reverting just this hunk and rebuilding brings the report straight back.I could not run the CppUnit suite (CppUnit isn't installed here, so
BUILD_TESTINGskips the tests directory) — the checks above are what I have.bitrateon the line above is fineWorth saying since it looks similar:
samplingFrequency * bitsPerSample * channelNumis unsigned arithmetic, so it wraps rather than overflowing, and dividing by 1000.0 leaves at most ~4.3e6, comfortably insideunsigned int.One I could not demonstrate
DSDIFF::Properties(dsdiffproperties.cpp:60) has the same shape, casting tointinstead. There the sample count comes from a chunk size thatchunkFitshas already validated against the file length, so triggering it needs a genuinely large file — roughly 262 KB with a sample rate of 1 — rather than a few mutated bytes. I did not build one, so I have left it alone rather than claim it. Happy to follow up if you'd like it guarded on the same grounds.How it was found
Mutating the files in
tests/dataand running each through a parse → read every property → modify →save()round trip, under ASan and UBSan.I calibrated the harness first: with a deliberate one byte over-read injected into
FileStream::readBlock, ASan reports it through that path, and the report goes away when the injection is removed. Without that check a clean run would not have meant anything.Disclosure
Written with Claude Code assisting, under my direction; the commit carries an
Assisted-Bytrailer. I verified the before/after myself on macOS/arm64.X: manuaudiomix