Skip to content

fix(rawobj): 拒绝超过 4 GiB 的 tree,杜绝 uint32 偏移索引回绕 - #9

Closed
OrbitZore wants to merge 1 commit into
mainfrom
fix/tree-offset-uint32-overflow
Closed

OrbitZore wants to merge 1 commit into
mainfrom
fix/tree-offset-uint32-overflow

Conversation

@OrbitZore

Copy link
Copy Markdown
Owner

问题(#5)

index_tree 把每个条目的起始偏移截断为 uint32 存入 TreeIndex(std::vector<std::uint32_t>)。序列化 tree 超过 UINT32_MAX 字节时,截断不被察觉、循环正常成功,后果全部静默:

  • 4 GiB 之后的条目偏移回绕指向前段字节:readdir 从该点静默截断(gitmount.cpp 空 return)或列出重复/错误条目;
  • tree_find 二分路径在无序 offsets 上运行,可能返回错误结果(含错误 oid → 错误文件内容);
  • 偏移恰好与先前某条目重合(T_j − 2^32 = T_i)是攻击者可确定性构造的,不是概率事件。

非内存安全问题(与安全审查结论一致):entry_at 全链路有界,回绕后不会越界读;真实 git 无法产生如此大的树(单目录需约 1 亿+ 条目),只有手工构造的 loose / 非-delta pack 对象可达。本修复把"静默错误"变成"响亮失败",属手工对象存储的加固。

实现

  • src/rawobj.hpp / src/rawobj.cpp:index_tree 开头增加守卫 if (len > max_len) return std::nullopt;(偏移回绕防护:偏移为 uint32,>4 GiB 的树在偏移索引中不可表示)。上限为默认参数测试缝,生产默认 kMaxIndexableTree = UINT32_MAX 不变;拒绝后走既有 malformed 路径(bad = true → walk_tree 返回 EIO,对不可表示的树是唯一诚实语义)。
  • src/gitmount.cpp:tree_locked 的 bad 分支补一行 log::warn(含对象 oid)——兑现 rawobj.hpp 注释承诺、但此前缺失的 warning。
  • tests/unit/test_rawobj.cpp:"index_tree rejects malformed buffers" 用例追加小阈值注入断言:max_len = len 接受、max_len = len − 1 拒绝;生产默认不受影响。
  • CHANGELOG.md:Unreleased 记录(非越界、真实仓库不可达、属加固)。

测试结果

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DGITMOUNT_WERROR=ON
cmake --build build -j$(nproc)
ctest --test-dir build --output-on-failure

unit 与 integration 全绿(2/2 Passed);改动文件已过 clang-format 18.1.8。

风险

  • CHANGELOG 的 Unreleased 节可能与其他并行 PR 冲突,合并时取并集;
  • 行为变化仅影响 >4 GiB 的 tree:此前静默错误内容,现在 EIO + warning,对真实仓库零影响。
English summary

index_tree stores entry offsets as uint32; a serialized tree larger than 4 GiB (hand-crafted loose or non-delta pack object only — real git cannot produce one) silently wraps offsets past the 4 GiB mark back into the first 4 GiB, yielding truncated/duplicated readdir listings and wrong lookup results. No out-of-bounds access is possible (entry_at bounds-checks everything); this is silent-wrong-content hardening. Fix: reject len > UINT32_MAX up front as malformed (EIO), with the threshold as a default parameter for small-threshold unit-test injection; also add the missing log::warn for malformed trees in tree_locked that the source comments had promised.

Closes #5

修复 #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 记录(非越界、真实仓库不可达、属手工对象
  存储加固)。
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in 14 days. It will be closed in 7 days if no further activity occurs.

@github-actions github-actions Bot added the Stale label Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Closed automatically due to inactivity. Feel free to reopen.

@github-actions github-actions Bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

index_tree 对 >4GiB tree 的 uint32 偏移回绕——静默错误条目/静默截断(非越界)

1 participant