Skip to content

fix(sandbox): 宿主 resolv.conf 软链进 /run 时沙盒内恢复 DNS - #2534

Merged
deepcoldy merged 2 commits into
masterfrom
fix/sandbox-resolv-run-symlink
Oct 29, 2026
Merged

deepcoldy merged 2 commits into
masterfrom
fix/sandbox-resolv-run-symlink

Conversation

@deepcoldy

@deepcoldy deepcoldy commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

改了什么

沙盒任务②(网络策略 / DNS)的第一部分:不配网络策略的默认路径下,宿主 /etc/resolv.conf 是指向 /run 的软链时,沙盒里没有 DNS。

  • 现象(用户反馈):「终端能上网、机器人会话不能」「getaddrinfo EAI_AGAIN」。
  • 原因:systemd-resolved(/etc/resolv.conf → ../run/systemd/resolve/stub-resolv.conf)、resolvconf、NetworkManager 都把 /etc/resolv.conf 做成指向 /run 的软链;oncall(compileToBwrap)和 scratch 沙盒都在 /run 挂一个新的 tmpfs,软链在沙盒里悬空。原来只有配了网络策略的分支处理了 /run,默认路径没处理。
  • 修法:src/adapters/backend/sandbox.ts 新增 shadowedResolverPath / hostResolverBindArgs:沿软链逐跳解析(会跟随 /var/run → /run 这类父目录软链),找到第一个落在 /run 下的路径(沙盒内查找就停在这里),把宿主的真实 resolver 文件只读 bind 到那个位置。
    • oncall(prepareDirectSandbox,不配策略时)和 scratch(prepareScratchSandbox)都调用。
    • 只 bind 这一个文件,不 bind /run/systemd/resolve 整个目录(目录里有 resolved 的 Unix socket)。
    • 运营方在 sandboxPaths.deny / scratch denyPaths 里禁了 /etc/resolv.conf 时不 bind。worker 会把策略路径规范化,所以 deny /etc/resolv.conf 实际会变成 /run/... 目标路径,链上三个名字(入口、第一跳、真实文件)任一被 deny 都不 bind。
  • 配了网络策略的分支:自定义 resolv.conf 原来 bind 在 realpath(/etc/resolv.conf),多跳链接(/etc/resolv.conf → /run/a → /run/b)时沙盒停在 /run/a 仍然失效;改为 bind 在同一个「第一跳」,单跳与普通文件时结果和原来一样。

取舍

  • bind 文件会钉住 inode:宿主中途用 rename 替换这个文件时,正在跑的会话看到旧内容,新会话正常。systemd-resolved 的 stub 文件内容固定是 nameserver 127.0.0.53,不受影响;会频繁换网的笔记本才可能碰到。选这个而不是 bind 整个目录,是为了不把 resolved 的 Unix socket 暴露进沙盒。
  • 如果宿主 nsswitch 用 nss-resolve(走 /run/systemd/resolve/io.systemd.Resolve socket),沙盒里仍然拿不到 socket,会回落到 dns(常见发行版默认配置都有 dns 兜底)。

影响面

  • 只改沙盒参数拼装:没开沙盒的 bot 完全不经过这段代码。
  • /etc/resolv.conf 是普通文件(本机就是)时不加任何参数,argv 与改前完全一致。
  • 影响 Linux 上的 oncall(PTY / tmux 等本地后端都走 prepareDirectSandbox)与 scratch;macOS Seatbelt 不涉及(它没有 /run tmpfs)。
  • 改到了 scratch-sandbox.ts(属于沙盒任务① scratch 的范围),只加了一行调用。
  • 未处理:v3 workflow 的 distillation runner(src/workflows/v3/distillation-runner.ts)同样挂 /run tmpfs 并把 /etc 换成快照,是另一套拼装,不在本 PR 范围。
  • 复审追加修复(fb68d736a):链路穿过一个本身位于 /run 内的符号链接目录时(/etc/resolv.conf → /run/dnsproxy/resolv.conf,/run/dnsproxy → /run/sub/dnsproxy),逐跳解析原先把父目录 realpath 成宿主 canonical 落点,但沙盒的 /run 是全新 tmpfs、内核按字面路径停在 tmpfs 里不存在的名字,bind 会落空且沙盒照常启动、只是 DNS 全死。现在链路已字面位于 shadowed root 下时不再 realpath 父目录,直接 bind 到字面跳点;网络策略分支调用同一函数,同一形状一并修好。
  • 明确不覆盖:链接文本指向 /var/run/... 时(如个别机器 /etc/resolv.conf → /var/run/NetworkManager/resolv.conf),oncall 的根是空 tmpfs、里面没有 /var/run → /run 这个符号链接,链在第一跳就断,bind 到 /run/... 救不了;scratch 的根是宿主合并树(/var/run 真实存在)不受影响。这类「链接文本逃出已绑定树」的形状留待后续,本 PR 不承诺。

测试验证

新增 test/sandbox-resolver-bind.test.ts(15 例):

  • 纯函数:单跳相对链接、多跳链停在第一跳、/var/run → /run 父目录软链、/run 内的目录本身是符号链接时返回字面跳点(不 realpath 父目录)、普通文件 / 链外 / 悬空 / 循环不加参数、链上任一名字被 deny 不加参数、/runtime 不被当成 /run。
  • 端到端:用仓库已有的 test/helpers/private-mount-ns.ts 起私有 mount + PID 命名空间,在里面用 overlay 把 /etc、/run 改成 systemd-resolved 的形状(宿主文件不被改动,挂载随命名空间消失),真起 oncall bwrap 和 scratch 容器读 /etc/resolv.conf;以及 deny /etc/resolv.conf、deny 规范化后的 /run/... 两种写法都仍然生效。复审追加 3 个端到端例:oncall / scratch 对「/run 内符号链接目录」形状读到宿主 resolver,以及网络策略分支经真实 sandbox-network-runner 在该形状读到自定义 DNS 文件(node 走 build 后的 dist、bun 走 .ts 源,因为 runner 的 --inside 自重入固定解析 .js 入口)。
npx vitest run test/sandbox-resolver-bind.test.ts   → 15 passed
bun test test/sandbox-resolver-bind.test.ts         → 15 pass
bun run build                                       → 通过
isolate-run -- npx vitest run test/sandbox.test.ts test/scratch-sandbox-unit.test.ts test/sandbox-network-linux.test.ts \
  test/fs-policy.test.ts test/fs-policy-bwrap.e2e.test.ts test/scratch-submount-isolation.test.ts test/sandbox-mask-manifest.test.ts \
  test/cli-identity-symlinked-home.test.ts test/sandbox-session-data-dir.test.ts test/test-hygiene.test.ts → 247 passed / 55 skipped

注:这 5 个真实 bwrap 用例依赖 root + setpriv/unshare 私有命名空间,CI 的非 root test / bun-test 分片结构性 skip 它们,真实行为证据来自 root 本机;网络策略例还要求 node 下存在 build 后的 dist(bun run build)。

skipped 的是本机 isolate-run 里起不了嵌套 --proc 的 bwrap 用例;在 unshare --mount-proc 下再跑这几个文件,sandbox-session-data-dir 有 6 例失败,换回 master 的源码同样 6 例失败(node:internal/modules/package_json_reader,是本机 /tmp/package.json 环境问题),和本改动无关。sandbox-network-linux 的真网络用例需要 CI 的 sandbox-network.yml 环境(Ubuntu runner 本身就是 systemd-resolved,会走到新代码)。

真实 DNS 探针(isolate-run 内私有命名空间,/etc/resolv.conf → ../run/systemd/resolve/stub-resolv.conf,stub 内容拷自宿主):

沙盒内 cat /etc/resolv.conf 沙盒内 getent hosts github.com
改前 No such file or directory exit 2
改后 宿主 nameserver 20.205.243.166 github.com,exit 0

变异(每条单独施加后跑本文件,均变红):

变异 变红用例
oncall 不 bind oncall reads the host resolver file
scratch 不 bind scratch reads the host resolver file
去掉 deny 检查 纯函数 deny、oncall deny /run/...、scratch deny
deny 只看 /etc/resolv.conf 字面 同上三例
不跟随父目录软链 /var/run → /run
/run 前缀不带 / /runtime 用例
bind 到真实文件而非第一跳 多跳链、deny 名字用例
进入 /run 后仍 realpath 父目录(回退复审修复) /run 内目录软链纯函数例、oncall / scratch / 网络策略三个 linkdir 端到端例(vitest + bun 两引擎各 4 例)

后续 PR(同一任务):网络策略保存时的 DNS / 代理提示、错误码文案、运行时报错中文化、文档。

🤖 Generated with Claude Code

systemd-resolved / resolvconf / NetworkManager 把 /etc/resolv.conf 做成指向
/run 的软链,而 oncall 与 scratch 沙盒都在 /run 挂新的 tmpfs,链接在沙盒里
悬空,表现为宿主终端能上网、机器人会话 getaddrinfo EAI_AGAIN。

不配网络策略时,沿软链逐跳找到第一个落在 /run 下的路径,把宿主的真实
resolver 文件只读 bind 到那里;只 bind 这一个文件,不暴露所在目录。运营方
deny 了 /etc/resolv.conf(含规范化后的 /run 路径)时不 bind。配了网络策略时
自定义 resolv.conf 也改为 bind 到同一跳,修正多跳链接下的失效。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
shadowedResolverPath 原先每跳都 realpath 父目录;当链路穿过一个本身位于
/run 内的符号链接目录(/etc/resolv.conf → /run/dnsproxy/resolv.conf,
/run/dnsproxy → /run/sub/dnsproxy)时返回宿主 canonical 落点,但沙盒的
/run 是全新 tmpfs,内核按字面路径解析停在 tmpfs 中不存在的名字,bind
落空且沙盒照常起来、只是 DNS 全死——正是本系列要消灭的静默失败形状。

修法:current 已字面位于 shadowed root 下时跳过 realpath 父目录,直接
返回字面跳点;/var/run → /run 这类 shadowed root 之外的父目录软链仍在
宿主解析(沙盒复制了那一个)。网络策略分支调用同一个函数,同一修法同时
覆盖其自定义 DNS 文件的 bind 点。

测试新增 4 例(11 → 15):纯函数「/run 内目录软链返回字面跳点」、
oncall / scratch 在私有 mount+PID 命名空间内对该形状的真 bwrap 端到端
例,以及网络策略分支经真实 sandbox-network-runner 的端到端例(node 走
build 后的 dist、bun 走 .ts 源,两引擎 15/15;该修法的反向变异在两引擎
各咬红这 4 例,原 11 例全绿)。函数注释收窄到实际承诺范围:链接文本
逃出已绑定树(oncall 空 tmpfs 根下没有 /var/run 软链,
/etc/resolv.conf → /var/run/… 在第一跳就断)不在覆盖内,在 PR 描述注明。

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner Author

已按复审意见把一个边界修复随本 PR 提交(commit fb68d736a,追加在原 commit 之上,未改动原有提交):

修复:shadowedResolverPath 原先每跳都 realpath 父目录。当 resolver 链穿过一个本身位于 /run 内的符号链接目录(/etc/resolv.conf → /run/dnsproxy/resolv.conf,/run/dnsproxy → /run/sub/dnsproxy)时,返回的是宿主 canonical 落点 /run/sub/...;但沙盒里 /run 是全新 tmpfs,内核按字面路径解析会停在 tmpfs 中不存在的 /run/dnsproxy/...,bind 落空,且沙盒照常启动、只是 DNS 全死。改为:当前路径已字面位于 shadowed root(/run)下时不再 realpath 父目录,直接 bind 到字面跳点;/var/run → /run 这类 shadowed root 之外的父目录软链仍按宿主解析(沙盒复制了那一个)。网络策略分支调用同一函数,同一形状一并覆盖。

测试 11 → 15 例:

  • 纯函数新增「/run 内目录本身是符号链接时返回字面跳点」;
  • 端到端新增 oncall、scratch 对该形状的真 bwrap 用例,以及网络策略分支经真实 sandbox-network-runner 在该形状读到自定义 DNS 文件的用例(node 走 build 后的 dist、bun 走 .ts 源)。

验证:vitest 15/15、bun test 15/15、bun run build 通过;相关 10 个沙盒测试文件 247 passed / 55 skipped(skipped 为本机嵌套 bwrap 环境限制)。反向变异(恢复成无条件 realpath 父目录)在 vitest 与 bun 下各咬红新增的 4 例、原 11 例全绿。

另在 PR 描述「影响面」补充说明了该修复,并注明不覆盖的一类形状:链接文本直接指向 /var/run/... 时,oncall 的空 tmpfs 根里没有 /var/run → /run 软链,链在第一跳就断(scratch 的合并树根不受影响),留待后续。

以上为自动评审流程按复审意见做的代改,最终以维护者审阅为准。

@deepcoldy
deepcoldy merged commit e3b5586 into master Oct 29, 2026
15 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner Author

合入核验(e3b558637):

  • 状态 MERGED,提交标题带 (#2534)、正文为空,作者为 PR 创建者;
  • 落地改动与评审过的 head fb68d736a 的 patch-id 一致;
  • master 上该提交的 CI 已开始排队。

follow-up(不阻断,已在描述「影响面」写明):

  1. 链接文本直接写成 /var/run/... 时,oncall 沙盒根里没有 /var/run → /run 软链,第一跳就断(scratch 不受影响);
  2. v3 workflow 的 distillation runner 也挂了 /run tmpfs,是另一套参数拼装,未处理。

同一任务的第二部分(网络策略 DNS/代理提示与文案)在 #2540。

deepcoldy pushed a commit that referenced this pull request Oct 29, 2026
# Conflicts:
#	src/adapters/backend/sandbox.ts
deepcoldy pushed a commit that referenced this pull request Oct 29, 2026
#2534 的 hostResolverBindArgs 判 operator deny 时读的是 relocate 前的
opts.policy.rules,而同函数编译挂载用的是 relocateSymlinkedBaselineRules
之后的 policy。统一成编译用哪套规则就按哪套判定,消除两份规则源。

当前可达策略形状下判定结果不变(用户 deny 由 worker 提前 canonical 化,
linux 基线软链只涉及 /opt、/etc 等,/run 是编译器原语不在规则里),纯
一致性修正;sandbox-resolver-bind 真机用例(含两条 deny)双引擎全绿。

Co-Authored-By: Claude Code <[email protected]>
deepcoldy pushed a commit to uestcwangci/botmux that referenced this pull request Oct 30, 2026
…ity-recovery-2473-final

四次同步 master(0345cc5f1..eb43116,14 提交,含 deepcoldy#2514/deepcoldy#2534/deepcoldy#2535/deepcoldy#2541/
deepcoldy#2467/deepcoldy#2550/deepcoldy#2553/deepcoldy#2554/deepcoldy#2557/deepcoldy#2561/deepcoldy#2562 等)。与本 PR 仅 worker-pool.ts /
worker.ts 文件交集,git 自动合并干净、无冲突标记;PR 关键接线(codexApp
TurnFailureCode 四处、!turn.error 守卫、pool 恢复落账、worker 签名 final
失败字段、Pi boot 20s 预算)逐处 grep 仍在原位。合并树 build 过、
codex 集成/容量/engine/deployment/schedule 共 276 项 vitest 绿、
容量+Pi argv 46 项 bun 绿。

Co-Authored-By: Claude Code <[email protected]>
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

🚀 Released in v3.56.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant