Fix kernelwatch-apt-update failing on apt 2.7+

With User=root, NoNewPrivileges=yes and any seccomp-based sandbox option
together, apt 2.7+ (Ubuntu 24.04+, Debian 13) can't drop to _apt:

  E: seteuid 42 failed - seteuid (1: Operation not permitted)
  E: Method http has died unexpectedly!

so apt-get update exited 100 on every run on 10 of 13 hosts. The '-'
prefix hid it, and apt touches lists/partial even when it fails, so the
agent's list age looked fresh. Only hosts with older apt (22.04, Debian
12) refreshed. On Ubuntu apt-daily kept the lists fresh regardless; a
Debian 13 host relying on this unit alone had lists three days old.

Root is the default, so drop User=root. Let a failed update fail the
unit (the agent only Wants= it, so the report still goes), and touch
/var/lib/apt/periodic/update-success-stamp, the stamp Ubuntu writes after
each successful apt update, only when it succeeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-01 14:52:36 -05:00
1 parent 233645c738
commit 195fd035e0
2 files changed
+26 -6

No files matched your search

+14 -5
View File
@@ -10,16 +10,25 @@ After=network-online.target
[Service]
Type=oneshot
# Same as `apt-get update`: downloads lists, installs nothing. A failure (no
# network, lock held by an admin's apt run) mustn't stop the report; the
# dashboard flags lists that stay old.
ExecStart=-/usr/bin/apt-get -q update
# network, lock held by an admin's apt run) fails this unit but not the report,
# since kernelwatch-agent.service only Wants= it. The stamp then stops moving
# and the dashboard flags the lists as old.
ExecStart=/usr/bin/apt-get -q update
# Reached only when apt succeeded. It's the stamp Ubuntu's update-notifier hook
# writes after every successful apt update, and the agent reads it as the lists'
# age; Debian has nothing else that writes it. lists/partial, the agent's last
# resort, changes even when the update fails.
ExecStartPost=/bin/sh -c 'mkdir -p /var/lib/apt/periodic && touch /var/lib/apt/periodic/update-success-stamp'
TimeoutStartSec=5min
Nice=10
IOSchedulingClass=idle
# apt needs root to write /var/lib/apt and drops to _apt for downloads, so this
# keeps root but loses everything apt update doesn't touch.
User=root
# runs as root (the default) but loses everything apt update doesn't touch.
# Don't add User=root: with it, NoNewPrivileges and any of the seccomp-based
# options below, apt 2.7+ (Ubuntu 24.04, Debian 13) can't switch to _apt
# ("seteuid 42 failed - seteuid (1: Operation not permitted)") and every update
# fails.
NoNewPrivileges=yes
ProtectHome=yes
PrivateTmp=yes
+12 -1
View File
@@ -310,11 +310,22 @@ class PackagingTest(unittest.TestCase):
def test_apt_update_is_opt_in_and_runs_before_reports(self):
unit = self.read("kernelwatch-apt-update.service")
self.assertIn("ConditionPathExists=/etc/kernelwatch/apt-update", unit)
self.assertIn("ExecStart=-/usr/bin/apt-get -q update", unit, "failure mustn't block the report")
self.assertIn("\nExecStart=/usr/bin/apt-get -q update\n", unit,
"a failed update must fail the unit, so ExecStartPost doesn't mark it fresh")
post = next(l for l in unit.splitlines() if l.startswith("ExecStartPost="))
self.assertIn("touch /var/lib/apt/periodic/update-success-stamp", post)
agent = self.read("kernelwatch-agent.service")
for key in ("Wants=", "After="):
line = next(l for l in agent.splitlines() if l.startswith(key))
self.assertIn("kernelwatch-apt-update.service", line)
self.assertNotRegex(agent, r"(?m)^(Requires|Requisite|BindsTo)=.*kernelwatch-apt-update",
"a failed apt update mustn't stop the report")
def test_apt_update_runs_as_root_without_user_root(self):
# User=root plus NoNewPrivileges plus any seccomp-based option stops apt 2.7+
# dropping to _apt, so every update fails. Root is the default anyway.
unit = self.read("kernelwatch-apt-update.service")
self.assertNotRegex(unit, r"(?m)^(User|DynamicUser)=")
if __name__ == "__main__":