fix(profiler): quote paths in pprof command builders to prevent shell injection - #2243
Closed
noron12234 wants to merge 1 commit into
Closed
fix(profiler): quote paths in pprof command builders to prevent shell injection#2243noron12234 wants to merge 1 commit into
noron12234 wants to merge 1 commit into
Conversation
… injection
The pprof_cmd builders in tools/profiler/{gperftools,pprof}/utils/pprof_cmd.py
concatenated caller-supplied file paths straight into shell command strings
that are later run via subprocess.run(cmd, shell=True) in the four dumper /
comparer scripts. A path containing shell metacharacters (e.g. `; touch X; #`)
was interpreted by the shell rather than passed to google-pprof / go tool pprof.
Fix: wrap every path with shlex.quote() when building the command string.
Preserves the existing str return type and the '>' redirection expected by all
downstream callers — no caller changes needed.
Verified:
payload BEFORE AFTER
'; touch /tmp/MARKER; #' injection ran injection blocked
Benign paths produce byte-for-byte identical commands (shlex.quote is a no-op
on shell-safe strings).
Contributor
Author
|
Closing this for now — I'm stepping back to re-review my own work before asking for anyone's time. Thanks, and sorry for the noise. |
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.
Problem
tools/profiler/gperftools/utils/pprof_cmd.pyandtools/profiler/pprof/utils/pprof_cmd.pybuild shell command strings by concatenating caller-supplied file paths directly into the string. The resulting string is then executed by four dumper / comparer scripts viasubprocess.run(cmd, shell=True):tools/profiler/gperftools/dump_heap_files_to_raw.py:38tools/profiler/gperftools/dump_raw_files_to_text.py:37tools/profiler/gperftools/compare_heaps.py:25tools/profiler/pprof/dump_heap_files_to_text.py:37If any of
bin,heapFile,rawFile,textFile,outputFile,port, oroutputTypecontains shell metacharacters, the shell interprets them rather than passing them togoogle-pprof/go tool pprof. All six builders have this issue.This mirrors the concern addressed in #2240 (
shell=Trueinrun_script.py), one layer up.Verification
Drove the real
pprof_cmdmodules against atrue-substituted binary so the shell parses+evaluates argv without needinggoogle-pprof/goinstalled. Payload:'; touch /tmp/PPROF_PWNED_MARKER; #passed asheapFile.gperf.convert_heap_to_text_cmdgperf.convert_heap_to_raw_cmdgperf.convert_raw_to_text_cmdgperf.compare_heaps_cmdgo.convert_heap_to_text_cmdgo.show_heap_in_browser_cmdBenign paths produce byte-for-byte identical commands (
shlex.quoteis a no-op on shell-safe strings), so no caller behavior changes:Why this shape
Two possible fixes:
shlex.quoteat the string-building layer (this PR) — preservesstrreturn type, keeps the>redirection in the string, no changes to the four downstream callers.list[str]argv + move redirection into Python — deeper hardening (removesshell=Trueentirely, matching fix: avoid shell=True in run_script.py build helpers #2240), but touches four caller files and changes their" & ".join(...)parallel-execution pattern.I picked (1) because the parallel-execution pattern (
" & ".join(cmds)in the dumpers) has its own semantics worth preserving in a separate change — reviewer can decide independently whether to accept (2) as a follow-up. Happy to extend this PR to option (2) if preferred.Files changed
tools/profiler/gperftools/utils/pprof_cmd.py(4 builders quoted)tools/profiler/pprof/utils/pprof_cmd.py(2 builders quoted,portcast + quoted)