From 8174f1df817ed678632ec39a3ba2aaa0b1cf382e Mon Sep 17 00:00:00 2001 From: patrick Date: Sun, 1 Nov 2026 15:25:57 -0500 Subject: [PATCH] Put hosts with unloaded CPU microcode updates in reboot On bare metal, needrestart -b reports NEEDRESTART-UCSTA: 2 when the installed intel-microcode/amd64-microcode is newer than what the CPU loaded at boot, which only a reboot fixes. The agent dropped those lines. It now reports needrestart_ucsta, _uccur and _ucexp, and the server puts the host in reboot with both revisions. Livepatch doesn't cover it. needrestart skips the check in VMs, so only physical hosts (debian-homelab today) report it; the host page gets a "CPU microcode" row. Co-Authored-By: Claude Opus 5.5 --- agent/kernelwatch-agent | 12 ++++++++++++ docs/how-it-works.md | 10 +++++++++- server/kernelwatch/report.py | 3 +++ server/kernelwatch/status.py | 7 +++++++ server/kernelwatch/web.py | 11 +++++++++++ tests/test_agent.py | 14 ++++++++++++++ tests/test_status.py | 13 +++++++++++++ tests/test_web.py | 10 ++++++++++ 8 files changed, 79 insertions(+), 1 deletion(-) diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index b91d1ca..25180de 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -134,9 +134,15 @@ NEEDRESTART_MAX_AGE=7200 # Sets NR_* from the results file, if it's there and fresh. KSTA: 0 unknown, # 1 current, 2 ABI-compatible upgrade pending, 3 version upgrade pending. +# UCSTA (CPU microcode, bare metal only; needrestart skips it in VMs): 0 unknown, +# 1 current, 2 a newer revision is installed but needs a reboot to load. UCCUR +# and UCEXP are the loaded and installed revisions. NR_OK=0 NR_KSTA= NR_KEXP= +NR_UCSTA= +NR_UCCUR= +NR_UCEXP= NR_SVCS= NR_SESS=0 read_needrestart() { @@ -148,6 +154,9 @@ read_needrestart() { case $line in NEEDRESTART-KSTA:*) NR_KSTA=${line#*: } ;; NEEDRESTART-KEXP:*) NR_KEXP=${line#*: } ;; + NEEDRESTART-UCSTA:*) NR_UCSTA=${line#*: } ;; + NEEDRESTART-UCCUR:*) NR_UCCUR=${line#*: } ;; + NEEDRESTART-UCEXP:*) NR_UCEXP=${line#*: } ;; NEEDRESTART-SVC:*) NR_SVCS+=${line#*: }$'\n' ;; NEEDRESTART-SESS:*) NR_SESS=$((NR_SESS + 1)) ;; esac @@ -439,6 +448,9 @@ build_report() { printf '"kernel_newest_installed":%s,' "$(json_str "$newest")" printf '"needrestart_ksta":%s,' "$(json_int "$NR_KSTA")" printf '"needrestart_kexp":%s,' "$(json_str "$NR_KEXP")" + printf '"needrestart_ucsta":%s,' "$(json_int "$NR_UCSTA")" + printf '"needrestart_uccur":%s,' "$(json_str_or_null "$NR_UCCUR")" + printf '"needrestart_ucexp":%s,' "$(json_str_or_null "$NR_UCEXP")" if ((NR_OK)); then printf '"services_need_restart":%s,' "$(sort -u <<<"$NR_SVCS" | json_str_array)" printf '"sessions_need_restart":%s,' "$NR_SESS" diff --git a/docs/how-it-works.md b/docs/how-it-works.md index d8e9f22..17a251f 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -9,7 +9,7 @@ Each host gets one status. When several apply, the worst one wins: | Status | Meaning | |----------|---------| | `stale` | No report in `stale_after_hours` (default 3h), or never. Other data can't be trusted. | -| `reboot` | A newer kernel is installed but not running, or `/run/reboot-required` exists. | +| `reboot` | A newer kernel is installed but not running, `/run/reboot-required` exists, or newer CPU microcode is installed but not loaded. | | `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. Also when `/boot` has less than `boot_min_free_mb` free, dpkg left packages half-installed, a kernel package is on hold, the OS release no longer gets security updates, unattended-upgrades is enabled but hasn't succeeded in `unattended_upgrades_stale_after_days`, the release's security repository is missing from apt or hasn't refreshed in `security_repo_stale_after_days`, or one of kernelwatch's own helper units has failed three runs in a row. | | `ok` | Running the newest installed kernel, with no security or kernel updates pending. | @@ -39,6 +39,14 @@ How the agent works it out: The agent never runs as root, and needrestart never gets network access. Fix it on the host: `sudo needrestart -r a` (or restart the listed services), and the next report clears it. +- **CPU microcode**: on bare metal, needrestart also compares the microcode + revision the CPU loaded at boot with the one the installed `intel-microcode` + or `amd64-microcode` package provides. A newer one only loads on a reboot, so + it puts the host in `reboot`: "CPU microcode update installed but not loaded + (0x001e → 0x0020)". Livepatch doesn't cover it. needrestart skips the check in + virtual machines (the hypervisor handles microcode), so the host page's "CPU + microcode" row only appears on physical hosts. Agents before 0.10.0 don't + report it. - **Ubuntu Livepatch**: with [Livepatch](https://ubuntu.com/security/livepatch) (free for personal use with Ubuntu Pro), a newer kernel being installed doesn't mean the running one is unpatched. If Livepatch is healthy (`applied` or diff --git a/server/kernelwatch/report.py b/server/kernelwatch/report.py index 969c97c..952a9b0 100644 --- a/server/kernelwatch/report.py +++ b/server/kernelwatch/report.py @@ -27,6 +27,9 @@ FIELDS: dict[str, str] = { "kernel_newest_installed": "str", "needrestart_ksta": "int", "needrestart_kexp": "str", + "needrestart_ucsta": "int", + "needrestart_uccur": "str", + "needrestart_ucexp": "str", "services_need_restart": "strlist", "sessions_need_restart": "int", "reboot_required": "bool", diff --git a/server/kernelwatch/status.py b/server/kernelwatch/status.py index 2c9b55e..601f826 100644 --- a/server/kernelwatch/status.py +++ b/server/kernelwatch/status.py @@ -30,6 +30,8 @@ LIVEPATCH_HEALTHY_STATES = ("applied", "nothing-to-apply") KSTA_CURRENT = 1 KSTA_ABI_UPGRADE = 2 KSTA_VERSION_UPGRADE = 3 +# needrestart's NEEDRESTART-UCSTA: a newer CPU microcode is installed but not loaded. +UCSTA_OBSOLETE = 2 @dataclass @@ -309,6 +311,11 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float, status.raise_to("reboot", f"/run/reboot-required{detail}") if livepatch_note and not covered: status.note(livepatch_note) + # Microcode loads at boot; Livepatch doesn't cover it. + if report.get("needrestart_ucsta") == UCSTA_OBSOLETE: + cur, exp = report.get("needrestart_uccur"), report.get("needrestart_ucexp") + detail = f" ({cur} → {exp})" if cur and exp else "" + status.raise_to("reboot", f"CPU microcode update installed but not loaded{detail}") # An agent whose apt simulation failed sends no update counts, so "no security # updates" would be a guess. Agents before 0.5.0 don't send the field. diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py index 21de8a4..3a29b5e 100644 --- a/server/kernelwatch/web.py +++ b/server/kernelwatch/web.py @@ -105,6 +105,16 @@ def security_repo_summary(report: dict[str, Any]) -> str | None: return f"{suite}, published {duration(age)} before the report" +def microcode_summary(report: dict[str, Any]) -> str | None: + state = {1: "current", 2: "update needs a reboot"}.get(report.get("needrestart_ucsta")) + if state is None: + return None # unknown, a VM, or no needrestart + cur, exp = report.get("needrestart_uccur"), report.get("needrestart_ucexp") + revs = f" (loaded {cur}, installed {exp})" if state != "current" and cur and exp else \ + f" ({cur})" if cur else "" + return state + revs + + def helper_units_summary(report: dict[str, Any]) -> str | None: units = helper_units(report) if not units: @@ -532,6 +542,7 @@ class Handler(BaseHTTPRequestHandler): field("Installed kernels", r.get("kernels_installed"), code=True), field("needrestart", None if r.get("needrestart_ksta") is None else f"KSTA={r['needrestart_ksta']} expected={r.get('needrestart_kexp') or '?'}"), + field("CPU microcode", microcode_summary(r)), field("Livepatch", None if r.get("livepatch_state") is None else f"{r['livepatch_state']}, {r.get('livepatch_fix_count') or 0} fixes applied, " f"{r.get('livepatch_supported') or '?'} until {r.get('livepatch_upgrade_required') or '?'}"), diff --git a/tests/test_agent.py b/tests/test_agent.py index 78f5121..7ccc887 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -136,6 +136,20 @@ class AgentReportTest(AgentCase): self.assertEqual(r["services_need_restart"], ["nginx.service", "ssh.service"]) self.assertEqual(r["sessions_need_restart"], 1) + def test_needrestart_microcode(self): + self.write_needrestart("NEEDRESTART-KSTA: 1\nNEEDRESTART-UCSTA: 2\n" + "NEEDRESTART-UCCUR: 0x001e\nNEEDRESTART-UCEXP: 0x0020\n") + r = self.report() + self.assertEqual((r["needrestart_ucsta"], r["needrestart_uccur"], r["needrestart_ucexp"]), + (2, "0x001e", "0x0020")) + + def test_needrestart_without_microcode_lines(self): + # Virtual machines: needrestart leaves the UC* lines out. + self.write_needrestart("NEEDRESTART-KSTA: 1\n") + r = self.report() + self.assertIsNone(r["needrestart_ucsta"]) + self.assertIsNone(r["needrestart_uccur"]) + def write_livepatch(self, text, age=60): d = os.path.join(self.root, "run/kernelwatch") os.makedirs(d, exist_ok=True) diff --git a/tests/test_status.py b/tests/test_status.py index 37abf43..e09e5ac 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -339,6 +339,19 @@ class EvaluateTest(unittest.TestCase): self.assertEqual(self.level(report(dpkg_unfinished_packages=[])).level, "ok") self.assertEqual(self.level(report(dpkg_unfinished_packages=None)).level, "ok") + def test_microcode_update_needs_a_reboot(self): + s = self.level(report(needrestart_ucsta=2, needrestart_uccur="0x001e", needrestart_ucexp="0x0020")) + self.assertEqual(s.level, "reboot") + self.assertEqual(s.reasons, ["CPU microcode update installed but not loaded (0x001e → 0x0020)"]) + for ucsta in (0, 1, None): + with self.subTest(ucsta=ucsta): + self.assertEqual(self.level(report(needrestart_ucsta=ucsta)).level, "ok") + + def test_livepatch_doesnt_cover_microcode(self): + s = evaluate(NOW - 60, livepatched(needrestart_ucsta=2), NOW, CFG) + self.assertEqual(s.level, "reboot") + self.assertIn("CPU microcode update installed but not loaded", s.reasons) + def test_held_kernel_package_is_a_problem(self): s = self.level(report(held_packages=["linux-image-amd64"])) self.assertEqual(s.level, "update") diff --git a/tests/test_web.py b/tests/test_web.py index 62a3d6b..6a9671a 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -239,6 +239,16 @@ class DashboardTest(ServerCase): self.assertIn("noble-security: not in apt's sources", body) self.assertIn("apt has no noble-security repository", body) + def test_host_page_shows_microcode(self): + self.add_user("admin", "a-long-password") + token = self.add_host("web01") + self.post_report(token, {"kernel_running": "6.12.57+deb13-amd64", "apt_lists_age_seconds": 60, + "needrestart_ucsta": 2, "needrestart_uccur": "0x001e", + "needrestart_ucexp": "0x0020"}) + _, headers, _ = self.login("admin", "a-long-password") + body = self.request("GET", "/host/web01", headers={"Cookie": headers["Set-Cookie"].split(";")[0]})[2] + self.assertIn("update needs a reboot (loaded 0x001e, installed 0x0020)", body) + def test_host_page_shows_helper_units(self): self.add_user("admin", "a-long-password") token = self.add_host("web01")