Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Harden file loaders against malformed input (found by fuzzing)
islandmeda0a99d9eecb1
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cppindex 2a9d5f14..755c9399 100644--- a/src/core/Mesh.cpp+++ b/src/core/Mesh.cpp@@ -244,8 +244,15 @@ Mesh weldImported(const Mesh& m, const std::vector<SculptLayer>& layers) {unique.reserve(m.positions.size());std::vector<uint32_t> remap(m.positions.size());std::vector<uint32_t> kept;+ // Quantise relative to the box corner and clamp, so far-off, infinite or NaN coordinates from a malformed file+ // never overflow the int conversion.+ const glm::vec3 origin = box.valid() ? box.min : glm::vec3(0.0f);+ auto quantise = [&](float v) {+ return std::isfinite(v) ? int(std::clamp(std::round(v / tol), -2.0e9f, 2.0e9f)) : 0;+ };for (uint32_t v = 0; v < m.positions.size(); ++v) {- glm::ivec3 key(glm::round(m.positions[v] / tol));+ const glm::vec3 d = m.positions[v] - origin;+ const glm::ivec3 key(quantise(d.x), quantise(d.y), quantise(d.z));auto [it, inserted] = unique.try_emplace(key, uint32_t(kept.size()));if (inserted) kept.push_back(v);remap[v] = it->second;@@ -312,7 +319,12 @@ void transferVertexAttributes(const Mesh& src, Mesh& dst) {const float longest = std::max({ext.x, ext.y, ext.z, 1e-12f});const float cell = std::max(longest / std::max(1.0f, std::cbrt(float(src.positions.size()))), longest * 1e-4f);constexpr int kOffset = 1 << 20;- auto cellOf = [&](const glm::vec3& p) { return glm::ivec3(glm::floor((p - box.min) / cell)); };+ // Clamped so NaN, infinite or huge coordinates from a malformed file never overflow the int cast.+ auto cellOf = [&](const glm::vec3& p) {+ const glm::vec3 f = glm::floor((p - box.min) / cell);+ auto axis = [](float v) { return std::isfinite(v) ? int(std::clamp(v, -float(kOffset), float(kOffset))) : 0; };+ return glm::ivec3(axis(f.x), axis(f.y), axis(f.z));+ };auto key = [](const glm::ivec3& c) {return (uint64_t(uint32_t(c.x + kOffset) & 0x1fffffu) << 42) | (uint64_t(uint32_t(c.y + kOffset) & 0x1fffffu) << 21) |uint64_t(uint32_t(c.z + kOffset) & 0x1fffffu);diff --git a/src/io/PlyIO.cpp b/src/io/PlyIO.cppindex dcf44052..def77cf5 100644--- a/src/io/PlyIO.cpp+++ b/src/io/PlyIO.cpp@@ -212,6 +212,21 @@ bool loadPly(const std::string& path, Mesh& mesh, SplatCloud& splats, PlyContentconst bool rgbIsByte = rgb[0] >= 0 && typeSize(vertex->props[size_t(rgb[0])].type) == 1;Body body{file.data() + bodyStart, file.size() - bodyStart, 0, format == "ascii", format == "binary_big_endian"};+ // A malformed header can claim billions of rows: check the counts against the bytes actually present before+ // allocating anything (every binary row needs its fixed-size fields; every ASCII value at least two characters).+ {+ size_t needed = 0;+ for (const auto& e : elements) {+ size_t rowBytes = 0;+ for (const auto& prop : e.props) rowBytes += body.ascii ? 2 : typeSize(prop.list ? prop.countType : prop.type);+ rowBytes = std::max<size_t>(rowBytes, 1);+ if (e.count > (body.size - std::min(needed, body.size)) / rowBytes) {+ error = "The PLY header describes more data than the file contains";+ return false;+ }+ needed += e.count * rowBytes;+ }+ }Mesh m;SplatCloud cloud;std::vector<double> row;@@ -233,15 +248,21 @@ bool loadPly(const std::string& path, Mesh& mesh, SplatCloud& splats, PlyContentrow[p] = body.read(prop.type);continue;}- const size_t n = size_t(body.read(prop.countType));- if (!body.ok || n > 1'000'000) {+ const double count = body.read(prop.countType);+ if (!body.ok || !(count >= 0.0 && count <= 1'000'000.0)) {error = "Corrupt PLY list";return false;}+ const size_t n = size_t(count);const bool wanted = isFace && (prop.name == "vertex_indices" || prop.name == "vertex_index");for (size_t k = 0; k < n; ++k) {const double v = body.read(prop.type);- if (wanted) poly.push_back(uint32_t(v));+ if (!wanted) continue;+ if (!(v >= 0.0 && v < 4294967295.0)) {+ error = "PLY face index out of range";+ return false;+ }+ poly.push_back(uint32_t(v));}}if (!body.ok) {diff --git a/tests/test_malformed_files.cpp b/tests/test_malformed_files.cppnew file mode 100644index 00000000..781f4f8d--- /dev/null+++ b/tests/test_malformed_files.cpp@@ -0,0 +1,80 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// Malformed and hostile input files found by fuzzing the loaders: each must be+// rejected with an error (or loaded safely), never crash or allocate wildly.+#include "TestHarness.h"++#include "core/Mesh.h"+#include "core/Splats.h"+#include "io/ObjIO.h"+#include "io/PlyIO.h"++#include <cmath>+#include <filesystem>+#include <fstream>+#include <string>++using namespace sf;++namespace {++std::string writeFile(const char* name, const std::string& bytes) {+ const std::string path = tempPath(name);+ std::ofstream(path, std::ios::binary).write(bytes.data(), std::streamsize(bytes.size()));+ return path;+}++bool plyLoads(const std::string& text, std::string& err) {+ const std::string path = writeFile("sculptforge_malformed.ply", text);+ Mesh m;+ SplatCloud s;+ PlyContent c;+ const bool ok = loadPly(path, m, s, c, err);+ std::filesystem::remove(path);+ return ok;+}++} // namespace++TEST(ply_header_claiming_billions_of_rows_is_rejected) {+ // A splat header promising 3 billion rows in a file of a few hundred bytes used to allocate before reading.+ std::string err;+ CHECK(!plyLoads("ply\nformat binary_little_endian 1.0\nelement vertex 3000000000\nproperty float x\nproperty float y\n"+ "property float z\nproperty float scale_0\nproperty float rot_0\nproperty float opacity\nend_header\n"+ "0123456789abcdef",+ err));+ CHECK(err.find("more data than the file contains") != std::string::npos);+ CHECK(!plyLoads("ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\n"+ "element face 2000000000\nproperty list uchar int vertex_indices\nend_header\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n",+ err));+}++TEST(ply_negative_or_huge_face_values_are_rejected) {+ std::string err;+ const std::string head = "ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\n"+ "element face 1\nproperty list int float vertex_indices\nend_header\n0 0 0\n1 0 0\n0 1 0\n";+ CHECK(!plyLoads(head + "-3 0 1 2\n", err)); // negative list count+ CHECK(!plyLoads(head + "3 0 -65 2\n", err)); // negative index+ CHECK(!plyLoads(head + "3 0 1 7.8e34\n", err)); // index beyond uint32+ CHECK(plyLoads(head + "3 0 1 2\n", err)); // the well-formed file still loads+}++TEST(non_finite_coordinates_do_not_break_import) {+ // NaN and infinite positions reach the attribute-transfer grid during welding; they must not be cast to int.+ const std::string path = writeFile("sculptforge_malformed.obj",+ "v nan 0 0 1 0 0\nv 0 inf 0 0 1 0\nv 0 0 1e38 0 0 1\nv 1 1 1 1 1 1\nf 1 2 3\nf 2 3 4\n");+ Mesh m;+ std::string err;+ loadObj(path, m, err); // either outcome is fine; it must not crash or trip the sanitizers+ std::filesystem::remove(path);+ CHECK(true);+}++TEST(weld_handles_far_off_and_non_finite_positions) {+ Mesh m;+ m.positions = {{1e30f, 0, 0}, {0, 1e30f, 0}, {0, 0, std::nanf("")}, {1, 2, 3}, {1, 2, 3}};+ m.triangles = {{0, 1, 2}, {1, 3, 4}};+ const Mesh w = weldImported(m);+ CHECK(w.positions.size() <= m.positions.size()); // no crash or UB; duplicates may merge+}