Skip to content
Tcl Source Code View Ticket
Login

View Ticket

exec on Windows broken when '%' is used

Closed / Fixed344b1a0c0f0e2fda1e4ad6520cbf5e0af35eb332Created 8 years agoUpdated 3 years ago
Descriptionplain2019-01-15 17:54:58
The following command apparently does not work correctly in Tcl 8.6.9:

set res [exec -- $::env(ComSpec) /c echo %CD%]

From "https://www.sqlite.org/src/artifact/aafcd5b0bc0f7?ln=177"
Referenced by1
  • 0cd4bc9531Closed ticket [efd15d181a]: problem with % in [exec] on windows plus 6 other changes ticket2020-08-04 16:09:02
sebreswiki2019-01-15 18:43:49

Confirmed.

Basically, this is not a bug, but feature (I would say, I'm blame for this) to avoid windows vulnerable handling by (un)escape of the parameters, see [21b0629c81].

Just a bit incompatible feature in exactly this case.

The substitution of system variables using percent-char was nowhere described resp. not documented in the exec-manual (still worse it is unclear in documentation of windows command processor too). Furthermore it could cause unexpected or even vulnerable behavior if calling batch-files, or executable from there.

Possible solutions:
- restore backwards-compatibility for call of the command-processor;
- special option like --safe or --unsafe to use (or avoid) safe escaping at all;
- rewrite the code in sqlite like:

- return [string map [list \\ /] \
-            [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
+ return [file normalize [pwd]]
Or just remove this workaround completely, because pwd always return normalized path now, so this strange attempt to solve "case-mismatch issues on windows" is not expected anymore.

I'm aware of the certain incompatibility here, and because the fix [21b0629c81] is anyway incomplete (see my last comment there), so I'm all ears for the suggestions or proposals how we could solve this.

mistachkinplain2019-01-15 19:16:02
Sorry, but this is a clear regression from 8.6.8.  Previously, this script,
which is technically correct, worked fine.  Now, it does not work.

If this is an incompatible change done on purpose, it should have been saved
for Tcl 9.0, or at least TIP'd.

The workaround is in the test script for a reason.
sebreswiki2019-01-15 19:38:46

> Sorry, but this is a clear regression from 8.6.8.

And I'm aware of it. Just at the moment I'm not sure about the possible fix of the vulnerability without lost of the backwards-compatibility.

So I'm basically with you - the regression has to be resolved now. Just searching for the proper solution.

> it should have been saved for Tcl 9.0, or at least TIP'd.

Normally sure, but not usually in case of the potential vulnerability (and unfortunately it is not just theoretical case).
A programming language like Tcl (as many others) must fundamentally follow "Safety ahead of Compatibility" rule.

sebreshtml2019-01-17 09:00:11

Just for the record

As for [pwd] normalization...
Well, as already said, I don't think it is still an issue under windows.

As for "the percent character has to be escaped"...
Sure it must be, but for batch/exe, and as regards the execution of command processor - it was just unintended after-effect of the fix. Shit happens.
The escape is necessary because of certain issue how Windows processes unpaired quotes (it is vulnerable at the moment and MS won't fix this due to compat-reasons, see Microsoft/console/issues/250), so it is possible to produce an unpaired quote after substitution of some env-vars (like paths, containing quotes, which are substituted in call, so no more under control of Tcl). Just because several quotation marks together are processed differently, there are even weird cases where triple-quotes will be unescaped similar to double-quotes, or they can even "reverse" unescape strategy MS used here.
For example C" "C, "C C" and ""C" "C"" are the same under windows, so following substitutions cause the same result:

> echo puts $::argv | tclsh - C" "C
- {C C}
> echo puts $::argv | tclsh - "C C"
- {C C}
> echo puts $::argv | tclsh - ""C" "C""
- {C C}
So if you basically don't have the quotes under your control and in principle you do not, because the internal %-substitution of variables has precedence here, so could damn your composited command line, also in case it was initially quoted properly.
Then the usage of some parameters like "| %COMSPEC% /c do-something-evil" after such break, e. g. passed from foreign user-input and normally proper quoted/escaped as regards the single argument, allow intruder to construct an injection and produce exploit.

That's as regards the batch-execution etc.
As already said, cmd /c is rather an exception here (and was just an after-effect, so blame on me), but I'll try to provide the fix as soon as possible.

As for "don't think that could be accomplished using double quotes". Yes you are right, it is not really possible, and this is also explained in my last comment to mentioned 0-day vulni-ticket (but it is at least securer as it was previously).
The magic word here is circumflex (^) character and I've already potential solution for this, just the mixed escape using circumflex and quotes together is a bit complex (see sebres/PoC/issues/1).
Additionally it deviates a bit by the call of batch and executable, and I would resolve this deviation before, as well as provide good test-coverage for both variants (now 3, inclusive comspec).

17.01.2019 01:19, Joe Mistachkin wrote:

The original problem being solved was that [pwd] did not preserve the exact case of the current directory.  I'm struggling to recall, but I think making the test result comparison case-insensitive was not an appropriate option.
 
That being said, given the command escaping algorithm in use for Windows [exec] in 8.6.9, I'm surprised that other people haven't noticed that things don't work quite the same.  Also, I'm confused by the percent character has to be escaped, since it does not typically impact argument boundaries on Windows.  Furthermore, *if* it had to be escaped, I don't think that could be accomplished using double quotes.

bllplain2020-08-04 17:30:24
Is this trying to fix a vulnerability in Windows? Or Tcl?
I don't think trying to fix Windows issues is a good use of time.
cmd.exe is known to be broken, nothing can be done about it.
sebreswiki2020-08-04 17:56:38

> Is this trying to fix a vulnerability in Windows? Or Tcl?

Basically both (or rather tcl's exec escape for windows, since an escape is already there, just it was wrong (vulnerable) before my fix in [21b0629c81] and it is still wrong (but not vulnerable anymore) now.

And yes, I'll try to restore the backwards compatibility with preserving of the vulnerability fix simultaneously (as safe as possible).

Your sentence from duplicate ticket "On Windows, the program should receive the arguments as is, as on windows, the programs parse the arguments, not the shell." is incorrect in my opinion, because in such a case the escape and quotation should be completely removed (belongs to the user task) but Tcl has from scratch tried to follow posix execv semantic, so had implement escape/quotation for windows simulating that (just partially, so not enough safe because impossibly).

sebreswiki2023-12-05 18:23:44

closed in [ee7ec33deb4d228f]

Log in (anonymous is fine) to add a comment.