feat(agentexec): run harness invocations as the Kit's user with no inherited environment
A Kit task runs worker and harness in one container, separated by user: the worker keeps its credentials as root, and every harness invocation runs as the Kit's user with only its declared environment. A nil env would inherit the worker's, so the runner always passes an explicit one; root is refused as a harness user, and a root worker refuses to start a harness with no user. The PRD's container layout now says one container per run, with the relay forwarding the proxy, NATS and the State Store.
This commit is contained in:
1 parent
2a9567cdf5
commit
9937caad7c
8 files changed
+189
-16
No files matched your search
@@ -2,8 +2,20 @@
|
||||
|
||||
package agentexec
|
||||
|
||||
import "os/exec"
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// setProcessGroup falls back to killing the harness process alone where
|
||||
// process groups are unavailable.
|
||||
func setProcessGroup(*exec.Cmd) {}
|
||||
|
||||
// runAsHarnessUser refuses a named harness user where user switching is
|
||||
// unavailable, rather than running the harness as the worker.
|
||||
func runAsHarnessUser(_ *exec.Cmd, name string) error {
|
||||
if name != "" {
|
||||
return errors.New("harness user switching is unsupported on this platform")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -3,7 +3,13 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -13,3 +19,57 @@ func setProcessGroup(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }
|
||||
}
|
||||
|
||||
// runAsHarnessUser drops the invocation to the Kit's user. A worker running
|
||||
// as root refuses to start a harness with no user, because the harness would
|
||||
// then share the worker's user and could read its credentials.
|
||||
func runAsHarnessUser(cmd *exec.Cmd, name string) error {
|
||||
if name == "" {
|
||||
if os.Geteuid() == 0 {
|
||||
return errors.New("harness has no user and the worker runs as root; refusing to give the harness the worker's privileges")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
cred, err := harnessCredential(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.SysProcAttr.Credential = cred
|
||||
return nil
|
||||
}
|
||||
|
||||
// harnessCredential resolves a user name or "uid[:gid]" to a credential.
|
||||
// Root is refused whichever way it is named.
|
||||
func harnessCredential(name string) (*syscall.Credential, error) {
|
||||
uidText, gidText, hasGid := strings.Cut(name, ":")
|
||||
uid, uidErr := strconv.ParseUint(uidText, 10, 32)
|
||||
var cred syscall.Credential
|
||||
if uidErr == nil {
|
||||
cred.Uid, cred.Gid = uint32(uid), uint32(uid)
|
||||
} else {
|
||||
u, err := user.Lookup(uidText)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("harness user %q: %w", uidText, err)
|
||||
}
|
||||
id, err := strconv.ParseUint(u.Uid, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("harness user %q has non-numeric uid %q", uidText, u.Uid)
|
||||
}
|
||||
group, err := strconv.ParseUint(u.Gid, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("harness user %q has non-numeric gid %q", uidText, u.Gid)
|
||||
}
|
||||
cred.Uid, cred.Gid = uint32(id), uint32(group)
|
||||
}
|
||||
if hasGid {
|
||||
gid, err := strconv.ParseUint(gidText, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("harness group %q is not numeric", gidText)
|
||||
}
|
||||
cred.Gid = uint32(gid)
|
||||
}
|
||||
if cred.Uid == 0 {
|
||||
return nil, fmt.Errorf("harness user %q is root; a harness never runs with the worker's privileges", name)
|
||||
}
|
||||
return &cred, nil
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func (r *HarnessRunner) Run(ctx context.Context, workspace string, req Request,
|
||||
for {
|
||||
res.Iterations++
|
||||
out := r.output()
|
||||
exitErr := invoke(runCtx, workspace, harnessArgv(req.Harness, verb, prompt, session), out, report)
|
||||
exitErr := invoke(runCtx, workspace, req.Harness, harnessArgv(req.Harness, verb, prompt, session), out, report)
|
||||
if id := out.SessionID(); id != "" {
|
||||
session = id
|
||||
}
|
||||
@@ -197,11 +197,16 @@ func resumeVerb(h *HarnessSpec, sessionID string) []string {
|
||||
// invoke runs one harness invocation in its own process group, feeding
|
||||
// stdout to out line by line. Cancelling ctx kills the whole group, so a
|
||||
// CLI's children cannot outlive the step.
|
||||
func invoke(ctx context.Context, workspace string, argv []string, out HarnessOutput, report ToolCallReporter) error {
|
||||
func invoke(ctx context.Context, workspace string, h *HarnessSpec, argv []string, out HarnessOutput, report ToolCallReporter) error {
|
||||
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
|
||||
cmd.Dir = workspace
|
||||
cmd.WaitDelay = killGrace
|
||||
// A nil Env would inherit the worker's environment and its credentials.
|
||||
cmd.Env = append([]string{}, h.Env...)
|
||||
setProcessGroup(cmd)
|
||||
if err := runAsHarnessUser(cmd, h.User); err != nil {
|
||||
return err
|
||||
}
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -28,6 +28,7 @@ commit) echo x > a.txt; git add a.txt; git -c user.email=a@b.c -c user.name=a co
|
||||
config) git config remote.origin.url https://attacker.invalid/repo.git ;;
|
||||
protected) echo broken >> widget_test.go ;;
|
||||
sleep) sleep 30 & echo $! > "$FAKE_LOG.child"; wait ;;
|
||||
env) env > "$FAKE_LOG.env" ;;
|
||||
fix-on-resume) if [ -f .attempted ]; then echo ok > fixed.txt; else touch .attempted; fi ;;
|
||||
esac
|
||||
echo "session=S123"
|
||||
@@ -91,13 +92,12 @@ func newFixture(t *testing.T, scenario string) *fixture {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log := filepath.Join(t.TempDir(), "invocations")
|
||||
t.Setenv("FAKE_SCENARIO", scenario)
|
||||
t.Setenv("FAKE_LOG", log)
|
||||
return &fixture{workspace: ws, log: log, req: Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "implement", Model: "harness",
|
||||
Mission: "Write the notes file.",
|
||||
Budget: Budget{WallClock: 20 * time.Second},
|
||||
Harness: &HarnessSpec{
|
||||
Env: []string{"PATH=" + os.Getenv("PATH"), "FAKE_SCENARIO=" + scenario, "FAKE_LOG=" + log},
|
||||
Launch: []string{"/bin/sh", bin},
|
||||
Prompt: []string{"-p", "{{.Prompt}}"},
|
||||
Resume: []string{"--resume", "{{.SessionID}}"},
|
||||
@@ -287,3 +287,60 @@ func assertChildDead(t *testing.T, pidFile string) {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessNeverInheritsTheWorkerEnvironment(t *testing.T) {
|
||||
f := newFixture(t, "env")
|
||||
// The fake's own variables are in the worker's environment too, so an
|
||||
// inherited environment would run the fake fully and expose the leak.
|
||||
t.Setenv("FAKE_SCENARIO", "env")
|
||||
t.Setenv("FAKE_LOG", f.log)
|
||||
t.Setenv("STATE_STORE_TOKEN", "worker-secret")
|
||||
t.Setenv("NATS_TOKEN", "worker-secret")
|
||||
f.req.Harness.Env = []string{
|
||||
"PATH=" + os.Getenv("PATH"),
|
||||
"FAKE_SCENARIO=env", "FAKE_LOG=" + f.log,
|
||||
"HTTPS_PROXY=http://archie:token@archie-egress:3128",
|
||||
}
|
||||
res, _ := f.run(t, t.Context())
|
||||
if res.Status != StatusPassed {
|
||||
t.Fatalf("status %q (%s)", res.Status, res.Detail)
|
||||
}
|
||||
raw, err := os.ReadFile(f.log + ".env")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := string(raw)
|
||||
if strings.Contains(env, "worker-secret") {
|
||||
t.Fatalf("the harness saw the worker's credentials:\n%s", env)
|
||||
}
|
||||
if !strings.Contains(env, "HTTPS_PROXY=http://archie:token@archie-egress:3128") {
|
||||
t.Fatalf("the harness did not get its declared environment:\n%s", env)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessUser(t *testing.T) {
|
||||
tests := []struct {
|
||||
user string
|
||||
uid uint32
|
||||
gid uint32
|
||||
wantErr bool
|
||||
}{
|
||||
{"1000", 1000, 1000, false},
|
||||
{"1000:1001", 1000, 1001, false},
|
||||
{"0", 0, 0, true},
|
||||
{"root", 0, 0, true},
|
||||
{"not-a-user-anywhere", 0, 0, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
cred, err := harnessCredential(tt.user)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("harnessCredential(%q) = %+v, want an error", tt.user, cred)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil || cred.Uid != tt.uid || cred.Gid != tt.gid {
|
||||
t.Errorf("harnessCredential(%q) = %+v, %v; want %d:%d", tt.user, cred, err, tt.uid, tt.gid)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,13 @@ type Request struct {
|
||||
// (image entrypoint and cmd) and the agent-sessions@1 verb tails appended
|
||||
// to it. Prompt must carry {{.Prompt}}; Resume, when set, {{.SessionID}}.
|
||||
type HarnessSpec struct {
|
||||
// User is the Kit's user, a name or "uid[:gid]", that every invocation
|
||||
// runs as. It is never root: the worker's credentials live in the same
|
||||
// container, readable only by the worker's user.
|
||||
User string `json:"user,omitempty"`
|
||||
// Env is the invocation's entire environment. Nothing is inherited from
|
||||
// the worker, whose environment carries its credentials.
|
||||
Env []string `json:"env,omitempty"`
|
||||
Launch []string `json:"launch"`
|
||||
Prompt []string `json:"prompt"`
|
||||
Resume []string `json:"resume,omitempty"`
|
||||
|
||||
Reference in new issue
Block a user