You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix ProcessStartInfo environment for alternate Windows users - #137445
On Windows, initialize ProcessStartInfo.Environment from the specified user's environment using LogonUser and CreateEnvironmentBlock, without inheriting the caller's variables. Network-only credentials continue to use the caller's environment.
Share the existing Windows environment-block parser between CoreLib and Process. Keep the logon code trimmable when UserName is never set.
Reject credential changes that would invalidate an already captured environment snapshot.
Accessing the environment for a specified user now authenticates that user and can throw if the credentials are invalid. The user's profile is not loaded to construct the snapshot, so profile-specific variables may be missing even when LoadUserProfile is set.
The administrator-only target-user test was reported passing on an earlier revision; its subsequently added setter-order assertions have not been rerun elevated.
NativeAOT trimming was verified on an earlier revision: an app that did not set UserName did not retain the logon helper or Userenv imports. It was not rerun after the parser and ordering changes.
A checked CoreLib build targeting clr.corelib+clr.nativecorelib+libs.pretest -rc checkedfailed during crossgen with 0xC0000005; the cause has not been determined.
….Environment with UserName set
On Windows, when UserName is set (and UseCredentialsForNetworkingOnly is not),
ProcessStartInfo.Environment is now populated using LogonUser + CreateEnvironmentBlock,
so the child process gets the target user's environment instead of the parent's.
Fixesdotnet#136746
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f1f4be50-afad-498e-96d6-62c7358bd322
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The Windows-only parser include leaves Linux/macOS CoreCLR callers without its definition, and the tests do not adequately verify environment isolation for alternate-user and network-only credentials.
This PR updates Windows ProcessStartInfo.Environment to use the specified user's environment rather than the caller's, addressing incorrect variables when launching a process under alternate credentials.
Changes:
Creates target-user environment snapshots through Windows logon APIs while preserving caller variables for network-only credentials.
Shares environment-block parsing with CoreLib and guards credential changes after snapshot capture.
Adds Windows tests and documents authentication and profile-loading behavior.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Regression tests omit coverage of domain and network-only credential mutation guards, and the documentation incorrectly promises the target user's environment for network-only credentials.
This also describes network-only credentials, but GetDefaultEnvironmentVariables deliberately returns the caller's environment in that case. The current wording therefore incorrectly promises the target user's environment—and exclusion of caller-only variables—whenever UserName is set. Qualify this statement with UseCredentialsForNetworkingOnly == false.
The reason will be displayed to describe this comment to others. Learn more.
Also, the new behavior is non-intuitive - it violates .NET Framework design guidelines: "DO allow properties to be set in any order, even if this results in a temporary invalid state of the object."
I understand what you are trying to do here. The cure may be worse than the disease.
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can fix this bug without introducing a breaking change.
It is not clear whether the current behavior - that existed for decades - is a bug. The behavior may be unexpected by some users, but it does not make it a bug.
You can fix the user scenario by introducing a new API, without introducing a breaking change.
The reason will be displayed to describe this comment to others. Learn more.
It is not clear whether the current behavior - that existed for decades - is a bug.
In my opinion it's a bug (depending if I use given property, the process is started with different set of env vars which can include secrets from the parent process), but most likely very few people have noticed it.
I know that I can introduce a new API (get env vars for specific user), the problem is that the users would need to be aware of the problem and of the fact that we provide a new API. If we introduce a breaking change that throws, it forces the users to read the docs and act.
worked before, and it is going to throw exception now.
(Original reporter here, hello) IMO "worked" seems like a bit of a misnomer here. The child process ended up in an environment that was effectively completely broken. Any usage of foundational environment information like USERNAME, APPDATA, and even TEMP would yield paths that may be unusable at best, or an unexpected cross-user privilege management failure at worst (depending on whether the parent or child had higher privileges).
Like was mentioned, secrets from the environment of the host process would also end up being unexpectedly propagated into the child environment, which could be considered a vulnerability.
The reason will be displayed to describe this comment to others. Learn more.
Short answer: Go does not have a dedicated solution for this case either. It gives the user a way to obtain the token user's default environment, modify it, and assign it back. It never merges a custom environment with anything.
Custom environment: Cmd.Env is a plain []string that completely replaces the default (exec.go#L162-L164). The common idiom cmd.Env = append(os.Environ(), "K=V") passes the caller's environment to the other user, which is the same problem .NET has today.
golang.org/x/sys/windows exposes Token.Environ(inheritExisting bool), a thin wrapper over CreateEnvironmentBlock (env_windows.go#L33).
Caveat: if CreateEnvironmentBlock fails, environ() falls back to os.Environ() (exec.go#L1218), and the public Environ() intentionally discards the error (exec.go#L1255). So in Go, a failure silently gives the caller's environment to the other user.
How this maps to this PR: ProcessStartInfo.Environment is the .NET equivalent of Cmd.Environ(). It is lazily populated, and with this PR it is populated from the target user when UserName is set (and UseCredentialsForNetworkingOnly is not), so customizing it in place works as expected. Differences from Go:
Go relies on the user setting Token before calling Environ(). We enforce that ordering by throwing InvalidOperationException when UserName, Domain, or UseCredentialsForNetworkingOnly change in a way that would affect the environment after Environment was accessed.
Go receives an existing token, while ProcessStartInfo only has user name, domain, and password, so we call LogonUser ourselves to get the token.
We throw Win32Exception on failure instead of falling back to the caller's environment.
Note
This comment was generated with the help of GitHub Copilot.
Added needs-breaking-change-doc-created label because this PR has the breaking-change label.
When you commit this breaking change:
Create and link to this PR and the issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.
Ask a committer to mail the .NET Breaking Change Notification DL.
Tagging @dotnet/compat for awareness of the breaking change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ProcessStartInfo.Environmentfrom the specified user's environment usingLogonUserandCreateEnvironmentBlock, without inheriting the caller's variables. Network-only credentials continue to use the caller's environment.UserNameis never set.Accessing the environment for a specified user now authenticates that user and can throw if the credentials are invalid. The user's profile is not loaded to construct the snapshot, so profile-specific variables may be missing even when
LoadUserProfileis set.Validation
dotnet build src\libraries\System.Diagnostics.Process\src\System.Diagnostics.Process.csproj— passed.dotnet build src\libraries\System.Diagnostics.Process\tests\System.Diagnostics.Process.Tests.csproj /t:Test "/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessStartInfoTests"— passed: 75 total, 0 failed, 2 skipped.UserNamedid not retain the logon helper or Userenv imports. It was not rerun after the parser and ordering changes.clr.corelib+clr.nativecorelib+libs.pretest -rc checkedfailed during crossgen with0xC0000005; the cause has not been determined.Resolves #136746
Note
This PR description was generated with GitHub Copilot.