Skip to content

Keep each MSYS argument one word after Cygwin splits the command line - #25

Merged
hex2f merged 1 commit into
hex2f:mainfrom
chinmayajha:fix-msys-quote-word-splitting
Nov 12, 2026
Merged

hex2f merged 1 commit into
hex2f:mainfrom
chinmayajha:fix-msys-quote-word-splitting

Conversation

@chinmayajha

Copy link
Copy Markdown
Contributor

Problem

msys_quote builds the command line for MSYS programs. Cygwin splits that line again, and four kinds of argument came out wrong:

Argument What the program receives
C:\x\" -la two words: C:\x\\ and -la
C:\file<LF>-la two words: C:\file and -la
C:\Program Files\ followed by another argument one word that holds both arguments
\Windows\System32 \\Windows\\System32, which is read as a UNC path

The 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 \n and \r, which Cygwin treats as separators. A quoted \name\... starts with \\, so Cygwin read it as a UNC path.

Fix

  • Add \n and \r to the characters that stop the fast path.
  • Always escape \ and " inside the quotes.
  • Start the word with an empty '' 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.rs is a port of build_argv, quoted and globify from Cygwin's winsup/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 alphabet C : \ " ' 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_quote through 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.

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
@chinmayajha

Copy link
Copy Markdown
Contributor Author

@hex2f

@chinmayajha

Copy link
Copy Markdown
Contributor Author

@hex2f This PR also closes an argument injection in msys_quote.

Issue: For an argument that starts with a drive letter, msys_quote did not double backslashes and did not quote \n or \r. Cygwin's tokenizer then split one argument into two:

  • C:\x\" -la became C:\x\\ and -la
  • C:\file<LF>-la became C:\file and -la

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 -o for ssh). In bash, "$x" always stays one word, so script authors do not expect this. The attacker must control the argument text. Filenames cannot hold " or newlines, so the text must come from another source, such as stdin, the network or the environment. I rate it low to medium.

@hex2f hex2f left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>-la comes out as two words.
  • 'C:\Program Files\' next comes out as one word that holds Program 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.

@chinmayajha

Copy link
Copy Markdown
Contributor Author

@hex2f friendly ping, is there anything else you'd like changed here?

@hex2f
hex2f merged commit e9e2099 into hex2f:main Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants