diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index acd0d26358..14e775d647 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -26,6 +26,7 @@ jobs: filters: | go: - '**/*.go' + - 'pkg/inject/**' - 'go.mod' - 'go.sum' - '.goreleaser.yml' @@ -121,6 +122,18 @@ jobs: printf '%s\n' "$packages" | xargs go test -race -short + - name: test agent bootstrap with BusyBox sh + if: runner.os == 'Linux' + shell: bash + run: | + set -euo pipefail + sudo apt-get update -qq + sudo apt-get install -y busybox-static + original_sh="$(readlink /bin/sh)" + trap 'sudo ln -sf "$original_sh" /bin/sh' EXIT + sudo ln -sf /bin/busybox /bin/sh + go test -race -count=1 -timeout 90s ./pkg/inject -run '^(TestBootstrapSuite|TestInjectScript_RealShell.*)$' + - name: test E2E runtime helpers run: go test -race -count=1 ./e2e/framework/runenv/... ./e2e/framework/dockerwsl ./e2e/cmd/devsy-ci @@ -216,6 +229,7 @@ jobs: matrix: include: - runner: ubuntu-latest + job_timeout_minutes: 45 - runner: macos-latest - runner: windows-latest # Cold builds need room to save the Go cache after artifact uploads. @@ -265,14 +279,28 @@ jobs: name: devsy-ci-podman-${{ steps.os.outputs.runner_os }} path: ${{ runner.temp }}/devsy-ci-podman* + - name: disk usage before Linux snapshot + if: runner.os == 'Linux' + run: | + df -h /tmp "$RUNNER_TEMP" "$GITHUB_WORKSPACE" + du -sh "$(go env GOCACHE)" "$(go env GOMODCACHE)" + - uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7 with: distribution: goreleaser version: "~> v2" - args: build --id devsy-${{ steps.os.outputs.runner_os }} --snapshot + args: >- + build --id devsy-${{ steps.os.outputs.runner_os }} --snapshot + ${{ runner.os == 'Linux' && '--parallelism=1' || '' }} env: DEVSY_CLI_VERSION: v0.0.0 + - name: disk usage after Linux snapshot + if: always() && runner.os == 'Linux' + run: | + df -h /tmp "$RUNNER_TEMP" "$GITHUB_WORKSPACE" + du -sh "$(go env GOCACHE)" "$(go env GOMODCACHE)" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: name: devsy-${{ steps.os.outputs.runner_os }} diff --git a/pkg/agent/delivery/exec_stream_cancellation_unix_test.go b/pkg/agent/delivery/exec_stream_cancellation_unix_test.go index a73b98a70f..528b4930a4 100644 --- a/pkg/agent/delivery/exec_stream_cancellation_unix_test.go +++ b/pkg/agent/delivery/exec_stream_cancellation_unix_test.go @@ -129,27 +129,17 @@ func cancellationDeliveryExecutor( t *testing.T, runtime driver.ArgvExecDriver, canceled chan<- error, ) func(context.Context, []string, driver.Streams) error { return func(ctx context.Context, argv []string, streams driver.Streams) error { + err := runtime.CommandContainerArgv(ctx, "owned-delivery-test", argv, streams) if streams.Stdin != nil { - callbackDone := make(chan struct{}) - stop := context.AfterFunc(ctx, func() { - defer close(callbackDone) + if cause := context.Cause(ctx); cause != nil { select { - case canceled <- context.Cause(ctx): + case canceled <- cause: default: t.Error("unexpected extra attempt cancellation") } - }) - defer func() { - if !stop() { - select { - case <-callbackDone: - case <-time.After(execStreamCancellationTestBound): - t.Error("attempt observer callback did not join") - } - } - }() + } } - return runtime.CommandContainerArgv(ctx, "owned-delivery-test", argv, streams) + return err } } diff --git a/pkg/agent/inject.go b/pkg/agent/inject.go index 65ff6c2988..011220ae22 100644 --- a/pkg/agent/inject.go +++ b/pkg/agent/inject.go @@ -82,6 +82,11 @@ func (o *InjectOptions) Validate() error { if o.Exec == nil { return fmt.Errorf("exec function is required") } + for _, value := range []string{o.RemoteAgentPath, o.RemoteVersion, o.DownloadURL, o.Command} { + if strings.ContainsRune(value, 0) { + return fmt.Errorf("agent injection parameter contains NUL") + } + } return nil } @@ -277,12 +282,16 @@ type injectConfig struct { } func injectAgent(ctx context.Context, cfg *injectConfig) error { + params, err := buildScriptParams(cfg) + if err != nil { + return err + } buf := &bytes.Buffer{} //nolint:staticcheck wasExecuted, err := inject.Inject(ctx, inject.InjectOptions{ Exec: cfg.opts.Exec, LocalFile: createBinaryLoader(ctx, cfg), - ScriptParams: buildScriptParams(cfg), + ScriptParams: params, Stdin: cfg.opts.Stdin, Stdout: cfg.opts.Stdout, Stderr: setupStderr(cfg.opts, buf), @@ -312,16 +321,20 @@ func createBinaryLoader(ctx context.Context, cfg *injectConfig) func(bool) (io.R } } -func buildScriptParams(cfg *injectConfig) *inject.Params { +func buildScriptParams(cfg *injectConfig) (*inject.Params, error) { opts := cfg.opts + existsCheck, err := cfg.vc.buildExistsCheck(opts.RemoteAgentPath) + if err != nil { + return nil, err + } return &inject.Params{ Command: opts.Command, AgentRemotePath: opts.RemoteAgentPath, DownloadURLs: inject.NewDownloadURLs(opts.DownloadURL), - ExistsCheck: cfg.vc.buildExistsCheck(opts.RemoteAgentPath), + ExistsCheck: existsCheck, PreferAgentDownload: *opts.PreferDownloadFromRemoteUrl, ShouldChmodPath: true, - } + }, nil } func handleInjectError(err error, wasExecuted bool, buf *bytes.Buffer) error { @@ -394,12 +407,20 @@ func newVersionChecker(opts *InjectOptions) *versionChecker { } } -func (vc *versionChecker) buildExistsCheck(agentPath string) string { +func (vc *versionChecker) buildExistsCheck(agentPath string) (string, error) { + quotedPath, err := inject.QuotePOSIXLiteral(agentPath) + if err != nil { + return "", err + } if vc.skipCheck { - return fmt.Sprintf(`! [ -x "%s" ]`, agentPath) + return fmt.Sprintf(`! [ -x %s ]`, quotedPath), nil } - return fmt.Sprintf(`! { [ -x "%s" ] && [ "$("%s" --version 2>/dev/null)" = "%s" ]; }`, - agentPath, agentPath, vc.remoteVersion) + quotedVersion, err := inject.QuotePOSIXLiteral(vc.remoteVersion) + if err != nil { + return "", err + } + return fmt.Sprintf(`! { [ -x %s ] && [ "$(%s --version 2>/dev/null)" = %s ]; }`, + quotedPath, quotedPath, quotedVersion), nil } // session encapsulates the state and resources for an injection session. @@ -409,12 +430,16 @@ func (vc *versionChecker) detectRemoteAgentVersion( agentPath string, ) (string, error) { buf := &bytes.Buffer{} - versionCmd := fmt.Sprintf("%s --version", agentPath) + quotedPath, err := inject.QuotePOSIXLiteral(agentPath) + if err != nil { + return "", err + } + versionCmd := fmt.Sprintf("%s --version", quotedPath) checkCtx, cancel := context.WithTimeout(ctx, versionCheckTimeout) defer cancel() - log.Debugf("checking remote agent version: %s", versionCmd) - err := exec(checkCtx, versionCmd, nil, buf, io.Discard) + log.Debug("checking remote agent version") + err = exec(checkCtx, versionCmd, nil, buf, io.Discard) if err != nil { if errors.Is(checkCtx.Err(), context.DeadlineExceeded) && ctx.Err() == nil { return "", fmt.Errorf( diff --git a/pkg/agent/inject_literal_test.go b/pkg/agent/inject_literal_test.go new file mode 100644 index 0000000000..8f1d8e03fd --- /dev/null +++ b/pkg/agent/inject_literal_test.go @@ -0,0 +1,95 @@ +//nolint:gosec // These regressions execute generated shell checks and a private executable fixture. +package agent + +import ( + "context" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/inject" + "github.com/stretchr/testify/require" +) + +func TestVersionCheckerLiteralPathAndVersion(t *testing.T) { + if _, err := exec.LookPath("/bin/sh"); err != nil { + t.Skip("POSIX /bin/sh is unavailable") + } + root := t.TempDir() + agentPath := filepath.Join(root, "agent '\"\\\n雪 $(touch sentinel)`touch sentinel`") + version := "v1 ' \" \\ 雪 $(touch sentinel)`touch sentinel`" + literalVersion, err := inject.QuotePOSIXLiteral(version) + require.NoError(t, err) + require.NoError( + t, + os.WriteFile(agentPath, []byte("#!/bin/sh\nprintf '%s\\n' "+literalVersion+"\n"), 0o700), + ) + vc := &versionChecker{remoteVersion: version} + predicate, err := vc.buildExistsCheck(agentPath) + require.NoError(t, err) + cmd := exec.CommandContext( + t.Context(), + "/bin/sh", + "-c", + "if "+predicate+"; then printf install; else printf reuse; fi", + ) + cmd.Dir = root + output, err := cmd.CombinedOutput() + require.NoError(t, err, string(output)) + require.Equal(t, "reuse", string(output)) + + run := func(ctx context.Context, code string, stdin io.Reader, stdout, stderr io.Writer) error { + command := exec.CommandContext(ctx, "/bin/sh", "-c", code) + command.Dir = root + command.Stdin, command.Stdout, command.Stderr = stdin, stdout, stderr + return command.Run() + } + detected, err := vc.detectRemoteAgentVersion(t.Context(), run, agentPath) + require.NoError(t, err) + require.Equal(t, version, detected) + _, err = os.Stat(filepath.Join(root, "sentinel")) + require.True(t, os.IsNotExist(err)) + + vc.remoteVersion = "mismatch" + predicate, err = vc.buildExistsCheck(agentPath) + require.NoError(t, err) + require.NoError(t, exec.CommandContext(t.Context(), "/bin/sh", "-c", predicate).Run()) +} + +func TestVersionCheckerRejectsNULBeforeExecution(t *testing.T) { + vc := &versionChecker{remoteVersion: "v1"} + _, err := vc.buildExistsCheck("bad\x00path") + require.Error(t, err) + vc.remoteVersion = "v1\x00bad" + _, err = vc.buildExistsCheck("/tmp/agent") + require.Error(t, err) + _, err = vc.detectRemoteAgentVersion( + t.Context(), + func(context.Context, string, io.Reader, io.Writer, io.Writer) error { + t.Fatal("must not execute an invalid path") + return nil + }, + "bad\x00path", + ) + require.Error(t, err) + + for _, field := range []string{"path", "version", "url", "command"} { + opts := &InjectOptions{ + Exec: func(context.Context, string, io.Reader, io.Writer, io.Writer) error { return nil }, + } + switch field { + case "path": + opts.RemoteAgentPath = "bad\x00path" + case "version": + opts.RemoteVersion = "bad\x00version" + case "url": + opts.DownloadURL = "bad\x00url" + case "command": + opts.Command = "bad\x00command" + } + require.Error(t, opts.Validate(), strings.ToUpper(field)) + } +} diff --git a/pkg/inject/dockerless_test.go b/pkg/inject/dockerless_test.go new file mode 100644 index 0000000000..d1c9ce00f6 --- /dev/null +++ b/pkg/inject/dockerless_test.go @@ -0,0 +1,117 @@ +//nolint:gosec // Generated scripts and interpreter fixtures execute only under private test directories. +package inject + +import ( + "bytes" + "context" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const ( + standardInterpreter = "standard" + bundledInterpreter = "bundled" +) + +type installerShellCase struct { + name, flag, selected string + standard, bundled bool +} + +func (s *BootstrapSuite) TestInstallerShellSelection() { + for _, tc := range []installerShellCase{ + {name: "ordinary", standard: true, bundled: true, selected: standardInterpreter}, + { + name: "dockerless keeps standard", flag: strconv.FormatBool(true), + standard: true, bundled: true, selected: standardInterpreter, + }, + {name: "dockerless bundled", flag: strconv.FormatBool(true), bundled: true, selected: bundledInterpreter}, + {name: "ordinary missing standard", bundled: true}, + {name: "disabled dockerless", flag: "false", bundled: true}, + {name: "missing both", flag: strconv.FormatBool(true)}, + } { + s.Run(tc.name, func() { + root := s.T().TempDir() + installPath := filepath.Join(root, "agent '\\\n雪 $(touch substitution-ran)") + original := []byte("working-agent") + s.Require().NoError(os.WriteFile(installPath, original, 0o750)) + params := bootstrapParams(installPath) + params.Command = "printf 'ready\\n'" + script, err := GenerateScript(Script, params) + s.Require().NoError(err) + script = installerShellFixture(s.T(), script, root, tc) + ctx, cancel := context.WithTimeout(s.T().Context(), 5*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, "/bin/sh", "-c", script) + cmd.Dir = root + cmd.Env = append(os.Environ(), "DOCKERLESS="+tc.flag) + payload := bytes.Repeat([]byte{0, '\'', '\\', '$', '\n', 255}, 700) + cmd.Stdin = bytes.NewReader(binaryInput(payload)) + var stdout, stderr bytes.Buffer + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err = cmd.Run() + if tc.selected == "" { + s.Require().Error(err) + s.Contains(stderr.String(), "requires executable /bin/sh") + s.NotContains(stderr.String(), "su:") + s.Equal("ping\n", stdout.String()) + payload = original + } else { + s.Require().NoError(err, stderr.String()) + s.True(strings.HasSuffix(stdout.String(), "done\nready\n"), stdout.String()) + s.Equal(1, strings.Count(stdout.String(), "done\n")) + } + installed, readErr := os.ReadFile(installPath) + s.Require().NoError(readErr) + s.Equal(payload, installed) + info, statErr := os.Stat(installPath) + s.Require().NoError(statErr) + if tc.selected == "" { + s.Equal(os.FileMode(0o750), info.Mode().Perm()) + } else { + s.Equal(os.FileMode(0o755), info.Mode().Perm()) + } + for _, name := range []string{standardInterpreter, bundledInterpreter} { + _, statErr := os.Stat(filepath.Join(root, name+"-used")) + s.Equal(name == tc.selected, statErr == nil) + } + _, statErr = os.Stat(filepath.Join(root, "substitution-ran")) + s.True(os.IsNotExist(statErr)) + }) + } +} + +func installerShellFixture(t *testing.T, script, root string, tc installerShellCase) string { + t.Helper() + for _, interpreter := range []struct { + assignment, name string + available bool + }{ + {"INSTALLER_SHELL=/bin/sh", standardInterpreter, tc.standard}, + {"DOCKERLESS_SHELL=/.dockerless/bin/sh", bundledInterpreter, tc.bundled}, + } { + fixture := filepath.Join(root, interpreter.name+" shell '") + if interpreter.available { + marker, quoteErr := QuotePOSIXLiteral(filepath.Join(root, interpreter.name+"-used")) + require.NoError(t, quoteErr) + require.NoError(t, os.WriteFile(fixture, []byte( + "#!/bin/sh\nprintf x >> "+marker+"\nexec /bin/sh \"$@\"\n", + ), 0o700)) + } + quoted, quoteErr := QuotePOSIXLiteral(fixture) + require.NoError(t, quoteErr) + require.Equal(t, 1, strings.Count(script, interpreter.assignment)) + // Model an isolated target's fixed paths without changing the host filesystem. + script = strings.Replace(script, interpreter.assignment, strings.SplitN( + interpreter.assignment, "=", 2, + )[0]+"="+quoted, 1) + } + return script +} diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go index ef65c34817..0f8ed102f1 100644 --- a/pkg/inject/inject.go +++ b/pkg/inject/inject.go @@ -64,14 +64,14 @@ func Inject(ctx context.Context, opts InjectOptions) (bool, error) { logPreferredAgentDownloadURL(opts.ScriptParams) scriptRawCode, err := GenerateScript(Script, opts.ScriptParams) if err != nil { - return true, err + return false, err } log.Debug("execute inject script") defer log.Debug("done injecting") sess, err := newSession(opts, scriptRawCode, start) if err != nil { - return true, err + return false, err } defer sess.Close() select { diff --git a/pkg/inject/inject.sh b/pkg/inject/inject.sh index 07df29c3df..f49b8c103a 100644 --- a/pkg/inject/inject.sh +++ b/pkg/inject/inject.sh @@ -1,11 +1,15 @@ #!/bin/sh set -e -INSTALL_DIR="{{ .InstallDir }}" -INSTALL_FILENAME="{{ .InstallFilename }}" +INSTALL_DIR={{ .InstallDir }} +INSTALL_FILENAME={{ .InstallFilename }} INSTALL_PATH="$INSTALL_DIR/$INSTALL_FILENAME" -PREFER_DOWNLOAD="{{ .PreferDownload }}" -CHMOD_PATH="{{ .ChmodPath }}" +PREFER_DOWNLOAD={{ .PreferDownload }} +CHMOD_PATH={{ .ChmodPath }} +INSTALLER_MODE=direct +INSTALLER_SHELL=/bin/sh +DOCKERLESS_SHELL=/.dockerless/bin/sh +temp_file= command_exists() { command -v "$@" >/dev/null 2>&1 @@ -18,8 +22,51 @@ is_arm() { esac } +quote_installer_argument() { + quote_value=$1 + quoted_argument="'" + while :; do + case "$quote_value" in + *"'"*) + quoted_argument="$quoted_argument${quote_value%%\'*}'\\''" + quote_value=${quote_value#*\'} + ;; + *) break ;; + esac + done + quoted_argument="$quoted_argument$quote_value'" +} + +run_installer_script() { + installer_code=$1 + shift + case "$INSTALLER_MODE" in + direct) "$INSTALLER_SHELL" -c "$installer_code" devsy-install "$@" ;; + sudo) sudo -n -E "$INSTALLER_SHELL" -c "$installer_code" devsy-install "$@" ;; + su) + # su implementations disagree about forwarding arguments after -c. + quote_installer_argument "$installer_code" + su_command=" -c $quoted_argument devsy-install" + quote_installer_argument "$INSTALLER_SHELL" + su_command="$quoted_argument$su_command" + for installer_argument; do + quote_installer_argument "$installer_argument" + su_command="$su_command $quoted_argument" + done + su -s "$INSTALLER_SHELL" -c "$su_command" root + ;; + esac +} + +cleanup_temp() { + if [ -n "$temp_file" ]; then + run_installer_script 'rm -f -- "$1"' "$temp_file" || return 1 + temp_file= + fi +} + check_noexec() { - mount_path="$(df "${INSTALL_DIR}" | tail -n +2 | rev | cut -d' ' -f1 | rev)" + mount_path="$(df "$INSTALL_DIR" | tail -n +2 | rev | cut -d' ' -f1 | rev)" if mount | grep "on ${mount_path} " | grep -q noexec; then >&2 echo "Error: Installation directory $INSTALL_DIR is mounted with noexec flag" return 1 @@ -29,131 +76,120 @@ check_noexec() { handshake() { echo "ping" - read -r DEVSY_PING + read -r DEVSY_PING || return 1 if [ "$DEVSY_PING" != "pong" ]; then >&2 echo "Error: Handshake failed - received '$DEVSY_PING' instead of 'pong'" - exit 1 + return 1 fi } -inject_binary() { - echo "ARM-$(is_arm && echo -n 'true' || echo -n 'false')" +prepare_temp() { + cleanup_temp || return 1 + temp_file=$(run_installer_script 'mktemp "$1.XXXXXX"' "$INSTALL_PATH") || { + >&2 echo "Error: Failed to create a temporary agent in $INSTALL_DIR" + return 1 + } +} - temp_file="$(mktemp "$INSTALL_PATH.XXXXXX" 2>/dev/null || echo "$INSTALL_PATH.$$-$(date +%s)")" +publish_binary() { + if [ "$CHMOD_PATH" = "true" ]; then + run_installer_script 'chmod -- 0755 "$1"' "$temp_file" || { + >&2 echo "Error: Failed to make temporary agent executable" + return 1 + } + fi + run_installer_script '[ ! -d "$2" ] && mv -f -- "$1" "$2"' "$temp_file" "$INSTALL_PATH" || { + >&2 echo "Error: Failed to replace agent at $INSTALL_PATH" + return 1 + } + temp_file= +} +inject_binary() { + echo "ARM-$(is_arm && printf true || printf false)" if ! read -r BINARY_SIZE; then >&2 echo "Error: Failed to read binary size" return 1 fi - case "$BINARY_SIZE" in '' | *[!0-9]*) >&2 echo "Error: Invalid binary size: $BINARY_SIZE" return 1 ;; esac - - if ! $sh_c "head -c \"$BINARY_SIZE\" > \"$temp_file\""; then - >&2 echo "Error: Failed to write binary to $temp_file" - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true + prepare_temp || return 1 + run_installer_script 'head -c "$1" > "$2" && [ "$(wc -c < "$2" | tr -d "[:space:]")" = "$1" ]' "$BINARY_SIZE" "$temp_file" || { + >&2 echo "Error: Failed to receive complete agent binary" + cleanup_temp || true return 1 - fi - - if [ "$CHMOD_PATH" = "true" ]; then - $sh_c "chmod +x \"$temp_file\"" || { - >&2 echo "Error: Failed to make $temp_file executable" - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true - return 1 - } - fi - - if ! $sh_c "mv \"$temp_file\" \"$INSTALL_PATH\""; then - >&2 echo "Error: Failed to move binary to $INSTALL_PATH" - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true + } + publish_binary || { + cleanup_temp || true return 1 - fi - - return 0 + } } download_binary() { - DOWNLOAD_URL="{{ .DownloadAmd }}" + DOWNLOAD_URL={{ .DownloadAmd }} if is_arm; then - DOWNLOAD_URL="{{ .DownloadArm }}" + DOWNLOAD_URL={{ .DownloadArm }} fi - iteration=1 max_iteration=3 - while [ "$iteration" -le "$max_iteration" ]; do - temp_file="$(mktemp "$INSTALL_PATH.XXXXXX" 2>/dev/null || echo "$INSTALL_PATH.$$-$(date +%s)")" - trap 'rm -f "$temp_file"' EXIT - + prepare_temp || return 1 if command_exists curl; then - $sh_c "curl -fsSL \"$DOWNLOAD_URL\" -o \"$temp_file\"" && break + run_installer_script 'curl -fsSL --connect-timeout 10 --max-time 120 -o "$2" -- "$1"' "$DOWNLOAD_URL" "$temp_file" && break cmd_status=$? elif command_exists wget; then - $sh_c "wget -q \"$DOWNLOAD_URL\" -O \"$temp_file\"" && break + run_installer_script 'wget -q -T 120 -O "$2" -- "$1"' "$DOWNLOAD_URL" "$temp_file" && break cmd_status=$? else >&2 echo "Error: No download tool found (curl or wget required)" + cleanup_temp || true return 127 fi - - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true - - >&2 echo "Error: Download attempt $iteration failed (exit code: ${cmd_status})" - >&2 echo " URL: $DOWNLOAD_URL" - + cleanup_temp || return 1 + >&2 echo "Error: Download attempt $iteration failed (exit code: $cmd_status)" if [ "$iteration" -lt "$max_iteration" ]; then >&2 echo " Retrying in 10 seconds" - sleep 10 + sleep 10 || return 1 fi iteration=$((iteration + 1)) done - if [ "$iteration" -gt "$max_iteration" ]; then >&2 echo "Error: Failed to download devsy after $max_iteration attempts" return 1 fi - - if [ "$CHMOD_PATH" = "true" ]; then - $sh_c "chmod +x \"$temp_file\"" || { - >&2 echo "Error: Failed to make $temp_file executable" - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true - return 1 - } - fi - - if ! $sh_c "mv \"$temp_file\" \"$INSTALL_PATH\""; then - >&2 echo "Error: Failed to move downloaded binary to $INSTALL_PATH" - $sh_c "rm -f \"$temp_file\"" 2>/dev/null || true + publish_binary || { + cleanup_temp || true return 1 - fi - - return 0 + } } setup_sudo() { - if ! mkdir -p "$INSTALL_DIR" 2>/dev/null || - ! touch "$INSTALL_PATH" 2>/dev/null || - ! chmod +x "$INSTALL_PATH" 2>/dev/null || - ! rm -f "$INSTALL_PATH" 2>/dev/null; then - + # Dockerless injects before the image filesystem supplies /bin/sh. + if [ "${DOCKERLESS:-}" = "true" ] && [ ! -x "$INSTALLER_SHELL" ] && [ -x "$DOCKERLESS_SHELL" ]; then + INSTALLER_SHELL=$DOCKERLESS_SHELL + fi + if [ ! -x "$INSTALLER_SHELL" ]; then + >&2 echo "Error: Agent installation requires executable /bin/sh (or the bundled Dockerless shell in Dockerless containers)" + return 1 + fi + if ! run_installer_script 'mkdir -p -- "$1" && probe=$(mktemp "$1/.devsy-write.XXXXXX") && rm -f -- "$probe"' "$INSTALL_DIR" 2>/dev/null; then if command_exists sudo; then if ! sudo -nl >/dev/null 2>&1; then >&2 echo "Error: sudo requires a password. Please configure NOPASSWD" - exit 1 + return 1 fi - sh_c='sudo -E sh -c' + INSTALLER_MODE=sudo elif command_exists su; then - sh_c='su -c' + INSTALLER_MODE=su else >&2 echo "Error: Need root access but sudo/su not available" - exit 1 + return 1 fi - - $sh_c "mkdir -p \"$INSTALL_DIR\"" + run_installer_script 'mkdir -p -- "$1"' "$INSTALL_DIR" || return 1 fi } @@ -163,7 +199,7 @@ install_agent() { >&2 echo "Download failed, attempting stdin injection" inject_binary || { >&2 echo "Error: Both download and injection failed" - exit 1 + return 1 } fi else @@ -171,7 +207,7 @@ install_agent() { >&2 echo "Injection failed, attempting download" download_binary || { >&2 echo "Error: Both injection and download failed" - exit 1 + return 1 } fi fi @@ -179,22 +215,20 @@ install_agent() { execute_command() { echo "done" - export DEVSY_AGENT_URL="{{ .DownloadBase }}" + export DEVSY_AGENT_URL={{ .DownloadBase }} {{ .Command }} } main() { - handshake - + handshake || return 1 if {{ .ExistsCheck }}; then - sh_c='sh -c' - setup_sudo - check_noexec || exit 1 - $sh_c "rm -f \"$INSTALL_PATH\" 2>/dev/null || true" - install_agent + setup_sudo || return 1 + check_noexec || return 1 + install_agent || return 1 fi - execute_command } +trap 'cleanup_temp' 0 +trap 'exit 1' HUP INT TERM main diff --git a/pkg/inject/installer_modes_test.go b/pkg/inject/installer_modes_test.go new file mode 100644 index 0000000000..5f9359ce02 --- /dev/null +++ b/pkg/inject/installer_modes_test.go @@ -0,0 +1,143 @@ +//nolint:gosec // Generated scripts and privilege-command doubles execute only in private test directories. +package inject + +import ( + "bytes" + "context" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const ( + testSudoMode = "sudo" + testSuMode = "su" +) + +const installerModeDoubles = ` +sudo() { + [ "$#" -ge 6 ] && [ "$1" = -n ] && [ "$2" = -E ] && + [ "$3" = /bin/sh ] && [ "$4" = -c ] && [ "$6" = devsy-install ] || return 98 + printf 'sudo\n' >> "$INSTALLER_TEST_MARKER" || return 99 + shift 2 + "$@" +} +su() { + [ "$#" -eq 5 ] && [ "$1" = -s ] && [ "$2" = /bin/sh ] && + [ "$3" = -c ] && [ "$5" = root ] || return 98 + printf 'su\n' >> "$INSTALLER_TEST_MARKER" || return 99 + "$2" -c "$4" +} +` + +func installerModeFixture(t *testing.T, script, mode, marker string) string { + t.Helper() + require.Contains(t, []string{testSudoMode, testSuMode}, mode) + const assignment = "INSTALLER_MODE=direct" + require.Equal(t, 1, strings.Count(script, assignment)) + quotedMarker, err := QuotePOSIXLiteral(marker) + require.NoError(t, err) + return "INSTALLER_TEST_MARKER=" + quotedMarker + "\n" + installerModeDoubles + + strings.Replace(script, assignment, "INSTALLER_MODE="+mode, 1) +} + +func (s *BootstrapSuite) TestPrivilegedInstallerModesLiveStream() { + for _, mode := range []string{testSudoMode, testSuMode} { + s.Run(mode, func() { s.checkInstallerModeLiveStream(mode) }) + } +} + +func (s *BootstrapSuite) checkInstallerModeLiveStream(mode string) { + const liveInput = "live-tunnel\x00bytes\n" + root := s.T().TempDir() + marker := filepath.Join(root, "mode-used") + params := bootstrapParams(filepath.Join( + root, "dir '\\\n雪", "agent $(touch substitution-ran)`touch substitution-ran`", + )) + params.DownloadURLs.Base = "https://invalid/'\"`$(touch substitution-ran)\\\r\n雪" + params.Command = fmt.Sprintf("printf '%%s\\n' \"$DEVSY_AGENT_URL\"; head -c %d", len(liveInput)) + rendered, err := GenerateScript(Script, params) + s.Require().NoError(err) + fixture := installerModeFixture(s.T(), rendered, mode, marker) + payload := bytes.Repeat([]byte{0, '\'', '\\', '"', '$', '\n', '\r', 255}, 512) + var rawOutput, stderr syncBuffer + var userOutput bytes.Buffer + executed, err := Inject(s.T().Context(), InjectOptions{ + Exec: func(ctx context.Context, script string, stdin io.Reader, stdout, diagnostics io.Writer) error { + if script != rendered { + return fmt.Errorf("unexpected rendered bootstrap") + } + cmd := exec.CommandContext(ctx, "/bin/sh", "-c", fixture) + cmd.Dir = root + cmd.Stdin, cmd.Stdout, cmd.Stderr = stdin, io.MultiWriter( + stdout, + &rawOutput, + ), diagnostics + return cmd.Run() + }, + LocalFile: func(bool) (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(payload)), nil }, + ScriptParams: params, + Stdin: strings.NewReader(liveInput), + Stdout: &userOutput, + Stderr: &stderr, + Timeout: 5 * time.Second, + }) + s.Require().NoError(err, stderr.String()) + s.True(executed) + s.Empty(stderr.String()) + s.Equal(params.DownloadURLs.Base+"\n"+liveInput, userOutput.String()) + s.True(strings.HasPrefix(rawOutput.String(), "ping\nARM-"), rawOutput.String()) + s.Equal(1, strings.Count(rawOutput.String(), "done\n")) + s.True(strings.HasSuffix(rawOutput.String(), "done\n"+userOutput.String())) + installed, err := os.ReadFile(params.AgentRemotePath) + s.Require().NoError(err) + s.Equal(payload, installed) + info, err := os.Stat(params.AgentRemotePath) + s.Require().NoError(err) + s.Equal(os.FileMode(0o755), info.Mode().Perm()) + assertInstallerModeUsed(s.T(), marker, mode) + _, err = os.Stat(filepath.Join(root, "substitution-ran")) + s.True(os.IsNotExist(err)) +} + +func (s *BootstrapSuite) TestPrivilegedInstallerFailureStatus() { + for _, mode := range []string{testSudoMode, testSuMode} { + s.Run(mode, func() { + root := s.T().TempDir() + marker := filepath.Join(root, "mode-used") + script, err := GenerateScript(Script, bootstrapParams(filepath.Join(root, "agent"))) + s.Require().NoError(err) + script = installerModeFixture(s.T(), script, mode, marker) + const entry = "\nmain\n" + s.Require().Equal(1, strings.Count(script, entry)) + script = strings.Replace(script, entry, "\nrun_installer_script 'exit \"$1\"' 37\n", 1) + ctx, cancel := context.WithTimeout(s.T().Context(), 5*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, "/bin/sh", "-c", script) + output, err := cmd.CombinedOutput() + var exitErr *exec.ExitError + s.Require().ErrorAs(err, &exitErr) + s.Equal(37, exitErr.ExitCode()) + s.Empty(output) + assertInstallerModeUsed(s.T(), marker, mode) + }) + } +} + +func assertInstallerModeUsed(t *testing.T, marker, mode string) { + t.Helper() + used, err := os.ReadFile(marker) + require.NoError(t, err) + lines := strings.Fields(string(used)) + require.NotEmpty(t, lines) + for _, line := range lines { + require.Equal(t, mode, line) + } +} diff --git a/pkg/inject/script.go b/pkg/inject/script.go index 45eda1ac1f..1ee6fce59c 100644 --- a/pkg/inject/script.go +++ b/pkg/inject/script.go @@ -1,6 +1,7 @@ package inject import ( + "fmt" "path" "strconv" "strings" @@ -9,7 +10,7 @@ import ( ) func GenerateScript(script string, params *Params) (string, error) { - rawCode, err := template.FillTemplate(script, map[string]string{ + values := map[string]string{ "Command": params.Command, "ExistsCheck": params.ExistsCheck, "InstallDir": params.InstallDir(), @@ -19,12 +20,33 @@ func GenerateScript(script string, params *Params) (string, error) { "DownloadBase": params.DownloadURLs.Base, "DownloadAmd": params.DownloadURLs.Amd, "DownloadArm": params.DownloadURLs.Arm, - }) + } + for _, name := range []string{"Command", "ExistsCheck"} { + if strings.ContainsRune(values[name], 0) { + return "", fmt.Errorf("%s contains NUL", name) + } + } + for _, name := range []string{"InstallDir", "InstallFilename", "DownloadBase", "DownloadAmd", "DownloadArm"} { + quoted, err := QuotePOSIXLiteral(values[name]) + if err != nil { + return "", fmt.Errorf("quote %s: %w", name, err) + } + values[name] = quoted + } + rawCode, err := template.FillTemplate(stripCarriageReturns(script), values) if err != nil { return "", err } - return stripCarriageReturns(rawCode), nil + return rawCode, nil +} + +// QuotePOSIXLiteral encodes one literal word for a POSIX shell, not a login shell. +func QuotePOSIXLiteral(value string) (string, error) { + if strings.ContainsRune(value, 0) { + return "", fmt.Errorf("shell literal contains NUL") + } + return "'" + strings.ReplaceAll(value, "'", "'\\''") + "'", nil } type Params struct { diff --git a/pkg/inject/script_test.go b/pkg/inject/script_test.go new file mode 100644 index 0000000000..f7bb93c5a9 --- /dev/null +++ b/pkg/inject/script_test.go @@ -0,0 +1,248 @@ +//nolint:gosec // These regressions execute generated scripts and executable fixtures under private test directories. +package inject + +import ( + "bytes" + "context" + "crypto/rand" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type BootstrapSuite struct{ suite.Suite } + +func TestBootstrapSuite(t *testing.T) { suite.Run(t, new(BootstrapSuite)) } + +func (s *BootstrapSuite) SetupSuite() { + if _, err := exec.LookPath("/bin/sh"); err != nil { + s.T().Skip("POSIX /bin/sh is unavailable") + } +} + +func (s *BootstrapSuite) TestLiteralRoundTrip() { + for _, value := range []string{"", "a'b\\c\n雪\r", "$(exit 42)`exit 43`\";$HOME()"} { + s.Run(fmt.Sprintf("%q", value), func() { + quoted, err := QuotePOSIXLiteral(value) + s.Require().NoError(err) + cmd := exec.CommandContext(s.T().Context(), "/bin/sh", "-c", "printf %s "+quoted) + output, err := cmd.Output() + s.Require().NoError(err) + s.Equal(value, string(output)) + }) + } + _, err := QuotePOSIXLiteral("a\x00b") + s.Error(err) +} + +func bootstrapParams(installPath string) *Params { + return &Params{ + Command: "cat", AgentRemotePath: installPath, DownloadURLs: &DownloadURLs{}, + ExistsCheck: "true", ShouldChmodPath: true, + } +} + +func runBootstrap( + t *testing.T, + params *Params, + stdin []byte, + faultHooks ...string, +) (string, string, error) { + t.Helper() + script, err := GenerateScript(Script, params) + require.NoError(t, err) + if len(faultHooks) > 0 { + hooks := strings.Join(faultHooks, "\n") + "\n" + quotedHooks, quoteErr := QuotePOSIXLiteral(hooks) + require.NoError(t, quoteErr) + const installerAssignment = "installer_code=$1" + require.Equal(t, 1, strings.Count(script, installerAssignment)) + // BusyBox standalone applets bypass PATH doubles; functions override them in both shells. + script = hooks + strings.Replace( + script, installerAssignment, "installer_code="+quotedHooks+"\"$1\"", 1, + ) + } + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, "/bin/sh", "-c", script) + cmd.Stdin = bytes.NewReader(stdin) + var stdout, stderr bytes.Buffer + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err = cmd.Run() + return stdout.String(), stderr.String(), err +} + +func binaryInput(binary []byte) []byte { + return append(fmt.Appendf(nil, "pong\n%d\n", len(binary)), binary...) +} + +func bootstrapFailureHooks(chmodFails bool) string { + hooks := "curl() { return 1; }\nsleep() { :; }\n" + if chmodFails { + hooks += "chmod() { printf '%s\\n' 'injected chmod failure' >&2; return 1; }\n" + } + return hooks +} + +func (s *BootstrapSuite) TestHostilePathAndLiveBinaryStream() { + const liveInput = "live-tunnel\x00bytes\n" + root := s.T().TempDir() + sentinel := filepath.Join(root, "substitution-ran") + installPath := filepath.Join(root, "bin ' \\ \n雪 $(touch substitution-ran)", "agent '\"`$()") + params := bootstrapParams(installPath) + params.DownloadURLs = &DownloadURLs{Base: "https://invalid/'\"`$(touch " + sentinel + ")\\\r\n"} + params.Command = fmt.Sprintf("printf '%%s\\n' \"$DEVSY_AGENT_URL\"; head -c %d", len(liveInput)) + binary := make([]byte, 4096) + _, err := rand.Read(binary) + s.Require().NoError(err) + copy(binary, []byte{0, '\'', '\\', '"', '$', '\n'}) + var rawOutput, stderr syncBuffer + var userOutput bytes.Buffer + executed, err := Inject(s.T().Context(), InjectOptions{ + Exec: func(ctx context.Context, script string, stdin io.Reader, stdout, diagnostics io.Writer) error { + cmd := exec.CommandContext(ctx, "/bin/sh", "-c", script) + cmd.Stdin, cmd.Stdout, cmd.Stderr = stdin, io.MultiWriter( + stdout, + &rawOutput, + ), diagnostics + return cmd.Run() + }, + LocalFile: func(bool) (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(binary)), nil }, + ScriptParams: params, + Stdin: strings.NewReader(liveInput), + Stdout: &userOutput, + Stderr: &stderr, + Timeout: 5 * time.Second, + }) + s.Require().NoError(err, stderr.String()) + s.True(executed) + stdout := rawOutput.String() + s.True(strings.HasPrefix(stdout, "ping\nARM-"), stdout) + s.Equal(1, strings.Count(stdout, "done\n")) + s.True( + strings.HasSuffix(stdout, "done\n"+params.DownloadURLs.Base+"\n"+liveInput), + stdout, + ) + s.Equal(params.DownloadURLs.Base+"\n"+liveInput, userOutput.String()) + installed, err := os.ReadFile(installPath) + s.Require().NoError(err) + s.Equal(binary, installed) + info, err := os.Stat(installPath) + s.Require().NoError(err) + s.Equal(os.FileMode(0o755), info.Mode().Perm()) + _, err = os.Stat(sentinel) + s.True(os.IsNotExist(err)) +} + +func (s *BootstrapSuite) TestFailedReplacementPreservesAgent() { + const chmodFailure = "chmod" + for _, failure := range []string{"short-transfer", chmodFailure} { + s.Run(failure, func() { + installPath := filepath.Join(s.T().TempDir(), "agent") + original := []byte("old-working-agent") + s.Require().NoError(os.WriteFile(installPath, original, 0o750)) + before, err := os.Stat(installPath) + s.Require().NoError(err) + input := binaryInput([]byte("new-agent")) + if failure == "short-transfer" { + input = []byte("pong\n100\nshort") + } + stdout, stderr, err := runBootstrap( + s.T(), + bootstrapParams(installPath), + input, + bootstrapFailureHooks(failure == chmodFailure), + ) + s.Error(err, stderr) + if failure == chmodFailure { + s.Contains(stderr, "injected chmod failure") + s.Contains(stderr, "Failed to make temporary agent executable") + } else { + s.Contains(stderr, "Failed to receive complete agent binary") + } + s.NotContains(stdout, "done\n") + installed, err := os.ReadFile(installPath) + s.Require().NoError(err) + s.Equal(original, installed) + after, err := os.Stat(installPath) + s.Require().NoError(err) + s.Equal(before.Mode(), after.Mode()) + files, err := filepath.Glob(installPath + ".*") + s.Require().NoError(err) + s.Empty(files) + }) + } +} + +func (s *BootstrapSuite) TestDownloadFailureFallsBackToStdin() { + installPath := filepath.Join(s.T().TempDir(), "agent") + params := bootstrapParams(installPath) + params.PreferAgentDownload = true + stdout, stderr, err := runBootstrap( + s.T(), + params, + binaryInput([]byte("replacement")), + bootstrapFailureHooks(false), + ) + s.Require().NoError(err, stderr) + s.Contains(stderr, "Download failed, attempting stdin injection") + s.Equal(3, strings.Count(stderr, "Download attempt ")) + s.Contains(stdout, "done\n") + installed, err := os.ReadFile(installPath) + s.Require().NoError(err) + s.Equal("replacement", string(installed)) +} + +func (s *BootstrapSuite) TestWarmAgentDoesNotInstall() { + params := bootstrapParams(filepath.Join(s.T().TempDir(), "missing", "agent")) + params.ExistsCheck = "false" + stdout, stderr, err := runBootstrap(s.T(), params, []byte("pong\nlive\n")) + s.Require().NoError(err, stderr) + s.Equal("ping\ndone\nlive\n", stdout) +} + +func (s *BootstrapSuite) TestGenerateRejectsNULAndParses() { + params := bootstrapParams("/tmp/agent") + fields := []*string{ + ¶ms.AgentRemotePath, + ¶ms.DownloadURLs.Base, + ¶ms.DownloadURLs.Amd, + ¶ms.DownloadURLs.Arm, + ¶ms.Command, + ¶ms.ExistsCheck, + } + for _, field := range fields { + old := *field + *field = "bad\x00value" + _, err := GenerateScript(Script, params) + s.Error(err) + *field = old + } + script, err := GenerateScript(Script, params) + s.Require().NoError(err) + cmd := exec.CommandContext(s.T().Context(), "/bin/sh", "-n") + cmd.Stdin = strings.NewReader(script) + output, err := cmd.CombinedOutput() + s.NoError(err, string(output)) +} + +func (s *BootstrapSuite) TestInvalidLiteralDoesNotReportExecution() { + params := bootstrapParams("bad\x00path") + executed, err := Inject(s.T().Context(), InjectOptions{ + Exec: func(context.Context, string, io.Reader, io.Writer, io.Writer) error { + s.T().Fatal("invalid literal must fail before execution") + return nil + }, + ScriptParams: params, + }) + s.Error(err) + s.False(executed) +} diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/linux-troubleshooting.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/linux-troubleshooting.mdx index c8e9237212..c4d0c1ae83 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/linux-troubleshooting.mdx +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/linux-troubleshooting.mdx @@ -15,7 +15,19 @@ sudo chown -R $(id -un):$(id -gn) . Local folders suit early work. Once the devcontainer works, push it to a Git repository and use that instead. -### Fish shell +### SSH host login shells + +The SSH provider supports Bash, Zsh, and Fish login shells on Linux. Agent installation runs through the host's executable `/bin/sh`; interactive terminals and environment probing continue to use the account's configured shell. The workspace container's shell is configured separately. + +Dockerless containers are a separate bootstrap case: before their image filesystem is built, core can use the fixed `/.dockerless/bin/sh` bundle when `DOCKERLESS=true`, `/bin/sh` is not executable, and that bundled shell is executable. `/bin/sh` remains preferred when available. This does not change the SSH host prerequisite above. + +The host needs standard bootstrap tools, including `mkdir`, `mktemp`, `head` with `-c`, `wc`, `chmod`, `mv`, and `rm`. A download uses `curl` or `wget`; when neither is available, Devsy can transfer the agent over the SSH connection. The agent directory must permit execution and either be writable by the account or allow noninteractive privilege escalation. + +If startup reports `command not found: sh -c`, check the installed SSH provider version and its release notes for the POSIX bootstrap fix. Changing the account's shell is unnecessary. Both the external OpenSSH client and the built-in SSH transport use the same bootstrap launcher; switching transports does not repair a missing `/bin/sh` or a restricted account. + +For locked-down accounts, ask the administrator to permit `/bin/sh` and the required tools, provide a writable executable agent directory, and check forced-command restrictions. Keep noninteractive shell startup output off stdout: it carries the bootstrap protocol and the live SSH tunnel. Send diagnostics to stderr and limit banners and prompts to interactive sessions. + +### Fish startup configuration Anything in `config.fish` runs on every `fish -c` call, which can interfere with `devsy agent workspace up`. Move your customizations inside the `if status is-interactive` block: