Skip to content

security.ShellEscape is not shell-safe (it is strconv.Quote): audit and migrate the remaining call sites #680

Closed

Description

@scttfrdmn

Split out of #660, which fixed one instance of this. security.ShellEscape is strconv.Quote:

func ShellEscape(s string) string { return strconv.Quote(s) }

Its doc comment claimed it "handles all special shell characters". It does not. strconv.Quote is Go/C escaping inside double quotes, and a POSIX shell expands $VAR, $(...) and backticks inside double quotes. So it neutralises none of them. It also collapses a multi-word value into one argv word, which is a second, independent bug wherever the value is a command line.

#660 fixed --mpi-command (the worst case: it preserved neither argv nor safety) and added security.ShellQuote, which single-quotes properly. The comment on ShellEscape now says what it actually does and points here. This issue is the audit of the remaining call sites.

Call sites, by risk

1. cmd/config.go:70,72 — unvalidated input, remote execution.

sporedCmd = fmt.Sprintf("sudo /usr/local/bin/spored config set %s %s 2>&1",
    security.ShellEscape(args[2]), security.ShellEscape(args[3]))

args[2]/args[3] come straight from the command line and the result is executed on the instance. spawn config set foo '$(id)' runs id on the instance rather than storing the literal string.

Severity is limited — the caller already owns the instance, so this is self-inflicted rather than a privilege boundary — but it is still a correctness bug (you cannot store a value containing $) and it is the one that would be embarrassing if a value ever arrived from somewhere other than argv. Should be ShellQuote.

2. pkg/userdata/storage.go (8 sites) — mount points and mount options from flags.

{{.FSxMountPoint | shellEscape}}, {{.EFSMountOptions | shellEscape}}, {{.DeviceName | shellEscape}} and friends are interpolated into mkdir -p, mount -o and /etc/profile.d/*.sh. A mount point containing $ expands at boot instead of being used literally. These come from --efs-mount-point / --fsx-mount-point / --attach-volume, so they are user-supplied but not attacker-supplied. Should be ShellQuote.

3. pkg/launcher/bootstrap.go:189 — safe, but only by accident of validation.

security.ShellEscape(username) and ShellEscape(publicKeyBase64) are both independently validated first (ValidateUsername, ValidateBase64), so neither can contain a metacharacter. Worth a comment saying the safety comes from the validation rather than from the escaping, so a future change to either validator does not quietly remove the guarantee.

Already doing the right thing

Two places had already worked around this and said so in comments, which is how I found the pattern:

  • pkg/userdata/container.go:82 — "rather than security.ShellEscape, whose strconv.Quote-based double-quote…"
  • pkg/taskproto/wrapper.go:581 — uses its own shQuote

So there are now three single-quoting implementations in the tree (taskproto.shQuote, launcher.shellQuoteJoin, security.ShellQuote). #660 pointed launcher at the shared one; taskproto.shQuote should follow, or be documented as deliberately separate.

Suggested resolution

  1. Migrate groups 1 and 2 to ShellQuote, with a test per site that a $-bearing value survives literally.
  2. Point taskproto.shQuote at security.ShellQuote (or note why not).
  3. Comment the bootstrap.go site to record that validation is what makes it safe.
  4. Once no caller remains, delete ShellEscape rather than leaving a deprecated footgun named as if it were the safe choice.

Step 4 is the real fix: the name is the trap. Anyone reaching for "the escape function" gets the unsafe one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions