From 4ba81cc90ea3dc154fa00d56c78649263aa56f1d Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 19:04:27 +0100 Subject: [PATCH] feat(packages): each rpm names the source rpm it was built from perf, python3-perf and bpftool are built from the kernel's source rpm, and Red Hat's VEX lists them in every kernel CVE: 18 532 findings on an Oracle Linux 9 host's three kernel tools alone. The rpm query now reads %{SOURCERPM}, and each package carries its name as `source_rpm` (`perf` -> `kernel`), absent where rpm names none (`gpg-pubkey`). Not dpkg's `source_package`: netOrk reads that as the name a distribution files its verdicts under, which holds for Debian and not for Oracle or Red Hat, whose records name the binary (NetOrk/netork#2394). --- CHANGELOG.md | 6 +++++ napalm_linux/_rpm.py | 59 ++++++++++++++++++++++++++++++++++---------- tests/test_linux.py | 52 +++++++++++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0216685..97d39f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- On an rpm host each package names the source rpm it was built from, as `source_rpm` + (`perf` → `kernel`); absent where rpm names none (`gpg-pubkey`). Red Hat lists `perf`, + `python3-perf` and `bpftool` in every kernel CVE, and the source is what tells them apart + from user space (NetOrk/netork#2394). Not dpkg's `source_package`: netOrk reads that as + the name a distribution files its verdicts under, and Oracle's and Red Hat's name the + binary. - `get_installed_advisory_cves()` through napalm-device-types' `UpdateMixin` (4.37.0): the CVEs a dnf host's installed security advisories close, per package, read as root from the cache. netOrk reads it where its knowledge base holds no records of the host's diff --git a/napalm_linux/_rpm.py b/napalm_linux/_rpm.py index 1a55543..3c2bcd7 100644 --- a/napalm_linux/_rpm.py +++ b/napalm_linux/_rpm.py @@ -2,35 +2,68 @@ from __future__ import annotations +from typing import NotRequired + from napalm_device_types.models import PackageDict #: One line per package, the version as ``[epoch:]version-release`` the way dpkg #: writes it: the epoch only where the package has one. Oracle's and Red Hat's #: fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as newer -#: than every installed build (NetOrk/netork#2394). +#: than every installed build (NetOrk/netork#2394). The source rpm before the +#: summary, which stays last and keeps whatever it contains. RPM_QUERY_COMMAND = ( "rpm -qa --queryformat " - "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'" + "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SOURCERPM}" + "\\t%{SUMMARY}\\n'" " 2>/dev/null" ) +class RpmPackageDict(PackageDict): + """An rpm package with the name of the source rpm it was built from. + + ``perf``, ``python3-perf`` and ``bpftool`` come from ``kernel``, and Red Hat + lists them in every kernel CVE; their source is what tells them apart from + user space (NetOrk/netork#2394). Deliberately not dpkg's ``source_package``: + netOrk reads that as the name a distribution files its verdicts under, which + holds for Debian and not for Oracle or Red Hat, whose records name the binary. + Absent where rpm names none (``gpg-pubkey``). + """ + + source_rpm: NotRequired[str] + + +def source_rpm_name(sourcerpm: str) -> str | None: + """``kernel-5.14.0-687.66.1.el9_8.src.rpm`` → ``kernel``; ``None`` for ``(none)``.""" + stem = sourcerpm.strip() + for suffix in (".src.rpm", ".nosrc.rpm"): + if stem.endswith(suffix): + stem = stem.removesuffix(suffix) + break + else: + return None + name, *version_release = stem.rsplit("-", 2) + return name if len(version_release) == 2 and name else None + + def parse_rpm_query(out: str, source: str) -> list[PackageDict]: """The packages in ``RPM_QUERY_COMMAND``'s output, each marked as *source*'s.""" packages: list[PackageDict] = [] for line in out.splitlines(): - parts = line.split("\t", 3) + parts = line.split("\t", 4) if len(parts) < 2: continue size = parts[2].strip() if len(parts) > 2 else "" - packages.append( - { - "name": parts[0].strip(), - "version": parts[1].strip(), - "installed": True, - "description": parts[3].strip() if len(parts) > 3 else "", - "size": int(size) if size.isdigit() else 0, - "source": source, - } - ) + package: RpmPackageDict = { + "name": parts[0].strip(), + "version": parts[1].strip(), + "installed": True, + "description": parts[4].strip() if len(parts) > 4 else "", + "size": int(size) if size.isdigit() else 0, + "source": source, + } + built_from = source_rpm_name(parts[3]) if len(parts) > 3 else None + if built_from: + package["source_rpm"] = built_from + packages.append(package) return packages diff --git a/tests/test_linux.py b/tests/test_linux.py index 41ba0cc..ff6a4b8 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -7,6 +7,7 @@ import pytest from napalm_device_types import DNF_INSTALLED_CVES_COMMAND from napalm_linux._routes import IpRoute, parse_ip_route +from napalm_linux._rpm import source_rpm_name from napalm_linux.linux import ( _YUM_SECURITY_COMMAND, LinuxDriver, @@ -2598,11 +2599,17 @@ class TestIpv6NextHops: # get_packages (rpm) and the installed advisories (NetOrk/netork#2394) # --------------------------------------------------------------------------- -# rpm -qa on Oracle Linux 9.8 (aarch64): gmp and openssl carry epoch 1. +# rpm -qa on Oracle Linux 9.8 (aarch64): gmp and openssl carry epoch 1; perf is +# built from the kernel's source rpm; gpg-pubkey has none. RPM_QA = ( - "glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tCommon binaries and locale data for glibc\n" - "gmp\t1:6.2.0-13.el9\t805146\tA GNU arbitrary precision library\n" - "openssl\t1:3.5.8-2.0.1.el9_8\t2106925\tUtilities from the general purpose cryptography library\n" + "glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tglibc-2.34-284.0.1.el9_8.4.src.rpm" + "\tCommon binaries and locale data for glibc\n" + "gmp\t1:6.2.0-13.el9\t805146\tgmp-6.2.0-13.el9.src.rpm\tA GNU arbitrary precision library\n" + "openssl\t1:3.5.8-2.0.1.el9_8\t2106925\topenssl-3.5.8-2.0.1.el9_8.src.rpm" + "\tUtilities from the general purpose cryptography library\n" + "perf\t5.14.0-687.66.1.el9_8\t5401292\tkernel-5.14.0-687.66.1.el9_8.src.rpm" + "\tPerformance monitoring for the Linux kernel\n" + "gpg-pubkey\t8d8b756f-629e59ec\t0\t(none)\tOracle OSS group (Open Source Software group)\n" ) @@ -2620,6 +2627,26 @@ class TestRpmPackages: assert pkgs["glibc-common"]["version"] == "2.34-284.0.1.el9_8.4" assert pkgs["glibc-common"]["source"] == "dnf" assert pkgs["glibc-common"]["size"] == 1210460 + assert pkgs["openssl"]["description"] == ( + "Utilities from the general purpose cryptography library" + ) + + def test_each_package_names_the_source_rpm_it_was_built_from(self, driver): + """perf, python3-perf and bpftool are built from the kernel's source rpm, + and Red Hat lists them in every kernel CVE: netOrk tells them apart from + user space by that name (NetOrk/netork#2394).""" + driver._pkg_manager = "dnf" + with patch.object(driver, "_send", return_value=RPM_QA) as send: + pkgs = {p["name"]: p for p in driver.get_packages()} + + assert "%{SOURCERPM}" in send.call_args.args[0] + assert pkgs["perf"]["source_rpm"] == "kernel" + assert pkgs["glibc-common"]["source_rpm"] == "glibc" + assert pkgs["openssl"]["source_rpm"] == "openssl" + assert "source_rpm" not in pkgs["gpg-pubkey"] + # Not `source_package`: netOrk reads that as the name a distribution + # files its verdicts under, and Oracle's and Red Hat's name the binary. + assert all("source_package" not in p for p in pkgs.values()) class TestInstalledAdvisoryCves: @@ -2654,3 +2681,20 @@ class TestInstalledAdvisoryCves: with pytest.raises(NotImplementedError): driver.get_installed_advisory_cves() + + +class TestSourceRpmName: + @pytest.mark.parametrize( + ("sourcerpm", "name"), + [ + ("kernel-5.14.0-687.66.1.el9_8.src.rpm", "kernel"), + ("kernel-uek-6.12.0-104.43.4.3.el9uek.src.rpm", "kernel-uek"), + ("python-dateutil-2.8.1-7.el9.src.rpm", "python-dateutil"), + ("dummy-1.0-1.nosrc.rpm", "dummy"), + ("(none)", None), + ("", None), + ("broken.src.rpm", None), + ], + ) + def test_the_name_is_what_precedes_version_and_release(self, sourcerpm, name): + assert source_rpm_name(sourcerpm) == name