Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(execd): do not run a background script that was not fully written

Maohaoc278bfceb551
diff --git a/components/execd/pkg/runtime/isolated_background.go b/components/execd/pkg/runtime/isolated_background.go
index b1f321a94..4fc2c16ae 100644
--- a/components/execd/pkg/runtime/isolated_background.go
+++ b/components/execd/pkg/runtime/isolated_background.go
@@ -198,9 +198,11 @@ func (r *IsolatedRunner) RunInIsolatedSessionBackground(
// otherwise leave the run stuck `running` (the wrapper's completion lines
// are only parsed after the code). The file is written byte for byte by
// the printf builtin, which is not subject to the 128 KiB limit Linux puts
- // on a single exec argument (and so on `-c`). It is removed when the code
- // exits. The code's output is redirected to the log file; its exit code
- // is captured and written to the code file. The
+ // on a single exec argument (and so on `-c`); if the write fails, the code
+ // is not run and the run exits with 126, as `-c` did for oversized code.
+ // The file is removed when the code exits. Unlike with `-c`, the code sees
+ // that file as $0. The code's output is redirected to the log file; its
+ // exit code is captured and written to the code file. The
// script's mkdir creates the run dir as the session's own uid (so it is
// writable even when the session runs as a different uid than execd); if
// the dir cannot be created or is not writable (e.g. a lower-layer .execd
@@ -218,9 +220,9 @@ func (r *IsolatedRunner) RunInIsolatedSessionBackground(
}
}
scriptFile := "\"$D/" + runID + ".sh\""
- script += "printf '%s' " + shellescape(code) + " >" + scriptFile + "; "
- script += shell + " " + scriptFile + " </dev/null >\"$D/" + runID + ".log\" 2>&1"
- script += "\ncode=$?; rm -f " + scriptFile + "; mkdir -p \"$D\" 2>/dev/null && [ -w \"$D\" ] || D=" +
+ script += "if printf '%s' " + shellescape(code) + " >" + scriptFile + "; then " +
+ shell + " " + scriptFile + " </dev/null >\"$D/" + runID + ".log\" 2>&1; code=$?" +
+ "\nelse code=126; fi; rm -f " + scriptFile + "; mkdir -p \"$D\" 2>/dev/null && [ -w \"$D\" ] || D=" +
shellescape(paths.nsWorkspace)
script += "\necho \"$code\" >\"$D/" + runID + ".code\"; } >/dev/null 2>&1 &\n"
diff --git a/components/execd/pkg/runtime/isolated_background_test.go b/components/execd/pkg/runtime/isolated_background_test.go
index a912c455e..0f18091c4 100644
--- a/components/execd/pkg/runtime/isolated_background_test.go
+++ b/components/execd/pkg/runtime/isolated_background_test.go
@@ -834,9 +834,8 @@ func TestRunInIsolatedSessionBackground_LargeCode(t *testing.T) {
}
// TestRunInIsolatedSessionBackground_RunsCodeVerbatim verifies that the code
-// runs exactly as given: a missing final newline is not added (it would turn
-// a trailing backslash into a line continuation), and the wrapper does not
-// change the caller's environment.
+// reaches the shell byte for byte and that the wrapper does not change the
+// caller's environment.
func TestRunInIsolatedSessionBackground_RunsCodeVerbatim(t *testing.T) {
runner := newTestRunner(t)
id := newBackgroundTestSession(t, runner, "rw")
@@ -847,7 +846,7 @@ func TestRunInIsolatedSessionBackground_RunsCodeVerbatim(t *testing.T) {
envs map[string]string
want string
}{
- {code: `printf x\`, want: `x\`},
+ {code: `printf 'a\\b\n'`, want: "a\\b\n"},
{code: `printf %s "$F"`, envs: map[string]string{"F": "from-caller"}, want: "from-caller"},
} {
runID, _, err := runner.RunInIsolatedSessionBackground(id, tc.code, tc.envs)
@@ -865,6 +864,40 @@ func TestRunInIsolatedSessionBackground_RunsCodeVerbatim(t *testing.T) {
}
}
+// TestRunInIsolatedSessionBackground_ScriptWriteFailure verifies that a
+// script file that cannot be written completely is not run: the run fails
+// with exit code 126 instead of running a truncated prefix of the code.
+func TestRunInIsolatedSessionBackground_ScriptWriteFailure(t *testing.T) {
+ runner := newTestRunner(t)
+ id := newBackgroundTestSession(t, runner, "rw")
+ defer runner.DeleteIsolatedSession(id)
+
+ // Limit file sizes in the session shell, and ignore SIGXFSZ so that an
+ // oversized write fails instead of killing the shell.
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := runner.RunInIsolatedSession(ctx, id, "ulimit -f 8; trap '' XFSZ", nil, nil); err != nil {
+ t.Fatalf("limit file size: %v", err)
+ }
+
+ code := "echo truncated-prefix-ran\n#" + strings.Repeat("x", 150*1024) + "\necho done"
+ runID, _, err := runner.RunInIsolatedSessionBackground(id, code, nil)
+ if err != nil {
+ t.Fatalf("RunInIsolatedSessionBackground: %v", err)
+ }
+ snapshot := waitForBackgroundRun(t, runner, id, runID)
+ output, _, err := runner.SeekIsolatedBackgroundOutput(id, runID, 0)
+ if err != nil {
+ t.Fatalf("SeekIsolatedBackgroundOutput: %v", err)
+ }
+ if snapshot.ExitCode == nil || *snapshot.ExitCode != 126 {
+ t.Errorf("ExitCode = %v, want 126; output = %q", snapshot.ExitCode, output)
+ }
+ if strings.Contains(string(output), "truncated-prefix-ran") {
+ t.Errorf("a truncated script ran: output = %q", output)
+ }
+}
+
// TestBackgroundRunCompletesAfterRunDirDeleted verifies that a background
// command deleting its own .execd/background-runs directory cannot strand the
// run: the exit-code marker falls back to the workspace root and the watcher