fix(security): cover privilege wrappers and command-string options
Review follow-up on #84203. Both points reproduce; neither was a regression from the first pass, but both are live bypasses of the same guard. **Privilege and namespace wrappers were missing.** The allowlist covered the coreutils-shaped wrappers but not the privilege ones, so each of these ran a lifecycle script straight past the walk: pkexec bash ~/restart.sh runuser -u root -- bash ~/restart.sh setpriv --reuid=0 -- bash ~/restart.sh systemd-run --scope bash ~/restart.sh nsenter --target 1 --mount bash ~/restart.sh unshare -r bash ~/restart.sh Added `pkexec`, `su`, `runuser`, `setpriv`, `systemd-run`, `nsenter` and `unshare`, each with the value-taking options that would otherwise be mistaken for the command (`nsenter -t 1`, `systemd-run -p X=1`, `runuser -u root`, …). **An option can carry a command STRING, not an argv tail.** `env -S` and `su`/`runuser` `-c` take shell source. The peel treated the operand as an opaque value and skipped it, so `env -S 'bash ~/restart.sh'` was never scanned — the string went unread rather than being recursed into. `_STRING_COMMAND_OPTIONS` now names those options and their values are re-scanned as shell source, the same treatment `sh -c` payloads already get. They are read at the ORIGINAL command token, before the transparent-prefix peel, because peeling past `su`/`env` would discard the very option carrying the command. `--opt value` and `--opt=value` are both handled. Scope, stated plainly: this is an enumerated allowlist, not a general solution to "wrapper that execs its tail". A wrapper outside the set, or a value-taking option outside these tables, still resolves to no reference — that fails open, exactly as it did before this PR, and it is a miss rather than a false block. The reviewer offered "extend the set with tests, or document that the list is heuristic"; this does the first and states the second. Tests: 23 new cases (220 in the file) — every added wrapper against a script reference including the value-operand option forms, both command-string option spellings for env/su/runuser, and the same wrappers around ordinary work (`pkexec systemctl status nginx`, `su -c 'ls -la'`, `env -S 'echo hi'`, `nsenter -t 1 -m ps aux`) which must stay allowed. 15 fail on the tree before this commit. False positives re-checked at scale: the 9,258 command lines from this repo's own scripts and docs give an identical verdict set before and after — 0 new false positives, 0 lost detections, 0 exceptions. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011cSddnhxiUmdGbgyKnpg8p
This commit is contained in:
@@ -499,6 +499,9 @@ def _executable_name(token: str) -> str:
|
||||
_TRANSPARENT_COMMAND_PREFIXES = frozenset({
|
||||
"sudo", "doas", "env", "nohup", "setsid", "nice", "ionice", "stdbuf",
|
||||
"timeout", "exec", "command", "builtin", "eatmydata",
|
||||
# Privilege and namespace wrappers. Same shape — options, then the
|
||||
# command they hand execution to.
|
||||
"pkexec", "su", "runuser", "setpriv", "systemd-run", "nsenter", "unshare",
|
||||
})
|
||||
|
||||
# Options of those wrappers that consume the NEXT token as their value, so a
|
||||
@@ -512,6 +515,30 @@ _TRANSPARENT_PREFIX_VALUE_OPTIONS = {
|
||||
"ionice": {"-c", "-n", "-p", "--class", "--classdata"},
|
||||
"stdbuf": {"-i", "-o", "-e", "--input", "--output", "--error"},
|
||||
"timeout": {"-s", "-k", "--signal", "--kill-after"},
|
||||
"pkexec": {"--user"},
|
||||
"su": {"-s", "--shell", "-g", "--group", "-G", "--supp-group"},
|
||||
"runuser": {"-u", "--user", "-s", "--shell", "-g", "--group",
|
||||
"-G", "--supp-group"},
|
||||
"setpriv": {"--reuid", "--regid", "--groups", "--inh-caps",
|
||||
"--ambient-caps", "--bounding-set", "--selinux-label",
|
||||
"--apparmor-profile"},
|
||||
"systemd-run": {"-u", "--unit", "-p", "--property", "-E", "--setenv",
|
||||
"--slice", "--description", "--uid", "--gid",
|
||||
"--on-calendar", "--service-type"},
|
||||
"nsenter": {"-t", "--target", "-S", "--setuid", "-G", "--setgid",
|
||||
"-r", "--root", "-w", "--wd"},
|
||||
"unshare": {"--map-user", "--map-group", "--setgroups", "-R", "--root",
|
||||
"-w", "--wd"},
|
||||
}
|
||||
|
||||
# Wrappers whose option carries a COMMAND STRING rather than an argv tail.
|
||||
# The string is shell source and must be re-scanned like `sh -c` — skipping
|
||||
# it as an opaque option value would hide whatever it runs
|
||||
# (`env -S 'bash ~/restart.sh'`).
|
||||
_STRING_COMMAND_OPTIONS = {
|
||||
"env": ("-S", "--split-string"),
|
||||
"su": ("-c", "--command"),
|
||||
"runuser": ("-c", "--command"),
|
||||
}
|
||||
|
||||
# Wrappers whose first non-option operand is a VALUE, not the command
|
||||
@@ -725,6 +752,19 @@ def _resolve_terminal_script_path(candidate: str, cwd: Optional[str]) -> Optiona
|
||||
return path
|
||||
|
||||
|
||||
def _iter_option_values(
|
||||
segment: list[str], start: int, option: str
|
||||
) -> Iterator[str]:
|
||||
"""Yield values given to *option*, in both ``--opt v`` and ``--opt=v`` form."""
|
||||
prefix = option + "="
|
||||
for position in range(start + 1, len(segment)):
|
||||
token = segment[position]
|
||||
if token == option and position + 1 < len(segment):
|
||||
yield segment[position + 1]
|
||||
elif token.startswith(prefix):
|
||||
yield token[len(prefix):]
|
||||
|
||||
|
||||
def _references_at(
|
||||
segment: list[str], index: int, cwd: Optional[str]
|
||||
) -> Iterator[Path]:
|
||||
@@ -808,6 +848,12 @@ def _iter_shell_command_payloads(command: str) -> Iterator[str]:
|
||||
index = _command_token_index(segment)
|
||||
if index is None:
|
||||
continue
|
||||
# Command-string options are read at the ORIGINAL token: peeling past
|
||||
# `su`/`env` would discard the very option carrying the command.
|
||||
for option in _STRING_COMMAND_OPTIONS.get(
|
||||
_executable_name(segment[index]), ()
|
||||
):
|
||||
yield from _iter_option_values(segment, index, option)
|
||||
index = _peel_transparent_prefixes(segment, index)
|
||||
if index >= len(segment):
|
||||
continue
|
||||
|
||||
@@ -1571,6 +1571,48 @@ class TestTransparentWrapperPrefixes:
|
||||
f"{prefix} launchctl submit -l com.example.helper -- /usr/bin/true"
|
||||
) is True
|
||||
|
||||
@pytest.mark.parametrize("prefix", [
|
||||
# Privilege and namespace wrappers, including the option forms whose
|
||||
# operand is a VALUE rather than the command.
|
||||
"pkexec", "pkexec --user root",
|
||||
"runuser -u root --", "setpriv --reuid=0 --",
|
||||
"systemd-run --scope", "systemd-run -p X=1",
|
||||
"nsenter --target 1 --mount", "nsenter -t 1 -m",
|
||||
"unshare -r", "sudo pkexec",
|
||||
])
|
||||
def test_privilege_and_namespace_wrappers_are_scanned(
|
||||
self, tmp_path, helper, prefix
|
||||
):
|
||||
assert self._scan(f"{prefix} bash {helper}", cwd=str(tmp_path)) is True
|
||||
|
||||
@pytest.mark.parametrize("command", [
|
||||
# An option carrying a COMMAND STRING is shell source, not an opaque
|
||||
# value: skipping it would hide whatever it runs.
|
||||
"env -S 'bash {path}'",
|
||||
"env --split-string='bash {path}'",
|
||||
"su -c 'bash {path}'",
|
||||
"su root -c 'bash {path}'",
|
||||
"runuser -u root -c 'bash {path}'",
|
||||
])
|
||||
def test_command_string_options_are_rescanned(self, tmp_path, helper, command):
|
||||
assert self._scan(command.format(path=helper), cwd=str(tmp_path)) is True
|
||||
|
||||
@pytest.mark.parametrize("command", [
|
||||
# The same wrappers around ordinary work must not start blocking.
|
||||
"pkexec systemctl status nginx",
|
||||
"su -c 'ls -la'",
|
||||
"unshare -r whoami",
|
||||
"systemd-run --scope make -j4",
|
||||
"nsenter -t 1 -m ps aux",
|
||||
"setpriv --reuid=0 -- id",
|
||||
"runuser -u nobody -- whoami",
|
||||
"env -S 'echo hi'",
|
||||
])
|
||||
def test_privilege_wrappers_around_benign_work_are_allowed(
|
||||
self, tmp_path, command
|
||||
):
|
||||
assert self._scan(command, cwd=str(tmp_path)) is False
|
||||
|
||||
@pytest.mark.parametrize("command", [
|
||||
# Wrappers around ordinary work must stay allowed — peeling must not
|
||||
# invent a script reference where there is none.
|
||||
|
||||
Reference in New Issue
Block a user