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

Use GetTempPath2W if available. #72452

Merged
merged 7 commits into from
Aug 11, 2022
Merged

Conversation

teo-tsirpanis
Copy link
Contributor

@teo-tsirpanis teo-tsirpanis commented Jul 19, 2022

Since Windows 11 there is a new API to get the temporary files path called GetTempPath2. This API returns a directory inaccessible to non-SYSTEM processes if the calling process runs as SYSTEM, and it is recommended to call this function instead of GetTempPath.

This PR tries to find GetTempPath2W and uses that, otherwise it falls back to GetTempPathW.

I also fixed a bug where the original GetTempPathW P/Invoke was not marked with SetLastError and would fail with Operation Completed Successfuly in case of a failure.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jul 19, 2022
@ghost
Copy link

ghost commented Jul 19, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Since Windows 11 there is a new API to get the temporary files path called GetTempPath2. This API returns a directory inaccessible to non-SYSTEM processes if the calling process runs as SYSTEM, and it is recommended to call this function instead of GetTempPath.

This PR tries to find GetTempPath2W and uses that, otherwise it falls back to GetTempPath.

I also fixed a bug where the original GetTempPathW P/Invoke was not marked with SetLastError and would fail with Operation Completed Successfuly in case of a failure.

Author: teo-tsirpanis
Assignees: -
Labels:

area-System.IO

Milestone: -

@jkotas
Copy link
Member

jkotas commented Jul 19, 2022

Does this behavior change need to be treated as a breaking change?

@danmoseley
Copy link
Member

Does this behavior change need to be treated as a breaking change?

This would be something like -- I wrote a file to temp, upgrade my .NET or OS, and try to read it? That seems unlikely to be something we want to guarantee. In general it is good to not assume that a reboot will preserve temp.

@teo-tsirpanis
Copy link
Contributor Author

And the path would change only if the program runs as SYSTEM. Most user apps are unaffected.

@danmoseley
Copy link
Member

Have people tried running .NET code as SYSTEM?

@GrabYourPitchforks
Copy link
Member

It's not uncommon for Windows services to run as SYSTEM. It's also possible that GetTempPath2W uses the current impersonation token instead of the process's own token, which could means the answer could change while the application is running. But I don't have a good way of testing this right now.

…s.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>
teo-tsirpanis and others added 2 commits August 5, 2022 22:59
@danmoseley
Copy link
Member

GetGCMemoryInfo failure is already resolved.

@danmoseley
Copy link
Member

@teo-tsirpanis could you please resolve the conflict so we can merge?

@teo-tsirpanis
Copy link
Contributor Author

Conflicts were resolved.

@danmoseley
Copy link
Member

Failure is #73679

@@ -42,7 +42,8 @@ public static unsafe Architecture OSArchitecture
// If we are running an x64 process on a non-x64 windows machine, we will report x64 as OS architecture.
//
// IsWow64Process2 is only available on Windows 10+, so we will perform run-time introspection via indirect load
if (NativeLibrary.TryGetExport(NativeLibrary.Load(Interop.Libraries.Kernel32), "IsWow64Process2", out IntPtr isWow64Process2Ptr))
IntPtr kernel32 = Interop.Kernel32.LoadLibraryEx(Interop.Libraries.Kernel32, 0, Interop.Kernel32.LOAD_LIBRARY_SEARCH_SYSTEM32);
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated change to make load library more secure?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for your contribution @teo-tsirpanis !

@adamsitnik
Copy link
Member

The failure is unrelated (#73679), merging

@adamsitnik adamsitnik merged commit cbf2e32 into dotnet:main Aug 11, 2022
@adamsitnik adamsitnik added this to the 7.0.0 milestone Aug 11, 2022
@teo-tsirpanis teo-tsirpanis deleted the gettemppath2 branch August 11, 2022 12:14
@ghost ghost locked as resolved and limited conversation to collaborators Sep 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants