fix(channel): a privileged stream sends the sudo password only to a sudo that asks (NetOrk/netork#818)
driver / standard (pull_request) Failing after 16s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Failing after 1m1s
driver / test (3.13) (pull_request) Failing after 54s
driver / lowest (pull_request) Failing after 30s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Failing after 2m41s
driver / publish (pull_request) Skipped

open_stream(privileged=True) with a sudo password wrote the password first,
for sudo -S. A sudo that does not prompt (NOPASSWD, a cached timestamp) left it
on stdin, where it reached the command as the first bytes of the stream: the
Podman API, over sudo podman system dial-stdio. The stream now asks sudo first
(sudo -n sh -c true) and runs the command through sudo -n when it needs no
password.
This commit is contained in:
Christian Manivong committed 2026-11-16 23:28:13 +01:00
1 parent 647a5cc857
commit 553b4d5153
3 files changed
+89 -10

No files matched your search

+19 -4
View File
@@ -158,6 +158,11 @@ _PRIVILEGED_STDIN = (
"mktemp, cat) and have root install that file."
)
#: Whether sudo runs a command for the login user without a password now: in
#: the shape every privileged command takes (``sh -c``), so a sudoers rule that
#: covers one covers the other (``open_stream``).
_SUDO_ASKS_NOTHING = "sudo -n sh -c true"
#: First in every script ``_sudo`` runs: a sudo that does not prompt (NOPASSWD,
#: a cached timestamp) leaves the password on stdin, and nothing may read it.
_NO_STDIN = "exec </dev/null; "
@@ -558,14 +563,24 @@ class LinuxDriver(
def open_stream(self, command: str, *, privileged: bool = False) -> ByteStream:
"""Start *command* on an exec channel and return a stream to it.
A privileged stream with a sudo password starts with that password, for
``sudo -S``; a sudo that does not prompt leaves it for *command*. Write
nothing to such a stream that must not follow the password, as
``_PRIVILEGED_STDIN`` says for ``run_command``.
A privileged stream with a sudo password asks sudo first whether it
wants the password (``sudo -n sh -c true``): a sudo that does not prompt
-- NOPASSWD, a cached timestamp -- would leave it on stdin, and the
stream's own data follows it there, the Podman API over ``sudo podman
system dial-stdio`` say (NetOrk/netork#818). Such a sudo gets the
command through ``sudo -n``; one that asks gets the password first, for
``sudo -S``.
"""
line, prefix = self._privileged(command, privileged)
if prefix and self._sudo_asks_nothing():
line, prefix = f"sudo -n sh -c {_shlex_quote(command)}", None
return open_stream_on_transport(self._transport(), line, stdin_prefix=prefix)
def _sudo_asks_nothing(self) -> bool:
"""Whether sudo runs a command for the login user without its password now."""
probe = run_on_transport(self._transport(), _SUDO_ASKS_NOTHING, timeout=15)
return probe.exit_code == 0
def reboot_host(self) -> None:
"""Restart the host (``HostRebootMixin``); returns once the restart is under way.