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

+62 -6
View File
@@ -20,9 +20,10 @@ from napalm_linux import LinuxDriver
class FakeChannel:
def __init__(self):
def __init__(self, exits):
self.command = None
self.sent = b""
self._exits = exits
def exec_command(self, command):
self.command = command
@@ -55,21 +56,28 @@ class FakeChannel:
return True
def recv_exit_status(self):
return 0
return self._exits.get(self.command, 0)
#: The probe a privileged stream sends first, and how a sudo that asks for its
#: password answers it.
SUDO_PROBE = "sudo -n sh -c true"
ASKS = {SUDO_PROBE: 1}
class FakeTransport:
def __init__(self):
def __init__(self, exits=None):
self.channels = []
self.exits = ASKS if exits is None else exits
def open_session(self):
self.channels.append(FakeChannel())
self.channels.append(FakeChannel(self.exits))
return self.channels[-1]
def _driver(*, root=False, sudo_password=None):
def _driver(*, root=False, sudo_password=None, exits=None):
driver = LinuxDriver("h", "u", "p", optional_args={"sudo_password": sudo_password})
transport = FakeTransport()
transport = FakeTransport(exits)
driver._device = SimpleNamespace(
remote_conn_pre=SimpleNamespace(get_transport=lambda: transport)
)
@@ -166,6 +174,54 @@ def test_a_privileged_stream_gets_the_password_first():
assert channel.sent == b"pw\n"
class TestNoPasswordIntoAStream:
"""A privileged stream carries data after the password: the Podman API over
``sudo podman system dial-stdio`` (NetOrk/netork#818). A sudo that does not ask
-- NOPASSWD, a cached timestamp -- would hand the password to the command as
the first bytes of that data. So the stream asks sudo first, and sends the
password only to a sudo that wants it."""
def test_sudo_is_asked_first(self):
driver, transport = _driver(sudo_password="pw")
driver.open_stream("podman system dial-stdio", privileged=True)
assert transport.channels[0].command == SUDO_PROBE
assert transport.channels[0].sent == b""
def test_a_sudo_that_asks_nothing_gets_no_password(self):
driver, transport = _driver(sudo_password="pw", exits={})
driver.open_stream("podman system dial-stdio", privileged=True)
channel = transport.channels[-1]
assert channel.command == "sudo -n sh -c 'podman system dial-stdio'"
assert channel.sent == b""
def test_without_a_password_there_is_nothing_to_ask(self):
driver, transport = _driver()
driver.open_stream("podman system dial-stdio", privileged=True)
assert [c.command for c in transport.channels] == [
"sudo -n sh -c 'podman system dial-stdio'"
]
def test_a_root_login_asks_nothing_either(self):
driver, transport = _driver(root=True, sudo_password="pw")
driver.open_stream("podman system dial-stdio", privileged=True)
assert [c.command for c in transport.channels] == ["podman system dial-stdio"]
def test_podman_is_reached_as_root(self):
driver, transport = _driver(root=True)
driver.open_container_engine("podman").open_api()
assert transport.channels[-1].command == "podman system dial-stdio"
def test_the_container_engine_api_is_a_stream_over_dial_stdio():
driver, transport = _driver()