feat(kernel): the live patches the running kernel carries, with the kernel facts (NetOrk/netork#2701)

/sys/kernel/livepatch with each patch's enabled and transition, kpatch
list, and Canonical Livepatch's status for the running kernel with the
CVEs it names. status reads the daemon's last state; refresh is never run.
Where the client is installed and its status needs root, a driver that
supplies _run_livepatch_status_command reads it again as root.
This commit is contained in:
christianmanivong committed 2026-11-28 04:23:17 +01:00
1 parent 14217b6eeb
commit b9b6297815
6 files changed
+597 -2

No files matched your search

+321
View File
@@ -0,0 +1,321 @@
"""get_kernel_facts' live patches: what the running kernel has patched in while it runs.
A live patch fixes a kernel CVE without a new release, so a consumer that judges the
running release alone reports the CVE open and a reboot as worth it (NetOrk/netork#2701).
The kernel facts read three things, each where it is kept: the loaded patches in
``/sys/kernel/livepatch`` (every distribution), ``kpatch list`` where kpatch is
installed, and ``canonical-livepatch status --format json`` -- the daemon's last state,
which names the CVEs each patch fixes. Canonical's status may need root: a driver that
can gain it supplies ``_run_livepatch_status_command``.
"""
from __future__ import annotations
import base64
import gzip
import json
import pytest
from napalm_device_types.kernel import (
KERNEL_FACTS_COMMAND,
LIVEPATCH_STATUS_COMMAND,
KernelFactsMixin,
parse_kernel_facts,
)
from napalm_device_types.livepatch import (
canonical_release,
parse_canonical_status,
parse_kpatch_list,
parse_sysfs_livepatches,
)
#: Ubuntu 22.04 with Livepatch applied: the client of the 10.x series, as the Ubuntu
#: Pro client reads it (``Status`` -> ``Livepatch`` -> ``Fixes``).
CANONICAL = {
"client-version": "10.8.3",
"architecture": "x86_64",
"cpu-model": "Intel(R) Xeon(R) CPU",
"last-check": "2026-11-28T09:12:44Z",
"boot-time": "2026-11-01T07:00:00Z",
"uptime": "2312h12m44s",
"Status": [
{
"Kernel": "5.15.0-91.101-generic",
"Running": True,
"Livepatch": {
"CheckState": "checked",
"State": "applied",
"Version": "101.1",
"Fixes": [
{
"Name": "cve-2023-1077",
"Description": "sched: ...",
"Bug": "",
"Patched": True,
},
{
"Name": "cve-2023-3090",
"Description": "ipvlan: ...",
"Bug": "",
"Patched": True,
},
{
"Name": "cve-2023-3389",
"Description": "io_uring: ...",
"Bug": "",
"Patched": False,
},
{"Name": "lp-2023-1", "Description": "not a CVE", "Bug": "", "Patched": True},
],
},
"Supported": "supported",
}
],
"tier": "updates",
}
#: The 9.x client wrote its fixes as one text, a ``* cve-...`` line each.
CANONICAL_9 = {
"Status": [
{
"Kernel": "5.4.0-42.46-generic",
"Running": True,
"Livepatch": {
"CheckState": "checked",
"State": "applied",
"Version": "70.1",
"Fixes": "* cve-2020-11935\n* cve-2020-15780 (bug 1883337)\n",
},
}
]
}
REPORT = """[release]
5.15.0-91-generic
[loaded]
lkp_Ubuntu_5_15_0_91_101_generic_101
[livepatch]
lkp_Ubuntu_5_15_0_91_101_generic_101 1 0
[canonical_livepatch]
{json}
"""
def _wire(report: str) -> str:
payload = base64.encodebytes(gzip.compress(report.encode())).decode()
return f"KFACTS_BEGIN\n{payload}KFACTS_END\n"
class TestSysfs:
def test_each_loaded_patch_enabled_or_not(self):
lines = ["livepatch_cve_2024_1 1 0", "kpatch_5_14_0_427 0 1", ""]
assert parse_sysfs_livepatches(lines) == [
{"name": "livepatch_cve_2024_1", "enabled": True, "transition": False},
{"name": "kpatch_5_14_0_427", "enabled": False, "transition": True},
]
def test_an_unreadable_flag_is_none(self):
assert parse_sysfs_livepatches(["lkp_x"]) == [
{"name": "lkp_x", "enabled": None, "transition": None}
]
class TestKpatch:
def test_loaded_and_installed_modules(self):
text = [
"Loaded patch modules:",
"kpatch_5_14_0_427_13_1 [enabled]",
"kpatch_cumulative_old [disabled]",
"",
"Installed patch modules:",
"kpatch_5_14_0_427_13_1 (5.14.0-427.13.1.el9_4.x86_64)",
"kpatch_5_14_0_362 (5.14.0-362.8.1.el9_3.x86_64)",
]
assert parse_kpatch_list(text) == {
"loaded": [
{"name": "kpatch_5_14_0_427_13_1", "enabled": True},
{"name": "kpatch_cumulative_old", "enabled": False},
],
"installed": [
{"name": "kpatch_5_14_0_427_13_1", "kernel": "5.14.0-427.13.1.el9_4.x86_64"},
{"name": "kpatch_5_14_0_362", "kernel": "5.14.0-362.8.1.el9_3.x86_64"},
],
}
def test_nothing_loaded(self):
assert parse_kpatch_list(["Loaded patch modules:", "", "Installed patch modules:"]) == {
"loaded": [],
"installed": [],
}
class TestCanonical:
def test_the_running_kernels_status_and_its_cves(self):
status = parse_canonical_status(json.dumps(CANONICAL))
assert status == {
"kernel": "5.15.0-91.101-generic",
"running": True,
"state": "applied",
"check_state": "checked",
"version": "101.1",
"fixes": [
{"cve": "CVE-2023-1077", "patched": True},
{"cve": "CVE-2023-3090", "patched": True},
{"cve": "CVE-2023-3389", "patched": False},
],
}
def test_the_9_x_clients_text_of_fixes(self):
status = parse_canonical_status(json.dumps(CANONICAL_9))
assert status is not None
assert status["fixes"] == [
{"cve": "CVE-2020-11935", "patched": True},
{"cve": "CVE-2020-15780", "patched": True},
]
def test_keys_in_either_case(self):
lowered = json.loads(
json.dumps(CANONICAL).replace('"Status"', '"status"').replace('"State"', '"state"')
)
assert parse_canonical_status(json.dumps(lowered))["state"] == "applied"
def test_the_entry_of_the_running_kernel(self):
other = {
"Kernel": "5.15.0-88.98-generic",
"Running": False,
"Livepatch": {"State": "applied", "Fixes": []},
}
data = {"Status": [other, CANONICAL["Status"][0]]}
assert parse_canonical_status(json.dumps(data))["kernel"] == "5.15.0-91.101-generic"
@pytest.mark.parametrize("text", ["", "error: must be root", "{}", '{"Status": []}', "[1, 2]"])
def test_no_status_is_none(self, text):
assert parse_canonical_status(text) is None
@pytest.mark.parametrize(
"kernel, release",
[
("5.15.0-91.101-generic", "5.15.0-91-generic"),
("6.8.0-49.49~22.04.1-generic", "6.8.0-49-generic"),
("5.4.0-1045.47-aws", "5.4.0-1045-aws"),
("6.8.0-49-generic", "6.8.0-49-generic"),
(None, None),
],
)
def test_its_kernel_as_uname_names_it(self, kernel, release):
assert canonical_release(kernel) == release
class TestInTheKernelFacts:
def test_read_with_the_kernel_facts(self):
facts = parse_kernel_facts(_wire(REPORT.format(json=json.dumps(CANONICAL))))
patches = facts["live_patches"]
assert patches["loaded"] == [
{"name": "lkp_Ubuntu_5_15_0_91_101_generic_101", "enabled": True, "transition": False}
]
assert patches["kpatch"] is None
assert patches["canonical"]["state"] == "applied"
def test_a_kernel_without_live_patching_has_none_loaded(self):
"""No ``/sys/kernel/livepatch``: the kernel was built without it, nothing is loaded."""
facts = parse_kernel_facts(_wire("[release]\n6.1.0-25-amd64\n"))
assert facts["live_patches"] == {
"loaded": None,
"kpatch": None,
"canonical": None,
"canonical_installed": False,
}
def test_an_empty_directory_is_none_loaded(self):
facts = parse_kernel_facts(_wire("[release]\n6.1.0-25-amd64\n[livepatch]\n"))
assert facts["live_patches"]["loaded"] == []
class _Driver(KernelFactsMixin):
def __init__(self, report: str, status: str | None = None):
self.report = report
self.status = status
self.asked: list[str] = []
def _run_kernel_facts_command(self, command: str) -> str:
return _wire(self.report)
class _RootDriver(_Driver):
def _run_livepatch_status_command(self, command: str) -> str:
self.asked.append(command)
return self.status or ""
class TestCanonicalAsRoot:
"""``canonical-livepatch status`` may refuse a user who is not root: where the
kernel facts found the client but no status, a driver that can gain root asks again."""
UNREAD = "[release]\n5.15.0-91-generic\n[livepatch]\n[canonical_livepatch]\n"
def test_asked_again_as_root(self):
driver = _RootDriver(self.UNREAD, json.dumps(CANONICAL))
facts = driver.get_kernel_facts()
assert driver.asked == [LIVEPATCH_STATUS_COMMAND]
assert facts["live_patches"]["canonical"]["version"] == "101.1"
def test_not_asked_when_read_already(self):
driver = _RootDriver(REPORT.format(json=json.dumps(CANONICAL)))
driver.get_kernel_facts()
assert driver.asked == []
def test_not_asked_without_the_client(self):
driver = _RootDriver("[release]\n6.1.0\n")
driver.get_kernel_facts()
assert driver.asked == []
def test_a_driver_without_root_leaves_it_unread(self):
facts = _Driver(self.UNREAD).get_kernel_facts()
assert facts["live_patches"]["canonical"] is None
assert facts["live_patches"]["canonical_installed"] is True
def test_a_failed_root_read_leaves_it_unread(self):
class Failing(_RootDriver):
def _run_livepatch_status_command(self, command: str) -> str:
raise RuntimeError("sudo: a password is required")
assert Failing(self.UNREAD).get_kernel_facts()["live_patches"]["canonical"] is None
class TestTheCommands:
def test_the_kernel_facts_read_the_live_patches(self):
assert "/sys/kernel/livepatch" in KERNEL_FACTS_COMMAND
assert "kpatch list" in KERNEL_FACTS_COMMAND
assert "status --format json" in KERNEL_FACTS_COMMAND
def test_they_ask_canonical_nothing(self):
"""``status`` reads the daemon's last state; ``refresh`` would call Canonical."""
for command in (KERNEL_FACTS_COMMAND, LIVEPATCH_STATUS_COMMAND):
for verb in (
"refresh",
"livepatch enable",
"livepatch disable",
"kpatch load",
"kpatch unload",
"kpatch install",
"kpatch uninstall",
):
assert verb not in command
assert LIVEPATCH_STATUS_COMMAND.endswith("status --format json")