Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packaging/linux-host/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def main():
and not item.name.endswith(".test.mjs") and item.name != "logo.png"):
shutil.copyfile(item, target / item.name)
(app / "runtime").mkdir()
shutil.copy2(node, app / "runtime/node")
shutil.copyfile(node, app / "runtime/node")
(app / "runtime/node").chmod(0o755)
notices = app / "licenses"
notices.mkdir()
shutil.copyfile(node.parents[1] / "LICENSE", notices / "Node-LICENSE.txt")
Expand Down Expand Up @@ -78,7 +79,13 @@ def main():
for name in ("preinst", "prerm"):
shutil.copyfile(HERE / "check-stopped", control / name)
os.chmod(control / name, 0o755)
# CI tool caches can contain group- or world-writable binaries. Set package permissions
# independently of the build account's umask and of the original tool permissions.
for item in [package, *package.rglob("*")]:
if not item.is_symlink():
item.chmod(0o755 if item.is_dir() or item.stat().st_mode & 0o100 else 0o644)
subprocess.run(["dpkg-deb", "--root-owner-group", "-Zzstd", "-z3", "--build", package, deb], check=True)
subprocess.run([sys.executable, "-I", HERE / "verify_files.py", deb], check=True)


if __name__ == "__main__":
Expand Down
57 changes: 57 additions & 0 deletions packaging/linux-host/verify_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Check the files and access modes dpkg will actually install."""

import posixpath
import subprocess
import sys
import tarfile
from pathlib import Path, PurePosixPath

APP = PurePosixPath("usr/lib/etherlighting-host")
SYSTEM_FILES = {
PurePosixPath("usr/lib/systemd/system/etherlighting-host.service"),
PurePosixPath("usr/lib/systemd/system/etherlighting-host.socket"),
PurePosixPath("usr/share/applications/etherlighting-rack.desktop"),
}
PARENT_DIRS = {parent for path in SYSTEM_FILES | {APP} for parent in path.parents}
EXECUTABLES = {APP / name for name in ("runtime/node", "engine/EEEngine", "client/EtherlightingEvolved")}


def check(entry):
path = PurePosixPath(entry.name)
if path.is_absolute() or ".." in path.parts:
raise ValueError(f"Invalid package path: {path}")
if not (path.is_relative_to(APP) or (entry.isdir() and path in PARENT_DIRS) or path in SYSTEM_FILES):
raise ValueError(f"Unexpected installation path: {path}")
if entry.uid or entry.gid:
raise ValueError(f"Expected root ownership: {path}")
if path in SYSTEM_FILES | EXECUTABLES and not entry.isfile():
raise ValueError(f"Expected a regular file: {path}")
if entry.issym() or entry.islnk():
base = path.parent if entry.issym() else PurePosixPath(".")
target = PurePosixPath(posixpath.normpath(str(base / entry.linkname)))
if not path.is_relative_to(APP) or not target.is_relative_to(APP):
raise ValueError(f"Invalid package link: {path}")
elif entry.isfile() or entry.isdir():
if entry.mode & 0o7022 or (entry.isdir() or path in EXECUTABLES) and entry.mode != 0o755:
raise ValueError(f"Unsafe package mode {entry.mode:o}: {path}")
else:
raise ValueError(f"Unexpected package file type: {path}")
return path


def verify(package):
seen = set()
with subprocess.Popen(["dpkg-deb", "--fsys-tarfile", package], stdout=subprocess.PIPE) as reader:
try:
with tarfile.open(fileobj=reader.stdout, mode="r|") as archive:
for member in archive:
seen.add(check(member))
finally:
reader.stdout.close()
if reader.wait() or not (SYSTEM_FILES | EXECUTABLES).issubset(seen):
raise ValueError("Incomplete rack host package.")
print("Verified installed paths, ownership, executable modes and package links.")


if __name__ == "__main__":
verify(Path(sys.argv[1]))
30 changes: 30 additions & 0 deletions tests/test_linux_host_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import runpy
import tarfile
from pathlib import Path

import pytest

check = runpy.run_path(
str(Path(__file__).resolve().parents[1] / "packaging/linux-host/verify_files.py")
)["check"]


@pytest.mark.parametrize("mode", [0o777, 0o775, 0o6755, 0o644])
def test_rejects_unsafe_runtime_executable(mode):
node = tarfile.TarInfo("./usr/lib/etherlighting-host/runtime/node")
node.mode = mode
with pytest.raises(ValueError, match="mode"):
check(node)
node.mode = 0o755
assert check(node).name == "node"


def test_package_cannot_install_ssh_configuration_or_links_to_it():
entry = tarfile.TarInfo("./etc/ssh/sshd_config")
with pytest.raises(ValueError, match="installation path"):
check(entry)
entry.name = "usr/lib/etherlighting-host/engine/ssh-config"
entry.type = tarfile.SYMTYPE
entry.linkname = "/etc/ssh/sshd_config"
with pytest.raises(ValueError, match="package link"):
check(entry)
Loading