Skip to content

Prevent .tsh/environment values from overloading prior set values#34277

Merged
jentfoo merged 3 commits intomasterfrom
jent/env_overload_protection
Nov 15, 2023
Merged

Prevent .tsh/environment values from overloading prior set values#34277
jentfoo merged 3 commits intomasterfrom
jent/env_overload_protection

Conversation

@jentfoo
Copy link
Copy Markdown
Contributor

@jentfoo jentfoo commented Nov 6, 2023

It's not possible to have duplicate environment values within an environment. And in fact the last value in the string slice will be preserved. Prior to this change that allows users to possibly change any environment values through the use of the .tsh/environment file. This is a user level control, where other environment value originate from a more protected location (for example the PAM configuration).

I believe this user level override is not expected, and may be dangerous if users can control where temporary files are written or other system level administration attributes.

This PR makes it so that the administrative set values will be preferred, and any duplicate records will be ignored.

This is a follow up from #34177. We pulled this change into a separate PR so that we can discuss if this behavior change can be safely backported to our current major versions. I can not conceive of a valid use case of this behavior, and I believe that our customers would likely be concerned that a user could influence the system level configuration set by an administrator.

However I have not been able to find a specific exploit case where this manipulation could lead to privilege escalation or other more serious impacts.

changelog: Environment values can not be overridden from the .tsh/environment file, only unique keys will be inserted into the environment.

Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment.go Outdated
Comment on lines 152 to 154
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are environment variables case sensitive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They are in linux and macOS, but they are not in windows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Aren't we breaking windows by doing a case insensitive comparison here then? foo != FOO != Foo but since we are converting to upercase all three of those keys would be equivalent and only the first one defined would be allowed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually this check is mostly to protect Windows, arguably we could be breaking Linux / MacOS. In Windows regardless of the casing used for lookup you would find a value. However in Linux and MacOS the lookup must match the casing exactly. So, if some tooling needs to use the key Foo and FOO as two separate and distinct values, then we would not allow that distinction.

That said, I feel like this condition is extremely corner case. And I think it's reasonable for Teleport to generally not allow any duplication regardless of casing. But if necessary we can put OS specific logic in to handle the way different operating systems consider environment key conflicts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't this a breaking change then? What if my pam config defines foo and FOO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would be extremely surprised if people are passing different values through a variation in casing. For that reason, I feel that considering duplicates generally to be case insensitive is fine for simplicity of code and generally being defensive. However if we need to do OS specific logic I can add that.

@zmb3 and @r0mant what are your thoughts?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it would be least disruptive if we could do the following:

  • Still allow both foo and Foo in the PAM config, maintaining existing behavior.
  • Prevent overriding any variation of an existing variable in .tsh/environment. This means if Foo is already set, then any variation (foo, FoO, FOO, etc) of that name in .tsh/environment is ignored.

Seem reasonble?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me, I made this change in dbc5896

@jentfoo jentfoo force-pushed the jent/env_overload_protection branch from 81fd410 to 968c0d0 Compare November 7, 2023 17:05
Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment.go Outdated
Comment thread lib/utils/envutils/environment_test.go Outdated
It's not possible to have duplicate environment values within an environment.  And in fact the last value in the string slice will be preserved.  Prior to this change that allows users to possibly change any environment values through the use of the `.tsh/environment` file.  This is within user level control, where other environment value sources originate from a more protected location (for example the PAM configuration).

Prior to this change that allows users to possibly change any environment passed configuration through the use of the `.tsh/environment` file.

This change makes it so that the administrative set values will be preferred, and any duplicate records will be ignored.
This change updates `SafeEnv` to be allow the caller to select if the value should be checked for duplicates.
We then leverage this to avoid this check when sourced from a trusted source.  But then exclude potential duplicates when sourced from .tsh/environment file or the local environment.
@jentfoo jentfoo force-pushed the jent/env_overload_protection branch from ef81e33 to b495731 Compare November 10, 2023 16:39
Copy link
Copy Markdown
Contributor

@rosstimothy rosstimothy left a comment

Choose a reason for hiding this comment

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

Thanks for this @jentfoo!

@jentfoo jentfoo requested a review from codingllama November 13, 2023 19:25
@jentfoo jentfoo added this pull request to the merge queue Nov 15, 2023
Merged via the queue into master with commit d0f2b44 Nov 15, 2023
@jentfoo jentfoo deleted the jent/env_overload_protection branch November 15, 2023 17:08
@public-teleport-github-review-bot
Copy link
Copy Markdown

@jentfoo See the table below for backport results.

Branch Result
branch/v12 Create PR
branch/v13 Create PR
branch/v14 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants