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

do not leak PATH, LD_LIBRARY_PATH, and TMPDIR into the shell environment by default #2574

Closed
benjaminp opened this issue Feb 23, 2017 · 47 comments
Assignees
Labels
P1 I'll work on this now. (Assignee required) type: bug
Milestone

Comments

@benjaminp
Copy link
Collaborator

BazelConfiguration.setupShellEnvironement makes the default shell environment inherit the PATH, LD_LIBRARY_PATH, and TMPDIR environmental variables from the server environment (i.e., what they are at server startup time). I can see why this might be useful, but I don't think it should be the default behavior. I observed that this behavior was a source of indeterminism when building the same code on different machines; the build products were the same, but as environment affects the action key, remote caching across the machines didn't work. Why not be hermetic by default and have users who need to change these variables pass, e.g., --action_env=PATH?

If the behavior can't be changed, I would at least like a way to unset those environmental variables in the shell environment from the command line. AFAICT, with --action_env currently, the best you can do is set them to an empty string.

@iirina
Copy link
Contributor

iirina commented Feb 23, 2017

@gregestren can you take a look at this? Thanks.

@ulfjack ulfjack assigned aehlig and unassigned gregestren Mar 14, 2017
@ulfjack
Copy link
Contributor

ulfjack commented Mar 14, 2017

This is definitely on our todo list, but I'm not sure when we'll get to it. Adding 1.0 as the expected milestone, because it needs to be fixed by then (although earlier would be better).

@AustinSchuh
Copy link
Contributor

There's a pretty easy workaround.

Modify //tools/bazel in your repo to set the environment when it executes Bazel. We do something like:

ENVIRONMENT_VARIABLES=()
ENVIRONMENT_VARIABLES+=(HOSTNAME="${HOSTNAME}")
ENVIRONMENT_VARIABLES+=(SHELL="${SHELL}")
ENVIRONMENT_VARIABLES+=(USER="${USER}")
ENVIRONMENT_VARIABLES+=(PATH="${PATH}")
ENVIRONMENT_VARIABLES+=(LANG="${LANG}")
ENVIRONMENT_VARIABLES+=(HOME="${HOME}")
ENVIRONMENT_VARIABLES+=(LOGNAME="${LOGNAME}")
ENVIRONMENT_VARIABLES+=(TERM="${TERM}")

if [[ ! -z "${DISPLAY+x}" ]]; then
  ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}")
fi

exec -a "${VERSION_BAZEL}" env -i \
    "${ENVIRONMENT_VARIABLES[@]}" \
    "${VERSION_BAZEL}-real" "$@"

That lets us control which version of bazel is in use for each revision of the repository, and the environment. That makes it easy to handle compatibility.

@ulfjack
Copy link
Contributor

ulfjack commented Jun 24, 2017

Ugh, this could be a problem...

@ittaiz
Copy link
Member

ittaiz commented Jun 25, 2017

@ulfjack Why would this be a problem?

@ulfjack
Copy link
Contributor

ulfjack commented Jun 26, 2017

We have some users who are very eager to use remote caching, and we've been trying to get all the necessary changes into 0.5.2, so this bug could be a problem for them.

@ulfjack
Copy link
Contributor

ulfjack commented Jun 26, 2017

I confirm that I see cache misses on changes to PATH in combination with bazel server restarts. :-(

@ulfjack
Copy link
Contributor

ulfjack commented Jun 28, 2017

I've started on adding a flag for migration. We can't just remove the env variables, since that is a breaking change.

bazel-io pushed a commit that referenced this issue Jul 12, 2017
- --experimental_strict_action_env makes Bazel not forward PATH,
  LD_LIBRARY_PATH, and TMPDIR to all actions. This is intended to be a
  transitional flag; as part of rollout, we'll need to update all users that
  rely on the current behavior to specify their needs explicitly (with
  --action_env). But note that action_env is not yet applied to all actions,
  which also needs to be fixed.

- --shell_executable can be used to explicitly set the shell executable to
  use in actions. On Windows, if --experimental_strict_action_env is unset,
  then the PATH is computed to include the path to the shell executable.

Progress on #2574.

PiperOrigin-RevId: 161652996
@ulfjack
Copy link
Contributor

ulfjack commented Aug 1, 2017

I'd suggest setting --experimental_strict_action_env. It requires Bazel 0.5.3 or higher.

@benjaminp
Copy link
Collaborator Author

Yep, that option seems do precisely the right thing for me. Thanks.

Why does the option description mention $LANG, though? It doesn't actually seem to get set ever.

@ulfjack
Copy link
Contributor

ulfjack commented Aug 1, 2017

Sorry about that. That was from an earlier version of the change where I set LANG, which I decided not to do after all.

@laszlocsomor
Copy link
Contributor

@mboes : No, because Bazel uses different output directories for these workspaces, plus the workspace-relative file names (of the output files) are different.

@ulfjack
Copy link
Contributor

ulfjack commented Jan 22, 2018

There are multiple caches here. You can get a cache hit on the local or remote spawn cache, but not on the action cache, which is workspace-local, or on the in-memory cache, which is also workspace-local. If the two rules are in different locations (say //foo and //bar, then the names of the output files are likely different, in which case you can't get a cache hit either.

@mboes
Copy link
Contributor

mboes commented Jan 22, 2018

@ulfjack thanks. Is there any place you can point me to, to understand the difference between the spawn cache and the action cache? Does the (local or remote) spawn cache cache action outputs, or is that the action cache?

Being able to cache action outputs is pretty important for our use case. Haskell packages are notoriously slow to build. Many of them have source code (but not binary artifacts) published in a central location (called Hackage). We're considering running a remote cache that obviates the need for developers to have to rebuild these centrally published open source package every time they create a new project that uses them.

@ulfjack
Copy link
Contributor

ulfjack commented Jan 22, 2018

The spawn caches cache action outputs, the action cache works with outputs in the output tree (it doesn't cache files separately).

@mboes
Copy link
Contributor

mboes commented Jan 22, 2018

Gotcha. So IIUC, in the use case envisioned above, we should be able to avoid package rebuilds even across multiple (differently named) workspaces.

@ulfjack
Copy link
Contributor

ulfjack commented Jan 22, 2018

Yes, absolutely.

@ittaiz
Copy link
Member

ittaiz commented Jan 22, 2018 via email

@danny-kuminov
Copy link

qq: is there an ETA for when --experimental_strict_action_env=true will be the default ?

@ulfjack
Copy link
Contributor

ulfjack commented May 23, 2018

I've just fixed a number of cases where --action_env did not properly apply. I think we might want to wait for another release before we flip the default.

@danny-kuminov
Copy link

I see - makes sense, thank you!

@mboes
Copy link
Contributor

mboes commented Jul 14, 2018

I think we might want to wait for another release before we flip the default.

Will it become the default after 0.16.0? I too am pretty keen on this. :)

benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Sep 27, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.
benjaminp added a commit to benjaminp/bazel that referenced this issue Oct 2, 2018
See bazelbuild#2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.

RELNOTES: The --experimental_strict_action_env option is now on by default. This means Bazel will no longer use the client's PATH and LD_LIBRARY_PATH environmental variables in the default action environment. If the old behavior is desired, pass --action_env=PATH and --action_env=LD_LIBRARY_PATH. --noexperimental_strict_action_env will also temporarily restore the old behavior. However, as --action_env is a more general and explicit way to pass client environmental variables into actions, --noexperimental_strict_action_env will eventually be deprecated and removed.
@ulfjack ulfjack assigned buchgr and unassigned ulfjack and ola-rozenfeld Nov 19, 2018
bazel-io pushed a commit that referenced this issue Nov 19, 2018
Also rename it to --incompatible_strict_action_env. See #6648 and #2574. For a while, many actions did not respect the --action_env command line option, but those have been fixed now. So, I think it's time to flip on this flag for greater hermeticity by default.

Closes #6263.

RELNOTES: The --experimental_strict_action_env option has been renamed to --incompatible_strict_action_env and is now on by default. This means Bazel will no longer use the client's PATH and LD_LIBRARY_PATH environmental variables in the default action environment. If the old behavior is desired, pass --action_env=PATH and --action_env=LD_LIBRARY_PATH. --noincompatible_strict_action_env will also temporarily restore the old behavior. However, as --action_env is a more general and explicit way to pass client environmental variables into actions, --noincompatible_strict_action_env will eventually be deprecated and removed. See #6648 for more details.
PiperOrigin-RevId: 222053105
@buchgr
Copy link
Contributor

buchgr commented Jan 16, 2019

This has been enabled in Bazel 0.22

@buchgr buchgr closed this as completed Jan 16, 2019
bmclarnon added a commit to google-parfait/confidential-federated-compute that referenced this issue Mar 25, 2024
This requires both updating the version of rules_oci used as well
as patching the zot launcher since TMPDIR is not set when using
remote execution (likely due to
bazelbuild/bazel#2574).

Bug: 321291571
Change-Id: If6a7b8b7afbcee1e34bb8233edeb623db00d86c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 I'll work on this now. (Assignee required) type: bug
Projects
None yet
Development

No branches or pull requests