From d9992cd1941ca7a4f14a2b0ec866ca32cdc3d18c Mon Sep 17 00:00:00 2001 From: OrbitZore Date: Mon, 2 Nov 2026 02:33:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(rawobj):=20=E6=8B=92=E7=BB=9D=E8=B6=85?= =?UTF-8?q?=E8=BF=87=204=20GiB=20=E7=9A=84=20tree,=E6=9D=9C=E7=BB=9D=20uin?= =?UTF-8?q?t32=20=E5=81=8F=E7=A7=BB=E7=B4=A2=E5=BC=95=E5=9B=9E=E7=BB=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 #5:序列化 tree 超过 UINT32_MAX 字节时,index_tree 把条目起始 偏移截断为 uint32 存入 TreeIndex,截断本身不被察觉——4 GiB 之后的 条目偏移回绕指向前段字节,导致 readdir 静默截断或列出重复/错误条目, tree_find 二分路径在无序 offsets 上运行可能返回错误结果(含错误 oid)。偏移恰好与先前条目重合(T_j − 2^32 = T_i)是攻击者可确定性 构造的,不是概率事件。 非内存安全问题:entry_at 全链路有界(off >= len 拒绝、mode 前缀解析 限界、memchr 定界、oid 截断检查),回绕后不会越界读,属"静默错误 内容"类解析健壮性缺陷;真实 git 无法产生如此大的树(单目录需约 1 亿+ 条目,git 自身 checkout/status 早已不可用),只有手工构造的 loose / 非-delta pack 对象可达。 修复内容: - rawobj:index_tree 开头以 len > max_len 拒绝为 malformed(偏移 回绕防护);上限为默认参数测试缝,生产默认 kMaxIndexableTree = UINT32_MAX 不变,映射到既有 bad = true → EIO 路径; - gitmount:tree_locked 的 bad 分支补上 rawobj.hpp 注释承诺、但 此前缺失的 log::warn(含对象 oid); - 单测:index_tree rejects malformed buffers 追加小阈值注入断言 (max_len = len 接受、max_len = len - 1 拒绝),生产默认不受影响; - CHANGELOG:Unreleased 记录(非越界、真实仓库不可达、属手工对象 存储加固)。 --- CHANGELOG.md | 17 +++++++++++++++++ src/gitmount.cpp | 3 ++- src/rawobj.cpp | 6 +++++- src/rawobj.hpp | 14 +++++++++++--- tests/unit/test_rawobj.cpp | 6 ++++++ 5 files changed, 41 insertions(+), 5 deletions(-) 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]") {