Files
ARTEX/server/envseal.go
T
BaaRai 04a7ab3c39 security(server): keep server secrets from agent processes
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.
2026-11-20 05:35:53 +01:00

149 lines
4.6 KiB
Go

package server
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
// Sealing the server environment. ScrubSecretEnv removes the secrets from the Go
// environment, but the kernel keeps the environment block the process was executed
// with: /proc/<pid>/environ still shows ARTEX_PG_DSN, ARTEX_SECRET_KEY and the API
// keys to any process that can read it. SealProcessEnv re-executes the server once,
// with those variables left out of the block; the values travel to the new image
// through an inherited pipe and are put back into the Go environment there, so the
// readers that run next see the same configuration.
//
// Only Linux has /proc and an exec that keeps the PID, so the handoff is Linux-only
// (envseal_linux.go). Elsewhere SealProcessEnv logs that the environment is not sealed.
// sealFDEnv names the inherited descriptor that carries the sealed values. It is set
// only for the re-executed image and removed as soon as that image reads it.
const sealFDEnv = "ARTEX_SEALED_FD"
// sealMaxPayload keeps the handoff well inside a pipe buffer (64 KiB on Linux), so the
// write before the exec never blocks. The secrets are a few hundred bytes in practice.
const sealMaxPayload = 16 << 10
// errSealUnsupported reports a platform where the environment block cannot be replaced.
var errSealUnsupported = errors.New("replacing the process environment is not supported on this platform")
// SealProcessEnv keeps the secrets out of the process environment block. Call it first
// in main, before any listener opens or any child starts. In the re-executed image it
// restores the values; a failure to restore is fatal, since the server cannot run
// without its configuration.
func SealProcessEnv() {
if raw, ok := os.LookupEnv(sealFDEnv); ok {
os.Unsetenv(sealFDEnv)
if err := restoreSealedEnv(raw); err != nil {
log.Fatalf("[env] sealed environment handoff failed: %v", err)
}
return
}
var kvs []string
for _, k := range secretEnv {
if v, ok := os.LookupEnv(k); ok {
kvs = append(kvs, k+"="+v)
}
}
if len(kvs) == 0 {
return
}
payload := encodeSealedEnv(kvs)
if len(payload) > sealMaxPayload {
log.Printf("[env] server environment not sealed: the secrets are too large for the handoff; they remain readable from /proc/<pid>/environ")
return
}
// reexecSealed returns only when the exec did not happen.
err := reexecSealed(withoutSecretEnv(os.Environ()), payload)
if errors.Is(err, errSealUnsupported) {
log.Printf("[env] server environment not sealed on this platform: secrets remain in the process environment")
return
}
log.Printf("[env] server environment not sealed (%v): secrets remain readable from /proc/<pid>/environ", err)
}
// restoreSealedEnv reads the handoff from the descriptor named by raw and sets the
// values in the Go environment.
func restoreSealedEnv(raw string) error {
fd, err := strconv.Atoi(raw)
if err != nil || fd < 0 {
return fmt.Errorf("bad descriptor %q", raw)
}
f := os.NewFile(uintptr(fd), "artex-sealed-env")
data, err := io.ReadAll(f)
f.Close()
if err != nil {
return err
}
kvs, err := decodeSealedEnv(data)
if err != nil {
return err
}
for _, kv := range kvs {
k, v, _ := strings.Cut(kv, "=")
if err := os.Setenv(k, v); err != nil {
return err
}
}
return nil
}
// encodeSealedEnv writes NAME=VALUE entries, each terminated by a NUL. Values may be
// empty or span lines; an environment value cannot contain NUL, so the terminator is
// unambiguous.
func encodeSealedEnv(kvs []string) []byte {
var b []byte
for _, kv := range kvs {
b = append(b, kv...)
b = append(b, 0)
}
return b
}
// decodeSealedEnv parses encodeSealedEnv's output. Only the names in secretEnv are
// accepted: the handoff carries nothing else, and an unknown name means corruption.
func decodeSealedEnv(b []byte) ([]string, error) {
var kvs []string
for len(b) > 0 {
i := bytes.IndexByte(b, 0)
if i < 0 {
return nil, errors.New("sealed environment is not terminated")
}
entry := string(b[:i])
b = b[i+1:]
name, _, ok := strings.Cut(entry, "=")
if !ok || !isSecretName(name) {
return nil, fmt.Errorf("unexpected entry in the sealed environment")
}
kvs = append(kvs, entry)
}
return kvs, nil
}
func isSecretName(name string) bool {
for _, k := range secretEnv {
if k == name {
return true
}
}
return false
}
// withoutSecretEnv returns the environment block without the secret entries.
func withoutSecretEnv(env []string) []string {
out := make([]string, 0, len(env))
for _, kv := range env {
name, _, _ := strings.Cut(kv, "=")
if !isSecretName(name) {
out = append(out, kv)
}
}
return out
}