Artifacts Associated With Ticket fb2fa9b3f6
Ticket change [8b197a46cf] (rid 219756) by anonymous on 2023-10-23 19:25:52:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-plain"
- comment initialized to:
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"%""
- foundin initialized to: "8.6.13"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- private_contact initialized to: "8ee18ff7c4792aafadc7ba500e00ce7e35edcc4b"
- resolution initialized to: "None"
- severity initialized to: "Critical"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "- New Builtin Commands"
- title initialized to:
exec broken for command having windows environment variables
- type initialized to: "Bug"
Ticket change [5953d42127] (rid 219758) by oehhar on 2023-10-24 11:51:50:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [fc1bcb66a5] (rid 219765) by apnadkarni on 2023-10-25 02:24:39:
- icomment:
Harald, any link to "revised quoting rules"? I can't find a TIP that mentions this change to exec. /Ashok
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [2526ce7d0d] (rid 219766) by oehhar on 2023-10-25 07:24:54:
- icomment:
I remember a mix of bug fixing (mainly by Sergey) and exec reform. Will search later, Harald
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [b7c7db4ca8] (rid 219767) by oehhar on 2023-10-25 08:08:59:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [807316771a] (rid 219768) by apnadkarni on 2023-10-25 10:37:47:
- icomment:
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/
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [ee5f14ba5c] (rid 219769) by apnadkarni on 2023-10-25 11:21:38:
- icomment:
@[email protected] I presume you are aware as a workaround that you can directly pass the environment ``` exec cmd /c echo $::env(systemroot) ```
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [03e8d71fdc] (rid 219770) by oehhar on 2023-10-25 11:24:01:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [0b49a77628] (rid 219771) by anonymous on 2023-10-25 11:42:32:
- icomment:
@apnadkarni, yes, i am aware of the various workarounds including $::env(SystemRoot) Thanks for looking into it.
- login: "anonymous"
- mimetype: "text/plain"
- icomment:
Ticket change [ebaccf2847] (rid 219784) by juliannoble2 on 2023-10-26 11:41:11:
- icomment:
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.
- login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [21cd0efad7] (rid 219785) by oehhar on 2023-10-26 11:52:32:
- icomment:
Julian, wow ! Neat workaround ! +1 on what you are writing. Take care, Harald
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [6be49fd251] (rid 219791) by sebres on 2023-10-27 08:33:41:
- icomment:
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 <code>%var%</code> 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 <code>exec</code>, like <code>-shellescape</code> or <code>-unescape</code> (expected TIP) etc, it was fixed in this way. Feel free to revert it in part affecting <code>%</code>-escape, just as already said it would be a half solution then. However I don't realy understand why someone'd need to subst <code>%var%</code> in the command processor, where it is pretty simple with <code>$::env(var)</code> by invokation of <code>exec</code>.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [88bef6c972] (rid 219792) by oehhar on 2023-10-27 08:43:34:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [b25e1bc5cc] (rid 219799) by sebres on 2023-10-27 11:04:23:
- icomment:
Here you go - branch [https://core.tcl-lang.org/tcl/vdiff?from=7ed548b6b59bb496&to=fix-fb2fa9b3f6--percent-subst-regr|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), <code>exec</code> as well as <code>open |...</code> 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 <code>TCL_WIN_PIPE_FULLESC</code>) 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.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [f6d1baef27] (rid 219800) by oehhar on 2023-10-27 11:36:07:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [b57b50b9d0] (rid 219824) by juliannoble2 on 2023-10-27 19:32:10:
- icomment:
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. - login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [9048d1b03e] (rid 219825) by oehhar on 2023-10-27 21:20:07:
- icomment:
Discussion continues on the tcl core list. Take care, Harald
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [b0285f9059] (rid 219826) by oehhar on 2023-10-28 07:21:49:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [56eecd1e46] (rid 219889) by juliannoble2 on 2023-11-02 17:13:14:
- icomment:
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.
- login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [3a0752cd47] (rid 220682) by oehhar on 2023-11-30 19:14:07:
- icomment:
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
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [80dc6f0e93] (rid 220683) by oehhar on 2023-11-30 19:14:39:
- closedate changed to: "2460279.30184826"
- closer changed to: "oehhar"
- login: "oehhar"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"