Run the agent unprivileged; detect in-place kernel upgrades

The agent ran as (capability-less) root only so needrestart could read
Ubuntu's root-only /boot images. Switch the unit to DynamicUser=yes and
make needrestart optional.

To cover the case needrestart was catching (running kernel's package
upgraded in place, same uname -r), the agent now reports the installed
package version of the running kernel and the server compares it with
the build in uname -v: "Debian 6.12.57-1" vs 6.12.57-1 on Debian,
"#85-Ubuntu" / "#33~24.04.1-Ubuntu" vs the upload part of 6.8.0-85.85 /
6.14.0-33.33~24.04.1 on Ubuntu. Unrecognised builds fall back to
needrestart and the newest-installed comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-17 10:19:41 -04:00
1 parent 07749b01b0
commit 48f2d155af
9 files changed
+109 -23

No files matched your search

-4
View File
@@ -33,10 +33,6 @@ esac
if ! command -v curl >/dev/null; then
apt-get install -y --no-install-recommends curl ca-certificates
fi
if ! command -v needrestart >/dev/null; then
echo "note: needrestart is not installed; kernel ABI-compatible upgrades won't be detected." >&2
echo " Install it with: apt-get install needrestart" >&2
fi
install -d -m 0755 /etc/kernelwatch
install -m 0755 "$here/kernelwatch-agent" /usr/local/sbin/kernelwatch-agent
+11 -1
View File
@@ -114,8 +114,17 @@ installed_kernels() {
sort -uV || true
}
# Package version of the image for the running kernel (e.g. 6.12.57-1). The
# server compares it with the build in `uname -v` to spot in-place upgrades.
running_kernel_pkg_version() {
dpkg-query -W -f='${db:Status-Abbrev} ${Version}\n' \
"linux-image-$1" "linux-image-unsigned-$1" 2>/dev/null |
awk '$1 == "ii" { print $2; exit }' || true
}
# Sets NR_KSTA / NR_KEXP from `needrestart -b`. KSTA: 0 unknown, 1 current,
# 2 ABI-compatible upgrade pending, 3 version upgrade pending.
# 2 ABI-compatible upgrade pending, 3 version upgrade pending. Optional: as a
# normal user it can't read Ubuntu's root-only /boot images and reports 0.
NR_KSTA=
NR_KEXP=
read_needrestart() {
@@ -186,6 +195,7 @@ build_report() {
printf '"os_pretty_name":%s,' "$(json_str "$(os_release_field PRETTY_NAME)")"
printf '"kernel_running":%s,' "$(json_str "$running")"
printf '"kernel_build":%s,' "$(json_str "$(uname -v)")"
printf '"kernel_running_pkg_version":%s,' "$(json_str "$(running_kernel_pkg_version "$running")")"
printf '"kernels_installed":%s,' "$(json_str_array <<<"$kernels")"
printf '"kernel_newest_installed":%s,' "$(json_str "$newest")"
printf '"needrestart_ksta":%s,' "$(json_int "$NR_KSTA")"
+3 -4
View File
@@ -8,10 +8,9 @@ Type=oneshot
ExecStart=/usr/local/sbin/kernelwatch-agent
LoadCredential=token:/etc/kernelwatch/agent.token
# Runs as root only so needrestart can read the root-only kernel images in
# /boot (mode 0600 on Ubuntu). It holds no capabilities and sees a read-only
# filesystem.
User=root
# Everything the agent reads is world-readable, so it runs as a throwaway
# unprivileged user. The token credential is made readable to that user.
DynamicUser=yes
CapabilityBoundingSet=
AmbientCapabilities=
NoNewPrivileges=yes