filament icon indicating copy to clipboard operation
filament copied to clipboard

Ktx1Reader uses uninitialized stack memory when imageSize=0

Open Alearner12 opened this issue 6 months ago • 0 comments

Summary

ktxreader::Ktx1Reader::createTexture uses uninitialized stack variables when processing a KTX1 file that has a mip level with imageSize=0. Because the parser correctly sets the size to 0, Ktx1Bundle::getBlob returns false without writing to the data and size output parameters. The caller ignores the return value, resulting in uninitialized stack values being passed directly to the Filament backend for texture upload. This leads to a highly reliable Denial of Service (segmentation fault) or potential memory corruption/info-leak when loading untrusted KTX1 files.

Technical Details

In libs/ktxreader/src/Ktx1Reader.cpp:73-74, the following uninitialized variables are declared:

uint8_t* data;
uint32_t size;

Inside the loop over mip levels, getBlob is called and its return value is ignored:

ktx.getBlob({ level, 0, 0 }, &data, &size);

When a KTX1 file specifies imageSize=0, getBlob returns false and does not write to *data or *size. The engine then passes these uninitialized stack values to texture->setImage(...), causing the backend to read size (garbage) bytes from data (garbage pointer).

Proof of Concept

Loading a 68-byte KTX1 file with imageSize=0 for mip 0 triggers this bug. Running a standalone reproducer through Valgrind confirms the Undefined Behavior at the consumer site:

==44096== Conditional jump or move depends on uninitialised value(s)
==44096==    at 0x48D8C05: __vfprintf_internal (vfprintf-internal.c:1516)
==44096==  Uninitialised value was created by a stack allocation
==44096==    at 0x109189: createTexture_buggy(fake_ktx::Bundle const&)

Proposed Fix

I have a patch that initializes the stack locals, validates the getBlob return value, and ensures that the free callback invariant is preserved by decrementing remainingBuffers when skipping a mip. I will be raising a PR shortly.

Alearner12 avatar May 13 '26 05:05 Alearner12