- TA main.rs 删手写 #[no_mangle] 弱符号,改 include! $OUT_DIR/user_ta_header.rs (ta_head/ta_heap/ta_props/trace_* 全套由 optee-utee-build 生成,官方示例 aes-rs 同款;手写与生成定义冲突,漏写则链接器逐个 undefined) - ta/ca Cargo.toml 的 package 字段显式写死 version/edition/license,不再 workspace 继承(teaclave examples/ca 的 workspace.package 无 edition) - ca main.rs 命令分发挪进 fn run() -> R<()>,main 只收尾(? 不能用在 返回 () 的 main,E0277 x4) - p3_optee_qemu.sh / run_qemu.py:USTC apt/cargo/rustup 镜像、显式 commands.run 超时、traffic token 落盘、ghfast.top clone 回退等 判据:QEMU 正常世界 nax-tee-ca selftest 全 8 步通过(provision -> seal/unseal -> 重开 session -> 篡改拒 -> AAD 绑定 -> sign -> attest), sw.log 两条 ffff000f Security 拒绝为预期篡改/AAD 检测,NAX_RC=0。 宿主侧 cargo test --workspace 129 全绿。
167 lines
5.5 KiB
Python
167 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""P3 OP-TEE QEMU 验证:在 cube-sandbox 的 toolchain 模板里跑全链。
|
|
|
|
用法(key 经 xop 注入环境,不进 argv):
|
|
xop --secret CUBE_API_KEY=llm_key -- python3 build/run_qemu.py < /dev/null
|
|
|
|
⚠ cube-auth-gate/2.0 的兼容补丁:强制 Accept-Encoding: identity。
|
|
⚠ SDK commands.run() 默认 read=None,必须显式设 timeout。
|
|
⚠ traffic token 只在 create 返回一次,立即落盘备用。
|
|
"""
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
os.environ.setdefault("CUBE_API_URL", "http://192.168.1.30:3000")
|
|
os.environ.setdefault("CUBE_PROXY_NODE_IP", "192.168.1.30")
|
|
os.environ.setdefault("CUBE_PROXY_PORT_HTTP", "80")
|
|
|
|
import requests as _requests
|
|
|
|
_orig_req = _requests.Session.request
|
|
|
|
|
|
def _req_no_gzip(self, method, url, **kw):
|
|
headers = dict(kw.pop("headers", None) or {})
|
|
headers["Accept-Encoding"] = "identity"
|
|
return _orig_req(self, method, url, headers=headers, **kw)
|
|
|
|
|
|
_requests.Session.request = _req_no_gzip
|
|
|
|
try:
|
|
import httpx as _httpx
|
|
|
|
_orig_hreq = _httpx.Client.request
|
|
|
|
def _hreq_no_gzip(self, method, url, **kw):
|
|
headers = dict(kw.pop("headers", None) or {})
|
|
headers.setdefault("Accept-Encoding", "identity")
|
|
return _orig_hreq(self, method, url, headers=headers, **kw)
|
|
|
|
_httpx.Client.request = _hreq_no_gzip
|
|
except Exception:
|
|
pass
|
|
|
|
from cubesandbox import Sandbox
|
|
|
|
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
BUILD_TIMEOUT = 3600 # 前台跑构建,最多 60 分钟
|
|
|
|
|
|
def make_src_tgz() -> bytes:
|
|
import io
|
|
import tarfile
|
|
|
|
def _filter(ti: tarfile.TarInfo):
|
|
parts = ti.name.split("/")
|
|
if "target" in parts or ".git" in parts or "__pycache__" in parts:
|
|
return None
|
|
return ti
|
|
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
|
|
for sub in ("crates/nax-core", "crates/nax-tee"):
|
|
tf.add(os.path.join(REPO, sub), arcname=sub, filter=_filter)
|
|
tf.add(
|
|
os.path.join(REPO, "build", "p3_optee_qemu.sh"),
|
|
arcname="p3_optee_qemu.sh",
|
|
)
|
|
return buf.getvalue()
|
|
|
|
|
|
def run_cmd(sb, cmd, timeout=30):
|
|
"""带超时的命令执行,打印 stderr 便于调试。"""
|
|
r = sb.commands.run(cmd, timeout=timeout)
|
|
if r.stdout.strip():
|
|
print(r.stdout.rstrip(), flush=True)
|
|
if r.stderr.strip():
|
|
print(f" [stderr] {r.stderr.rstrip()[:200]}", flush=True)
|
|
return r
|
|
|
|
|
|
def main():
|
|
print("== 创建 toolchain 沙箱 (16C/16G, 2h) ==", flush=True)
|
|
sb = Sandbox.create(
|
|
template="toolchain", timeout=7200, network={"allow_public_traffic": False}
|
|
)
|
|
print(f" sandbox_id = {sb.sandbox_id}", flush=True)
|
|
|
|
token = sb._data.get("trafficAccessToken", "")
|
|
token_file = f"/tmp/sb_token_{sb.sandbox_id}.txt"
|
|
with open(token_file, "w") as f:
|
|
f.write(token)
|
|
print(f" token → {token_file}", flush=True)
|
|
|
|
try:
|
|
# 上传
|
|
print("== 上传源码 ==", flush=True)
|
|
tgz = make_src_tgz()
|
|
print(f" src.tgz = {len(tgz) / 1e6:.2f} MB", flush=True)
|
|
run_cmd(sb, "mkdir -p /root/src")
|
|
sb.files.write("/root/src/src.tgz", tgz)
|
|
run_cmd(sb, "tar xzf /root/src/src.tgz -C /root/src && ls /root/src")
|
|
|
|
# 前台跑构建(不 nohup),输出直接打出来
|
|
print("== 构建开始(前台,最多 60 分钟)==", flush=True)
|
|
t0 = time.time()
|
|
try:
|
|
r = sb.commands.run(
|
|
"cd /root && SRC=/root/src/crates bash /root/src/p3_optee_qemu.sh; echo EXIT_CODE=$?",
|
|
timeout=BUILD_TIMEOUT,
|
|
)
|
|
print(r.stdout[-3000:] if len(r.stdout) > 3000 else r.stdout, flush=True)
|
|
if r.stderr.strip():
|
|
print(f"[stderr] {r.stderr[-1000:]}", flush=True)
|
|
except Exception as e:
|
|
elapsed = int(time.time() - t0)
|
|
print(f"\n⚠ 构建异常 ({elapsed}s): {e}", flush=True)
|
|
# 尝试取日志
|
|
try:
|
|
r2 = sb.commands.run("tail -30 /root/build.log 2>/dev/null", timeout=15)
|
|
print("build.log 尾部:", flush=True)
|
|
print(r2.stdout, flush=True)
|
|
except Exception:
|
|
pass
|
|
|
|
elapsed = int(time.time() - t0)
|
|
print(f"\n== 构建耗时 {elapsed}s ==", flush=True)
|
|
|
|
# 检查判据
|
|
try:
|
|
r = sb.commands.run("tail -40 /root/run/nw.log 2>/dev/null", timeout=15)
|
|
if r.stdout.strip():
|
|
print("正常世界日志:", flush=True)
|
|
print(r.stdout, flush=True)
|
|
except Exception:
|
|
pass
|
|
|
|
# 判据:ALL OK 在 nw.log
|
|
try:
|
|
r = sb.commands.run("grep -c 'ALL OK' /root/run/nw.log 2>/dev/null", timeout=10)
|
|
count = r.stdout.strip()
|
|
if count and int(count) > 0:
|
|
print("\n✅ P3 QEMU 验证通过!(ALL OK)", flush=True)
|
|
try:
|
|
sb.kill()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
os.unlink(token_file)
|
|
except Exception:
|
|
pass
|
|
return 0
|
|
except Exception:
|
|
pass
|
|
|
|
print("\n❌ P3 QEMU 验证失败", flush=True)
|
|
print(f" 沙箱保留: {sb.sandbox_id}", flush=True)
|
|
print(f" token: {token_file}", flush=True)
|
|
return 1
|
|
finally:
|
|
pass # 失败时保留沙箱供调试,成功时已在上方销毁
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|