mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 09:20:53 +08:00
GHSA-prm8-vw2v-wjwh: the agent command executor ran as root in the server's container and PID namespace, so it could read /app/keys and recover the server secrets from /proc/<pid>/environ. Deliverable 1, executor identity. Agent processes (Bash, the interactive shell, custom tools, stdio MCP servers) run as the uid/gid set by -exec-uid and -exec-gid (default 10001). Custom tools use SysProcAttr.Credential. Norma starts Bash and the shell itself, so a PATH shim (the server binary named bash) drops the identity before exec; MCP stdio commands run through the same binary in drop mode. The task and chat workspace trees are shared with the executor group (setgid, group rw, umask 002). The key files and the config file become root-only, and /app/keys is 0700 in the image. An explicit -exec-uid that cannot be applied stops the start; the default logs once that same-uid isolation is unavailable when the server is not root. Deliverable 2, sealed environment. On Linux, SealProcessEnv re-executes the server once with the secret variables removed from its environment block, passing the values through an inherited pipe. The new image restores them into the Go environment, so readers see the same configuration. Off Linux it logs that the environment is not sealed. Deliverable 3, approval gate. Custom tools, MCP servers and skill files (create_/update_ of custom tool and MCP, create_skill, update_skill_file) meet built-in ask rules seeded once (intercept_persistence_rules_v1). A user allow rule cannot skip them; a user deny still wins. The rules are data the agent cannot edit. Tests: resolution and refusal of root identities, drop command wrapping, sealed-environment encoding, and an end-to-end handoff test on Linux that checks the kernel environment block and a child process hold no secret names and that the values survive (empty and multi-line included). Approval matching is tested in intercept.
107 lines
4.3 KiB
Go
107 lines
4.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"sync"
|
|
)
|
|
|
|
// Agent process privilege. The server keeps running as root (the Docker image does),
|
|
// and every process the agent can start (Bash, the interactive shell, custom tools,
|
|
// stdio MCP servers) runs as the executor identity below instead. Without this a
|
|
// prompt-injected command runs as root in the same container: it reads /app/keys,
|
|
// the config file and /proc/<server-pid>/environ with root's rights.
|
|
//
|
|
// The drop is applied to the child, never to the server: SysProcAttr.Credential for
|
|
// the processes ARTEX starts itself, and for the processes the Norma library starts
|
|
// (Bash, shell, MCP stdio) a shim in the same binary that drops and then execs the
|
|
// real program. See execpriv_unix.go.
|
|
|
|
// Default executor identity. It is a numeric id, not a system account: the kernel
|
|
// accepts any uid. Override it with -exec-uid and -exec-gid.
|
|
const (
|
|
DefaultExecUID = 10001
|
|
DefaultExecGID = 10001
|
|
)
|
|
|
|
// execDropArg is the argv marker of the MCP drop shim:
|
|
// artex __artex-exec-drop <uid> <gid> -- <command> <args...>.
|
|
const execDropArg = "__artex-exec-drop"
|
|
|
|
// ExecPrivilege is the identity agent processes run under. The zero value is
|
|
// disabled: children keep the server's identity, which is the state before this
|
|
// change and is logged as such.
|
|
type ExecPrivilege struct {
|
|
Enabled bool
|
|
UID uint32
|
|
GID uint32
|
|
}
|
|
|
|
// ResolveExecPrivilege turns the -exec-uid and -exec-gid options into an
|
|
// ExecPrivilege. A negative uid disables the drop explicitly. explicit reports
|
|
// whether the operator set either option: an explicit drop that cannot be applied
|
|
// is an error (fail closed), while the default degrades to a single log line.
|
|
func ResolveExecPrivilege(uid, gid int, explicit bool) (ExecPrivilege, error) {
|
|
if uid < 0 {
|
|
logExecOnce("[exec] agent processes keep the server's identity (-exec-uid=%d): no privilege drop", uid)
|
|
return ExecPrivilege{}, nil
|
|
}
|
|
if uid == 0 || gid <= 0 || uid > 1<<31-1 || gid > 1<<31-1 {
|
|
return ExecPrivilege{}, fmt.Errorf("-exec-uid and -exec-gid must name an unprivileged id (got uid %d, gid %d; 0 is root)", uid, gid)
|
|
}
|
|
p := ExecPrivilege{Enabled: true, UID: uint32(uid), GID: uint32(gid)}
|
|
if !execPrivilegeSupported() {
|
|
return p.unavailable(explicit, "the privilege drop is not supported on this platform")
|
|
}
|
|
if !serverIsRoot() {
|
|
return p.unavailable(explicit, "the server is not root, so it cannot start agent processes as another user")
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// unavailable answers a drop that cannot be applied: an error when the operator asked
|
|
// for it, otherwise a disabled privilege with one log line that says so.
|
|
func (p ExecPrivilege) unavailable(explicit bool, why string) (ExecPrivilege, error) {
|
|
if explicit {
|
|
return ExecPrivilege{}, fmt.Errorf("agent process isolation cannot be applied: %s", why)
|
|
}
|
|
logExecOnce("[exec] same-uid isolation unavailable: %s; agent processes run with the server's identity", why)
|
|
return ExecPrivilege{}, nil
|
|
}
|
|
|
|
var execLogOnce sync.Map
|
|
|
|
// logExecOnce logs a message once per distinct text for the life of the process.
|
|
func logExecOnce(format string, args ...any) {
|
|
msg := fmt.Sprintf(format, args...)
|
|
if _, loaded := execLogOnce.LoadOrStore(msg, true); !loaded {
|
|
log.Print(msg)
|
|
}
|
|
}
|
|
|
|
// execIDString renders an executor id for the environment and the shim arguments.
|
|
func execIDString(id uint32) string { return strconv.FormatUint(uint64(id), 10) }
|
|
|
|
// WithExecPrivilege sets the identity agent processes run under. The binary passes the
|
|
// result of ResolveExecPrivilege; tests and other callers keep the zero value.
|
|
func WithExecPrivilege(p ExecPrivilege) Option {
|
|
return func(s *Server) { s.execPriv = p }
|
|
}
|
|
|
|
// execDropCommand returns the command line that runs command as the executor
|
|
// identity. The MCP stdio client starts its command with exec.Command and offers no
|
|
// credential, so the command becomes the server binary in drop mode (RunExecShim).
|
|
func execDropCommand(p ExecPrivilege, command string, args []string) (string, []string, error) {
|
|
if !p.Enabled {
|
|
return command, args, nil
|
|
}
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
return "", nil, fmt.Errorf("locate the server binary for the privilege drop: %w", err)
|
|
}
|
|
out := []string{execDropArg, execIDString(p.UID), execIDString(p.GID), "--", command}
|
|
return self, append(out, args...), nil
|
|
}
|