Repository navigation
Keep each MSYS argument one word after Cygwin splits the command line - #25
Conversation
msys_quote left backslashes single in drive-letter arguments and let newlines through its fast path. Cygwin's tokenizer then split or merged words: - C:\x\" -la became two words - C:\file<LF>-la became two words - C:\Program Files\ swallowed the next argument - \Windows\System32 was read as a UNC path Always escape \ and " inside the quotes. Start the word with an empty '' when it would look like a DOS path, so Cygwin unescapes it like any other word. Add \n and \r to the characters that stop the fast path. src/cygwin_argv.rs ports Cygwin's build_argv, quoted and globify and checks that the quoted form of every argument over a 12-character alphabet, up to length 5, comes back as one word. Assisted-By: Claude Fable 5.1
|
@hex2f This PR also closes an argument injection in Issue: For an argument that starts with a drive letter,
Impact: If a script passes untrusted text as such an argument to an MSYS program, the attacker can add extra options to that program (for example |
hex2f
left a comment
There was a problem hiding this comment.
Thanks for this. The fix is correct, and I checked it against real MSYS programs, not only the port. I passed each argument to /usr/bin/printf '[%s]\n'. On main:
'C:\x\" -la'comes out as two words,C:\x\and-la.C:\file<LF>-lacomes out as two words.'C:\Program Files\' nextcomes out as one word that holdsProgram Files" next.
With this branch, all of them match Git Bash exactly. cargo test passes, and so does the compat suite merged with current main and with #22 and #23.
The rooted-path case doesn't reproduce. With Git for Windows' MSYS runtime (3.6.10), \Windows\System32 and \dir\sub dir already come through intact on main. The '' prefix does no harm there, but the description and rooted_path_is_not_read_as_unc overstate it. src/cygwin_argv.rs is ported from upstream Cygwin's dcrt0.cc, and msys2-runtime evidently handles network paths differently, so the oracle isn't quite the runtime gash talks to.
Please add an end-to-end test: a tests/scripts case that passes these arguments to /usr/bin/printf '[%s]\n'. The first three cases above are enough. Git Bash hands the arguments over directly, so the expected output is just the arguments, and it would catch any gap between the port and the real runtime.
|
@hex2f friendly ping, is there anything else you'd like changed here? |
Problem
msys_quotebuilds the command line for MSYS programs. Cygwin splits that line again, and four kinds of argument came out wrong:C:\x\" -laC:\x\\and-laC:\file<LF>-laC:\fileand-laC:\Program Files\followed by another argument\Windows\System32\\Windows\\System32, which is read as a UNC pathThe first two let data inside one argument become a separate argument, for example an extra option. The third joins two arguments. The fourth changes the path.
Cause
Drive-letter arguments kept single backslashes, because Cygwin does not unescape a word that looks like a DOS path. But the tokenizer treats
\"as an escaped quote, so a backslash before the closing quote hides it. A"inside such a word also ends the quoted run. The fast path did not check\nand\r, which Cygwin treats as separators. A quoted\name\...starts with\\, so Cygwin read it as a UNC path.Fix
\nand\rto the characters that stop the fast path.\and"inside the quotes.''when it would look like a DOS path. Cygwin then unescapes it like any other word.Arguments that did not need the new handling are quoted as before.
Tests
src/cygwin_argv.rsis a port ofbuild_argv,quotedandglobifyfrom Cygwin'swinsup/cygwin/dcrt0.cc. The tests check that the quoted form of an argument comes back as the same single word, in the middle and at the end of a command line. They cover the four cases above, non-ASCII text, and every argument of up to five characters over the alphabetC : \ " ' space LF tab * ~ @ x.I also compared the port with the original C functions on 4.2 million command lines (no differences). I ran the new
msys_quotethrough the C functions for 11 million arguments of up to nine characters (no failures). The previous version failed 880 of 19,608 arguments in a smaller version of the same run.