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

Always create a new environment block before we spawn a process #7243

Merged
3 commits merged into from
Aug 11, 2020

Conversation

DHowett
Copy link
Member

@DHowett DHowett commented Aug 11, 2020

This commit ensures that we always furnish a new process with the
cleanest, most up-to-date environment variables we can. There is a minor
cost here in that WT will no longer pass environment variables that it
itself inherited to its child processes.

This could be considered a reasonable sacrifice. It will also remove
somebody else's TERM, TERM_PROGRAM and TERM_PROGRAM_VERSION from the
environment, which could be considered a win.

I validated that GetCurrentProcessToken returns a token we're
technically able to use with this API; it is roughly equivalent to
OpenProcessToken(GetCurrentProcess) in that it returns the current
active access token (which is what CreateEnvironmentBlock wants.)

There's been discussion about doing a 3-way merge between WT's
environment and the new one. This will be complicated and I'd like to
scream test the 0-way merge first ;P

Related to #1125 (but it does not close it or resolve any of the other
issues it calls out.)

Fixes #7239
Fixes #7204 ("App Paths" value creeping into wt's environment)

This commit ensures that we always furnish a new process with the
cleanest, most up-to-date environment variables we can. There is a minor
cost here in that WT will no longer pass environment variables that it
itself inherited to its child processes.

This could be considered a reasonable sacrifice. It will also remove
somebody else's TERM, TERM_PROGRAM and TERM_PROGRAM_VERSION from the
environment, which could be considered a win.

Related to #1125 (but it does not close it or resolve any of the other
issues it calls out.)

Fixes #7239
Fixes #7204 ("App Paths" value creeping into wt's environment)
// Return Value:
// - S_OK if we succeeded, or an appropriate HRESULT for failing
HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept

Choose a reason for hiding this comment

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

It seems a bit strange to take std::optional<void*> and then treat nullptr as equivalent to std::nullopt. I don't see std::optional<T*> anywhere else in the source tree. But perhaps it makes sense if you consider nullptr an invalid argument here.

Copy link
Member

Choose a reason for hiding this comment

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

I concur - why isn't the parameter just a EnvironmentBlockPtr that we compare to nullptr to check if it was provided?

@zadjii-msft
Copy link
Member

I'm cool with this as-is, but I think I want to hear the optional<void*> discussion first

@zadjii-msft zadjii-msft added Area-TerminalConnection Issues pertaining to the terminal<->backend connection interface Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal. labels Aug 11, 2020
@DHowett
Copy link
Member Author

DHowett commented Aug 11, 2020

So, that's a fair question and one for which I don't really have a good answer. std::optional<void*> was driven by my innate aversion to a raw void*. I would have passed an EnvironmentBlockPtr as @zadjii-msft suggested, but since that's a typedef over wil::unique_any it enforces ownership semantics at the function signature. It's generally incorrect for a function to require particular ownership semantics of its parameters.

I'm fine just dropping it to void*, but I somewhat wish that we had a better type to express that it's an environment block. I'd much rather not write more boilerplate just to do so, so I'm not interested in introducing another manual reference counting struct type. 🤷

@DHowett
Copy link
Member Author

DHowett commented Aug 11, 2020

There, I switched it to a std::optional<gsl::not_null<void*>>.

@KalleOlaviNiemitalo
Copy link

I don't see any gsl::not_null in the diff….

@DHowett
Copy link
Member Author

DHowett commented Aug 11, 2020

Well, I decided that an optional-not-null-void* was the same as a void* so I cut out the middle party 😉

@DHowett DHowett added the Needs-Second It's a PR that needs another sign-off label Aug 11, 2020
Copy link
Member

@miniksa miniksa left a comment

Choose a reason for hiding this comment

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

While I think the 3-way merge is probably the ideal solution, I can see it being prohibitively expensive to figure out before we know that it's warranted.

Making the 0-way merge of a fresh block solves the immediate problem. And I think it would/will be minor to add a follow-on of "which vars would you like to inherit and stomp in the new block" if that becomes necessary (at a much lower cost than a full merge resolution).

So I'm OK with this.

@ghost ghost added Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) labels Aug 11, 2020
@DHowett DHowett added AutoMerge Marked for automatic merge by the bot when requirements are met and removed Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) labels Aug 11, 2020
@ghost
Copy link

ghost commented Aug 11, 2020

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 849243a into master Aug 11, 2020
@ghost ghost deleted the dev/duhowett/what_if_we_just_ branch August 11, 2020 23:58
EnvironmentBlockPtr Microsoft::Console::Utils::CreateEnvironmentBlock()
{
void* newEnvironmentBlock{ nullptr };
if (!::CreateEnvironmentBlock(&newEnvironmentBlock, GetCurrentProcessToken(), FALSE))
Copy link
Member Author

Choose a reason for hiding this comment

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

@Nathpete can you test what happens if you replace GetCurrentProcessToken with..

auto processToken{ wil::open_current_access_token() };
CreateEnv(...processToken.get()...)

If that doesn't restore USERNAME, can you try passing TOKEN_QUERY|TOKEN_DUPLICATE as the first arg to open_current_acc?

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops - @nathpete-msft

Copy link
Member

Choose a reason for hiding this comment

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

auto processToken{ wil::open_current_access_token(TOKEN_QUERY | TOKEN_DUPLICATE) };

That does it!

Copy link

@KalleOlaviNiemitalo KalleOlaviNiemitalo Aug 25, 2020

Choose a reason for hiding this comment

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

Ugh, I am surprised CreateEnvironmentBlock behaves that way. Tested:

  • NULL => USERNAME=SYSTEM
  • GetCurrentProcessToken() => no USERNAME
  • GetCurrentThreadEffectiveToken() => no USERNAME
  • OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_QUERY_SOURCE, &hToken) => no USERNAME
  • OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &hToken) => correct USERNAME
  • (HANDLE)0x123456 => error 6

Perhaps it is because GetCurrentProcessToken() lacks TOKEN_DUPLICATE access.

Copy link
Member Author

Choose a reason for hiding this comment

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

You know, you're probably right about that last bit. That makes sense.

At least there's a nice WIL helper for us to get our current effective access token 😄

@ghost ghost removed the Needs-Second It's a PR that needs another sign-off label Aug 25, 2020
ghost pushed a commit that referenced this pull request Aug 25, 2020
This fixes a regression in environment variable loading introduced as part
of the new environment block creation that prevents some system-defined,
volatile environment variables from being defined.

## References
#7243 (comment)

## Validation Steps Performed
Manually verified locally.

Closes #7399
DHowett pushed a commit that referenced this pull request Aug 25, 2020
This fixes a regression in environment variable loading introduced as part
of the new environment block creation that prevents some system-defined,
volatile environment variables from being defined.

## References
#7243 (comment)

## Validation Steps Performed
Manually verified locally.

Closes #7399

(cherry picked from commit 64f10a0)
@ghost
Copy link

ghost commented Aug 26, 2020

🎉Windows Terminal Preview v1.3.2382.0 has been released which incorporates this pull request.:tada:

Handy links:

DHowett added a commit that referenced this pull request Sep 21, 2020
donno2048 added a commit to donno2048/terminal that referenced this pull request Sep 28, 2020
This fixes a regression in environment variable loading introduced as part
of the new environment block creation that prevents some system-defined,
volatile environment variables from being defined.

## References
microsoft/terminal#7243 (comment)

## Validation Steps Performed
Manually verified locally.

Closes #7399
DHowett added a commit that referenced this pull request Nov 9, 2020
…ss (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
DHowett added a commit that referenced this pull request Jan 25, 2021
…ss (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit fce00ff)
DHowett added a commit that referenced this pull request Feb 23, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
DHowett added a commit that referenced this pull request Apr 7, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
DHowett added a commit that referenced this pull request May 24, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
DHowett added a commit that referenced this pull request Jul 12, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
DHowett added a commit that referenced this pull request Aug 26, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
DHowett added a commit that referenced this pull request Oct 11, 2021
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
(cherry picked from commit b25dc74)
@kpentaris
Copy link

@DHowett Hello. Has this issue regressed? I'm on WT version 1.11.2921.0 and I'm having the same problem albeit using the "setx /M" command to update the system wide env variable. Is this different?

@KalleOlaviNiemitalo
Copy link

@kpentaris, these changes have been repeatedly reverted from stable releases of Windows Terminal, e.g. in 616a71d for Windows Terminal 1.11.2921.0. If you want this feature, use Windows Terminal Preview for now. I guess it will continue this way until the Windows PowerShell (x86) incompatibility #7418 is fixed somehow.

@zadjii-msft
Copy link
Member

@kpentaris I believe we never ended up shipping this to stable, and it's only in the Preview builds of the Terminal

@kpentaris
Copy link

D'oh! It's actually mentioned in the latest commit... Very well, hopefully at some point this is fixed.

miniksa pushed a commit that referenced this pull request Jan 10, 2022
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
(cherry picked from commit b25dc74)
DHowett added a commit that referenced this pull request Jan 11, 2022
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
(cherry picked from commit b25dc74)
(cherry picked from commit 5f7c66b)
ghost pushed a commit that referenced this pull request Jan 12, 2022
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
(cherry picked from commit b25dc74)
(cherry picked from commit 5f7c66b)

Fixes #7418
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalConnection Issues pertaining to the terminal<->backend connection interface AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal.
Projects
None yet
8 participants