Skip to content
Tcl Source Code View Ticket
Login

View Ticket

cmdAH-25.2 (Tcl_FileObjCmd: owned) fails on Windows

Closed / Fixed7de2d722bd599da5db26b42ed8addb16a66c832cCreated 7 years agoUpdated 7 years ago
Descriptionwiki2019-09-05 20:09:59
core-8-6-branch built with MS Visual Studio Express 2008 on Vista, or also on Win10:
==== cmdAH-25.2 Tcl_FileObjCmd: owned FAILED
==== Contents of test case:

    file owned $gorpfile

---- Result was:
0
---- Result should have been (exact matching):
1
==== cmdAH-25.2 FAILED
Referenced by3
  • 4be8da3560cmdAH.test (win-only): rewrite test to prefer SystemRoot (readonly) instead of windir to check windows directory is not owned, bug [7de2d722bd] check-in2019-09-12 19:34:23
  • 1b22a4a786merge 8.5 (bug [7de2d722bd]) check-in2019-09-11 11:51:48
  • dac34b4081windows, close [7de2d722bd]: prefer temp file to check owner and reown it before trying to check in order to avoid dependency on admin with UAC and the setting of "System objects: Default owner for objects created by members of the Administrators group" check-in2019-09-11 11:49:59
fvogelwiki2019-09-08 10:38:11

A quick debug session shows that on this line 'owned' becomes false because EqualSid() returns false.

Simple test case derived from cmdAH-25.2:

set gorpfile "C:/gorp.file"
set fd [open $gorpfile w]
puts $fd abcdef
close $fd
file owned $gorpfile  ; # returns 0, but 1 is expected
file delete $gorpfile

I would haved expected that 'file owned' returned true for files I had just created myself as in the above test script.

sebreswiki2019-09-09 08:13:48

> I would haved expected that 'file owned' returned true for files I had just created myself

Nope, it is related to NTFS default security for drive (since Vista/Win7 it is owned by default owner after creation, also note that creator owner should have permission to take ownership, as well as the policies for that).

Anyway many tests would only work properly if you start tcl-test from %TEMP% folder.
In automake (mingw) build I extended tcltest target with helpers doing that, here is for example auto-generated tcltest.cmd:

@echo off
set BDP=%~dp0
set OWD=%CD%
cd /d %TEMP%
rem "%BDP%\tclsh86.exe" "%BDP%/../../tests/all.tcl" %TESTFLAGS% -load "package ifneeded Tcltest 8.6.9 [list load [file normalize tcltest86.dll] Tcltest]; package ifneeded dde 1.4.1 [list load [file normalize tcldde14.dll] dde]; package ifneeded registry 1.3.3 [list load [file normalize tclreg13.dll] registry]" %*
"%BDP%\tcltest.exe" ""%BDP%/../../tests/all.tcl" %TESTFLAGS% -load "package ifneeded dde 1.4.1 [list load [file normalize tcldde14.dll] dde]; package ifneeded registry 1.3.3 [list load [file normalize tclreg13.dll] registry]" %*
cd /d %OWD%
Can you test your build using this method?
Then we could create such helpers and test from it also in VS-build.

I have also fixed several tests (or provided few additional constraint) on github to satisfy travis, have still to rebase that all to fossil from github.

For 8.5 - https://github.com/sebres/tcl/compare/core-8-5-branch...test/travis-8-5-branch
For 8.6 - https://github.com/sebres/tcl/compare/core-8-6-branch...tests/sebres-travis-windows-debug

fvogelwiki2019-09-09 21:11:14

Thanks for your answer!

I have tried your tcltest.cmd idea (the file needs to be installed in .../tcl/win/Debug_VC9 for me). Here is the outcome:

  • cmdAH-25.2 still fails. Don't you also see this test failing on Windows?
  • three tests fail with your tcltest.cmd that do not fail when the tests are launched directly: basic-48.25.1 (because testevalex is unknown) and lrange-3.7c.2 and lrange-3.7d.2 (because testpurebytesobj is unknown). This should be fixable.
  • the winFCmd tests that I see failing (see [ad28319dc2]) when launched directly do not longer fail with tcltest.cmd

> note that creator owner should have permission to take ownership, as well as the policies for that

Couldn't this be implemented in the cmdAH-25.2 test? I mean, add a step to take ownership of the gorpfile, and only afterwards exercise 'file owned $gorpfile'?

sebreswiki2019-09-10 12:03:58

> Don't you also see this test failing on Windows?

Nope, it works on my system (Win 7 as well as Win 10 in VM) for x86 and x64 builds (mingw, VC9.0 or VS2015):

> d:\Projects\tcl8.6\win\Release_VC1900\tcltest.cmd -file "cmdAH.test" -match "cmdAH-25.2"
Tests running in interp:  d:/Projects/tcl8.6/win/Release_VC1900/tcltest.exe
Tests located in:  D:/Projects/tcl8.6/tests
Tests running in:  C:/Temp
Temporary files stored in C:/Temp
Test files run in separate interpreters
Running tests that match:  cmdAH-25.2
Skipping test files that match:  l.*.test
Only running test files that match:  cmdAH.test
Tests began at Tue Sep 10 12:57:46 +0200 2019
cmdAH.test

Tests ended at Tue Sep 10 12:57:47 +0200 2019 all.tcl: Total 323 Passed 1 Skipped 322 Failed 0 Sourced 1 Test Files.

If I debug the test (output a file name), I see the file is pretty well created in my %TEMP% directory so as C:/Temp/gorp.file and I'm an owner (normally default policies for TEMP should set owner for the creater).

May be we should use sub-directory (often it gets other permissions per default)...
Do you see 1 if you execute both of this in tclsh?

close [open [set fn $::env(TEMP)/test-owner-from-tcl.txt] w]; puts [file owned $fn]; file delete $fn

set fn $::env(TEMP)/test-owner-from-tcl/1.txt; file mkdir [file dirname $fn]; close [open $fn w]; puts [file owned $fn]; file delete -force [file dirname $fn]

> three tests fail with your tcltest.cmd ...

Well I posted the helper for release build, may be for debug it miss some suffix (like g) in some loads or we've just issues with constraints recognizing tcltest is available. Anyway I see no tests failing if calling test suite from TEMP folder.

> Couldn't this be implemented in the cmdAH-25.2 test?

Well we can rewrite it to use env(TEMP) folder (the test has a constraint win). But it seems to fail on your PC for some reason.

Another variant would be to write a helper in tcltest taking ownership to current user (for example using takeown command or coded in C like described in https://docs.microsoft.com/en-us/windows/win32/secauthz/taking-object-ownership-in-c--), just it can cause another issues due to default security on some systems (and/or UAC). Better would be to skip it by some constraint (e. g. takeown would fail). Can you try to change the owner using takeown if above mentioned script fails for you.
But somehow I'm pretty sure exec takeown /F [file nativename $fn] would fail for you.

Also note that in Windows (since 8.6) we check ownership using TclWinFileOwned, which can return 0 in case of GetNamedSecurityInfo, OpenProcessToken or GetTokenInformation fail, as well as the method with EqualSid by token-info may be a bit questionable in case of special permissions for a drive (at least with UAC and/or custom UAC-control set).
But not in case of TEMP-folder (this makes no sense).

fvogelwiki2019-09-10 22:12:36

> If I debug the test (output a file name), I see the file is pretty well created in my %TEMP% directory so as C:/Temp/gorp.file

For me the file is C:/Users/francois/AppData/Local/Temp/gorp.file and echo %TEMP% returns C:\Users\francois\AppData\Local\Temp. So yes for me too the file is pretty well created in my %TEMP% directory. A difference is that %TEMP% is located in my Windows profile (does this really make a difference?).

> Do you see 1 if you execute both of this in tclsh?

>  close [open [set fn $::env(TEMP)/test-owner-from-tcl.txt] w]; puts [file owned $fn]; file delete $fn
>
>  set fn $::env(TEMP)/test-owner-from-tcl/1.txt; file mkdir [file dirname $fn];
>  close [open $fn w]; puts [file owned $fn]; file delete -force [file dirname $fn]

Actually no, I see two zeroes.

> Can you try to change the owner using takeown if above mentioned script fails for you. But somehow I'm pretty sure exec takeown /F [file nativename $fn] would fail for you.

Nope, it works. I'm seeing 0 and 1 when executing the following:

  close [open [set fn $::env(TEMP)/test-owner-from-tcl.txt] w]; puts [file owned $fn]
  exec takeown /F [file nativename $fn]; puts [file owned $fn]

This is on a Vista computer on which I'm logged in and using from an account 'francois' having administrator privileges. Before running takeown, the owner of the file is mycomputername\Administrators, but after taking ownership it becomes mycomputername\francois. User 'francois' has admin privileges.

I didnt yet check on another computer, running Win 10, on which I'm logged in as an ordinary user. I'm seeing the problem described in the present ticket on both computers.

> Also note that in Windows (since 8.6) we check ownership using TclWinFileOwned, which can return 0 in case of GetNamedSecurityInfo, OpenProcessToken or GetTokenInformation fail

I have already confirmed that TclWinFileOwned returns false not because of the above but really because EqualSid()</> returns zero (see my first comment below dated 2019-09-08 10:38:11, first line). Given the switch made by takeown and the change of result for 'file owner', this is all consistent.

sebreswiki2019-09-11 11:54:16

Revision [1b22a4a786] should fix it now (regardless which test-directory or system is used), can you please test it and close ticked if it'd work.

fvogelwiki2019-09-11 19:46:21

Great! At [7395c026b7] his is now working (cmdAH-25.2 PASSES) on both my Vista and my Win10 computers.

What I'm seeing now is that another test is failing, on Win10 only. I didn't notice this before but perhaps it was already there. The test looks similar.

==== cmdAH-25.3.1 Tcl_FileObjCmd: owned FAILED
==== Contents of test case:

    file owned $env(windir)

---- Result was:
1
---- Result should have been (exact matching):
0
==== cmdAH-25.3.1 FAILED

sebresplain2019-09-11 21:04:40
How it can be? Have we some bug in core or Tcl_FileObjCmd for some constelation, or are you really owner of C:\Windows folder?
(Did you tacking the ownership manually at some point?)
For this case we'd need to check the owner is not a caller of test (like dir /q).
fvogelwiki2019-09-12 16:16:40

On the Win10 PC where cmdAH-25.3.1 is failing, the owner of C:\Windows is 'TrustedInstaller'. That's not me.

What is weird is that running file owned $env(windir) directly in tclsh returns the 0 expected by the test.

So the test must be testing the owner of $env(windir) but this variable does not, at the time the test is executed, contain C:\Windows. Adding a puts inside the test indeed reveals that $env(windir) actually is E:\tcltk-fossil\tcl\win\..\win, i.e. the 'win' subdirectory of the tcl repository.

On the Vista PC on which cmdAH-25.3.1 is passing, the owner of C:\Windows also is 'TrustedInstaller', which is not me. The puts in cmdAH-25.3.1 reveals that again $env(windir) is not C:\Windows but the 'win' subdirectory of the tcl repository. So on this Vista PC the test passes by pure accident.

Conclusion: cmdAH-25.3.1 is not testing the owner of C:\Windows as was probably the original intent. It seems that something sets $env(windir) so that it rather contains the 'win' subdirectory of the tcl repository, and the test passes or fails depending on which user runs the tests. I think the original intent in cmdAH-25.3.1 rather was to test something like file owned $env(SystemRoot), don't you think so?

sebreswiki2019-09-12 18:11:03

As for SystemRoot vs WinDir, yes we can switch it (also in init.tcl etc, because auto_execok may then also stop to work or add some invalid path).

SystemRoot is readonly in opposite to WinDir and WinDir was superseded by SystemRoot at some point (but does not exists in earlier Windows versions). (see also https://superuser.com/questions/638321/what-is-difference-between-windir-and-systemroot).

Also I suspect many scripts still depending on env(WinDir), so possibly still better to cover that...

So crucial question is what exactly overwrites it in your case? Just good to know.
Does it also happen if you starts only this test alone, so using -file cmdAH.test -match cmdAH-25.3.1?
Can you possibly extend your tests (init.tcl, tcltest.tcl, whatever) with some trace over ::env in order to see who exactly changes that?

sebreswiki2019-09-12 18:31:02

Oh... I think I know what it could be (I guess you're starting test-sute using nmake? or in shell where nmake was previously executed) - it could be then this one - rules.vc#114.

Perhaps we should use another name for this. Anyway in win/Makefile.in it is called WIN_DIR and not WINDIR.

sebreswiki2019-09-12 19:41:36

I think cmdAH-25.3.1 (and related issues) are fixed now ([1b04153fe9] and [4be8da3560])...

Please test it, Francois.

@Jan: I know now why you've used some constraint on this test (resp. why it could fail in Travis :)

fvogelwiki2019-09-12 20:01:31

That was it, nice catch! Now on neither Vista or Win10 $env(windir) is 'C:\Windows' as we may expect. And the test passes on both OSes. Great! And many thanks, Serguey!

Actually it works even without [4be8da3560].

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