Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-24
12:08
Merge [ee7ec33deb4d228f]: [fb2fa9b3f6] [21b0629c81]: ms-win: revert security patch to quote each %-c... check-in: 2cd781249b user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2023-11-30
19:23
MS-Win: document the exec %var% re-allowance. Ticket [fb2fa9b3f6] check-in: 7ffab2f5dd user: oehhar tags: core-8-6-branch
19:19 • Closed ticket [21b0629c81]: 0-day vulnerability - insufficient escape by exec of... artifact: e3e71c4f57 user: oehhar
19:14 • Closed ticket [fb2fa9b3f6]: exec broken for command having windows environment v... artifact: 80dc6f0e93 user: oehhar
19:14 • Ticket [fb2fa9b3f6]: 4 changes artifact: 3a0752cd47 user: oehhar
18:57
Tickets [fb2fa9b3f6] [21b0629c81]: ms-win: revert security patch to quote each %-character seperatel... check-in: ee7ec33deb user: oehhar tags: core-8-6-branch
2023-11-03
11:35 • Ticket [21b0629c81] 0-day vulnerability - insufficient escape by exec of batch-f... artifact: 5b92321ad0 user: oehhar
2023-11-02
17:13 • Ticket [fb2fa9b3f6] exec broken for command having windows environment variables... artifact: 56eecd1e46 user: juliannoble2
2023-10-28
07:21 • Ticket [fb2fa9b3f6]: 4 changes artifact: b0285f9059 user: oehhar
2023-10-27
21:20 • Ticket [fb2fa9b3f6]: 4 changes artifact: 9048d1b03e user: oehhar
19:32 • Ticket [fb2fa9b3f6]: 3 changes artifact: b57b50b9d0 user: juliannoble2
11:36 • Ticket [fb2fa9b3f6]: 4 changes artifact: f6d1baef27 user: oehhar
11:04 • Ticket [fb2fa9b3f6]: 3 changes artifact: b25e1bc5cc user: sebres
10:46
fixes percent-subst regression [fb2fa9b3f6] introduced by fixing of vulnerability [21b0629c81]; warn... check-in: ea3b6d6792 user: sebres tags: fix-fb2fa9b3f6--percent-subst-regr
08:43 • Ticket [fb2fa9b3f6] exec broken for command having windows environment variables... artifact: 88bef6c972 user: oehhar
08:33 • Ticket [fb2fa9b3f6]: 3 changes artifact: 6be49fd251 user: sebres
2023-10-26
11:52 • Ticket [fb2fa9b3f6]: 4 changes artifact: 21cd0efad7 user: oehhar
11:41 • Ticket [fb2fa9b3f6]: 3 changes artifact: ebaccf2847 user: juliannoble2
2023-10-25
11:42 • Ticket [fb2fa9b3f6]: 3 changes artifact: 0b49a77628 user: anonymous
11:24 • Ticket [fb2fa9b3f6]: 4 changes artifact: 03e8d71fdc user: oehhar
11:21 • Ticket [fb2fa9b3f6]: 3 changes artifact: ee5f14ba5c user: apnadkarni
10:37 • Ticket [fb2fa9b3f6]: 3 changes artifact: 807316771a user: apnadkarni
08:08 • Ticket [fb2fa9b3f6]: 4 changes artifact: b7c7db4ca8 user: oehhar
07:24 • Ticket [fb2fa9b3f6]: 4 changes artifact: 2526ce7d0d user: oehhar
02:24 • Ticket [fb2fa9b3f6]: 3 changes artifact: fc1bcb66a5 user: apnadkarni
2023-10-24
11:51 • Ticket [fb2fa9b3f6]: 4 changes artifact: 5953d42127 user: oehhar
2023-10-23
19:25 • New ticket [fb2fa9b3f6]. artifact: 8b197a46cf user: anonymous

exec broken for command having windows environment variables

Closed / Fixedfb2fa9b3f6467ee66f543ec904e37aa8508f2fb1Created 3 years agoUpdated 3 years ago
Descriptionplain2023-10-23 19:25:52
in tcl8.5.13 shell (tclsh.exe) following command used to work

exec cmd /c echo %SystemRoot%

it correctly prints SystemRoot environment variable value (C:\Windows).

but same command in tcl8.6.13 shell outputs

""%"SystemRoot"%""
Referenced by6
  • 2cd781249bMerge [ee7ec33deb4d228f]: [fb2fa9b3f6] [21b0629c81]: ms-win: revert security patch to quote each %-character seperately to avoid environment expansion. More general solution with command parameters required. check-in2024-08-24 12:08:04
  • 7ffab2f5ddMS-Win: document the exec %var% re-allowance. Ticket [fb2fa9b3f6] check-in2023-11-30 19:23:50
  • e3e71c4f57Closed ticket [21b0629c81]: 0-day vulnerability - insufficient escape by exec of batch-files for windows plus 6 other changes ticket2023-11-30 19:19:26
  • ee7ec33debTickets [fb2fa9b3f6] [21b0629c81]: ms-win: revert security patch to quote each %-character seperately to avoid environment expansion. More generel solution with command parameters required. check-in2023-11-30 18:57:36
  • 5b92321ad0Ticket [21b0629c81] 0-day vulnerability - insufficient escape by exec of batch-files for windows status still Open with 4 other changes ticket2023-11-03 11:35:00
  • ea3b6d6792fixes percent-subst regression [fb2fa9b3f6] introduced by fixing of vulnerability [21b0629c81]; warning (todo): since it'd reopen a injection-vector by execution of command processor/batch-files as described in [fb2fa9b3f6] (unexpected tripple/double quote), [exec] as well as [open |...] should get new option for safe escape (or no .bat/.cmd/comspec execution with arguments from foreign input can be considered as safe without extra parameters validation). check-in2023-10-27 10:46:17
oehharwiki2023-10-24 11:51:50

I fear, this is a side-effect of the revised qoting rules for exec. If no wizard answers here, it might be wise to ask on the comp.lang.tcl newsgroup.

Sorry, Harald

apnadkarniplain2023-10-25 02:24:39
Harald, any link to "revised quoting rules"? I can't find a TIP that mentions this change to exec.

/Ashok
oehharwiki2023-10-25 07:24:54

I remember a mix of bug fixing (mainly by Sergey) and exec reform. Will search later, Harald

oehharwiki2023-10-25 08:08:59

I have found it: [344b1a0c0]

So, it is an open issue.

Perhaps, we might politely ask Sebres about his view how to re-make this work.

Thank you and take care, Harald

apnadkarnimarkdown2023-10-25 10:37:47

Thanks Harald, you're a real historian for Tcl :-)

Rightly or wrongly, Tcl seems to be the only language (it's been proposed for other languages as well) that has incorporated this "fix". I'm skeptical of the utility of this given that for programs exec'ing cmd.exe, this would be like plugging a single hole in a rather large sieve. If the attacker has control of the arguments as seemingly required here, what is to stop him from simply having the arguments be "format C:" for example? Or given that cmd.exe is a (very poor) language, to construct a command (using pipes, for and friends) or recurse in any number of ways?

So even though the issue might be real, I would not have been in favour of a change that is incompatible, afaict precludes legitimate uses, and only addresses a single point in the attack surface.

It's water under the bridge but for future reference for anyone having the inclination to pursue this, informative links on intricacy of Windows command parsing:

https://daviddeley.com/autohotkey/parameters/parameters.htm#WIN

https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/

apnadkarnimarkdown2023-10-25 11:21:38

@[email protected]

I presume you are aware as a workaround that you can directly pass the environment

exec cmd /c echo $::env(systemroot)
oehharwiki2023-10-25 11:24:01

Yes, I also want to note, that I am quite unhappy with the situation. Basically, anything should be possible. Of cause, environment variables may be enteered using the env array, but that is not the same.

I write configurable software. And there is an entry field "execute me". And that should just work, and no quoting should interfere. At least, anything should be possible. And no backward compatibility to 8.5 is also bad.

To bring this forward, the new "save" quoting rules may only be used with a "-savequoting" switch.

Sergey, should I care about this ?

Thank you and take care, Harald

anonymousplain2023-10-25 11:42:32
@apnadkarni, yes, i am aware of the various workarounds including $::env(SystemRoot)

Thanks for looking into it.
juliannoble2plain2023-10-26 11:41:11
For this particular case I would use

exec cmd /v /c echo !SystemRoot!

(which raises the question in my mind of why % is even treated differently)

I'd also note that % is a legitimate character in the NTFS filesystem - so this sort of magic quoting of % can interfere in other places :/

I am strongly of the opinion that all this sort of quoting in exec/open is a mistake. 
I appreciate the effort made in analysing https://core.tcl-lang.org/tcl/info/21b0629c81 as a 'vulnerability' but I disagree. It's at best an argument for a new higher level function or wrapper over exec. I'm sorry I missed the chance to rail against this change when it happened.

If Tcl is to function as a glue language we need the primitives to shoot ourselves in the foot and pass things verbatim to the underlying commands as much as possible. We respect that Tcl commands have the freedom to interpret their arguments as they wish - we should respect that commandlines can be interpreted as they wish - including as to how they do or don't separate arguments.

That's somewhat at odds with providing a neat interface that one can safely throw arguments at - but the current situation makes reasoning about what is going on so much harder and cripples Tcl.

Exec/Open already had some oddities before this - so they weren't perfect - but this makes the situation that much worse.
oehharwiki2023-10-26 11:52:32

Julian, wow ! Neat workaround ! +1 on what you are writing.

Take care, Harald

sebreswiki2023-10-27 08:33:41

Well, the reason why I called it vulnerability is that as already said in [344b1a0c0] as well as by fixing in [21b0629c81] - windows doesn't really have correct way to escape quotes by calling of command processor, and because substitution of %var% may insert quotes too, although they are mostly paired, the previously escaped string may become extra double/triple quotes, which could topple the whole escape, sometimes to completely unescaped parameters (and so can open a vector for an inject, because can invoke commands after pipe or ampersand chars in some parameter).

Sure it is a clear regression in sense of backwards compatibility, just since there is no way to fix it properly without to introduce some new options for exec, like -shellescape or -unescape (expected TIP) etc, it was fixed in this way.

Feel free to revert it in part affecting %-escape, just as already said it would be a half solution then.

However I don't realy understand why someone'd need to subst %var% in the command processor, where it is pretty simple with $::env(var) by invokation of exec.

oehharwiki2023-10-27 08:43:34

Hi Sebres, thank you for your great action, I appreciate.

The reason, why I would like, that basically anything works in exec, including volnerabilities is as follows:

As shown at OpenACS/TCL/Tk conference, my main TCL/Tk based product has a box-moving editor to compose solutions around bar-code technologies. There is one of those function-blocks for exec. I have my own tab-replacement language using "<Varname>" the user may use. There is no direct access to the env array. So, my user expect, that it is basically the same, as typing on the cmd.exe command line. And if that does not work, I get justified complaints. And the quoting rules are specially critical, as nobody knows what is really called and thus debugging is very difficult.

Quoting is difficult. Passing a simple thing like a file name is difficult. But not having control on the script level does not allow to solve the issues like in the ticket.

I normally do the workaround, that I write a batch file on the fly and invoke the batch file. But you loose control over result values etc.

In consequence, I would really be glad, if this vulnerability protection would be reverted or put into a switch. I am ready to drive the TIP process. Unfortunately, the current bugfix would also require a TIP, as we have a dramatic change on the script level, that functionality present before is not present any more.

Thank you for your understanding and best regards, Harald

sebreswiki2023-10-27 11:04:23

Here you go - branch fix-fb2fa9b3f6--percent-subst-regr fixes the regression by %-char subst for execution of comspec/batch-files (restores old behaviour by %-escape).

However as already said (and how the warning in [ea3b6d6792] says) - there is a TODO now: since it'd reopen a injection-vector by execution of command processor/batch-files as described below (unexpected tripple/double quote issue), exec as well as open |... should get new option for safe escape (or no .bat/.cmd/comspec execution with arguments from foreign input can be considered as safe without extra parameters validation).

Previous fix has been still retained (ifdef-ed by TCL_WIN_PIPE_FULLESC) for future usage with new option (if it gets implemented at some point) or for the people like me which prefer the safe escape to the backwards compat issues.

oehharwiki2023-10-27 11:36:07

Sergey,

I thank you for the quick action. It feels so critical and normally, you are just right.

I plan to author a message on the core list to ask for decission on this point.

A windows-only flag "-shellescape" would be great, as both solutions have their merit.

Thank you soooo much, Harald

juliannoble2plain2023-10-27 19:32:10
Another way around this is to use powershell or sh (e.g from msys2)
For example - sometimes you want things like an unbalanced quote to go through.
That seems to be impossible with cmd.exe.  I had always thought this was a quirk of cmd - but now I suspect it's exec (which apparently has been interfering like this for a long time).  I have not noticed such interference on other platforms.

I don't know how pwsh and sh are bypassing both the double quote escaping and the % escapes - (perhaps something to do with applType?) but however it happens I hope this behaviour in exec can be preserved and better yet - enabled in some way for cmd and batch files.  ( -nomolestation flag?)


    % pwsh -nop -nolo -c cmd.exe /c echo '%CD%unbalancedquote"'
    C:\tcl9b\binunbalancedquote"
    %
    % sh -c {echo unbalancedquote\"}
    unbalancedquote"
    %
    % cmd.exe /c echo {%CD%unbalancedquote"}
    "%"CD"%"unbalancedquote\"


Note that calling cmd.exe from powershell brings another level of quoting quirks as you need to use powershell rules to single quote some things. sh also has different rules regarding quotes.
This way madness lies - I'd really like the power to call things (everything) directly from Tcl in a way that can be reasoned about sanely.
oehharwiki2023-10-27 21:20:07

Discussion continues on the tcl core list. Take care, Harald

oehharwiki2023-10-28 07:21:49

Sergey, if a replacement of "%ENV%" is seen as possible breake of quoting, then "!ENV!" should show the same vulnerability. In consequence, "!" should also be quotes, as "%" ?

Take care, Harald

juliannoble2plain2023-11-02 17:13:14
After playing around with it, !env! and the delayed expansion flag /v are actually the safest way to access variables as they don't seem to be able to escape.

use of !var! definitely shouldn't be hindered in my opinion.

The issues are pretty complicated - but note that powershell and cmd.exe without Tcl involvement show the same issues, so it's arguable we shouldn't be limiting Tcl too much here anyway. 

I will post some more info on the core list - but I have developed a 'runcmd' proof of concept using just twapi and script, which quotes with the ^ (caret character) and gives a much more acceptable balance of features vs risk I feel. To use %var% completely safely if you don't trust your environment variables would require require Tcl to scan the values - which I don't think is practical/desirable.
oehharwiki2023-11-30 19:14:07

Branch [fix-fb2fa9b3f6--percent-subst-regr] was merged with commit [ee7ec33deb]. Only the "%env%" part of ticket [ea3b6d6792] is reverted.

Open work for a general multi-mode exec is pending.

Thanks to Sergey !

Harald

oehhar Closed ticket [fb2fa9b3f6]. [History diff]2023-11-30 19:14:39
Log in (anonymous is fine) to add a comment.