Don't run config ffmpeg as root (#24891)

* don't run a config ffmpeg.path as root at boot

The frigate and go2rtc run scripts probed "$ffmpeg_path" -version as root before dropping privileges, and resolve_ffmpeg_path only refused runtime-user-writable paths when frigate itself was a root service. In the default mode uid 1000 could set ffmpeg.path to a /config directory, plant bin/ffmpeg there, and get root on the next restart, and under FRIGATE_ROOT_SERVICES=go2rtc a root go2rtc used that path as its ffmpeg bin. The probe now runs as the service user whenever the service drops privileges, and resolve_ffmpeg_path refuses writable paths for whichever service will run the binary, returning the resolved path so a symlink can't be retargeted after the check. go2rtc falls back to the bundled build's libavformat version if its user can't run the custom build.

* resolve the full ffmpeg path under granular root
This commit is contained in:
Josh Hawkins
2026-11-08 07:15:02 -07:00
committed by GitHub
parent d9ce4b061b
commit b55723146d
8 changed files with 176 additions and 32 deletions
+24 -15
View File
@@ -46,14 +46,14 @@ DROPPED_DETECTOR_OPTIONS = {
}
# Trees the unprivileged runtime user can write. A root frigate service must
# not execute a binary from any of them; a compromised uid-1000 process could
# Trees the unprivileged runtime user can write. A root service must not
# execute a binary from any of them; a compromised uid-1000 process could
# plant one and be root after the next restart.
RUNTIME_USER_WRITABLE_DIRS = (CONFIG_DIR, BASE_DIR, CACHE_DIR, "/dev/shm", "/tmp")
def frigate_service_is_granular_root() -> bool:
"""Report whether FRIGATE_ROOT_SERVICES runs frigate as root.
def service_is_granular_root(service: str) -> bool:
"""Report whether FRIGATE_ROOT_SERVICES runs the given service as root.
The escape hatch is excluded: it never sweeps /config and leaves no
unprivileged service running, so custom binaries stay as safe as they
@@ -66,7 +66,7 @@ def frigate_service_is_granular_root() -> bool:
return False
entries = os.environ.get("FRIGATE_ROOT_SERVICES", "").split(",")
return any("".join(entry.split()) == "frigate" for entry in entries)
return any("".join(entry.split()) == service for entry in entries)
def is_runtime_user_writable(path: str) -> bool:
@@ -79,11 +79,12 @@ def is_runtime_user_writable(path: str) -> bool:
@cache
def _warn_ignored_ffmpeg_path(path: str) -> None:
def _warn_ignored_ffmpeg_path(path: str, service: str) -> None:
"""Warn once per path; resolution runs per camera and per binary."""
logger.warning(
"Ignoring ffmpeg.path %s because FRIGATE_ROOT_SERVICES runs frigate as root and that location is writable by the unprivileged user; using the bundled build",
"Ignoring ffmpeg.path %s because FRIGATE_ROOT_SERVICES runs %s as root and that location is writable by the unprivileged user; using the bundled build",
path,
service,
)
@@ -97,16 +98,19 @@ def is_valid_ffmpeg_path(path: str) -> bool:
return not path.startswith("/") or _CUSTOM_FFMPEG_PATH.fullmatch(path) is not None
def resolve_ffmpeg_path(path: str, binary: str = "ffmpeg") -> str:
def resolve_ffmpeg_path(
path: str, binary: str = "ffmpeg", service: str = "frigate"
) -> str:
"""Resolve an ffmpeg version alias or custom path to a binary path.
A bare version alias that is no longer bundled (for example one that was
dropped when the default version changed) falls back to the default
bundled version so existing configs keep working across an upgrade or a
revert. Custom install paths (anything absolute) are used as-is, unless
they fail `is_valid_ffmpeg_path` (which falls back to the default), or
they are in a runtime-user-writable tree while FRIGATE_ROOT_SERVICES
makes frigate root; see RUNTIME_USER_WRITABLE_DIRS.
revert. Custom install paths (anything absolute) are used as-is. While
FRIGATE_ROOT_SERVICES makes the service that execs it root, a custom path
is resolved to its real location and refused inside a runtime-user-writable
tree; see RUNTIME_USER_WRITABLE_DIRS. Paths that fail
`is_valid_ffmpeg_path` also fall back to the default.
"""
if (
path == "default"
@@ -116,11 +120,16 @@ def resolve_ffmpeg_path(path: str, binary: str = "ffmpeg") -> str:
version = DEFAULT_FFMPEG_VERSION
elif path in INCLUDED_FFMPEG_VERSIONS:
version = path
elif not service_is_granular_root(service):
return f"{path}/bin/{binary}"
else:
if not (frigate_service_is_granular_root() and is_runtime_user_writable(path)):
return f"{path}/bin/{binary}"
# return the resolved form so a link in the path cannot be retargeted
# after the check
real = os.path.realpath(f"{path}/bin/{binary}")
if is_valid_ffmpeg_path(real) and not is_runtime_user_writable(real):
return real
_warn_ignored_ffmpeg_path(path)
_warn_ignored_ffmpeg_path(path, service)
version = DEFAULT_FFMPEG_VERSION
return f"/usr/lib/ffmpeg/{version}/bin/{binary}"