fix(bundles): allow down unaddressed kernel tunnel interfaces in offline sandbox check
`scripts/bundles/appimage_checks.py::main` previously required
`{name for _, name in socket.if_nameindex()} == {"lo"}`. On Linux hosts
with tunnel drivers loaded or built in (`ipip`, `sit`, `ip6_tunnel`,
`ip_gre`, etc., with default `net.core.fb_tunnels_only_for_init_net=0`),
the kernel creates down, unaddressed fallback devices (`tunl0`, `sit0`,
`ip6tnl0`, `gre0`, ...) in every new network namespace, including
`docker run --network none`. That caused `scripts/ci/linux_x64_userland.sh
accept` to exit immediately on Blacksmith x64 runners (canary runs
44690154688 and 44876973873).
Verify via `SIOCGIFFLAGS`, `SIOCGIFADDR`, and `/proc/self/net/{route,
if_inet6,ipv6_route}` that `lo` is up and loopback and every other
interface is down with no IPv4/IPv6 address and no routes, failing closed
with the concrete reason on root, active/addressed/routed interfaces, or
unreadable network state.
This commit is contained in:
committed by
Bear Huddleston
parent
62595296a9
commit
13141ee4ef
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import errno
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
@@ -10,11 +11,17 @@ import re
|
||||
import select
|
||||
import shutil
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
try:
|
||||
import fcntl
|
||||
except ImportError:
|
||||
fcntl = None
|
||||
|
||||
|
||||
def check_image(image: Path, sha256: str) -> None:
|
||||
with image.open("rb") as stream:
|
||||
@@ -202,9 +209,98 @@ def check_desktop(work: Path, image: Path) -> None:
|
||||
check_execs(work, "desktop", set(), floor=True)
|
||||
|
||||
|
||||
_SIOCGIFFLAGS = 0x8913
|
||||
_SIOCGIFADDR = 0x8915
|
||||
_IFF_UP = 0x1
|
||||
_IFF_LOOPBACK = 0x8
|
||||
|
||||
|
||||
def _ifreq(name: str) -> bytes:
|
||||
raw = name.encode("ascii")
|
||||
if not raw or len(raw) > 15:
|
||||
raise ValueError(f"invalid interface name: {name!r}")
|
||||
return struct.pack("16s24x", raw)
|
||||
|
||||
|
||||
def _iface_flags(sock: socket.socket, name: str) -> int:
|
||||
if fcntl is None:
|
||||
raise OSError("fcntl is unavailable")
|
||||
packed = fcntl.ioctl(sock.fileno(), _SIOCGIFFLAGS, _ifreq(name))
|
||||
return struct.unpack_from("H", packed, 16)[0]
|
||||
|
||||
|
||||
def _iface_has_ipv4(sock: socket.socket, name: str) -> bool:
|
||||
if fcntl is None:
|
||||
raise OSError("fcntl is unavailable")
|
||||
try:
|
||||
fcntl.ioctl(sock.fileno(), _SIOCGIFADDR, _ifreq(name))
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EADDRNOTAVAIL:
|
||||
return False
|
||||
raise
|
||||
return True
|
||||
|
||||
|
||||
def _ipv6_available() -> bool:
|
||||
try:
|
||||
with socket.socket(socket.AF_INET6, socket.SOCK_DGRAM):
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _proc_net_ifaces(path: Path, column: int, *, skip_header: bool = False) -> set[str]:
|
||||
lines = path.read_text(encoding="ascii").splitlines()
|
||||
if skip_header:
|
||||
lines = lines[1:]
|
||||
return {fields[column] for line in lines if len(fields := line.split()) > column}
|
||||
|
||||
|
||||
def _non_loopback_problem(sock: socket.socket, extra: list[str], proc_net: Path) -> str | None:
|
||||
# Kernels with tunnel drivers add down, unaddressed fallback devices (tunl0, sit0, ...) to --network none.
|
||||
active = [name for name in extra if _iface_flags(sock, name) & _IFF_UP]
|
||||
if active:
|
||||
return f"non-loopback interfaces are up: {', '.join(active)}"
|
||||
addressed_v4 = [name for name in extra if _iface_has_ipv4(sock, name)]
|
||||
if addressed_v4:
|
||||
return f"non-loopback interfaces have IPv4 addresses: {', '.join(addressed_v4)}"
|
||||
extra_set = set(extra)
|
||||
routed_v4 = sorted(extra_set & _proc_net_ifaces(proc_net / "route", 0, skip_header=True))
|
||||
if routed_v4:
|
||||
return f"non-loopback IPv4 routes: {', '.join(routed_v4)}"
|
||||
if _ipv6_available():
|
||||
addressed_v6 = sorted(extra_set & _proc_net_ifaces(proc_net / "if_inet6", 5))
|
||||
if addressed_v6:
|
||||
return f"non-loopback interfaces have IPv6 addresses: {', '.join(addressed_v6)}"
|
||||
routed_v6 = sorted(extra_set & _proc_net_ifaces(proc_net / "ipv6_route", 9))
|
||||
if routed_v6:
|
||||
return f"non-loopback IPv6 routes: {', '.join(routed_v6)}"
|
||||
return None
|
||||
|
||||
|
||||
def _sandbox_problem(proc_net: Path = Path("/proc/self/net")) -> str | None:
|
||||
if not hasattr(os, "geteuid") or fcntl is None or os.geteuid() == 0:
|
||||
return "running as root or unsupported platform"
|
||||
try:
|
||||
names = {name for _, name in socket.if_nameindex()}
|
||||
if "lo" not in names:
|
||||
return f"missing loopback interface ({sorted(names)})"
|
||||
extra = sorted(names - {"lo"})
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
||||
lo_flags = _iface_flags(sock, "lo")
|
||||
if not (lo_flags & _IFF_UP) or not (lo_flags & _IFF_LOOPBACK):
|
||||
return f"loopback interface is not up (flags=0x{lo_flags:x})"
|
||||
if extra:
|
||||
return _non_loopback_problem(sock, extra, proc_net)
|
||||
except (OSError, ValueError) as exc:
|
||||
return f"cannot verify network isolation: {exc}"
|
||||
return None
|
||||
|
||||
|
||||
def main(ubuntu: str, sha256: str, *, image: Path = Path("/image.AppImage")) -> None:
|
||||
if not hasattr(os, "geteuid") or os.geteuid() == 0 or {name for _, name in socket.if_nameindex()} != {"lo"}:
|
||||
raise SystemExit("refusing to run outside the offline non-root sandbox")
|
||||
problem = _sandbox_problem()
|
||||
if problem is not None:
|
||||
raise SystemExit(f"refusing to run outside the offline non-root sandbox ({problem})")
|
||||
work = Path.cwd()
|
||||
check_image(image, sha256)
|
||||
subprocess.run([image, "--appimage-extract"], stdout=subprocess.DEVNULL, check=True, timeout=300)
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
"""Receipts for the packaged x64 acceptance lanes; no release build or namespaces needed."""
|
||||
|
||||
import errno
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
try:
|
||||
import fcntl
|
||||
except ImportError:
|
||||
fcntl = None
|
||||
|
||||
import pytest
|
||||
|
||||
from scripts.bundles import appimage_checks
|
||||
@@ -44,6 +51,112 @@ def test_worker_main_verifies_image_before_extraction(tmp_path, monkeypatch):
|
||||
assert not extracted
|
||||
|
||||
|
||||
def _make_fake_ioctl(flags_by_name, ipv4_names=()):
|
||||
def fake_ioctl(_fd, request, arg):
|
||||
name = arg[:16].split(b"\0", 1)[0].decode("ascii")
|
||||
if request == appimage_checks._SIOCGIFFLAGS:
|
||||
if name not in flags_by_name:
|
||||
raise OSError(errno.ENODEV, "No such device")
|
||||
return struct.pack("16sH22x", name.encode("ascii"), flags_by_name[name])
|
||||
if request == appimage_checks._SIOCGIFADDR:
|
||||
if name in ipv4_names:
|
||||
return arg
|
||||
raise OSError(errno.EADDRNOTAVAIL, "Cannot assign requested address")
|
||||
raise AssertionError(f"unexpected ioctl: {request:#x}")
|
||||
|
||||
return fake_ioctl
|
||||
|
||||
|
||||
def _write_proc_net(directory: Path, *, route: str = "", if_inet6: str = "", ipv6_route: str = "") -> None:
|
||||
(directory / "route").write_text(
|
||||
"Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n" + route,
|
||||
encoding="ascii",
|
||||
)
|
||||
(directory / "if_inet6").write_text(
|
||||
"00000000000000000000000000000001 01 80 10 80 lo\n" + if_inet6,
|
||||
encoding="ascii",
|
||||
)
|
||||
(directory / "ipv6_route").write_text(
|
||||
"00000000000000000000000000000001 80 00000000000000000000000000000000 00 "
|
||||
"00000000000000000000000000000000 00000000 00000002 00000000 80200001 lo\n" + ipv6_route,
|
||||
encoding="ascii",
|
||||
)
|
||||
|
||||
|
||||
def test_worker_main_allows_down_unaddressed_kernel_tunnels(tmp_path, monkeypatch):
|
||||
bad = tmp_path / "not-an-appimage"
|
||||
bad.write_bytes(b"not an elf appimage")
|
||||
monkeypatch.setattr(appimage_checks.os, "geteuid", lambda: 1000)
|
||||
monkeypatch.setattr(
|
||||
appimage_checks.socket,
|
||||
"if_nameindex",
|
||||
lambda: [(1, "lo"), (2, "tunl0"), (3, "sit0"), (4, "ip6tnl0"), (5, "gre0")],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
fcntl,
|
||||
"ioctl",
|
||||
_make_fake_ioctl({"lo": 0x9, "tunl0": 0x80, "sit0": 0x80, "ip6tnl0": 0x80, "gre0": 0x80}, {"lo"}),
|
||||
)
|
||||
extracted = []
|
||||
monkeypatch.setattr(appimage_checks.subprocess, "run", lambda *a, **kw: extracted.append(a))
|
||||
with pytest.raises(ValueError, match="type 2 AppImage"):
|
||||
appimage_checks.main("22.04", hashlib.sha256(bad.read_bytes()).hexdigest(), image=bad)
|
||||
assert not extracted
|
||||
|
||||
|
||||
def test_sandbox_check_refuses_root_active_addressed_routed_and_unreadable_network(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(appimage_checks.os, "geteuid", lambda: 0)
|
||||
assert "root" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
monkeypatch.setattr(appimage_checks.os, "geteuid", lambda: 1000)
|
||||
monkeypatch.setattr(appimage_checks.socket, "if_nameindex", lambda: [(2, "tunl0")])
|
||||
assert "loopback" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
monkeypatch.setattr(appimage_checks.socket, "if_nameindex", lambda: [(1, "lo"), (2, "eth0")])
|
||||
monkeypatch.setattr(fcntl, "ioctl", _make_fake_ioctl({"lo": 0x8, "eth0": 0x0}, {"lo"}))
|
||||
assert "loopback" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
_write_proc_net(tmp_path)
|
||||
monkeypatch.setattr(fcntl, "ioctl", _make_fake_ioctl({"lo": 0x9, "eth0": 0x1043}, {"lo"}))
|
||||
assert "eth0" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
monkeypatch.setattr(fcntl, "ioctl", _make_fake_ioctl({"lo": 0x9, "eth0": 0x1002}, {"lo", "eth0"}))
|
||||
assert "IPv4" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
monkeypatch.setattr(fcntl, "ioctl", _make_fake_ioctl({"lo": 0x9, "eth0": 0x1002}, {"lo"}))
|
||||
_write_proc_net(tmp_path, route="eth0\t00000000\t0100000A\t0003\t0\t0\t0\t00000000\t0\t0\t0\n")
|
||||
assert "IPv4 routes" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
_write_proc_net(tmp_path, if_inet6="fd000000000000000000000000000001 02 40 00 c0 eth0\n")
|
||||
monkeypatch.setattr(appimage_checks, "_ipv6_available", lambda: True)
|
||||
assert "IPv6 addresses" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
_write_proc_net(
|
||||
tmp_path,
|
||||
ipv6_route="fd000000000000000000000000000000 40 00000000000000000000000000000000 00 "
|
||||
"00000000000000000000000000000000 00000100 00000001 00000000 00000001 eth0\n",
|
||||
)
|
||||
assert "IPv6 routes" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
_write_proc_net(tmp_path)
|
||||
assert appimage_checks._sandbox_problem(tmp_path) is None
|
||||
|
||||
(tmp_path / "if_inet6").unlink()
|
||||
(tmp_path / "ipv6_route").unlink()
|
||||
assert "cannot verify" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
monkeypatch.setattr(appimage_checks, "_ipv6_available", lambda: False)
|
||||
assert appimage_checks._sandbox_problem(tmp_path) is None
|
||||
|
||||
(tmp_path / "route").unlink()
|
||||
assert "cannot verify" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
def fail_ioctl(_fd, _request, _arg):
|
||||
raise PermissionError("blocked")
|
||||
|
||||
monkeypatch.setattr(fcntl, "ioctl", fail_ioctl)
|
||||
assert "cannot verify" in (appimage_checks._sandbox_problem(tmp_path) or "")
|
||||
|
||||
|
||||
def _engine(backend):
|
||||
return Path(f"/work/payload/tools/llamacpp-{backend}-123-linux-x64/llama-server")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user