diff --git a/CHANGELOG.md b/CHANGELOG.md index b96f654..b2ae832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,23 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed +- A serialized tree object larger than 4 GiB made `index_tree` silently + wrap its uint32 entry offsets past the 4 GiB mark back into the first + 4 GiB (security review 2026-11, finding ②): readdir listings were + silently truncated or showed duplicated/wrong entries past the wrap + point, and `tree_find` could resolve a lookup to the wrong entry — + deterministically constructible by an attacker-authored object store. + No out-of-bounds access was possible (`entry_at` bounds-checks every + step); real git cannot produce such a tree (a single directory would + need 100M+ entries), so this only hardens against hand-crafted loose + or non-delta pack objects. Such trees are now rejected as malformed + up front (EIO, with a warning naming the object), and `tree_locked` + now actually logs the warning for every malformed tree that the + source comments had promised. + ## [0.0.4] - 2026-09-26 ### Fixed diff --git a/src/gitmount.cpp b/src/gitmount.cpp index e750f38..b1ba06b 100644 --- a/src/gitmount.cpp +++ b/src/gitmount.cpp @@ -665,7 +665,8 @@ std::shared_ptr Gitmount::tree_locked(const git_oid& tre if (rc == 0) { auto idx = rawobj::index_tree(buf.data(), buf.size(), repo_->oid_type()); if (!idx) { - bad = true; // corrupt/hand-crafted garbage + bad = true; // corrupt/hand-crafted garbage (or an unindexable >4 GiB tree) + log::warn("tree object %s is malformed (unindexable); treated as unreadable", key.c_str()); } else { auto td = std::make_shared(); td->raw = std::move(buf); diff --git a/src/rawobj.cpp b/src/rawobj.cpp index f2f035c..14df942 100644 --- a/src/rawobj.cpp +++ b/src/rawobj.cpp @@ -35,7 +35,11 @@ bool oid_from_hex(const char* hex, std::size_t len, git_oid_t oid_type, git_oid* } // namespace -std::optional index_tree(const char* data, std::size_t len, git_oid_t oid_type) { +std::optional index_tree(const char* data, std::size_t len, git_oid_t oid_type, + std::size_t max_len) { + // Offset wraparound guard: offsets are uint32, so a tree larger than + // max_len (UINT32_MAX in production) is not representable in the index. + if (len > max_len) return std::nullopt; const std::size_t oid_raw = oid_type == GIT_OID_SHA1 ? 20 : 32; TreeIndex offsets; const char* p = data; diff --git a/src/rawobj.hpp b/src/rawobj.hpp index 7d34a26..093fc17 100644 --- a/src/rawobj.hpp +++ b/src/rawobj.hpp @@ -47,10 +47,18 @@ struct TreeData { std::size_t cost() const { return raw.size() + offsets.size() * sizeof(std::uint32_t); } }; +// TreeIndex offsets are uint32; a serialized tree longer than this cannot +// be indexed without wrapping (an offset past 4 GiB would alias an early +// entry), so such trees are rejected as malformed. +constexpr std::size_t kMaxIndexableTree = UINT32_MAX; + // Build the offset index over a serialized tree. Returns nullopt on a -// malformed buffer (bad mode, missing NUL, truncated oid) — callers map -// that to ENOENT plus a warning, matching the previous libgit2 behavior. -std::optional index_tree(const char* data, std::size_t len, git_oid_t oid_type); +// malformed buffer (bad mode, missing NUL, truncated oid, or larger than +// max_len) — callers map that to ENOENT plus a warning, matching the +// previous libgit2 behavior. `max_len` is a test seam for injecting a +// small indexable cap; production callers use the UINT32_MAX default. +std::optional index_tree(const char* data, std::size_t len, git_oid_t oid_type, + std::size_t max_len = kMaxIndexableTree); // Parse the entry at `off` inside a tree buffer previously validated by // index_tree. Returns nullopt if `off` is out of range. diff --git a/tests/unit/test_rawobj.cpp b/tests/unit/test_rawobj.cpp index b9bc3a4..d89da77 100644 --- a/tests/unit/test_rawobj.cpp +++ b/tests/unit/test_rawobj.cpp @@ -88,6 +88,12 @@ TEST_CASE("index_tree rejects malformed buffers", "[rawobj]") { std::string nonul = "100644 abcdef"; nonul.append(20, '\x01'); CHECK_FALSE(index_tree(nonul.data(), nonul.size(), GIT_OID_SHA1).has_value()); + // over the indexable cap (threshold injection; production default is + // UINT32_MAX, where a >4 GiB tree would wrap the uint32 offsets) + std::string ok; + append_entry(&ok, "100644", "a.txt", oid_of(kOid1)); + CHECK(index_tree(ok.data(), ok.size(), GIT_OID_SHA1, ok.size()).has_value()); + CHECK_FALSE(index_tree(ok.data(), ok.size(), GIT_OID_SHA1, ok.size() - 1).has_value()); } TEST_CASE("tree_find: exact match, miss, and mode dispatch", "[rawobj]") {