Summary
KubeAI's Ollama engine constructs a shell command for a Kubernetes startup probe using unsanitized model URL components and executes it via bash -c.
If an attacker can create or update Model custom resources, they can inject arbitrary shell commands that run inside model server pods.
Affected context
This vulnerability appears in KubeAI deployments that:
- Use the Ollama-based engine (
engine_ollama.go) for model serving - Rely on the
ollamaStartupProbeScript()helper to build startup probes - Allow users, services, or automation to create or update Model custom resources (CRs)
The issue is exposed whenever untrusted or loosely controlled tenants can influence the ref or modelParam fields that feed into the model URL used by the startup probe.
Exploit preconditions
- Attack surface: Kubernetes API surface for KubeAI Model custom resources
- Required access: Ability to create or modify KubeAI Model CRs in a namespace where the KubeAI operator runs
- Trigger point: Pod startup, when the Kubernetes startup probe invokes the generated shell script via
bash -c - Why exploitation is possible: The
ollamaStartupProbeScript()function interpolates unvalidatedrefandmodelParamvalues directly into a shell command string usingfmt.Sprintf, then executes it through a shell interpreter, allowing crafted input to break out of intended arguments and inject arbitrary commands.
Why this happens
The root cause is a classic OS command injection pattern, mapped to CWE-78: Improper Neutralization of Special Elements used in an OS Command.
Instead of treating user-controlled values (parts of a model URL) as opaque data, the code concatenates them into a shell command line. This string is then executed via bash -c, giving the shell an opportunity to parse metacharacters, separators, and control operators supplied by an attacker.
The bug arises from combining two insecure practices:
- Building command lines using string formatting rather than structured argument lists.
- Passing those command lines through a general-purpose shell that performs expansion and interpretation.
As a result, users with CR creation privileges can turn what should be a benign health-check probe into an arbitrary command execution mechanism inside the model server pod.
Potential impact
- Execution of arbitrary commands inside model server containers
- Reading or modification of data accessible from the pod (model files, tokens, configuration, cached inputs/outputs)
- Lateral movement within the Kubernetes cluster via network access from the compromised pod
- Deployment of malware, cryptominers, or backdoors on model-serving infrastructure
- Abuse of attached credentials (e.g., cloud IAM roles, service accounts) to access external resources
- Denial of service against the model-serving workload by crashing or halting pods
General mitigation guidance
Effective mitigation focuses on eliminating shell-based command construction and reducing trust in Model CR fields:
- Prefer structured command execution APIs that pass arguments as arrays (e.g.,
exec.Commandwith explicit args in Go), avoidingbash -cand similar shells wherever possible. - Treat all data sourced from Model CRs (
ref,modelParam, and similar fields) as untrusted input, regardless of the expected format, and avoid embedding them directly in command strings. - Where shell usage is unavoidable, apply strict input validation and normalization, restricting values to an allowlist of characters or patterns (e.g., specific model name formats or URL shapes) and rejecting anything outside the expected domain.
- Separate configuration data from executable logic: design probes and lifecycle hooks so that they do not interpret user-provided strings as part of the command itself.
- Apply least-privilege to pods spawned by the operator:
- Minimize filesystem, network, and credential access.
- Use non-root users and restricted Linux capabilities to limit the blast radius of a successful injection.
- Implement stronger RBAC boundaries around who can create or modify KubeAI Model CRs, aligning these permissions with trusted roles rather than broad developer groups or tenants.
How teams detect issues like this
Detection should combine static analysis, code review discipline, and runtime controls:
- Static analysis patterns:
- Flag uses of
bash -cand similar shell invocations in operator code. - Detect string interpolation (
fmt.Sprintf, concatenation) used to build command lines that include values derived from CR fields or other external sources. - Code review signals:
- Shell commands that contain placeholders for user-configurable values.
- Health checks, probes, or lifecycle hooks that run scripted logic influenced by CR spec fields.
- Any use of unvalidated URI/URL components in command execution paths.
- Policy enforcement:
- Use admission controllers or policy engines (e.g., Gatekeeper, Kyverno) to restrict risky patterns in CRs, such as disallowing unexpected characters in model identifiers or URLs.
- Integrate automated code scanning tools in CI pipelines to detect and block merges involving shell-based command construction with external input.
- Runtime visibility:
- Log startup probe command executions and anomalies such as unusual arguments or error patterns.
- Monitor for unexpected outbound connections, process trees, or file modifications originating from model server pods that may indicate command injection abuse.

