sudo SETENV (the environment env_reset does not clear)
Full command arsenal in the cheatsheetsudo ships with two defences against environment attacks. Defaults env_reset empties the environment before executing and lets through only a short allowlist, and secure_path imposes a fixed search path of system directories. With both in place, a PATH hijack should not work.
The SETENV tag on a sudoers rule disarms them. It lets the user pass environment variables on the sudo command line itself, and those variables reach the process that runs as root:
sudo -lMatching Defaults entries for user on host: env_reset, mail_badpass, secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin User user may run the following commands on host: (root) SETENV: NOPASSWD: /opt/cleanup.sh
That output is the whole lead: there is an env_reset and a secure_path, and immediately after, a rule that makes an exception to both. The same thing appears as Defaults env_keep += "VAR", which preserves one specific variable, and in its most direct form as a !env_reset in the Defaults.
What you pass depends on what the target reads#
The tag only opens the door; the privileged program supplies the vector.
PATH when the program calls a binary without an absolute path. This is photobomb, and it is the case that disproves the belief that secure_path protects you: there secure_path is defined and the attack works anyway, because what secure_path governs is the path sudo uses to resolve the command it is asked for, not the PATH the child inherits once SETENV lets us set it. The allowed script called find with no path:
cd /tmp
echo 'chmod u+s /bin/bash' > find
chmod +x find
sudo PATH=/tmp:$PATH /opt/cleanup.sh
bash -pPYTHONPATH when the program is a Python script. This variable is prepended to sys.path, so a module of ours is imported ahead of the standard library's (Python library hijack).
LD_PRELOAD and LD_LIBRARY_PATH when the program is a dynamically linked binary. You compile your own .so and the loader injects it into any allowed binary (shared library injection).
Any variable the script happens to consult. It does not have to be a system variable: if the privileged code does something with $MODE or $DEST, SETENV lets us write it. In linkvortex the rule preserved CHECK_CONTENT, which the script used to decide whether to dump the file's contents, and that turned a check into an arbitrary read as root.
Requirements#
A sudo rule carrying SETENV (or an env_keep with the variable you want, or a !env_reset), and something in the target that reads the environment: a call without an absolute path, an interpreter that resolves modules through a variable, a dynamically linked binary, or a variable the script itself consults. Without that second half the tag gives you nothing.
How to detect it. The signature is in the command line, and it is unmistakable: sudo logs the full COMMAND= to /var/log/auth.log (or journalctl _COMM=sudo), assignments included, so a sudo PATH=/tmp:... /opt/cleanup.sh or a sudo PYTHONPATH=/tmp ... reads literally. Passing variables through sudo is anomalous by itself in normal operation. At rest, the audit is a grep: grep -nE 'SETENV|env_keep|!env_reset' /etc/sudoers /etc/sudoers.d/* enumerates every rule that makes an exception to the sanitising. And the end of the chain usually follows, a chmod +s /bin/bash or an sh child of a process that does not spawn one.
Defence. Do not grant SETENV without a demonstrated need, and never over a script that calls other programs. In privileged code, always invoke by absolute path and sanitise the environment on startup instead of trusting that sudo did it; for interpreters, use the options that ignore the environment (python -I, perl -T). secure_path is still a good idea, but it is worth knowing what it protects: it does not cover the child's PATH if a rule allows setting it, nor module or library paths, which live in other variables.
Machines where it's used
- AdmirerHackTheBox
- LinkVortexHackTheBox
- PhotobombHackTheBox