-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Hermetic sandbox with support for hardlinks
Adds linux-sandbox flag: --experimental_use_hermetic_linux_sandbox - Configure linux-sandbox to run in a chroot environment to prevent access to files not mentioned in the bazel rules unless they can be found via explicitly whitelisted directories using --sandbox_add_mount_pair create hardlinks instead of symlinks, and fallback to copying. In case of writes to input files, the build will be aborted.
- Loading branch information
1 parent
238367a
commit ba775b2
Showing
14 changed files
with
683 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
100 changes: 100 additions & 0 deletions
100
src/main/java/com/google/devtools/build/lib/sandbox/HardlinkedSandboxedSpawn.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2016 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.sandbox; | ||
|
||
import com.google.common.flogger.GoogleLogger; | ||
import com.google.devtools.build.lib.exec.TreeDeleter; | ||
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs; | ||
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs; | ||
import com.google.devtools.build.lib.vfs.FileSystemUtils; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.devtools.build.lib.vfs.Symlinks; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Creates an execRoot for a Spawn that contains input files as hardlinks to their original | ||
* destination. | ||
*/ | ||
public class HardlinkedSandboxedSpawn extends AbstractContainerizingSandboxedSpawn { | ||
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); | ||
private boolean sandboxDebug = false; | ||
public HardlinkedSandboxedSpawn( | ||
Path sandboxPath, | ||
Path sandboxExecRoot, | ||
List<String> arguments, | ||
Map<String, String> environment, | ||
SandboxInputs inputs, | ||
SandboxOutputs outputs, | ||
Set<Path> writableDirs, | ||
TreeDeleter treeDeleter, | ||
@Nullable Path statisticsPath, | ||
boolean sandboxDebug) { | ||
super( | ||
sandboxPath, | ||
sandboxExecRoot, | ||
arguments, | ||
environment, | ||
inputs, | ||
outputs, | ||
writableDirs, | ||
treeDeleter, | ||
statisticsPath); | ||
this.sandboxDebug = sandboxDebug; | ||
} | ||
|
||
@Override | ||
protected void copyFile(Path source, Path target) throws IOException { | ||
hardLinkRecursive(source, target); | ||
} | ||
|
||
/** | ||
* Recursively creates hardlinks for all files in @param source path, in @param target path. | ||
* Symlinks are resolved. If files is located on another disk, hardlink will fail | ||
* and a copy will be made instead. | ||
* Throws IllegalArgumentException if source path is a subdirectory of target path. | ||
*/ | ||
private void hardLinkRecursive(Path source, Path target) throws IOException { | ||
if (source.isSymbolicLink()) { | ||
source = source.resolveSymbolicLinks(); | ||
} | ||
|
||
if (source.isFile(Symlinks.NOFOLLOW)) { | ||
try { | ||
source.createHardLink(target); | ||
} catch (IOException e) { | ||
if (sandboxDebug) { | ||
logger.atInfo().log("File %s could not be hardlinked, file will be copied instead.", source); | ||
} | ||
FileSystemUtils.copyFile(source, target); | ||
} | ||
} else if (source.isDirectory()) { | ||
if (source.startsWith(target)) { | ||
throw new IllegalArgumentException(source + " is a subdirectory of " + target); | ||
} | ||
target.createDirectory(); | ||
Collection<Path> entries = source.getDirectoryEntries(); | ||
for (Path entry : entries) { | ||
Path toPath = target.getChild(entry.getBaseName()); | ||
hardLinkRecursive(entry, toPath); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.