Details
Captured output (glances 4.5.5, Python 3.13, x86_64 Linux):
NEGATIVE CONTROL (escaped {{name}}{{cmdline}}): blocked (expected)
POSITIVE (unescaped {{{name}}}{{{cmdline}}}): INJECTED - 'touch' executed
The negative control confirms that the same attacker values under a standard
double-brace template are blocked (chevron escapes &). The positive case shows
the injected touch executing when the template uses two adjacent unescaped
variables: the file /tmp/glances_crossfield_pwned is created by the injected
command, not by the intended logger action.
Impact
- Arbitrary command execution as the OS user running Glances (frequently root on monitored hosts) whenever an operator uses an unescaped, adjacent-variable action template and an attacker controls two neighbouring stat fields.
- The same reconstruction reaches
secure_popen()'s file-redirection (>) and pipe (|) handling, allowing arbitrary file write and output piping in addition to command chaining. - The bypass defeats the dedicated
_sanitize_mustache_dict()control that was added specifically to stop attacker-controlled stat values from injecting shell operators.
Suggested fix
Enforce the operator ban on the rendered command string that comes from
template-variable expansion, rather than on the pre-render values in isolation.
One approach that mirrors the existing helper: render each variable, then reject /
neutralise operators in the concatenated result, or strip lone &/redirection
characters that originate from variable data.
def _sanitize_mustache_dict(mustache_dict):
if not mustache_dict:
return mustache_dict
safe = {}
for k, v in mustache_dict.items():
if isinstance(v, str):
# Neutralise every shell-significant character that secure_popen
# can interpret, including a lone '&' that could pair with an
# adjacent variable to reconstruct '&&'.
for ch in ('&', '|', '>', '<'):
v = v.replace(ch, ' ')
safe[k] = v
else:
safe[k] = v
return safe
Neutralising the single & (and the single > / |) in each value removes the
cross-field reconstruction because no operator character survives on either side
of a variable boundary. Alternatively, sanitize cmd_full afterchevron.render(), or pass the template-derived data as `secure_popen(...,
allow_operators=False)` when the command originates from stat-field substitution.
Credit
Reported by tonghuaroot.