Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/gitmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,8 @@ std::shared_ptr<const rawobj::TreeData> 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<rawobj::TreeData>();
td->raw = std::move(buf);
Expand Down
6 changes: 5 additions & 1 deletion src/rawobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeIndex> index_tree(const char* data, std::size_t len, git_oid_t oid_type) {
std::optional<TreeIndex> 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;
Expand Down
14 changes: 11 additions & 3 deletions src/rawobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeIndex> 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<TreeIndex> 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.
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_rawobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]") {
Expand Down
Loading