gltfio: heap OOB write in AssetLoaderExtended via slotIndices sizing mismatch
Summary
Heap out-of-bounds write in AssetLoaderExtended::createPrimitive due to a mismatch between the size used to allocate slotIndices and the index used to write into it. The vector is sized by a filtered morph target count, but indexed by the original unfiltered target index, allowing writes far past the end of the heap allocation.
Vulnerable file: libs/gltfio/src/extended/AssetLoaderExtended.cpp
Impact: Heap buffer overflow (WRITE), attacker-controlled index offset, triggered by a crafted GLB file
Vulnerability details
The filtering logic (lines 451-497)
The extended loader builds a filtered list called morphTargets that only includes morph targets needing tangent generation:
// line 451
std::vector<int> morphTargets;
for (cgltf_size targetIndex = 0; targetIndex < targetsCount; targetIndex++) {
bool morphTargetHasNormals = false;
cgltf_morph_target const& target = prim->targets[targetIndex];
for (cgltf_size aindex = 0; aindex < target.attributes_count; aindex++) {
// ...
if (atype == cgltf_attribute_type_tangent) { // line 488
morphTargetHasNormals = true;
morphTargets.push_back(targetIndex); // line 490
}
}
// line 494 - skipped entirely for unlit materials
if (!morphTargetHasNormals && prim->material && !prim->material->unlit) {
morphTargets.push_back(targetIndex);
}
}For a material with KHR_materials_unlit, the fallback at line 494 never fires. Only morph targets that explicitly declare a TANGENT attribute are added to morphTargets. This means morphTargets.size() can be much smaller than targetsCount.
The vulnerable resize (line 543)
out->slotIndices.resize(morphTargets.size()); // BUG: filtered countIf targetsCount is 10 and only target 9 has a TANGENT attribute, then morphTargets = {9} and slotIndices gets allocated with size 1.
The out-of-bounds write (line 558)
if (slot.offset == 0xdeadbeef) {
out->slotIndices[slot.slot] = outSlots.size() + i; // line 558
}slot.slot holds the original targetIndex value (9 in the example above). Writing to slotIndices[9] when the vector has size 1 is a heap buffer overflow of 8 int positions (32 bytes) past the allocation.
The assert_invariant that could catch this at runtime is compiled out in release builds.
Correct code in the non-extended path (AssetLoader.cpp line 1304)
The standard code path sizes slotIndices using targetsCount, which is the total number of morph targets before any filtering:
outPrim->slotIndices.resize(targetsCount); // CORRECT: total countTrigger conditions
All of these must be true:
- The asset is loaded through the extended algorithm path, enabled by passing
AssetConfigurationExtendedinAssetConfiguration.ext - The mesh primitive has a material with
KHR_materials_unlit - The mesh has N morph targets, but only a high-indexed target has a
TANGENTattribute - The gap between the highest included target index and
morphTargets.size()controls how far past the allocation the write lands
The OOB offset is fully controlled by the attacker through the GLB structure.
Reproduction
GLB structure
- 10 morph targets (index 0 through 9)
- Targets 0-8: only have
POSITIONattribute, noTANGENT - Target 9: has both
POSITIONandTANGENTattributes - Material:
KHR_materials_unlit - Single triangle mesh primitive
What happens at runtime
targetsCount= 10- Filtering loop runs: targets 0-8 have no TANGENT and material is unlit, so they are all skipped
- Target 9 has TANGENT, so
morphTargets.push_back(9) morphTargets.size()= 1slotIndices.resize(1)allocates space for 1 int (4 bytes)computeGeometriesproduces a slot withslot.slot = 9slotIndices[9]writes 32 bytes past the end of a 4-byte heap allocation
ASan output
=================================================================
==2532139==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7be3717e5754
WRITE of size 4 at 0x7be3717e5754 thread T0
#0 0x55ad3692744d in filament::gltfio::AssetLoaderExtended::createPrimitive(...)
AssetLoaderExtended.cpp:558:41
#1 0x55ad36871f40 in FAssetLoader::createPrimitives(...)
AssetLoader.cpp:705:44
#2 0x55ad36871f40 in FAssetLoader::recursePrimitives(...)
AssetLoader.cpp:570:9
#3 0x55ad3686ac3f in FAssetLoader::createRootAsset(...)
AssetLoader.cpp:539:9
#4 0x55ad3686ac3f in FAssetLoader::createInstancedAsset(...)
AssetLoader.cpp:409:30
#5 0x55ad36868f5c in FAssetLoader::createAsset(...)
AssetLoader.cpp:373:12
#6 0x55ad36545abc in main
poc.cpp:97:36
0x7be3717e5754 is located 32 bytes after 4-byte region [0x7be3717e5730,0x7be3717e5734)
SUMMARY: AddressSanitizer: heap-buffer-overflow
AssetLoaderExtended.cpp:558:41 in
filament::gltfio::AssetLoaderExtended::createPrimitiveBuild configuration
- Clang with
-fsanitize=address -fno-omit-frame-pointer - Release build (assert_invariant compiled out)
- NOOP backend (no GPU required)
- x86_64 Linux
Suggested fix
Replace morphTargets.size() with targetsCount at line 543 of AssetLoaderExtended.cpp:
- out->slotIndices.resize(morphTargets.size());
+ out->slotIndices.resize(targetsCount);This matches the existing correct behavior in the non-extended path at AssetLoader.cpp:1304.
A fix was submitted in #10499 but was auto-closed by the gltfio contribution policy bot.
PoC GLB generator and loader source available on request.
Source: google/filament