From 196b9420d31707f63eaeda9ec50731da7b67c9f6 Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 18 Sep 2026 09:57:02 +0800 Subject: [PATCH] =?UTF-8?q?executor:=20=E8=A1=A5=20SHELL=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E3=80=81per-exec=20tmpfs=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=8A=A0=20size/noexec=E3=80=81=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E8=87=AA=E6=A3=80=E5=8A=A0=20unshare=20=E6=8E=A2?= =?UTF-8?q?=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- executor/server.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/executor/server.js b/executor/server.js index bd8c514..b72c88f 100644 --- a/executor/server.js +++ b/executor/server.js @@ -43,7 +43,7 @@ const os = require('os'); const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); -const { spawn } = require('child_process'); +const { spawn, execFileSync } = require('child_process'); // ---------- 配置 ---------- const PORT = parseInt(process.env.PORT, 10) || 9000; @@ -189,6 +189,16 @@ function selfCheck() { try { ss = fs.statSync(SKILLS_DIR); } catch (_) {} if (!ss) warns.push('SKILLS_DIR=' + SKILLS_DIR + ' 不存在(若不使用技能可忽略)'); + // 行为验证:实际跑一次带挂载的 unshare,确认隔离层在当前 seccomp/apparmor/caps 组合下真的可用; + // 哪天部署配置回归(如 v4 类事故)会在这里立刻暴露,而不是等 AI 会话失败才发现。 + try { + execFileSync('unshare', ['--mount', '--pid', '--ipc', '--fork', '--propagation', 'private', + '/bin/sh', '-c', 'mount -t tmpfs -o nosuid,nodev tmpfs /tmp && true'], + { stdio: 'pipe', timeout: 10000 }); + } catch (e) { + problems.push('unshare 隔离探针失败 —— 检查 cap_add SYS_ADMIN / apparmor=unconfined / seccomp:' + (e.stderr && String(e.stderr) || e.message)); + } + if (problems.length) log.error('启动自检失败:\n - ' + problems.join('\n - ')); else if (warns.length) log.warn('启动自检通过(有提示):\n - ' + warns.join('\n - ')); else log.info('启动自检通过'); @@ -234,14 +244,14 @@ function buildIsolation(command, plan) { // --- 本次执行私有 tmpfs:/tmp(session 模式下随后被会话 .tmp 的 bind 覆盖)与 anchor 目录 --- L.push('mkdir -p /tmp'); - L.push('mount -t tmpfs -o nosuid,nodev,mode=1777 tmpfs /tmp || { echo "[isolate] /tmp failed" >&2; exit 92; }'); + L.push('mount -t tmpfs -o nosuid,nodev,mode=1777,size=256m tmpfs /tmp || { echo "[isolate] /tmp failed" >&2; exit 92; }'); // /run 整体私有化:父容器(compose tmpfs)的运行时状态是跨 exec 共享的,一律不进子 ns; // $RUNDIR 随后在这块新 tmpfs 上再叠一层,anchor 逻辑不变。 L.push('mkdir -p /run'); - L.push('mount -t tmpfs -o nosuid,nodev,mode=755,size=64m tmpfs /run || { echo "[isolate] /run failed" >&2; exit 102; }'); + L.push('mount -t tmpfs -o nosuid,nodev,noexec,mode=755,size=64m tmpfs /run || { echo "[isolate] /run failed" >&2; exit 102; }'); L.push('RUNDIR=/run/agent-exec'); L.push('mkdir -p "$RUNDIR"'); - L.push('mount -t tmpfs -o nosuid,nodev,mode=700 tmpfs "$RUNDIR" || { echo "[isolate] rundir failed" >&2; exit 93; }'); + L.push('mount -t tmpfs -o nosuid,nodev,mode=700,size=16m tmpfs "$RUNDIR" || { echo "[isolate] rundir failed" >&2; exit 93; }'); // --- 数据面 --- if (plan.mode === 'session') { @@ -571,6 +581,7 @@ async function handleExec(req, res) { if (!env.PATH) env.PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'; env.HOME = '/tmp'; // plain 模式指向 per-exec tmpfs;session 模式指向会话共享 .tmp,均不污染镜像层 env.TMPDIR = '/tmp'; + env.SHELL = '/bin/sh'; // 白名单不含 SHELL,不设则子进程里 $SHELL 为空,部分工具的 shell=$SHELL 会拿空值 env.PYTHONDONTWRITEBYTECODE = '1'; env.PYTHONPYCACHEPREFIX = '/tmp/pycache';