diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py index 4a40af24e9..fb4c18e91c 100644 --- a/cron/lifecycle_guard.py +++ b/cron/lifecycle_guard.py @@ -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 diff --git a/tests/hermes_cli/test_gateway_restart_loop.py b/tests/hermes_cli/test_gateway_restart_loop.py index 2ca339c2b1..a005b9c2e8 100644 --- a/tests/hermes_cli/test_gateway_restart_loop.py +++ b/tests/hermes_cli/test_gateway_restart_loop.py @@ -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.