Skip to content

Disallow output bases under GC-able directories #26138

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
import com.google.devtools.build.lib.vfs.RootedPath;
import com.google.devtools.common.options.OptionsBase;
import com.google.devtools.common.options.OptionsParsingResult;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
Expand Down Expand Up @@ -376,6 +377,50 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
repositoryCache.getRepoContentsCache().setPath(toPath(repoOptions.repoContentsCache, env));
}
Path repoContentsCachePath = repositoryCache.getRepoContentsCache().getPath();
if (repoContentsCachePath != null) {
// Check that the repo contents cache directory, which is managed by a garbage collecting
// idle task, does not contain the output base. Since the specified output base path may be
// a symlink, we resolve it fully. Intermediate symlinks do not have to be checked as the
// garbage collector ignores symlinks. We also resolve the repo contents cache directory,
// where intermediate symlinks also don't matter since deletion only occurs under the fully
// resolved path.
Path resolvedOutputBase = env.getOutputBase();
try {
resolvedOutputBase = resolvedOutputBase.resolveSymbolicLinks();
} catch (FileNotFoundException ignored) {
// Will be created later.
} catch (IOException e) {
throw new AbruptExitException(
detailedExitCode(
"could not resolve output base: %s".formatted(e.getMessage()),
Code.BAD_REPO_CONTENTS_CACHE),
e);
}
Path resolvedRepoContentsCache = repoContentsCachePath;
try {
resolvedRepoContentsCache = resolvedRepoContentsCache.resolveSymbolicLinks();
} catch (FileNotFoundException ignored) {
// Will be created later.
} catch (IOException e) {
throw new AbruptExitException(
detailedExitCode(
"could not resolve repo contents cache path: %s".formatted(e.getMessage()),
Code.BAD_REPO_CONTENTS_CACHE),
e);
}
if (resolvedOutputBase.startsWith(resolvedRepoContentsCache)) {
// This is dangerous as the repo contents cache GC may delete files in the output base.
throw new AbruptExitException(
detailedExitCode(
"""
The output base [%s] is inside the repo contents cache [%s]. This can cause \
spurious failures. Disable the repo contents cache with `--repo_contents_cache=`, \
or specify `--repo_contents_cache=<path that doesn't contain the output base>`.
"""
.formatted(resolvedOutputBase, resolvedRepoContentsCache),
Code.BAD_REPO_CONTENTS_CACHE));
}
}
if (repoContentsCachePath != null
&& env.getWorkspace() != null
&& repoContentsCachePath.startsWith(env.getWorkspace())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -344,6 +345,38 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
boolean enableRemoteDownloader = shouldEnableRemoteDownloader(remoteOptions);

if (enableDiskCache) {
// Check that the disk cache directory, which is managed by a garbage collecting idle task,
// does not contain the output base. Since the specified output base path may be a symlink,
// we resolve it fully. Intermediate symlinks do not have to be checked as the garbage
// collector ignores symlinks. We also resolve the disk cache directory, where intermediate
// symlinks also don't matter since deletion only occurs under the fully resolved path.
Path resolvedOutputBase = env.getOutputBase();
try {
resolvedOutputBase = resolvedOutputBase.resolveSymbolicLinks();
} catch (FileNotFoundException ignored) {
// Will be created later.
} catch (IOException e) {
throw createOptionsExitException(
"Failed to resolve output base: %s".formatted(e.getMessage()),
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
}
Path resolvedDiskCache = env.getWorkingDirectory().getRelative(remoteOptions.diskCache);
try {
resolvedDiskCache = resolvedDiskCache.resolveSymbolicLinks();
} catch (FileNotFoundException ignored) {
// Will be created later.
} catch (IOException e) {
throw createOptionsExitException(
"Failed to resolve disk cache directory: %s".formatted(e.getMessage()),
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
}
if (resolvedOutputBase.startsWith(resolvedDiskCache)) {
// This is dangerous as the disk cache GC may delete files in the output base.
throw createOptionsExitException(
"The output base [%s] cannot be a subdirectory of the --disk_cache directory [%s]"
.formatted(resolvedOutputBase, resolvedDiskCache),
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
}
var gcIdleTask =
DiskCacheGarbageCollectorIdleTask.create(remoteOptions, env.getWorkingDirectory());
if (gcIdleTask != null) {
Expand Down Expand Up @@ -1223,5 +1256,4 @@ static Credentials createCredentials(
Downloader getRemoteDownloader() {
return remoteDownloader;
}

}