7z, sound effects: Bound memory a crafted file can take

- A 7z folder that can't be streamed (BCJ2, filter chains) is extracted
  whole into memory at the size its header claims. Refuse folders over
  256 MB rather than run out of memory.
- When streaming, the LZMA and LZMA2 dictionaries are clamped to the
  folder size. The header can ask for up to 4 GB.
- Custom MP3 sound effects decode at most 60 seconds, and a sample file
  shorter than 4 bytes (or too large) is rejected without reading past it
  or leaking it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-11-05 21:12:21 +01:00
1 parent 5a569135dd
commit 2d30b81543
2 files changed
+44 -4

No files matched your search

+36 -2
View File
@@ -26,6 +26,8 @@
static constexpr size_t SEVENZIP_LOOKBUF_SIZE = 1 << 14;
static constexpr size_t SEVENZIP_STREAM_LOOKAHEAD = 1 << 18;
static constexpr size_t SEVENZIP_SKIPBUF_SIZE = 1 << 15;
// Folders that can't be streamed are extracted whole into memory, so only small ones are.
static constexpr UInt64 SEVENZIP_MAX_MEMORY_EXTRACT = 256 * 1024 * 1024;
static constexpr UInt32 SEVENZIP_METHOD_COPY = 0;
static constexpr UInt32 SEVENZIP_METHOD_LZMA = 0x30101;
@@ -250,16 +252,41 @@ static bool InitStreamingOpenFile(
return false;
}
// The header can ask for a dictionary of up to 4GB. Matches can't reach back further than the
// folder's own data, so a dictionary that covers the folder is enough.
const UInt64 dicNeeded = std::max(folderSize64, (UInt64)(1 << 12));
const Byte *props = folderData + coder.PropsOffset;
if (mode == SevenZipOpenFile::Mode::STREAM_LZMA) {
if (LzmaDec_Allocate(&openFile->lzma_, props, coder.PropsSize, alloc) != SZ_OK) {
if (coder.PropsSize != LZMA_PROPS_SIZE) {
openFile->Release();
return false;
}
Byte lzmaProps[LZMA_PROPS_SIZE];
memcpy(lzmaProps, props, LZMA_PROPS_SIZE);
const UInt32 dicSize = (UInt32)lzmaProps[1] | ((UInt32)lzmaProps[2] << 8) | ((UInt32)lzmaProps[3] << 16) | ((UInt32)lzmaProps[4] << 24);
if (dicSize > dicNeeded) {
const UInt32 clamped = (UInt32)dicNeeded;
for (int i = 0; i < 4; i++) {
lzmaProps[1 + i] = (Byte)(clamped >> (8 * i));
}
}
if (LzmaDec_Allocate(&openFile->lzma_, lzmaProps, LZMA_PROPS_SIZE, alloc) != SZ_OK) {
openFile->Release();
return false;
}
openFile->lzmaAllocated_ = true;
LzmaDec_Init(&openFile->lzma_);
} else if (mode == SevenZipOpenFile::Mode::STREAM_LZMA2) {
if (coder.PropsSize != 1 || Lzma2Dec_Allocate(&openFile->lzma2_, props[0], alloc) != SZ_OK) {
if (coder.PropsSize != 1 || props[0] > 40) {
openFile->Release();
return false;
}
// The smallest dictionary size property that still covers the folder (40 is 4GB).
Byte prop = 0;
while (prop < props[0] && prop < 40 && ((UInt64)(2 | (prop & 1)) << (prop / 2 + 11)) < dicNeeded) {
prop++;
}
if (Lzma2Dec_Allocate(&openFile->lzma2_, prop, alloc) != SZ_OK) {
openFile->Release();
return false;
}
@@ -540,6 +567,13 @@ uint8_t *SevenZipFileReader::ExtractFile(UInt32 fileIndex, size_t *size) {
return nullptr;
}
// SzArEx_Extract unpacks the file's whole folder into memory, at the size the header claims.
const UInt32 folderIndex = db_.FileToFolder[fileIndex];
if (folderIndex != (UInt32)-1 && SzAr_GetFolderUnpackSize(&db_.db, folderIndex) > SEVENZIP_MAX_MEMORY_EXTRACT) {
ERROR_LOG(Log::IO, "Can't extract '%s' from 7z archive: its folder is too big to unpack in memory", entries_[fileIndex].path.c_str());
return nullptr;
}
size_t offset = 0;
size_t outSizeProcessed = 0;
SRes res = SzArEx_Extract(
+8 -2
View File
@@ -516,7 +516,9 @@ static Sample *LoadMp3(const uint8_t *data, size_t size) {
int sampleRate = 0;
int delay = 0;
int padding = 0;
while (pos + 4 <= size) {
// Sound effects are short. A long file would decode to many times its own size.
constexpr int MAX_SECONDS = 60;
while (pos + 4 <= size && (sampleRate == 0 || samples.size() < (size_t)sampleRate * 2 * MAX_SECONDS)) {
int frameRate = 0;
int frameSize = Mp3FrameSize(data + pos, &frameRate);
if (frameSize == 0 || pos + frameSize > size) {
@@ -527,6 +529,9 @@ static Sample *LoadMp3(const uint8_t *data, size_t size) {
if (!decoder) {
sampleRate = frameRate;
decoder.reset(CreateAudioDecoder(PSP_CODEC_MP3, sampleRate, 2));
if (!decoder) {
return nullptr;
}
if (ParseXingFrame(data + pos, frameSize, &delay, &padding)) {
// It decodes to silence.
pos += frameSize;
@@ -559,8 +564,9 @@ static Sample *LoadMp3(const uint8_t *data, size_t size) {
Sample *Sample::Load(const std::string &path) {
size_t data_size = 0;
uint8_t *data = g_VFS.ReadFile(path.c_str(), &data_size);
if (!data || data_size > 100000000) {
if (!data || data_size < 4 || data_size > 100000000) {
WARN_LOG(Log::Audio, "Failed to load sample '%s'", path.c_str());
delete[] data;
return nullptr;
}