Summary
A crafted bentofile.yaml in BentoML can inject arbitrary shell commands into the generated Dockerfile via newline-injected values in envs[*].name.
When a victim runs bentoml containerize on a malicious bento, the unquoted RUN directives in the Docker build context may execute attacker-controlled commands on the host during image build.
Affected context
This vulnerability arises in workflows where:
- Developers or CI systems import or consume bentos that include a bentofile.yaml, and
bentoml containerizeis used to generate a Dockerfile and build images, and- The tool interpolates envs[*].name values into Dockerfile RUN directives without robust quoting or sanitization.
In such setups, newline characters and additional shell tokens in environment variable names can escape their intended context and form new Dockerfile instructions.
Exploit preconditions
- Attack surface: Supply-chain and project import paths (malicious or untrusted bentofile.yaml).
- Required access: Ability to introduce or modify a bentofile.yaml that will later be processed by
bentoml containerize(e.g., via shared repositories, templates, or downloaded examples). - Trigger point: Running
bentoml containerizeto generate and then build a Docker image from the resulting Dockerfile. - Why exploitation is possible: The code that renders envs[*].name into the Dockerfile fails to safely quote or validate the value, allowing newline injection that splits or appends Dockerfile RUN directives and embeds arbitrary shell commands.
Why this happens
The underlying flaw is a classic command injection pattern in code generation logic.
BentoML takes user-controlled values from bentofile.yaml (specifically envs[*].name) and interpolates them directly into Dockerfile RUN instructions without enforcing valid identifier formats, stripping control characters, or applying strict quoting.
Because Dockerfile RUN lines are executed by a shell during docker build, any injected newline and subsequent text is interpreted as independent shell commands.
This aligns with CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), and is related to CWE-94: Improper Control of Generation of Code ('Code Injection') where untrusted configuration drives generation of executable build scripts.
Potential impact
- Attackers may execute arbitrary shell commands on the Docker build host during image creation.
- Malicious commands could install backdoors, exfiltrate secrets, or tamper with build artifacts.
- Build outputs (container images) may be compromised, embedding malicious binaries or scripts.
- Continuous Integration (CI) environments running automated container builds could be fully compromised.
- Downstream deployments may run tampered images, enabling later-stage attacks in production.
General mitigation guidance
Robust mitigation centers on treating configuration-derived values as untrusted and constraining how they influence generated build instructions.
Defensive patterns include:
- Strictly validating envs[*].name to allow only safe characters (e.g., alphanumerics, underscores, and hyphens) and rejecting or normalizing inputs with newlines or shell metacharacters.
- Applying consistent quoting and escaping when interpolating configuration values into Dockerfiles or shell commands, ensuring they remain within their intended syntactic context.
- Separating data from code by avoiding direct string concatenation of untrusted values into executable directives; where possible, generate structured representations and apply templating with automatic escaping.
- Minimizing privileges of the Docker build environment and isolating build hosts, so that even if a generated RUN command misbehaves, its ability to impact the broader system is reduced.
- Encouraging workflows where bentofile.yaml files are treated as code and subjected to review, signing, or provenance checks before being used in build pipelines.
How teams detect issues like this
Detection is primarily about identifying unsafe code-generation practices and anomalous build behaviors.
Potential strategies include:
- Static analysis rules or linters that scan the codebase for patterns where configuration values are directly concatenated into Dockerfile instructions or shell commands without validation or quoting.
- Code review checklists that flag any templating logic generating Dockerfile RUN lines from user- or configuration-controlled fields, especially if they support arbitrary strings.
- CI policies that inspect generated Dockerfiles for unexpected line breaks, unusual RUN sequences, or obviously malformed environment variable names.
- Monitoring build logs for unexpected commands, outbound connections, or package installations that are not part of the expected build recipe.
- Optional use of automated security tools that model dataflow from configuration files into build scripts and highlight potential injection paths.

