Bazel's Threat Model

TL;DR

Bazel does not provide any security boundaries against malicious workspaces. If you want to guard against Bazel executing malicious code, either sandbox it or control the source code it has access to.

Scope

All of the non-test code under this source tree that is packaged into or compiled into Bazel itself, namely: the client and server implementations, Starlark engine, external services interfaces (e.g. clients for RBE and remote caching), process and sandbox wrappers, and existing security mitigations for flags and select tools.

Out of scope exceptions:

  • Files and behavior specific to Blaze, which is the Google-internal version of Bazel.
  • Rule definitions in other repositories.
  • Tools run specifically as build actions (e.g. singlejar, ijar) or manually-run debugging utilities.
  • Related external service implementations such as RBE and GCP.

What Bazel Is

Bazel is an open-source build system used to efficiently build and test software. Its user interface is the bazel CLI tool. Builds are configured through BUILD files with compilation/execution rules defined in “Starlark” .bzl files. Bazel exposes many flags to configure build/runtime behavior that can be specified on the command line or in .bazelrc files.

Assets

Bazel offers some protections for select assets.

  • Credentials: Bazel is designed to send credentials only to the intended service and endpoint. It does not guarantee protection against credential extraction via malicious code in the workspace.
  • In-Bazel code execution: Bazel is designed to only evaluate Starlark code within the Bazel process. All build and test actions are executed in separate processes.
  • Download integrity: Bazel is designed to only accept downloaded dependencies that match the expected hash, if specified.

Actors

Untrusted actors

Bazel generally runs in a “trust-first” mode. There are only minimal checksum verifications on metadata and blobs downloaded from remote services (e.g. RBE and remote dep hosts).

Trusted actors

  • Invoking user: the user running the bazel command. This user owns the workspace, including intermediate object files and build artifacts.
  • The workspace: the source code and build configuration (bazelrc, BUILD and bzl files, etc.) that the user runs Bazel on.
  • Unpinned remote deps: Specifying a remote dependency without a hash sum signals implicit trust of the remote endpoint and the downloaded contents. By omitting the hash, the user is implicitly trusting the remote source to provide a non-malicious build artifact.
  • Remote execution/caching services: services that execute build actions or store build artifacts. Bazel has no way to verify the correctness or integrity of the work performed by these services beyond a shallow checksum verification for blobs, when one exists.
  • Bzlmod registry: The registry supplies the contents and checksums of remote dependencies when bzlmod is enabled, and is specified in the user's workspace.

Security Boundaries

All boundaries described in this section hold true provided that the host machine is not compromised. See “Attacker-controlled workspaces” below for more details.

Cross-user

Bazel‘s server socket permissions are set to 700 to prevent cross-user build contamination. Users should not be able to access or control another user’s Bazel server.

Bazel server credentials vs build actions

Bazel may use authentication credentials to communicate with remote services (e.g. fetching external dependencies, accessing remote caches, or streaming BEP events). Those credentials and tokens should not be exposed to build actions (particularly remote actions) or other services outside of the intended service, or recorded in test logs, Java server logs, or the BEP.

Source verification, supply chain integrity

Bazel must always reject downloaded content whose hash does not match the expected hash assuming the host system is not compromised. Because this threat model presumes that the workspace visible to Bazel is already trusted, the remote dependency locations (i.e. websites hosting archives), their contents, and hashes are all therefore assumed to be trusted.

Non-issues

There are several categories of alleged vulnerabilities often reported to Bazel maintainers that are not considered security issues.

Arbitrary Code Execution

In general, achieving arbitrary code execution is expected behavior, to a certain extent (and indeed the whole point of a build tool). It is working-as-intended if a build initiated from Bazel executes arbitrary code either on the host machine or in a remote executor.

This is constrained in several ways:

  • Arbitrary code execution happens only in processes that Bazel spawns: build and test actions, persistent workers, etc. The Bazel server and client should not execute arbitrary code.
  • Those processes execute as the Bazel-invoking user, in the same security domain.
  • Starlark code is evaluated inside of the Bazel process, but only talks to the outside world through designated APIs. Running arbitrary code inside of the Bazel process without using those APIs is a vulnerability.

Admin access

Attacks that are predicated on an attacker already having admin access to the host machine are out of scope. Bazel does not need administrator permissions to perform its work, and so should not be considered a vector for privilege escalation.

“Sandbox” Breakout and Repository Rule Path Traversal Attacks

Bazel's sandbox is described here. The relevant text is copied below:

Bazel’s file system sandbox runs processes in a working directory that only contains known inputs, such that compilers and other tools don‘t see source files they should not access, unless they know the absolute paths to them. Sandboxing doesn’t hide the host environment in any way. Processes can freely access all files on the file system. However, on platforms that support user namespaces, processes can’t modify any files outside their working directory. This ensures that the build graph doesn’t have hidden dependencies that could affect the reproducibility of the build.

The local sandboxes Bazel offers guard against bugs, not malice. Their goal is to ensure that the build has declared its dependencies properly and actions don't access files not in their dependencies. By design, there are no guarantees that the sandbox has any kind of isolation across file system, network, user, or process boundaries. Therefore, the sandbox should not be considered a security boundary, or a truly hermetic environment in the usual sense.

A corollary to the minimal sandbox abstraction is that path traversal exploits are unfortunate, yet unavoidable. Build actions as well as repository rules require wide access to the file system, and cannot be meaningfully restricted.

Attacker-controlled workspaces

Bazel trusts the source code the user runs it on and does not offer a security boundary of any kind around code it executes from it. There is limited validation of boundedness in some places in the product, e.g. extracting remote archives.

The implication for attacker-controlled workspaces is that if an attacker already has control over a user‘s Bazel client and local environment, Bazel generally cannot worsen the attacker’s position.

Subtool exploits outside of the Bazel execution model

Bazel calls upon many subtools, which exist under its source tree, as part of compilation workflows. These are tools such as singlejar, ijar, and buildjar. Exploits that abuse vulnerabilities in these tools, or in the underlying Java runtime, are considered to be outside of Bazel's security scope.

Some subtools are part of Bazel's execution model, such as the process-wrapper and linux-sandbox. Vulnerabilities in these tools should be considered in-scope.

Real Issues

Note that this is not an exhaustive list, but should cover the most highly anticipated attack vectors.

Bazel Client/Service Communication Infiltration

Bazel's client and server communicate over a local network socket. This channel can theoretically be abused to inject malicious commands or code and should remain intact.

Path Traversal

Path traversal attacks cannot be meaningfully defended against in Bazel. As discussed earlier, Bazel's sandbox is only a weak layer meant to enforce declared dependency boundaries, not system resource access.

There are a few notable exceptions to this rule where Bazel does have some built-in protections. These subsystems and APIs are mostly restricted to path validation for interacting with and extracting external repositories and dependencies. Path validation in this context is limited to disallowing out-of-bounds symlinks, upwards-relative paths (i.e. ..), and absolute paths. Bypassing these existing validations is a vulnerability.

  • http_archive (used by Bazel in repo rules and bzlmod)
  • Bazel's built-in patch tools
  • Bzlmod module extension APIs (rctx.download, rctx.copy, rctx.patch, etc)
    • Note that these APIs exist as a first-order protection against untrusted inputs only. It is trivially easy to abuse rctx.execute() and impossible for Bazel to defend against.
  • Materializing remote execution/caching outputs, including symlinks and directory trees.

Note that in most cases, the above exceptions are intended to prevent unintentional path traversal. Bazel cannot protect against generalized path traversal exploits.

Command and Code Injection via Unescaped Inputs

Bazel accepts many inputs via parsed command-line flags and files. These inputs should be escaped as a first-order protection against arbitrary code injection in the Bazel process.

Supply Chain Attacks

As a build tool, Bazel is a potential vector for supply chain attacks, both on the receiving and giving end.

Bazel projects have many remote dependencies: external rule repositories, binaries (e.g. Java toolchains). An attacker could aim to intercept incoming downloads of these remote assets to inject malicious code.

In addition to remote dependencies, Bazel also can read cached intermediate artifacts from a remote cache. This cache, or the network connection to it, could be compromised to inject malicious code.

Bazel is also used to produce binaries, scripts, and other artifacts that are consumed by downstream users or systems. These can include shell scripts, container images, and more. An attacker could aim to compromise Bazel to inject malicious content into these outputs, which could then be used in a downstream system.

Credential Leak and Abuse

Bazel can interact with user credentials in several ways, with each access type presenting a potential attack vector.

Token Value Leak and Credential Scoping

Bazel supports many credential specification methods, e.g. flags, environment variables, .netrc, credential helpers, and cloud default credentials.:

  • Many token values, credentials, and secrets are already scrubbed by Bazel's BEP, server log, and console output (see the SafeRequestLogging class). The subsystems responsible for scrubbing these values should not be circumvented, and doing so is a vulnerability.
  • Credentials must only be passed to their intended remote service and origin. Forwarding credentials configured for one endpoint to an unrelated service (e.g. passing remote cache headers to a remote downloader or BES backend) or leaking authentication headers across cross-origin or protocol-downgraded HTTP redirects is a vulnerability.

Bazel Vulnerability Decision Criteria

  1. Scope: Does the exploit fall in the direct scope of Bazel, i.e. the issue does not pertain to rulesets or a related system (GCP, RBE, etc)?
  2. User intent: Is the exploit possible without known-bad user-driven actions, such as omitting checksums for remote archives?
  3. Boundary violations: Does the exploit violate a known security boundary (cross-user, privilege, etc)?
  4. Breaking existing guarantees: Does the exploit break an existing guarantee or safeguard?

If the answer to 1 + 2 and either 3 or 4 is “yes”, then the issue should be considered a vulnerability. All other cases should be considered “working as intended” from a security perspective, but may be considered bugs.