Skip to content

Bound glTF point cloud and mesh builder writes - #1277

Open
makstrogonpl wants to merge 1 commit into
google:mainfrom
makstrogonpl:gltf-point-cloud-builder-bounds
Open

makstrogonpl wants to merge 1 commit into
google:mainfrom
makstrogonpl:gltf-point-cloud-builder-bounds

Conversation

@makstrogonpl

Copy link
Copy Markdown

Summary

  • glTF point cloud builders are started with one point per index but written at the point given by the index value. An index value >= the number of indices writes past the attribute buffer (heap buffer overflow write).
  • Valid glTF 2.0 files trigger it: point primitives with sparse indices, and skinned or morph-target primitives that use the upper part of a vertex buffer.
  • Reachable from GltfDecoder::DecodeFromFileToScene (draco_transcoder) and DecodeFromFile.
  • Fix: bounds check on the builder writes that take their position from the file, correct builder size for skinned and morph primitives, point primitives stored in index order. Adds two regression tests and three test files.
  • Files: src/draco/io/gltf_decoder.cc, src/draco/io/gltf_decoder.h, src/draco/mesh/triangle_soup_mesh_builder.h, src/draco/point_cloud/point_cloud_builder.h, src/draco/io/gltf_decoder_test.cc.

Problem

When the glTF decoder builds a point cloud, it starts the builder with one point per index but writes each value at the point given by the index value:

// GltfDecoder::SetValuesForBuilder(..., PointCloudBuilder* builder)
const uint32_t v_id = indices_data[i];
const PointIndex pi(v_id + next_point_id_);   // builder holds indices_data.size() points
builder->SetAttributeValueForPoint(att_id, pi, ...);

Whenever an index value is greater than or equal to the number of indices, the write lands past the end of the attribute buffer.

This needs no malformed input. The following files are valid glTF 2.0 (they pass the Khronos validator) and overflow the heap:

  • a POINTS primitive with indices [5, 7] into 8 vertices;
  • a TRIANGLES primitive with JOINTS_0/WEIGHTS_0 or with morph targets, whose triangle uses the last three vertices of a 24-vertex buffer. Such primitives are decoded through the point cloud builder to keep vertex ids.

It happens whenever the largest index of such a primitive is not smaller than its number of indices, for example when several primitives share one vertex buffer and a small primitive uses its upper part.

Consequences

Heap buffer overflow write in PointCloudBuilder::SetAttributeValueForPoint, reachable from GltfDecoder::DecodeFromFileToScene (draco_transcoder) and DecodeFromFile (draco_encoder). The written data comes from the file's vertex attributes.

  • With the small test files in this pull request a release build does not crash: the write lands in neighboring heap memory and the tool reports success. draco_transcoder from the Debian 13 package (1.5.6) behaves this way for the point and skinned files.
  • A larger point model of the same kind (indices far above the index count) kills the Debian 13 packaged draco_transcoder with SIGSEGV.

I am aware SECURITY.md says the glTF path is not hardened for untrusted input. This one is hit by valid files, so it is also a correctness bug: such models cannot be transcoded reliably today.

Reproduce on main (d6c1acf)

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DDRACO_TRANSCODER_SUPPORTED=ON -DCMAKE_CXX_FLAGS="-fsanitize=address -g"
cmake --build build --target draco_transcoder
./build/draco_transcoder -i testdata/points_sparse_indices.gltf -o out.glb
./build/draco_transcoder -i testdata/skinned_triangle_high_indices.gltf -o out.glb
./build/draco_transcoder -i testdata/morph_triangle_high_indices.gltf -o out.glb
Sanitizer output (point model)
ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 12
    #1 draco::DataBuffer::Write(long long, void const*, unsigned long) data_buffer.h:54
    #2 draco::PointCloudBuilder::SetAttributeValueForPoint(...)
    #3 draco::GltfDecoder::SetValuesForBuilder<draco::VectorD<float, 3>>(...)
    #4 draco::GltfDecoder::AddTransformedDataToBuilder<draco::PointCloudBuilder>(...)
    #5 draco::GltfDecoder::AddAttributeValuesToBuilder<draco::PointCloudBuilder>(...)
    #6 draco::GltfDecoder::AddAttributesAndBuildMesh(...)
is located 36 bytes after 24-byte region

Fix

  1. Builder writes that take their position from the file are range checked. SetValuesForBuilder (point cloud), SetValuesPerFace and both material writers refuse to write outside the points or faces the builder was started with. This adds two const accessors, PointCloudBuilder::num_points() and TriangleSoupMeshBuilder::num_faces().
  2. Plain point primitives are stored in index order: point i gets the value of vertex indices[i]. This matches how the builder is sized and how the material writer already numbers points.
  3. Primitives with joints or morph targets keep vertex ids (new member keep_vertex_ids_). Their point clouds are started with at least one point per index and up to max(index) + 1 points, limited by the smallest accessor count that is backed by buffer data, and not raised at all when an attribute that will be decoded has no valid data. A claimed count therefore cannot enlarge the allocation.

Behavior change

Point primitives with indices that are not 0..n-1 now produce their points in index order, without unreferenced vertices. Before, they were written in vertex order and overflowed when an index value exceeded the index count. Files without indices, or with indices 0..n-1, produce byte-identical output.

Verification

Check Result
New tests TestPointPrimitiveWithSparseIndices, TestSkinAndMorphPrimitivesWithHighIndices heap-buffer-overflow on main, pass with the change
Unit tests (ASan + UBSan), macOS/clang, this change alone all pass
Unit tests (ASan + UBSan), Linux/GCC 13, together with the related PLY and glTF changes (separate pull requests) all pass
Windows build (MinGW GCC 14, tests run under Wine) both new tests fail or crash on main, and draco_transcoder.exe writes a wrong-sized file for the morph model without reporting an error; with the change the tests pass and the three models convert to the same sizes as on Linux
80 variants (points / morph / skin, boundary index values, several primitives, mismatched attribute counts) 16 memory errors on main, 0 with the change, no valid variant rejected
400 generated valid skinned and morph models 191 sanitizer errors on main; 0 with the change, vertex ids, faces and morph data correct in all 400
Peak memory on files that are rejected (4.8 MB input) 36 MB before and after
Every model under testdata/ through draco_encoder and draco_transcoder byte-identical output before and after

Performance

Input, draco_transcoder main With this change
DragonAttenuation.glb (6.4 MB) 221.8 ms 213.7 ms
SheenCloth.glb (3.9 MB) 101.1 ms 100.5 ms
CesiumMan.glb (479 KB, skinned) 14.1 ms 14.5 ms
Duck.glb (117 KB) 6.3 ms 6.4 ms

Release build, macOS/clang, median of 25 runs, this change alone against main. Output files are byte-identical.

The variant numbers were measured together with the companion change for glTF input checks.

New test files: testdata/points_sparse_indices.gltf, testdata/skinned_triangle_high_indices.gltf, testdata/morph_triangle_high_indices.gltf.

Does not overlap with #1247 or #1262.

Point cloud builders were started with one point per index, but values
were written at the point given by each index value. Valid glTF files
with point primitives, morph targets or joints whose index values are
greater than or equal to the number of indices wrote past the end of
the attribute buffers.

* Check every builder write against the number of faces or points the
  builder was started with (SetValuesForBuilder, SetValuesPerFace and
  the material writers). Adds PointCloudBuilder::num_points() and
  TriangleSoupMeshBuilder::num_faces().
* Store plain point primitives in the order given by the indices, one
  point per index.
* Primitives with morph targets or joints keep the input vertex ids.
  Their point clouds are sized to hold the largest referenced vertex,
  limited by the accessor counts that are backed by buffer data.

Adds three test files and two unit tests.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant