mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-10-18 07:29:45 +00:00
* Run the frigate service as the frigate user * Run go2rtc as its own restricted user * Run nginx as the frigate user with writable state in /tmp/nginx * Disable bandwidth stats gracefully when not running as root * Hand TensorRT model cache ownership to the runtime user * Document non-root operation and per-hardware device access * Create /media/frigate after the ownership sweep * Assert non-root services, JWT migration, and escape hatch in CI * only write the sweep sentinel when a media volume is mounted * tolerate homekit config chown failures in the go2rtc run script * chown the s6 log pipe so non-root nginx can reopen /dev/stdout * set HOME to /config for non-root services * run smoke nginx -t and the write probe as the runtime user * re-own the nginx shm cache on service restart * discard stdout for the unprivileged smoke nginx -t * unwrap hard-wrapped prose in the installation docs * report progress during the ownership sweep * document EXTRA_GROUPS as the only device access path for dropped services * expand the non-root device access docs with diagnosis steps and udev rules * document network storage ownership and the remaining detector hardware * skip lost+found during the ownership sweep * hand /tmp/cache to the runtime user before services start * make bundled models readable by the runtime user * reload nginx by signaling the master instead of parsing its config as root * harden root writes into unprivileged-owned paths Restrict the sweep sentinel to a mount at or below /media/frigate so a parent /media mount cannot bless a later-shadowed volume. Rebuild /tmp/nginx root-owned each start so root's cp and tempio writes cannot follow a symlink an unprivileged nginx planted in the previous run. * collapse the duplicated sentinel comment * add a service-runs-as-root helper for granular root services * validate FRIGATE_ROOT_SERVICES and fail fast on unknown names * let services listed in FRIGATE_ROOT_SERVICES skip the privilege drop * record the root-services mode in the sentinel and sweep small trees each boot * cache the runtime ids in the ownership helper * chown recordings, previews, and exports to the runtime user at create * chown the database files after init * recommend FRIGATE_ROOT_SERVICES in the bandwidth stats warning * assert granular root services in CI * document FRIGATE_ROOT_SERVICES * own every directory level created for a recording segment * clear the cached runtime ids when ownership tests finish * skip missing media paths in the per-boot ownership sweep * clarify granular root services docs * clean up * install acl for device access grants * grant runtime users access to mapped device nodes at boot * assert device access grants in CI * document automatic device access grants * stop telling users device access needs host side setup * clarify the non-root docs * link the migration script to the repo * group the manual device setup under one section * harden against symlink attacks /config is owned by the unprivileged runtime user after the ownership sweep, so root operations on files there could be redirected by a planted symlink. - go2rtc HomeKit setup: replace the root yq/jq normalization and chown with an O_NOFOLLOW helper (prepare_homekit.py), so a symlink at go2rtc_homekit.yml can't redirect a root write or chown onto another file - go2rtc binary override: ignore /config/go2rtc whenever the service runs as root, so a planted binary can't exec as root under FRIGATE_ROOT_SERVICES - sweep sentinel: read and write it through safe-sentinel, which trusts only a root-owned regular file and never follows a symlink, so it can't be forged to skip the migration or symlinked to clobber a root file - ownership sweep: chown with -execdir so a parent directory swapped for a symlink mid-walk can't redirect the chown out of the volume - validate inputs: restrict DEVICE_ACL_PATHS to /dev, require nonzero numeric EXTRA_GROUPS, and reject PUID/PGID that collide with the go2rtc ids - docs: correct the TLS key ownership note to match what actually happens * tweak docs * stop the ownership sweep chasing entries other mechanisms own * keep custom binaries out of root services only under granular root
155 lines
5.5 KiB
Python
155 lines
5.5 KiB
Python
"""Tests for custom ffmpeg path resolution and the root-mode guard."""
|
|
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from frigate.const import DEFAULT_FFMPEG_VERSION
|
|
from frigate.util.config import (
|
|
_warn_ignored_ffmpeg_path,
|
|
frigate_service_is_granular_root,
|
|
resolve_ffmpeg_path,
|
|
)
|
|
|
|
BUNDLED = f"/usr/lib/ffmpeg/{DEFAULT_FFMPEG_VERSION}/bin/ffmpeg"
|
|
CUSTOM = "/config/custom-ffmpeg"
|
|
|
|
|
|
class TestConfigFfmpegRootGuard(unittest.TestCase):
|
|
"""A user-writable ffmpeg must not run as root under FRIGATE_ROOT_SERVICES."""
|
|
|
|
def setUp(self) -> None:
|
|
# the warning is memoized so it fires once per path, not per camera
|
|
_warn_ignored_ffmpeg_path.cache_clear()
|
|
|
|
def _resolve(self, path: str, *, euid: int, env: dict, binary: str = "ffmpeg"):
|
|
with (
|
|
patch("os.geteuid", return_value=euid),
|
|
patch.dict("os.environ", env, clear=True),
|
|
):
|
|
return resolve_ffmpeg_path(path, binary)
|
|
|
|
def test_custom_path_used_when_service_is_unprivileged(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve(CUSTOM, euid=1000, env={}), f"{CUSTOM}/bin/ffmpeg"
|
|
)
|
|
|
|
def test_escape_hatch_keeps_working_exactly_as_before(self) -> None:
|
|
# FRIGATE_RUN_AS_ROOT never sweeps /config and leaves no unprivileged
|
|
# service, so a custom build there is as safe as it was pre-drop
|
|
self.assertEqual(
|
|
self._resolve(CUSTOM, euid=0, env={"FRIGATE_RUN_AS_ROOT": "true"}),
|
|
f"{CUSTOM}/bin/ffmpeg",
|
|
)
|
|
|
|
def test_custom_path_ignored_when_frigate_is_a_root_service(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve(CUSTOM, euid=0, env={"FRIGATE_ROOT_SERVICES": "frigate"}),
|
|
BUNDLED,
|
|
)
|
|
|
|
def test_ffprobe_is_guarded_too(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve(
|
|
CUSTOM,
|
|
euid=0,
|
|
env={"FRIGATE_ROOT_SERVICES": "frigate"},
|
|
binary="ffprobe",
|
|
),
|
|
f"/usr/lib/ffmpeg/{DEFAULT_FFMPEG_VERSION}/bin/ffprobe",
|
|
)
|
|
|
|
def test_another_root_service_does_not_trigger_the_guard(self) -> None:
|
|
# go2rtc running as root says nothing about who spawns ffmpeg
|
|
self.assertEqual(
|
|
self._resolve(CUSTOM, euid=0, env={"FRIGATE_ROOT_SERVICES": "go2rtc"}),
|
|
f"{CUSTOM}/bin/ffmpeg",
|
|
)
|
|
|
|
def test_root_services_has_no_effect_under_docker_user(self) -> None:
|
|
# docker's own user: means the service never had root to keep, which is
|
|
# also the case for get_ffmpeg_path.py in a --user container
|
|
self.assertEqual(
|
|
self._resolve(CUSTOM, euid=1000, env={"FRIGATE_ROOT_SERVICES": "frigate"}),
|
|
f"{CUSTOM}/bin/ffmpeg",
|
|
)
|
|
|
|
def test_path_outside_config_is_left_alone(self) -> None:
|
|
# only /config is runtime-user-owned; a root-owned tree stays usable
|
|
self.assertEqual(
|
|
self._resolve(
|
|
"/opt/custom-ffmpeg", euid=0, env={"FRIGATE_ROOT_SERVICES": "frigate"}
|
|
),
|
|
"/opt/custom-ffmpeg/bin/ffmpeg",
|
|
)
|
|
|
|
def test_traversal_out_of_config_does_not_evade_the_guard(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve(
|
|
"/config/../config/custom-ffmpeg",
|
|
euid=0,
|
|
env={"FRIGATE_ROOT_SERVICES": "frigate"},
|
|
),
|
|
BUNDLED,
|
|
)
|
|
|
|
def test_media_tree_is_guarded_too(self) -> None:
|
|
# config.yml is uid-1000-writable, so ffmpeg.path can be pointed at any
|
|
# writable tree; /config alone would be an evasion, not a guard
|
|
self.assertEqual(
|
|
self._resolve(
|
|
"/media/frigate/evil", euid=0, env={"FRIGATE_ROOT_SERVICES": "frigate"}
|
|
),
|
|
BUNDLED,
|
|
)
|
|
|
|
def test_config_dir_itself_is_guarded(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve("/config", euid=0, env={"FRIGATE_ROOT_SERVICES": "frigate"}),
|
|
BUNDLED,
|
|
)
|
|
|
|
def test_default_alias_is_unaffected(self) -> None:
|
|
self.assertEqual(
|
|
self._resolve("default", euid=0, env={"FRIGATE_ROOT_SERVICES": "frigate"}),
|
|
BUNDLED,
|
|
)
|
|
|
|
|
|
class TestFrigateServiceIsGranularRoot(unittest.TestCase):
|
|
"""Root via FRIGATE_ROOT_SERVICES only, never via the escape hatch."""
|
|
|
|
def _check(self, *, euid: int, env: dict) -> bool:
|
|
with (
|
|
patch("os.geteuid", return_value=euid),
|
|
patch.dict("os.environ", env, clear=True),
|
|
):
|
|
return frigate_service_is_granular_root()
|
|
|
|
def test_false_without_any_root_signal(self) -> None:
|
|
self.assertFalse(self._check(euid=0, env={}))
|
|
|
|
def test_escape_hatch_is_not_granular_root(self) -> None:
|
|
# the escape hatch restores old behavior wholesale, sweep included
|
|
self.assertFalse(self._check(euid=0, env={"FRIGATE_RUN_AS_ROOT": "true"}))
|
|
self.assertFalse(
|
|
self._check(
|
|
euid=0,
|
|
env={"FRIGATE_RUN_AS_ROOT": "true", "FRIGATE_ROOT_SERVICES": "frigate"},
|
|
)
|
|
)
|
|
|
|
def test_membership_ignores_whitespace_and_other_entries(self) -> None:
|
|
self.assertTrue(
|
|
self._check(euid=0, env={"FRIGATE_ROOT_SERVICES": "go2rtc, frigate"})
|
|
)
|
|
self.assertFalse(
|
|
self._check(euid=0, env={"FRIGATE_ROOT_SERVICES": "go2rtc,nginx"})
|
|
)
|
|
|
|
def test_substring_of_a_service_name_does_not_match(self) -> None:
|
|
self.assertFalse(self._check(euid=0, env={"FRIGATE_ROOT_SERVICES": "frigatee"}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|