Summary
OpenClaw versions 2026.2.19-2 prior to 2026.2.21 contain a command injection flaw in the logic that generates systemd unit files from environment configuration.
Because attacker-controlled environment values are not validated for CR/LF characters, an attacker can inject additional systemd directives and ultimately execute arbitrary commands with the privileges of the OpenClaw gateway service user when the service is installed or restarted.
Affected context
The vulnerability occurs in deployments where OpenClaw generates systemd unit files dynamically from configuration sources such as config.env.vars or similar environment-variable files.
The risk is present when:
- Environment values are influenced by less-trusted users, automation, or external inputs
- The systemd unit file is regenerated and installed based on these values
- Service install, reload, or restart operations are performed (for example during deployment or configuration changes)
In such setups, newline characters embedded in environment values can break the expected Environment= line structure in the generated unit and inject arbitrary systemd configuration.
Exploit preconditions
- Attack surface: Local or semi-local configuration interface where
config.env.varsor equivalent environment values can be modified (e.g., admin panel, config file, deployment pipeline, or scriptable API). - Required access: Ability to influence or write one or more environment variable values that OpenClaw uses when generating systemd unit files; and the ability to trigger or wait for a service install/restart/reload.
- Trigger point: Execution of the systemd unit generation/install logic, followed by systemd starting or reloading the affected OpenClaw gateway service.
- Why exploitation is possible: The generator writes unvalidated, attacker-controlled strings directly into
Environment=directives, without sanitizing or encoding CR/LF characters. Newlines inside a value terminate the current directive and start new lines that systemd interprets as independent configuration directives, including ones that can reference or execute external commands.
Why this happens
This issue stems from improper neutralization of special elements used in command contexts, aligning with CWE-77: Improper Neutralization of Special Elements used in a Command.
The code assumes that environment variable values can be safely embedded into Environment= lines in a systemd unit file without considering that CR (\r) and LF (\n) characters have structural meaning in this configuration language.
By not stripping, rejecting, or encoding newline characters, the implementation effectively allows untrusted input to escape its intended value boundary and become executable configuration, which systemd can use to run commands or change service behavior.
Potential impact
- Execution of arbitrary commands with the privileges of the OpenClaw gateway service user
- Modification of service behavior, including changing ExecStart/ExecStartPre/ExecStartPost directives
- Installation of backdoors through persistent malicious unit directives
- Lateral movement on the host, depending on the permissions of the service user and local configuration
- Disruption of service availability by injecting directives that cause startup failure or repeated crashes
General mitigation guidance
Effective mitigation focuses on treating environment-derived values as untrusted and preventing them from altering the structure of configuration files:
- Normalize and validate all environment values before embedding them in unit files, rejecting or stripping CR/LF characters and other structural delimiters.
- Use robust encoding or quoting mechanisms when interpolating values into configuration templates, ensuring that injected newlines cannot break out of the intended directive.
- Restrict who can modify configuration inputs like
config.env.vars, applying least-privilege principles to configuration management paths and deployment pipelines. - Prefer fixed, declarative unit files where possible and minimize dynamic unit generation, or constrain the set of allowed directives when generation is required.
- Run the OpenClaw gateway service under a minimally privileged account and avoid unnecessary local permissions, reducing the blast radius of any successful command execution.
How teams detect issues like this
Detection typically involves both code-focused and configuration-focused approaches:
- Static analysis and code review: Look for code paths that write user-controlled strings directly into systemd unit files or other configuration files without filtering CR/LF or performing strict validation. Pay attention to string concatenation or templating around
Environment=,ExecStart=, and similar directives. - Template and policy checks: Implement validation rules or schema checks for generated unit files prior to installation, flagging unexpected extra directives or lines derived from configurable inputs.
- Configuration hygiene monitoring: Periodically scan installed systemd unit files for anomalies such as unexpected directives, unusual Exec* commands, or suspicious environment values.
- Automated security tooling: Use configuration linters, SAST tools, or CI/CD checks that can detect newline injection patterns and unsafe interpolation of untrusted data into configuration or command contexts.

