fix(go): stop eval'ing GIT_CONFIG_VAL as shell#1098
Open
evilgensec wants to merge 1 commit into
Open
Conversation
go/prepare_workspace.inc passed `$GIT_CONFIG_VAL` through `eval`, so any $(...), backtick, ;, &&, || embedded in the value executed as shell when the gcr.io/cloud-builders/go step ran. That image is the canonical Go builder for Cloud Build; once a pipeline lets GIT_CONFIG_VAL be templated from any input the attacker can influence (PR-trigger substitutions, webhook payloads, branch/tag names, derived env), the embedded command runs as root inside the build step with the Cloud Build service-account token available from the metadata server. Verified against the live gcr.io/cloud-builders/go:latest image (digest sha256:e8c442f3eaa523a6a190bec463b29e82193a1ca4e36b9de69301fb4d2e1cee0e): setting GIT_CONFIG_VAL='user.email "x@y" && touch /workspace/RCE' created /workspace/RCE in the host bind-mount. The fix swaps the eval for `printf '%s' "$GIT_CONFIG_VAL" | xargs git config --global`, which tokenizes the value into argv positions while preserving legitimate quoted forms (verified that GIT_CONFIG_VAL='url."git@github.com:".insteadOf "https://github.com/"' still produces the expected gitconfig entry after the fix).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
go/prepare_workspace.inc:18runs:Any
\$(...), backtick,;,&&, or||embedded inGIT_CONFIG_VALis interpreted by the shell when this.incis sourced by thegcr.io/cloud-builders/gostep. Becausegcr.io/cloud-builders/gois the canonical Go builder for Cloud Build, any pipeline that letsGIT_CONFIG_VALbe templated from an input the attacker can influence (PR-trigger substitutions, webhook payloads, branch/tag names, derived env) ends up running attacker shell as root inside the build with the Cloud Build service-account token reachable from the metadata server.The
evalwas never necessary:git config --global <key> <value>accepts positional args. PR #500 (Jun 2019) introduced this knob and usedevalto handle shell-quoted values such asurl."git@github.com:".insteadOf "https://github.com/". We can keep that behavior — including quote handling — by piping throughxargs, which tokenizes the value into argv positions without re-evaluating shell metacharacters.Reproduction (before this PR)
Verified against the live
gcr.io/cloud-builders/go:latestimage (digestsha256:e8c442f3eaa523a6a190bec463b29e82193a1ca4e36b9de69301fb4d2e1cee0e):In Cloud Build the equivalent payload exfiltrates
http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/tokento an attacker-controlled URL.Reproduction (after this PR)
Re-running the same
docker runagainst the patched image:git configrejects the literal&&token (usage: git config [<options>]) and the attacker'stouch/idnever run.Legitimate usage with quoted values is preserved:
Test plan
gcr.io/cloud-builders/go:latest.FROM gcr.io/cloud-builders/go:latest+ COPY of the fixed.inc) and re-ran the PoC: injection is now blocked.url."git@github.com:".insteadOf "https://github.com/"legitimate-usage shape continues to set the expected~/.gitconfigentry.bash(go.bash) andash(go.ash) entrypoint variants, sinceprintf+xargsare POSIX.