真机联调(CubeSandbox 沙箱当异地站点端 → 海光真机 192.168.1.102)暴露并修掉
三个真缺口,并把「否定式判据恒为真」这个探针自指坑彻底堵上。
① hostd 服务端**完全没有鉴权**(真缺口)
原来 client.rs 只在客户端拼 Authorization 头,服务端根本不验 ——
之所以没出事只因默认只听回环。新增 crates/mpc-hostd/src/auth.rs:
- 回环信任(未配密钥)/ 凭据模式(已配)两档;服务端只存 sha256(secret),
摘要不能反向当 token 用;最低 32 字节,启动期硬拒;ct_eq 常量时间比对;
- 对外不区分「没带」与「带错了」,细分只进日志;
- 鉴权先于路由(含 /health),业务端点 POST /sandboxes 同样受保护;
- 非回环必须「共享密钥 + --allow-remote」两者齐备,互为兜底。
共享密钥是静态的,不是站点端签发的短期 token(那一层仍未落地)。
② DELETE /sandboxes/{id} 只停不删(真缺口)
api.rs 注释写着「显式删除」,实现却只调 stop() ⇒ 删完仍在 GET /sandboxes 里。
症状极静默:204 看着成功,列表越积越多。
新增 runtime.remove()(先 stop 收进程、再删记录与子进程句柄,顺序反了会留僵尸)。
③ guest 边界探针的否定式判据可能是空判据
实测 release+strip+LTO 后:eth_sendRawTransaction / 1234567812345678 在 musl
host 产物里**也找不到**(host 不含 chain)⇒ 拿它们判 guest 必然恒为真;
而 mpc-hostd / allow-remote 在 guest 产物里**也会有**(那是 main.rs 帮助文本)。
改法:标识串按归属分五组,binary 子命令同时吃 guest + host 两份产物做交叉对照 ——
先用 host 证明「这些串确实会进二进制」,再据此在 guest 上跑否定判据;
host 里找不到 ⇒ 探针失效 ⇒ 直接 FAIL,不安静跳过。
配套:
- 两种 musl 产物正式化:guest(--no-default-features) 与
host(--no-default-features --features host-only)。真机无 Rust 工具链,
hostd 必须以自包含静态二进制跑在真机上;openssl 仍禁入 musl 依赖图
(否则 pkg-config 会抢先于 compile_error! 守卫,守卫就成假判据)。
- 修 Request::header 大小写敏感(手写 Request 时带对 Authorization 头却被判没带)。
- 主门禁第 4 条改交叉对照(deps 30 + guard 3 + binary 39 条);
第 12 条 hostd 自测补 DELETE 真删判据。
- 文档:docs/REAL_MACHINE_FINDINGS.md 第 5 节(沙箱→真机闭环)+ CLAUDE.md 铁律。
真机实测全绿:沙箱经 e2b 兼容 HTTP 面 up/ps/attest/down 跨机跑通,
鉴权矩阵(正向 200 / 缺凭据 401 / 错凭据 401 且两响应体逐字节相同)全绿。
诚实边界:沙箱起的是真机上的普通进程不是真 TEE enclave(四布尔必然全 false),
这验证的是控制面编排而非证明链;hostd 仍是明文 HTTP;只连了海光一台。
91 lines
4.5 KiB
Rust
91 lines
4.5 KiB
Rust
//! 两件事:
|
||
//!
|
||
//! 1. 计算唯一一个 cfg `mpc_control_plane`(必须放在最前面,见下)。
|
||
//! 2. 只在带 `ray` feature 编译时,把 libmpc_ray.so 所在目录写进可执行文件的
|
||
//! RUNPATH。`cargo:rustc-link-arg-bins` 只作用于**本包**的 bin 目标,
|
||
//! 放在 mpc-ray 的 build.rs 里传不过来——症状是链接期能找到 .so、运行期报
|
||
//! `libmpc_ray.so: cannot open shared object file`。
|
||
//!
|
||
//! ★ 第 1 件的理由:mpc-hostd / mpc-chain 既是可选依赖(feature
|
||
//! `control-plane`),又只挂在非 musl 目标上(见 Cargo.toml 的
|
||
//! `[target.'cfg(not(target_env = "musl"))'.dependencies]`)。源码必须按
|
||
//! **同一个条件**裁掉模块与命令分派;把这个条件在 main.rs 里抄八遍,
|
||
//! 迟早有一处漏改。
|
||
//!
|
||
//! musl 目标即使开着 feature 也**不**设这个 cfg —— 于是 main.rs 的
|
||
//! `compile_error!` 能真正执行到,明确告诉调用方加 `--no-default-features`,
|
||
//! 而不是先撞上 openssl-sys 那句含混的 pkg-config 报错。
|
||
//! ★ 注意下面 ray 分支有提前 return,cfg 计算**必须**在它之前。
|
||
|
||
fn main() {
|
||
// ---- 1. 控制面 cfg(不产生构建产物、不引入依赖)----
|
||
//
|
||
// ★ 分成两个 cfg,不是两个 feature —— 因为它们对应的是**依赖差异**:
|
||
//
|
||
// mpc_control_plane —— 宿主控制面里**不依赖 OpenSSL** 的那一半:
|
||
// mpc-hostd(axum/tokio/reqwest)+ EasyTier 链路(mpc_hostd::link)。
|
||
// ★ 这一半**可以**在 musl 下静态编译 —— 真机没有 Rust 工具链,
|
||
// 需要一个自包含二进制把 hostd 跑起来。
|
||
//
|
||
// mpc_chain_plane —— 链上层:mpc-chain 链接 **系统 OpenSSL**
|
||
// (openssl-sys 要 musl 版 libcrypto,本机没有)。
|
||
// ★ 这一半**不能**进 musl:一旦进了,main.rs 的 compile_error!
|
||
// 守卫会被 pkg-config 抢先,守卫就变成永远测不到的假判据。
|
||
println!("cargo:rustc-check-cfg=cfg(mpc_control_plane)");
|
||
println!("cargo:rustc-check-cfg=cfg(mpc_chain_plane)");
|
||
let target = std::env::var("TARGET").unwrap_or_default();
|
||
let control_plane_on = std::env::var_os("CARGO_FEATURE_CONTROL_PLANE").is_some();
|
||
let host_only_on = std::env::var_os("CARGO_FEATURE_HOST_ONLY").is_some();
|
||
// guest 的静态产物只可能是 musl 目标;宿主(darwin / glibc linux)不受影响。
|
||
let is_guest_target = target.contains("musl");
|
||
|
||
// ★ musl + control-plane 是**明确错误**:那是 guest 产物,边界不许破。
|
||
// main.rs 的 compile_error! 会给出可执行的指引。
|
||
if control_plane_on && is_guest_target {
|
||
// 由 main.rs 的 compile_error! 负责报错文案,这里只负责 cfg 不被误设。
|
||
} else if control_plane_on || host_only_on {
|
||
// 宿主(darwin / glibc):control-plane 含 chain,host-only 不含。
|
||
println!("cargo:rustc-cfg=mpc_control_plane");
|
||
if control_plane_on {
|
||
println!("cargo:rustc-cfg=mpc_chain_plane");
|
||
}
|
||
}
|
||
|
||
// ---- 2. Ray 的 RUNPATH ----
|
||
println!("cargo:rerun-if-env-changed=MPC_RAY_LIB_DIR");
|
||
println!("cargo:rerun-if-env-changed=MPC_RAY_LIB_NAME");
|
||
println!("cargo:rerun-if-env-changed=MPC_RAY_LIBPYTHON");
|
||
|
||
// 没开 ray feature 就别管——默认构建是纯静态单二进制,不碰任何 .so。
|
||
if std::env::var("CARGO_FEATURE_RAY").is_err() {
|
||
return;
|
||
}
|
||
|
||
let lib = std::env::var("MPC_RAY_LIB_NAME").unwrap_or_else(|_| "mpc_ray".to_string());
|
||
let mut dirs: Vec<String> = Vec::new();
|
||
if let Ok(d) = std::env::var("MPC_RAY_LIB_DIR") {
|
||
dirs.push(d);
|
||
}
|
||
// 与 mpc-ray/build.rs 保持同一套搜索顺序
|
||
if let Ok(m) = std::env::var("CARGO_MANIFEST_DIR") {
|
||
if let Some(root) = std::path::Path::new(&m).parent().and_then(|p| p.parent()) {
|
||
dirs.push(root.join("target/ray").display().to_string());
|
||
}
|
||
}
|
||
dirs.push("/usr/local/lib".into());
|
||
|
||
for d in &dirs {
|
||
if std::path::Path::new(d).join(format!("lib{lib}.so")).exists() {
|
||
println!("cargo:rustc-link-arg-bins=-Wl,-rpath,{d}");
|
||
break;
|
||
}
|
||
}
|
||
|
||
// _raylet.so 自己带 RUNPATH 指向 libpython,但交叉编译的场景不一定,多带一个不亏。
|
||
if let Ok(p) = std::env::var("MPC_RAY_LIBPYTHON") {
|
||
if let Some(dir) = std::path::Path::new(&p).parent() {
|
||
println!("cargo:rustc-link-arg-bins=-Wl,-rpath,{}", dir.display());
|
||
}
|
||
}
|
||
}
|