diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5ba321130..ddcdc28c0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -156,8 +156,16 @@ jobs: # the usb tree gets recursive grants plus a default ACL that # newly created nodes inherit (the Coral re-enumeration path) docker exec frigate sh -c 'mkdir -p /dev/bus/usb/001 && mknod /dev/bus/usb/001/002 c 189 1' + docker exec frigate sh -c 'mkdir -p /opt/link-target && ln -s /opt/link-target /dev/bus/usb/001/link' docker exec frigate sh -c 'export PATH=/command:$PATH; exec /etc/s6-overlay/s6-rc.d/init-devices/run' docker exec frigate getfacl -p /dev/bus/usb/001 | grep -q "user:frigate:rwx" + # a symlink in the usb tree is never followed + target_acl=$(docker exec frigate getfacl -p /opt/link-target) + if grep -q "user:frigate" <<< "$target_acl"; then + echo "init-devices followed a symlink under /dev/bus/usb"; exit 1 + fi + # a fifo at the sentinel path is rejected at once instead of hanging the boot + docker exec frigate sh -c 'mkfifo /tmp/sentinel-fifo && timeout 5 /usr/local/bin/safe-sentinel read /tmp/sentinel-fifo; test $? -eq 1' docker exec frigate sh -c 'mknod /dev/bus/usb/001/099 c 189 98 && chmod 664 /dev/bus/usb/001/099' inherited=$(docker exec frigate getfacl -p /dev/bus/usb/001/099) echo "$inherited" @@ -244,6 +252,9 @@ jobs: done if [ "$up" -ne 1 ]; then echo "granular container never came back after restart"; docker logs frigate-granular; exit 1; fi docker exec frigate-granular stat -c %u /media/frigate/clips/straggler.webp | grep -qx 1000 + # the per-boot /config sweep leaves the sentinel root-owned, so the restart skips the full sweep + docker exec frigate-granular stat -c %u /config/.permissions_version | grep -qx 0 + docker logs frigate-granular 2>&1 | grep -q "already applied, skipping" docker rm -f frigate-granular - name: Assert unknown root service fails fast run: | diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/init-devices/run b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/init-devices/run index 44f3b400ed..c2e45cc0d7 100755 --- a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/init-devices/run +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/init-devices/run @@ -105,12 +105,14 @@ done # node), so the directories also get a default ACL new nodes inherit. The # inherited grant is clamped by the creating mode's group bits, which is rw on # udev hosts (0664) and nothing on raw devtmpfs (0600); hardware-verified. +# The tree may be a host devtmpfs bind mount the runtime users can write, so +# only real directories and device nodes are granted and links are never followed if [[ -d /dev/bus/usb ]]; then while IFS= read -r -d '' node; do grant "$node" - done < <(find /dev/bus/usb -mindepth 1 -print0) + done < <(find /dev/bus/usb -mindepth 1 \( -type c -o -type d \) -print0) while IFS= read -r -d '' dir; do - setfacl -d -m "u:frigate:rw,u:go2rtc:rw" "$dir" 2>/dev/null || \ + setfacl -P -d -m "u:frigate:rw,u:go2rtc:rw" "$dir" 2>/dev/null || \ echo "[WARN] could not set a default ACL on ${dir}; a re-enumerating USB device may lose access" done < <(find /dev/bus/usb -type d -print0) fi diff --git a/docker/main/rootfs/usr/local/bin/fix-ownership b/docker/main/rootfs/usr/local/bin/fix-ownership index 7649a208b1..787779a1ba 100755 --- a/docker/main/rootfs/usr/local/bin/fix-ownership +++ b/docker/main/rootfs/usr/local/bin/fix-ownership @@ -88,22 +88,19 @@ swept_clean=1 # Entries another mechanism deliberately owns. Chowning them undoes that work # and leaves the same "mismatch" waiting for the next boot, so /config could # never report itself clean: /config is chgrp'd to frigate-data below so go2rtc -# can traverse it, and the HomeKit file is handed to the go2rtc user by the -# go2rtc service. Only the GROUP on /config is exempt; a root-owned /config -# must still be chowned or the runtime user cannot write there at all. +# can traverse it, the HomeKit file is handed to the go2rtc user by the go2rtc +# service, and safe-sentinel only trusts a root-owned sentinel. The sentinel is +# excluded on every call, including the granular per-boot /config sweep that +# runs without --sentinel. Only the GROUP on /config is exempt; a root-owned +# /config must still be chowned or the runtime user cannot write there at all. # Shared by the counting and the chowning walk so the two cannot disagree. mismatch_expr=( "(" -not -uid "$target_uid" -o "(" -not -gid "$target_gid" -a ! -path /config ")" ")" -a ! -path /config/go2rtc_homekit.yml + -a ! -path /config/.permissions_version ) -if [[ -n "$sentinel" ]]; then - # safe-sentinel keeps the sentinel root-owned on purpose and rejects one - # owned by anybody else, so chowning it here would suppress the skip and - # make every boot re-sweep. Only the trailing write puts it back today. - mismatch_expr+=(-a ! -path "$sentinel") -fi exec 3>&1 diff --git a/docker/main/rootfs/usr/local/bin/safe-sentinel b/docker/main/rootfs/usr/local/bin/safe-sentinel index 13fe5ab1f4..72f3c90e9a 100755 --- a/docker/main/rootfs/usr/local/bin/safe-sentinel +++ b/docker/main/rootfs/usr/local/bin/safe-sentinel @@ -20,7 +20,8 @@ MODE = 0o644 def do_read(path: str) -> int: try: - fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW) + # O_NONBLOCK so a fifo opens at once and fails the regular-file check + fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) except OSError: return 1 try: