Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MinGW: link as terminal server aware #3942

Merged
merged 1 commit into from
Jul 27, 2022

Conversation

rimrul
Copy link
Member

@rimrul rimrul commented Jul 10, 2022

Whith Windows 2000, Microsoft introduced a flag to the PE header to mark executables as
"terminal server aware". Windows terminal servers provide a redirected Windows directory and
redirected registry hives when launching legacy applications without this flag set. Since we
do not use any INI files in the Windows directory and don't write to the registry, we don't
need this additional preparation. Telling the OS that we don't need this should provide
slightly improved startup times in terminal server environments.

When building for supported Windows Versions with MSVC the /TSAWARE linker flag is
automatically set, but MinGW requires us to set the --tsaware flag manually.

This partially addresses #3935

Whith Windows 2000, Microsoft introduced a flag to the PE header to mark executables as
"terminal server aware". Windows terminal servers provide a redirected Windows directory and
redirected registry hives when launching legacy applications without this flag set. Since we
do not use any INI files in the Windows directory and don't write to the registry, we don't
need  this additional preparation. Telling the OS that we don't need this should provide
slightly improved startup times in terminal server environments.

When building for supported Windows Versions with MSVC the /TSAWARE linker flag is
automatically set, but MinGW requires us to set the --tsaware flag manually.

This partially addresses git-for-windows#3935.

Signed-off-by: Matthias Aßhauer <[email protected]>
@dscho
Copy link
Member

dscho commented Jul 10, 2022

@rimrul thank you for providing this PR.

I have to admit that I am not really knowledgeable of the full extent of the impact of this tsaware flag. Is there any documentation? My worry is that in the endeavor to slightly speed up git.exe invocations in Terminal Services (which might even make too negligible a difference to be noticeable) we might introduce a regression in a weird corner case that would make us regret introducing this change.

@dscho
Copy link
Member

dscho commented Jul 10, 2022

FWIW I kicked off a git-artifacts build to have an installer and a Portable Git to test.

@dscho
Copy link
Member

dscho commented Jul 10, 2022

FWIW I kicked off a git-artifacts build to have an installer and a Portable Git to test.

@ElemenTP since you seem to have access to Windows Terminal Services, could you give this a good testing? I am really unsure of the risk, and that would give me some confidence.

@rimrul
Copy link
Member Author

rimrul commented Jul 11, 2022

There is some documentation, but it's not much.
https://docs.microsoft.com/en-us/cpp/build/reference/tsaware-create-terminal-server-aware-application

https://docs.microsoft.com/en-us/previous-versions/aa382957

The older versions of the first document are a little more detailed on when MSVC does it by default:

https://github.com/MicrosoftDocs/cpp-docs/blob/897e3bb83468f06e9f7410aeb783be1b7bea40fa/docs/build/reference/tsaware-create-terminal-server-aware-application.md

I've also found this stackoverflow discussion noticing that it leads to a separate .idata section, presumably to optimise use of memory pages in TS environments.

https://stackoverflow.com/questions/1261291/what-does-the-tsaware-linker-flag-do-to-the-pe-executable

The flag should be set for all cygwin executables, so I'm slightly confused why the original report mentions executables in /usr/bin.

https://cygwin.com/pipermail/cygwin/2012-April/202019.html

https://gcc-patches.gcc.gnu.narkive.com/eaLPK1Ru/patch-cygwin-add-tsaware-linker-flag-to-cygwin-linker-specs

@rimrul
Copy link
Member Author

rimrul commented Jul 11, 2022

I have to admit that I am not really knowledgeable of the full extent of the impact of this tsaware flag.

We could ping the documentation author and kindly ask them about this. They're fairly active on GitHub and might be able to tell us (and/or document) all the things that happen with/without this flag.

@ElemenTP
Copy link

Thank you for your attention.
Noticed that Terminal Service has been renamed to Remote Desktop Terminal Services has been renamed
These docs also may help:
https://docs.microsoft.com/en-us/windows/win32/termserv/application-compatibility-layer
https://docs.microsoft.com/en-us/troubleshoot/windows/win32/access-violation-bex-appcrash
https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectoryw

FWIW I kicked off a git-artifacts build to have an installer and a Portable Git to test.

@ElemenTP since you seem to have access to Windows Terminal Services, could you give this a good testing? I am really unsure of the risk, and that would give me some confidence.

I have tested the installer git in git-artifacts with my projects on a Windows 11 PC through Remote Desktop connection, and it works fine. This can't be good enough, though. I am sorry that I can not provide much help since I am not pro with git and git for windows.

@ElemenTP
Copy link

I've also found this stackoverflow discussion noticing that it leads to a separate .idata section, presumably to optimise use of memory pages in TS environments.

https://stackoverflow.com/questions/1261291/what-does-the-tsaware-linker-flag-do-to-the-pe-executable

I tested this for both mingw-gcc with linker version 2.38 and MSVC with linker version 14.32, the artifacts with or without tsaware flag are of the same number of sections.

Dump of file .\TestApplication_msvc_tsa.exe

File Type: EXECUTABLE IMAGE

  Summary

        1000 .data
        1000 .pdata
        1000 .rdata
        1000 .reloc
        1000 .rsrc
        1000 .text
Dump of file .\TestApplication_msvc_notsa.exe

File Type: EXECUTABLE IMAGE

  Summary

        1000 .data
        1000 .pdata
        1000 .rdata
        1000 .reloc
        1000 .rsrc
        1000 .text
Dump of file .\test_mingw_tsa.exe

File Type: EXECUTABLE IMAGE

  Summary

        1000 .CRT
        1000 .bss
        1000 .data
        1000 .idata
        1000 .pdata
        1000 .rdata
        1000 .reloc
        3000 .text
        1000 .tls
        1000 .xdata
Dump of file .\test_mingw_notsa.exe

File Type: EXECUTABLE IMAGE

  Summary

        1000 .CRT
        1000 .bss
        1000 .data
        1000 .idata
        1000 .pdata
        1000 .rdata
        1000 .reloc
        3000 .text
        1000 .tls
        1000 .xdata

@dscho
Copy link
Member

dscho commented Jul 14, 2022

https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectoryw

Quoting from there:

Terminal Services: If the application is running in a Terminal Services environment, each user has a private Windows directory. There is also a shared Windows directory for the system. If the application is Terminal-Services-aware (has the IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE flag set in the image header), this function returns the path of the system Windows directory, just as the GetSystemWindowsDirectory function does. Otherwise, it retrieves the path of the private Windows directory for the user.

Wouldn't this be something we want, to safe-guard Git users from other (potentially malicious) users on the same Terminal Services server?

@rimrul
Copy link
Member Author

rimrul commented Jul 14, 2022

Wouldn't this be something we want, to safe-guard Git users from other (potentially malicious) users on the same Terminal Services server?

Do we read any config from or write any config to the windows directory?

@dscho
Copy link
Member

dscho commented Jul 14, 2022

Wouldn't this be something we want, to safe-guard Git users from other (potentially malicious) users on the same Terminal Services server?

Do we read any config from or write any config to the windows directory?

Not that I am aware of, other than the registry entry to determine whether we're running in a container.

But I am worried about things I might have missed. For example, for years I did not realize that C:\Windows\Temp was word-writable. Or imagine my shock when I realized that some software installs an OpenSSL DLL into C:\Windows\system32 _and then leaves that file not updated for years.

@reflectronic
Copy link

safe-guard Git users from other (potentially malicious) users on the same Terminal Services server?

I do not think this flag is a security feature. If an attacker can write malicious payloads into the system's Windows directory, there is no need to exploit Git through some kind of DLL planting -- they already have administrative access. Nearly every app compiled with a 21st century toolchain has this flag set by default -- Microsoft seems to think that there's very little risk.

@dscho
Copy link
Member

dscho commented Jul 27, 2022

I do not think this flag is a security feature. If an attacker can write malicious payloads into the system's Windows directory, there is no need to exploit Git through some kind of DLL planting -- they already have administrative access

I guess you're right ;-)

@dscho dscho added this to the Next release milestone Jul 27, 2022
Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

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

Okay, I made up my mind, thank y'all for your patience convincing me!

@dscho dscho linked an issue Jul 27, 2022 that may be closed by this pull request
1 task
dscho added a commit to git-for-windows/build-extra that referenced this pull request Jul 27, 2022
Git's executables are
[now](#429)
marked [Terminal
Server-aware](git-for-windows/git#3942),
meaning: Git will be slightly faster when being run using Remote
Desktop Services.

Signed-off-by: Johannes Schindelin <[email protected]>
@dscho dscho merged commit c7ab7b0 into git-for-windows:main Jul 27, 2022
@dscho dscho assigned dscho and rimrul and unassigned dscho Jul 27, 2022
@rimrul rimrul deleted the mingw-tsaware branch July 27, 2022 21:13
dscho added a commit that referenced this pull request Jul 27, 2022
MinGW: link as terminal server aware
dscho added a commit that referenced this pull request Jul 28, 2022
MinGW: link as terminal server aware
dscho added a commit that referenced this pull request Jul 28, 2022
MinGW: link as terminal server aware
dscho added a commit that referenced this pull request Jul 28, 2022
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jul 28, 2022
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jul 28, 2022
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jul 29, 2022
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jul 29, 2022
MinGW: link as terminal server aware
dscho added a commit to dscho/git that referenced this pull request Dec 16, 2024
dscho added a commit to dscho/git that referenced this pull request Dec 17, 2024
git-for-windows-ci pushed a commit that referenced this pull request Dec 17, 2024
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Dec 19, 2024
MinGW: link as terminal server aware
dscho added a commit to dscho/git that referenced this pull request Dec 30, 2024
dscho added a commit to dscho/git that referenced this pull request Dec 30, 2024
git-for-windows-ci pushed a commit that referenced this pull request Dec 31, 2024
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Dec 31, 2024
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 1, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 1, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 1, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 1, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 1, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 2, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 2, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 2, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 3, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 6, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 6, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 7, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 7, 2025
MinGW: link as terminal server aware
@dscho dscho mentioned this pull request Jan 7, 2025
dscho added a commit to dscho/git that referenced this pull request Jan 7, 2025
dscho added a commit to dscho/git that referenced this pull request Jan 7, 2025
git-for-windows-ci pushed a commit that referenced this pull request Jan 7, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 7, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 8, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 8, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 8, 2025
MinGW: link as terminal server aware
git-for-windows-ci pushed a commit that referenced this pull request Jan 8, 2025
MinGW: link as terminal server aware
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.

git.exe and many other apps do not have the Terminal Server aware flag set
4 participants