summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-10-31 10:04:11 +0000
committerChristian Breunig <christian@breunig.cc>2026-11-01 06:53:43 +0000
commit4cfde2bafb79e54ffdb3cb1e5f93c5cd37aa8ad7 (patch)
treeea32de7a3bcbe5cdced187d12365a37412a370b8
parent140b1c1a89431f28ee2401255e006d1f4f516af2 (diff)
downloadvyos-build-4cfde2bafb79e54ffdb3cb1e5f93c5cd37aa8ad7.tar.gz
vyos-build-4cfde2bafb79e54ffdb3cb1e5f93c5cd37aa8ad7.zip
hooks: T9553: drop file capabilities while running live-build chroot hooks
vyos-1x grants cap_sys_module to kmod and cap_sys_time to date. In a build container whose capability bounding set lacks these (e.g. a default Proxmox LXC), the kernel refuses to execute both binaries, even as root. mkinitramfs ignores the failing modprobe calls and silently produces an initrd without any kernel modules - the ISO builds fine but cannot find its boot medium. The capabilities are now removed while the chroot hooks run and restored afterwards. The initramfs is regenerated before restoring them and live-build is told to skip its own final regeneration, which would otherwise run with the capabilities back in place. The build now fails if the initrd lacks the squashfs module.
-rw-r--r--data/live-build-config/hooks/live/00-drop-file-caps.chroot15
-rw-r--r--data/live-build-config/hooks/live/99-restore-file-caps.chroot31
2 files changed, 46 insertions, 0 deletions
diff --git a/data/live-build-config/hooks/live/00-drop-file-caps.chroot b/data/live-build-config/hooks/live/00-drop-file-caps.chroot
new file mode 100644
index 00000000..29612d8b
--- /dev/null
+++ b/data/live-build-config/hooks/live/00-drop-file-caps.chroot
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# vyos-1x grants file capabilities to some binaries. If the build container's
+# capability bounding set lacks them (e.g. Proxmox LXC drops sys_module and
+# sys_time), exec fails with EPERM - mkinitramfs then silently produces an
+# initrd without any kernel modules. Drop them for the duration of the hooks,
+# 99-restore-file-caps.chroot puts them back.
+
+CAPS_FILE=/tmp/vyos-file-caps
+
+getcap /usr/bin/kmod /usr/bin/date > $CAPS_FILE
+while read -r file caps; do
+ echo "I: Temporarily dropping file capabilities from $file"
+ setcap -r $file
+done < $CAPS_FILE
diff --git a/data/live-build-config/hooks/live/99-restore-file-caps.chroot b/data/live-build-config/hooks/live/99-restore-file-caps.chroot
new file mode 100644
index 00000000..2243f4cc
--- /dev/null
+++ b/data/live-build-config/hooks/live/99-restore-file-caps.chroot
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+# Restore file capabilities dropped by 00-drop-file-caps.chroot
+
+CAPS_FILE=/tmp/vyos-file-caps
+
+if [ ! -f $CAPS_FILE ]; then
+ echo "E: $CAPS_FILE missing, cannot restore file capabilities"
+ exit 1
+fi
+
+# live-build's chroot_hacks regenerates the initramfs after all hooks, which
+# would run with the capabilities restored. Do it here instead and tell
+# live-build to skip it.
+echo "I: Regenerating initramfs before restoring file capabilities"
+update-initramfs -k all -t -u || exit 1
+touch /initrd.img_has_already_been_regenerated
+
+# mkinitramfs ignores modprobe failures - without squashfs the image is unbootable
+for initrd in /boot/initrd.img-*; do
+ if ! lsinitramfs $initrd | grep -q '/squashfs\.ko'; then
+ echo "E: $initrd contains no squashfs module!"
+ exit 1
+ fi
+done
+
+while read -r file caps; do
+ echo "I: Restoring file capabilities $caps on $file"
+ setcap $caps $file || exit 1
+done < $CAPS_FILE
+rm -f $CAPS_FILE