Files
hermes-agent/tools/terminal_tool_sudo_askpass.py
unsupportedpastels 2d88ddcbf2 fix(terminal): run each sudo check in a subshell
The prologue now runs a `sudo` shell function or alias for the `-A` check,
in the command's own shell. A wrapper that calls `exec` or `exit` then
replaced or ended that shell before any of the user's command ran. Each
check now runs in a subshell, which still sees the session's functions
and aliases, so an `exec` or `exit` in a wrapper ends only the check.

Tests: on all four SDK backends, a wrapper that exits on `-A -V` and a
wrapper that `exec`s sudo both let the command run up to its own sudo
(a marker file written before it exists); both fail on the previous head
and pass on main. The two tests that pin the check text follow the change.
2026-11-13 13:24:29 -05:00

373 lines
19 KiB
Python

"""Askpass delivery of the sudo password for the terminal tool.
Where a backend's sudo supports an askpass helper, the password is handed to each counted
sudo invocation through a helper script instead of the stdin shared by the whole command.
``build_prologue`` emits the shell text that runs before the rewritten command: it reads the
single password line the caller prepends to stdin, keeps it in a detached writer that serves
it through a FIFO once per counted sudo, and tears everything down when the command's shell
(and every child holding fd 19) exits, or a timeout after that shell is gone. The per-environment
``CAPABILITY_PROBE`` decides whether a backend can use it, or the backend sets
``_sudo_askpass_runtime_checked`` and relies on the prologue's own setup checks alone;
``has_own_stdin_or_askpass`` keeps commands that already manage sudo's input on the stdin form.
"""
import logging
import platform
import re
import shlex
import subprocess
logger = logging.getLogger("tools.terminal_tool")
# Placeholder for the watchdog's ``read -t`` seconds. ``_prepare_command`` cannot know the
# command's timeout, so ``BaseEnvironment.execute`` fills it via ``fill_timeout_token``.
SUDO_ASKPASS_TIMEOUT_TOKEN = "__HERMES_SUDO_ASKPASS_T__"
# Extra seconds the watchdog waits past the command's own timeout.
_TIMEOUT_SLACK_S = 30
# Used when the effective timeout is not a number (the execute backstop uses 120 s then).
_FALLBACK_TIMEOUT_S = 120
SUDO_ASKPASS_PREFIX = 'SUDO_ASKPASS="$__hermes_sudo_dir/a" '
# Expands to ``-A`` once the helper is set up, and to ``-S -p ''`` when setup failed at run time
# (the command then reads its password lines from stdin exactly as the stdin form does).
SUDO_ASKPASS_SUFFIX = ' "${__hermes_sudo_o[@]}"'
# bash 3.2 syntax only. ``\command`` / ``\builtin`` keep aliases and functions from the session
# snapshot from shadowing the commands the prologue relies on; ``exec`` is written ``\exec``
# (alias-proof) because bash 3.2's ``command exec`` keeps saved copies of the redirected fds
# open, so the detached watchdog would hold the command's output pipe. A temp path holding a
# character that is special inside double quotes (the helper embeds it in one) counts as a
# setup failure.
_PROLOGUE_HEAD = "__hermes_sudo_pw=; __hermes_sudo_o=(-S -p '')\n"
_PROLOGUE_TEMPLATE = (
_PROLOGUE_HEAD
+ '__hermes_sudo_dir=$(\\builtin umask 077 && \\command mktemp -d "${TMPDIR:-/tmp}/hermes-sudo.XXXXXXXXXX"'
" 2>/dev/null) || __hermes_sudo_dir=\n"
'case $__hermes_sudo_dir in *[\\"\\\\\\$\\`]*) \\command rm -rf -- "$__hermes_sudo_dir" 2>/dev/null;'
" __hermes_sudo_dir= ;; esac\n"
# Setup comes before any read of stdin: when a step fails the password lines stay on stdin
# and every counted sudo keeps ``-S -p ''``, which is the stdin form. The first steps repeat
# the sudo and tool checks of ``CAPABILITY_PROBE``, so a backend that runs no probe gets the
# same answer here: each sudo word the command runs accepts ``-A`` (``-V`` authenticates
# nothing and reads no stdin; @SUDO_CHECKS@ is one such check per word, see
# ``build_prologue``) and the helper's programs are executable.
'if \\builtin test -n "$__hermes_sudo_dir" &&\n'
"@SUDO_CHECKS@"
" \\builtin test -x /bin/cat && \\builtin test -x /bin/rm && \\builtin test -x /bin/sleep &&\n"
' \\command mkfifo "$__hermes_sudo_dir/p" "$__hermes_sudo_dir/l" 2>/dev/null &&\n'
# A harmless helper at the same path must run before the real one is written: a writable
# but noexec temp directory passes every step above, yet sudo could not run the helper.
# The real helper is never run as this check, since it waits on the FIFO for its writer.
" \\builtin printf '#!/bin/sh\\nexit 0\\n' > \"$__hermes_sudo_dir/a\" 2>/dev/null &&\n"
' \\command chmod 700 "$__hermes_sudo_dir/a" 2>/dev/null &&\n'
' "$__hermes_sudo_dir/a" </dev/null >/dev/null 2>&1 &&\n'
# The helper takes a lock file for its whole exchange, so helpers of sudos that ask at the same
# time are served one after another instead of sharing one line. The lock is an O_EXCL create
# (``set -C``), not ``mkdir``: uutils' mkdir (Ubuntu 25.10+) lets two racing callers both
# succeed, so two helpers read one serving and a sudo gets an empty password.
" \\builtin printf '#!/bin/sh\\nd=\"%s\"\\n"
'until (set -C; : > "$d/k") 2>/dev/null; do [ -d "$d" ] || exit 1; /bin/sleep 0.1 2>/dev/null || /bin/sleep 1; done\\n'
'/bin/cat -- "$d/p" 2>/dev/null; r=$?\\n/bin/rm -f "$d/k"\\nexit $r\\n\' "$__hermes_sudo_dir"'
# ``>|`` replaces the harmless helper even when the session has ``noclobber`` (``set -C``) on.
' >| "$__hermes_sudo_dir/a" 2>/dev/null && \\command chmod 700 "$__hermes_sudo_dir/a" 2>/dev/null &&\n'
' \\exec 19<>"$__hermes_sudo_dir/l"; then\n'
# One password line per counted sudo is on stdin; the helper form needs the first only.
" IFS= \\builtin read -r __hermes_sudo_pw || \\builtin :\n"
" __hermes_sudo_i=1\n"
" while \\builtin test \"$__hermes_sudo_i\" -lt @COUNT@; do\n"
" IFS= \\builtin read -r __hermes_sudo_x || \\builtin break; __hermes_sudo_i=$((__hermes_sudo_i + 1))\n"
" done\n"
" \\builtin unset __hermes_sudo_x __hermes_sudo_i\n"
" __hermes_sudo_o=(-A)\n"
'( ( \\exec 8<"$__hermes_sudo_dir/l" 19>&- </dev/null >/dev/null 2>&1\n'
" \\builtin trap '' HUP PIPE; \\builtin trap '\\builtin :' TERM\n"
" __hermes_sudo_n=@COUNT@\n"
" { __hermes_sudo_i=0\n"
' while \\builtin test "$__hermes_sudo_i" -lt "$__hermes_sudo_n"; do\n'
" __hermes_sudo_i=$((__hermes_sudo_i + 1))\n"
# Each external command of this background group runs in its own subshell: Apple's bash 3.2
# replaces a background ``{ ...; } &`` group with the first ``command <program>`` it meets
# (no fork), so nothing after it would run.
' { if \\builtin test "$__hermes_sudo_i" -lt "$__hermes_sudo_n"; then\n'
' ( \\command mkfifo "$__hermes_sudo_dir/q" ) && ( \\command mv -f "$__hermes_sudo_dir/q" "$__hermes_sudo_dir/p" )\n'
' else ( \\command rm -f "$__hermes_sudo_dir/p" ); fi >/dev/null 2>&1\n'
" \\builtin printf '%s\\n' \"$__hermes_sudo_pw\"; } > \"$__hermes_sudo_dir/p\" || \\builtin break\n"
" done; } 8<&- &\n"
" __hermes_sudo_w=$!\n"
" \\builtin unset __hermes_sudo_pw\n"
# The wait ends at EOF on fd 19 (the shell and every child holding it are gone). It is
# checked every two seconds: while the command's shell ($$) runs, as after a yield to the
# background, serving continues; once the shell is gone it stops at once, unless a counted
# sudo was put in the background (@BG@ = 1), which is then served for up to @TIMEOUT@ more
# seconds. A timed-out read is status > 128 on bash 4+ and status 1 after the wait on bash
# 3.2, where EOF returns 1 at once (SECONDS counts whole seconds, hence ``-lt 1``).
" __hermes_sudo_t=@TIMEOUT@; __hermes_sudo_g=0\n"
" while \\builtin :; do\n"
" SECONDS=0; \\builtin read -r -t 2 __hermes_sudo_x <&8; __hermes_sudo_r=$?\n"
' if \\builtin test "$__hermes_sudo_r" -le 128 && \\builtin test "$SECONDS" -lt 1; then \\builtin break; fi\n'
" if \\builtin kill -0 $$ 2>/dev/null; then \\builtin continue; fi\n"
" if \\builtin test @BG@ -eq 0; then \\builtin break; fi\n"
" __hermes_sudo_g=$((__hermes_sudo_g + 2))\n"
' if \\builtin test "$__hermes_sudo_g" -ge "$__hermes_sudo_t"; then \\builtin break; fi\n'
" done\n"
'\\builtin kill "$__hermes_sudo_w" 2>/dev/null\n'
'\\exec 7<>"$__hermes_sudo_dir/p" 2>/dev/null\n'
'\\command rm -rf "$__hermes_sudo_dir"\n'
"\\exec 7>&-\n"
" ) & )\n"
"else\n"
' \\builtin test -z "$__hermes_sudo_dir" || \\command rm -rf -- "$__hermes_sudo_dir" 2>/dev/null\n'
" __hermes_sudo_dir=\n"
"fi\n"
"\\builtin unset __hermes_sudo_pw\n"
)
# No ``\command``: the check must run whatever the command runs for the word, a shell function
# or alias from the session snapshot included. The subshell still sees those, but an ``exec`` or
# ``exit`` in a wrapper then ends only the check, never the command's own shell.
_SUDO_CHECK = " ( SUDO_ASKPASS=/bin/false {word} -A -V ) </dev/null >/dev/null 2>&1 &&\n"
def build_prologue(count: int, timeout_token: str, background: bool = True,
sudo_words: tuple[str, ...] = ("sudo",)) -> str:
"""Shell text that serves the password read from stdin to *count* askpass helper calls.
Stdin carries *count* password lines, as for the stdin form. *timeout_token* is how long a
backgrounded sudo is still served after the command's shell has exited: a literal number,
or ``SUDO_ASKPASS_TIMEOUT_TOKEN`` for ``fill_timeout_token`` to replace later. With
*background* false (no counted sudo runs in the background) serving stops with the shell.
*sudo_words* are the sudo command words the counted invocations run, as written in the
command (``sudo_command_words``); each must accept ``-A`` or setup falls back to stdin.
"""
checks = "".join(_SUDO_CHECK.format(word=word) for word in sudo_words)
return (_PROLOGUE_TEMPLATE.replace("@SUDO_CHECKS@", checks).replace("@COUNT@", str(int(count)))
.replace("@TIMEOUT@", timeout_token).replace("@BG@", "1" if background else "0"))
def fill_timeout_token(command: str, timeout) -> str:
"""Replace the prologue's timeout token with ``int(timeout) + 30``.
Only a command that starts with the prologue is touched, and only its one token, so any
other command text stays byte-for-byte as given.
"""
if not isinstance(command, str) or not command.startswith(_PROLOGUE_HEAD):
return command
try:
seconds = int(timeout)
except (TypeError, ValueError, OverflowError):
seconds = _FALLBACK_TIMEOUT_S
return command.replace(SUDO_ASKPASS_TIMEOUT_TOKEN, str(seconds + _TIMEOUT_SLACK_S), 1)
# sudo options that take a separate value word (sudo 1.9 and sudo-rs).
_VALUE_SHORT_OPTIONS = frozenset("ugpCDrtUTRhc")
_VALUE_LONG_OPTIONS = frozenset({
"--user", "--group", "--prompt", "--close-from", "--chdir", "--chroot", "--role", "--type",
"--other-user", "--command-timeout", "--host", "--login-class",
})
_REDIRECTION_RE = re.compile(r"^[0-9]*(?:[<>]|&>)")
# A redirection operator written apart from its target (``> out``, ``2>``, ``<<``): the next
# word is that target, not sudo's command.
_BARE_REDIRECTION_RE = re.compile(r"^[0-9]*(?:<<<|<<-?|<>|>>|>\||<|>)$")
def _option_words_carry_own_input(words: list[str]) -> bool:
"""True when sudo's option words (up to its command) include a stdin or askpass option."""
skip_value = False
for raw in words:
if skip_value:
skip_value = False
continue
if _REDIRECTION_RE.match(raw):
skip_value = bool(_BARE_REDIRECTION_RE.match(raw))
continue
if "$" in raw or "`" in raw:
return True # an expansion could produce the option: keep the stdin form
try:
parsed = shlex.split(raw)
except ValueError:
return True
if len(parsed) != 1:
return True
word = parsed[0]
if word in ("--stdin", "--askpass"):
return True
if word == "--" or not word.startswith("-") or word == "-":
return False
if word.startswith("--"):
skip_value = word in _VALUE_LONG_OPTIONS
continue
for index, char in enumerate(word[1:], start=1):
if char in "SA":
return True
if char in _VALUE_SHORT_OPTIONS:
skip_value = index == len(word) - 1
break
return False
def has_own_stdin_or_askpass(command: str | None) -> bool:
"""True when any counted sudo already carries its own stdin or askpass option.
Single-letter clusters count (``-nS``); values of value-taking options are skipped
(``-u S``, ``-pS``). Such a command keeps the stdin form, since sudo rejects ``-A`` with
``-S``.
"""
if not command:
return False
# [0] is the sudo word, [1] the added option word.
return any(_option_words_carry_own_input(words[2:]) for words in _counted_sudo_words(command)[1])
def _counted_sudo_words(command: str) -> tuple[int, list[list[str]]]:
"""``(count, word lists)``: the words of each counted sudo after the askpass marker.
Each counted sudo reads ``<prefix><sudo word> -A <its options...>`` once marked. Text that
happens to contain the prefix adds a chunk, so the lists can outnumber *count*.
"""
from tools.terminal_tool_sudo import _rewrite_real_sudo_invocations, _scan_shell
marked, count = _rewrite_real_sudo_invocations(command, askpass=True)
if not count:
return 0, []
lists: list[list[str]] = []
for chunk in marked.split(SUDO_ASKPASS_PREFIX)[1:]:
words: list[str] = []
for kind, start, end, _ in _scan_shell(chunk):
if kind == "op" and chunk[start:end] == "&" and (
chunk[start - 1:start] in ("<", ">") or chunk[end:end + 1] == ">"
):
continue # ``2>&1`` / ``&> out``: part of a redirection, not the end of the command
if kind in ("op", "comment") or (kind == "ws" and chunk[start] == "\n"):
break
if kind == "word":
words.append(chunk[start:end])
lists.append(words)
return count, lists
def sudo_command_words(command: str | None) -> tuple[str, ...] | None:
"""The distinct sudo command words of the counted invocations, as written in *command*.
These are the words ``build_prologue`` checks for ``-A`` support, so the check runs the same
executable the command runs. For ``env ... sudo`` the word is the sudo word itself: ``env``
looks it up through PATH the same way the shell does, so checking the bare word is enough.
None when the words cannot be collected (no counted sudo, one list per counted sudo not
found) or a word holds ``$`` or a backquote, whose expansion could differ: the caller then
uses the stdin form.
"""
if not command:
return None
count, lists = _counted_sudo_words(command)
if not count or len(lists) != count or not all(lists):
return None
words: list[str] = []
for word_list in lists:
word = word_list[0]
if "$" in word or "`" in word:
return None
if word not in words:
words.append(word)
return tuple(words)
def has_background_sudo(command: str | None) -> bool:
"""True when a counted sudo may still ask after the command's shell has exited.
That is a counted sudo followed by a background ``&`` in the same list, or one inside a
group, subshell or function body with a background ``&`` anywhere after it. A command with
a ``case`` or ``coproc`` word counts too, because the list tracking below does not model
them. It errs toward True: a miss would stop serving a backgrounded sudo that the stdin
form still feeds.
"""
from tools.terminal_tool_sudo import _rewrite_real_sudo_invocations, _scan_shell
if not command:
return False
marked, count = _rewrite_real_sudo_invocations(command, askpass=True)
if not count:
return False
events = list(_scan_shell(marked))
if any(kind == "word" and marked[start:end] in ("case", "coproc") for kind, start, end, _ in events):
return True
prefix_word = SUDO_ASKPASS_PREFIX.strip()
depth = 0
in_list = in_group = False
for kind, start, end, _ in events:
text = marked[start:end]
if kind == "op":
if text == "(":
depth += 1
elif text == ")":
depth = max(0, depth - 1)
elif text == "&":
if marked[start - 1:start] in ("<", ">") or marked[end:end + 1] == ">":
continue # part of a redirection
if in_list or in_group:
return True
elif text in (";", ";;") and depth == 0:
in_list = False
elif kind == "ws" and text == "\n" and depth == 0:
in_list = False
elif kind == "word":
if text == "{":
depth += 1
elif text == "}":
depth = max(0, depth - 1)
elif text == prefix_word:
in_list = True
in_group = in_group or depth > 0
return False
# Exit 0 only when sudo accepts ``-A`` (``-V`` authenticates nothing) and a 0700 helper in a
# ``mktemp -d`` under ${TMPDIR:-/tmp} runs, next to a FIFO the shell can open read+write. The
# prologue repeats the sudo and tool checks at run time, so a passing probe only selects the form.
CAPABILITY_PROBE = (
"SUDO_ASKPASS=/bin/false sudo -A -V >/dev/null 2>&1 || exit 1\n"
'for t in /bin/cat /bin/rm /bin/sleep; do [ -x "$t" ] || exit 1; done\n'
'd=$(umask 077 && mktemp -d "${TMPDIR:-/tmp}/hermes-sudo.XXXXXXXXXX" 2>/dev/null) || exit 1\n'
'case $d in *[\\"\\\\\\$\\`]*) rm -rf -- "$d"; exit 1 ;; esac\n'
"r=1\n"
"if mkfifo \"$d/l\" 2>/dev/null && printf '#!/bin/sh\\nexec /bin/cat -- /dev/null\\n' > \"$d/a\""
' && chmod 700 "$d/a"; then\n'
' ( exec 3<>"$d/l" ) 2>/dev/null && "$d/a" </dev/null >/dev/null 2>&1 && r=0\n'
"fi\n"
'rm -rf -- "$d"\n'
"exit $r\n"
)
_PROBE_ERRORS = (OSError, RuntimeError, ValueError, TypeError, AttributeError, subprocess.SubprocessError)
def sudo_askpass_supported_once(env) -> bool:
"""Run ``CAPABILITY_PROBE`` once per environment object and cache the answer.
True without probing when the backend sets ``_sudo_askpass_runtime_checked``: the prologue
makes the same sudo and tool checks inside each command and falls back to the stdin form
there, so no separate command has to run (and later be cancelled) in the backend. Otherwise
False without probing when the backend does not opt in to in-backend sudo probes
(``_passwordless_sudo_probe_supported``) or is the local backend on Windows; False on any
error or timeout. Bound as ``BaseEnvironment._sudo_askpass_supported``.
"""
cached = getattr(env, "_sudo_askpass_ok", None)
if cached is not None:
return cached
supported = False
if getattr(env, "_sudo_askpass_runtime_checked", False):
supported = True
elif getattr(env, "_passwordless_sudo_probe_supported", False) and not (
getattr(env, "is_local", False) and platform.system() == "Windows"
):
timeout = getattr(env, "_SUDO_PROBE_TIMEOUT_S", 3)
try:
proc = env._run_bash(CAPABILITY_PROBE, timeout=timeout)
supported = env._wait_for_process(proc, timeout=timeout).get("returncode") == 0
except _PROBE_ERRORS:
logger.debug("sudo askpass capability probe failed", exc_info=True)
supported = False
env._sudo_askpass_ok = supported
return supported