Summary
Inspektor Gadget's ig image build command improperly interpolates user-controlled values into a Makefile template without adequate escaping, enabling command injection during image builds.
When an attacker can influence the buildOptions fields sourced from a gadget manifest, they may execute arbitrary commands on the host running ig (with --local) or inside the build container (without --local).
Affected context
This vulnerability occurs in Inspektor Gadget's image-building functionality, specifically when using the ig image build subcommand that relies on the Makefile.build template.
It is relevant in environments where:
- The
igCLI is used to build custom gadget OCI images. - The
buildOptionsstructure is populated from YAML gadget manifests (e.g.,build.yml). - Gadgets or their manifests may originate from untrusted or semi-trusted sources, such as CI/CD pipelines that build user-submitted or third-party gadgets.
The issue is most impactful in automated build systems or shared development environments where untrusted manifests are processed without manual review.
Exploit preconditions
- Attack surface: The
ig image buildcommand and its Makefile-based build process. - Required access:
- Ability to supply or modify the gadget manifest used for building (e.g., control over
build.ymlor relevant options within it). - Practical scenarios include CI/CD jobs that automatically build gadgets based on manifests from repositories, pull requests, or external submissions.
- Trigger point:
- Invocation of
ig image build, which: - Generates a Makefile from
Makefile.buildwith embeddedbuildOptionsvalues. - Executes Makefile targets that invoke shell commands containing these unescaped values.
- Why exploitation is possible:
- User-controlled data from
buildOptionsis inserted into shell command lines within the Makefile without proper escaping or sanitization. - Make invokes
/bin/shfor command execution, and shell metacharacters in the unescaped data can break out of the intended argument context and inject additional commands.
Why this happens
The root cause is unsafe construction of shell-executed command lines from user-controlled input, leading to CWE-78: Improper Neutralization of Special Elements used in an OS Command (OS Command Injection).
Within inspektor-gadget/cmd/common/image/build.go, values from the buildOptions structure are embedded into the Makefile.build template. These values are ultimately passed to the shell via Makefile rules without:
- Proper quoting or escaping suited for the target shell, or
- A separation between data and command logic (e.g., using structured APIs instead of raw command strings).
Conceptually, this reflects a common bug pattern:
- Treating configuration values as "safe" and interpolating them into executable command strings.
- Relying on Makefile and shell behavior where special characters (
;,&,|, backticks,$(), etc.) are interpreted as control or substitution operators.
The combination of template-based command generation and inadequate neutralization of shell metacharacters allows attackers to craft buildOptions values that alter the intended command sequence and execute arbitrary OS commands during the build process.
Potential impact
- Execution of arbitrary OS commands on the Linux host where
igis run when using the--localflag. - Execution of arbitrary OS commands within the build container environment when
--localis not used. - Compromise of CI/CD runners or build agents, potentially leading to broader infrastructure access.
- Insertion of backdoors, malicious tooling, or data exfiltration logic into built gadget images.
- Lateral movement opportunities if the compromised host or container has access to Kubernetes clusters, registries, or other internal systems.
General mitigation guidance
Mitigation should focus on eliminating direct command-string construction from untrusted data and ensuring shell interaction is strictly controlled.
Recommended defensive patterns include:
- Avoid shell-based templating when possible:
- Prefer programmatic build APIs or libraries that accept arguments as structured lists instead of concatenated command strings.
- Apply strict escaping and quoting:
- When shell execution is unavoidable, apply context-aware escaping to all user-controlled values before inserting them into Makefiles or shell commands.
- Use robust quoting strategies (e.g., single-quoting data segments and carefully handling embedded quotes) appropriate for
/bin/sh. - Constrain the configuration surface:
- Restrict which
buildOptionsfields can influence commands; prefer enumerated options or whitelists over free-form strings. - Validate manifest fields against schemas that limit the presence of shell metacharacters where they are not necessary.
- Isolate and harden the build environment:
- Run
ig image buildin locked-down, ephemeral build environments with minimal privileges and restricted network/file-system access. - Apply least-privilege principles to the build user and any helper tools.
- Treat manifests as untrusted input:
- Even internally generated manifests should undergo validation and sanitization before being used in command construction.
How teams detect issues like this
Detection can combine code-level review with runtime monitoring:
- Static analysis and code review:
- Search for patterns where user-controlled fields (e.g., from
buildOptions, YAML manifests, or CLI flags) are directly concatenated into Makefile templates or shell commands. - Flag uses of
fmt.Sprintf, string templates, or text replacement functions that populate Makefile rules with unescaped values. - Establish review checklists for:
- Any new Makefile templates that embed dynamic values.
- Any code that executes
make,sh, or other command interpreters with arguments built from configuration data. - Policy-based checks in CI:
- Enforce linting rules that disallow unsafe shell command construction and encourage structured argument passing.
- Use configuration-as-code scanning tools to detect suspicious shell expansions within templates and build scripts.
- Runtime monitoring:
- Log and review commands executed by build pipelines, especially those originating from
ig image build. - Monitor for unexpected child processes, network connections, or filesystem modifications during gadget image builds, which may indicate exploitation attempts.

