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

Bazel 7: --sandbox_add_mount_pair under /tmp fails #20527

Closed
fmeum opened this issue Dec 13, 2023 · 13 comments · Fixed by NixOS/nixpkgs#285544
Closed

Bazel 7: --sandbox_add_mount_pair under /tmp fails #20527

fmeum opened this issue Dec 13, 2023 · 13 comments · Fixed by NixOS/nixpkgs#285544
Assignees
Labels
team-Local-Exec Issues and PRs for the Execution (Local) team type: bug

Comments

@fmeum
Copy link
Collaborator

fmeum commented Dec 13, 2023

Description of the bug:

With Bazel 7, but neither Bazel 6.4.0 nor --noincompatible_sandbox_hermetic_tmp, builds with --sandbox_add_mount_pair referencing a path under /tmp fail since /tmp has been remounted before the manually specified mount pair is applied.

Which category does this issue belong to?

Local Execution

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

touch WORKSPACE

cat > .bazelrc <<'EOF'
build --sandbox_add_mount_pair=/tmp/some/path:/etc
EOF

cat > BUILD <<'EOF'
genrule(
    name = "gen",
    outs = ["data.txt"],
    cmd = "ls /etc > $@",
)
EOF

Then:

$ mkdir -p /tmp/some/path
$ bazel clean --expunge && bazel shutdown && bazel build //:gen
...
src/main/tools/linux-sandbox-pid1.cc:305: "mount(/tmp/some/path, /etc, nullptr, MS_BIND | MS_REC, nullptr)": No such file or directory
Target //:gen failed to build
...
# bazel clean --expunge && bazel shutdown && bazel build //:gen --noincompatible_sandbox_hermetic_tmp
...
INFO: Build completed successfully, 2 total actions

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

7.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 13, 2023

CC @lberki

A simple workaround would be to just disable the hermetic /tmp in this case, similar to what we do for tmpfs paths. What do you think of this?

diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java
index 3f6e49c72c..1e9b58ae00 100644
@@ -206,17 +207,21 @@ final class LinuxSandboxedSpawnRunner extends AbstractSandboxSpawnRunner {
       return false;
     }
 
-    Optional<PathFragment> tmpfsPathUnderTmp =
-        getSandboxOptions().sandboxTmpfsPath.stream()
+    Optional<PathFragment> mountUnderTmp =
+        Stream.concat(
+                getSandboxOptions().sandboxTmpfsPath.stream(),
+                getSandboxOptions().sandboxAdditionalMounts.stream()
+                    .map(Map.Entry::getKey)
+                    .map(PathFragment::create))
             .filter(path -> path.startsWith(SLASH_TMP))
             .findFirst();
-    if (tmpfsPathUnderTmp.isPresent()) {
+    if (mountUnderTmp.isPresent()) {
       if (warnedAboutNonHermeticTmp.compareAndSet(false, true)) {
         reporter.handle(
             Event.warn(
                 String.format(
-                    "Falling back to non-hermetic '/tmp' in sandbox due to '%s' being a tmpfs path",
-                    tmpfsPathUnderTmp.get())));
+                    "Falling back to non-hermetic '/tmp' in sandbox due to '%s' being a tmpfs path or mount source.",
+                    mountUnderTmp.get())));
       }
 
       return false;

@fmeum fmeum changed the title Bazel 7: --sandbox_add_mount_pair with source under /tmp fails Bazel 7: --sandbox_add_mount_pair under /tmp fails Dec 13, 2023
@fmeum
Copy link
Collaborator Author

fmeum commented Dec 13, 2023

Flipping source and target in my example also results in a failure, not sure why I couldn't reproduce this earlier. I have edited the issue description accordingly.

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 18, 2023

@bazel-io flag

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 18, 2023

@bazel-io fork 7.0.1

@lberki
Copy link
Contributor

lberki commented Dec 19, 2023

I have a fix for this one. It's a one-line fix, modulo the test.

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 19, 2023

@lberki Feel free to reuse the integration tests from #20583.

bazel-io pushed a commit to bazel-io/bazel that referenced this issue Dec 19, 2023
This makes it possible to mount directories under /tmp somewhere else. Before, /tmp was overridden by the implementation of hermetic /tmp.

Fixes bazelbuild#20527.

RELNOTES: None.
PiperOrigin-RevId: 592247867
Change-Id: Ib5b75cd21ffe4fa4c8ee3f75d82894da6dd61f54
@lberki
Copy link
Contributor

lberki commented Dec 19, 2023

Sorry, too late, I already wrote my own :) (very similar to yours, though)

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 19, 2023

@lberki Thanks for pushing the fix, but my mount targets under /tmp are still failing. I trimmed my PR at #20583 down to just the new tests.

@fmeum fmeum reopened this Dec 19, 2023
@lberki
Copy link
Contributor

lberki commented Dec 19, 2023

Mount targets? I seem to remember discussing this with you at some point in time, and IIRC the consensus was that mount targets under /tmp aren't a good idea: the whole point of --incompatible_sandbox_hermetic_tmp is that /tmp is pristine and mount points under it seem to compromise that.

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 19, 2023

I would say that's still entirely true, but with the flag flip, the user is no longer the one who specified both --incompatible_sandbox_hermetic_tmp and the mount pair. I think that it would be entirely reasonable to just automatically disable the hermetic /tmp in that case, but not doing so results in a confusing error.

iancha1992 pushed a commit that referenced this issue Dec 19, 2023
…20609)

This makes it possible to mount directories under /tmp somewhere else.
Before, /tmp was overridden by the implementation of hermetic /tmp.

Fixes #20527.

RELNOTES: None.
Commit
3748084

PiperOrigin-RevId: 592247867
Change-Id: Ib5b75cd21ffe4fa4c8ee3f75d82894da6dd61f54

Co-authored-by: Googler <lberki@google.com>
@lberki
Copy link
Contributor

lberki commented Dec 19, 2023

WDYT about emitting a non-confusing error?

I agree that the current situation isn't fantastic and technically speaking, allowing mount targets under /tmp is not impossible, but I'd rather not allow that, at least on the first attempt.

I also have an ardent desire to delete the old code path so reverting back to it under certain conditions isn't my favorite thing, either.

@tjgq tjgq removed the untriaged label Jan 2, 2024
bazel-io pushed a commit to bazel-io/bazel that referenced this issue Jan 4, 2024
This is achieved by rewriting the user-specified mounts to mounts onto subdirectories of the hermetic sandbox tmp directory.

Fixes bazelbuild#20527

Closes bazelbuild#20583.

PiperOrigin-RevId: 595815029
Change-Id: Ibfe5f67fb8fb59131b6c82a826ed5200f2b10a94
iancha1992 pushed a commit that referenced this issue Jan 5, 2024
…20749)

This is achieved by rewriting the user-specified mounts to mounts onto
subdirectories of the hermetic sandbox tmp directory.

Fixes #20527

Closes #20583.

Commit
5e68afd

PiperOrigin-RevId: 595815029
Change-Id: Ibfe5f67fb8fb59131b6c82a826ed5200f2b10a94

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
@iancha1992
Copy link
Member

@bazel-io fork 7.1.0

bazel-io pushed a commit to bazel-io/bazel that referenced this issue Jan 6, 2024
This is achieved by rewriting the user-specified mounts to mounts onto subdirectories of the hermetic sandbox tmp directory.

Fixes bazelbuild#20527

Closes bazelbuild#20583.

PiperOrigin-RevId: 595815029
Change-Id: Ibfe5f67fb8fb59131b6c82a826ed5200f2b10a94
iancha1992 pushed a commit to iancha1992/bazel that referenced this issue Jan 6, 2024
This is achieved by rewriting the user-specified mounts to mounts onto subdirectories of the hermetic sandbox tmp directory.

Fixes bazelbuild#20527

Closes bazelbuild#20583.

PiperOrigin-RevId: 595815029
Change-Id: Ibfe5f67fb8fb59131b6c82a826ed5200f2b10a94
github-merge-queue bot pushed a commit that referenced this issue Jan 12, 2024
…20772)

This is achieved by rewriting the user-specified mounts to mounts onto
subdirectories of the hermetic sandbox tmp directory.

Fixes #20527

Closes #20583.

Commit
5e68afd

PiperOrigin-RevId: 595815029
Change-Id: Ibfe5f67fb8fb59131b6c82a826ed5200f2b10a94

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
Co-authored-by: lberki <lberki@users.noreply.github.com>
@iancha1992
Copy link
Member

A fix for this issue has been included in Bazel 7.0.1 RC2. Please test out the release candidate and report any issues as soon as possible. Thanks!

Strum355 added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this issue Jan 31, 2024
Non-hermetic /tmp disabled until we're on bazel 7.0.1
bazelbuild/bazel#20527

## Test plan

`bazel build //:gazelle-buf`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Local-Exec Issues and PRs for the Execution (Local) team type: bug
Projects
None yet
6 participants