From 553b4d515314ad51ff787017a332072f2bd58c4e Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 16 Nov 2026 23:28:13 +0100 Subject: [PATCH 1/3] fix(channel): a privileged stream sends the sudo password only to a sudo that asks (NetOrk/netork#818) 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. --- CHANGELOG.md | 8 +++++ napalm_linux/linux.py | 23 +++++++++--- tests/test_command_channel.py | 68 +++++++++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad61a61..e1c56fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 (`1:6.2.0-13.el9`), as on a dpkg host. Oracle's and Red Hat's fixes name it, and without it an epoch-1 fix read as newer than every installed build (NetOrk/netork#2394). +### Fixed +- `open_stream(privileged=True)` with a sudo password asks sudo first whether it wants it + (`sudo -n sh -c true`). A sudo that does not prompt (NOPASSWD, a cached timestamp) used to + get the password on stdin anyway, where it went to the command as the first bytes of the + stream: into the Podman API, over `sudo podman system dial-stdio` (NetOrk/netork#818). + Such a sudo now gets the command through `sudo -n`, and only one that asks gets the + password. + ### Added - `get_time_sync()` and `configure_time_sync()` through napalm-device-types' `PosixTimeSyncMixin` (4.35.0): the host's NTP servers in its client's order, its time zone, diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index ac67d3d..93ff14e 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -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 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. diff --git a/tests/test_command_channel.py b/tests/test_command_channel.py index 4072792..ab60066 100644 --- a/tests/test_command_channel.py +++ b/tests/test_command_channel.py @@ -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() -- 2.54.0 From 69c43d3288a2a9b10b019793876f64adb89455ee Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 17 Nov 2026 00:16:33 +0100 Subject: [PATCH 2/3] refactor(channel): the sudo probe of a privileged stream lives in _shell (NetOrk/netork#818) linux.py stays at the size its quality ratchet records. --- napalm_linux/_shell.py | 26 ++++++++++++++++++++++++++ napalm_linux/linux.py | 25 +++++-------------------- tests/test_command_channel.py | 3 ++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/napalm_linux/_shell.py b/napalm_linux/_shell.py index fe9fde9..4d61730 100644 --- a/napalm_linux/_shell.py +++ b/napalm_linux/_shell.py @@ -7,6 +7,8 @@ import re from shlex import quote from typing import Any +from napalm_device_types.channel import run_on_transport + logger = logging.getLogger(__name__) #: The end of a command's output when nothing better is known: a line that looks @@ -54,6 +56,30 @@ def sudo_without_password(command: str) -> str: return f"sudo -n sh -c {quote(body)}{tail}" +#: 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. +SUDO_ASKS_NOTHING = "sudo -n sh -c true" + + +def unprompted_stream( + transport: Any, command: str, line: str, prefix: bytes | None +) -> tuple[str, bytes | None]: + """The line and stdin prefix a privileged stream of *command* starts with. + + With a sudo password (*prefix*) sudo is asked first whether it wants it: a + sudo that does not prompt -- NOPASSWD, a cached timestamp -- would leave the + password on stdin, where the stream's own data follows it, the Podman API + over ``sudo podman system dial-stdio`` say (NetOrk/netork#818). Such a sudo + gets *command* through ``sudo -n`` and no password; one that asks gets it. + """ + if not prefix: + return line, prefix + if run_on_transport(transport, SUDO_ASKS_NOTHING, timeout=15).exit_code == 0: + return f"sudo -n sh -c {quote(command)}", None + return line, prefix + + #: Sent before anything else in the interactive session (NetOrk/netork#1039): #: the shell writes no history file and records nothing more. The leading space #: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``; diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 93ff14e..e633333 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -83,7 +83,7 @@ from napalm_linux._posix import LinuxPosixMixin from napalm_linux._readings import LinuxProcessReadingsMixin from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length from napalm_linux._rpm import RPM_QUERY_COMMAND, parse_rpm_query -from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password +from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password, unprompted_stream from napalm_linux._snmp_agent import ( _SNMP_INSTALL, _SNMPD_CONF, @@ -158,11 +158,6 @@ _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 ByteStream: """Start *command* on an exec channel and return a stream to it. - 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``. + A privileged stream with a sudo password gets it first only from a sudo + that asks for it; one that does not would hand it to *command* as the + stream's first bytes (``unprompted_stream``, NetOrk/netork#818). """ line, prefix = self._privileged(command, privileged) - if prefix and self._sudo_asks_nothing(): - line, prefix = f"sudo -n sh -c {_shlex_quote(command)}", None + line, prefix = unprompted_stream(self._transport(), command, line, prefix) 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. diff --git a/tests/test_command_channel.py b/tests/test_command_channel.py index ab60066..9b128a5 100644 --- a/tests/test_command_channel.py +++ b/tests/test_command_channel.py @@ -17,6 +17,7 @@ import pytest from napalm.base.exceptions import ConnectionClosedException from napalm_linux import LinuxDriver +from napalm_linux._shell import SUDO_ASKS_NOTHING class FakeChannel: @@ -61,7 +62,7 @@ class FakeChannel: #: 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" +SUDO_PROBE = SUDO_ASKS_NOTHING ASKS = {SUDO_PROBE: 1} -- 2.54.0 From 98157f36737239c4ad554669b7f218b6e4886f6e Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 17 Nov 2026 00:17:39 +0100 Subject: [PATCH 3/3] refactor(channel): import run_on_transport from the package, as linux.py does (NetOrk/netork#818) --- napalm_linux/_shell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napalm_linux/_shell.py b/napalm_linux/_shell.py index 4d61730..7ce63c1 100644 --- a/napalm_linux/_shell.py +++ b/napalm_linux/_shell.py @@ -7,7 +7,7 @@ import re from shlex import quote from typing import Any -from napalm_device_types.channel import run_on_transport +from napalm_device_types import run_on_transport logger = logging.getLogger(__name__) -- 2.54.0