Files
NODEDC_MISSION_CORE/plugins/vesc/packaging/native_check.py
T

35 lines
1.8 KiB
Python

"""Installer acceptance under the service account; never opens a USB device."""
import hashlib
import json
import os
from pathlib import Path
import subprocess
import tempfile
def check(root):
manifest = json.loads((root / "manifest.json").read_text())
for name, expected in manifest["files"].items():
path = root / name
if path.is_symlink() or not path.resolve().is_relative_to(root.resolve()):
raise RuntimeError("Untrusted native runtime path")
if path.stat().st_size != expected["bytes"] or hashlib.sha256(path.read_bytes()).hexdigest() != expected["sha256"]:
raise RuntimeError("Native runtime integrity check failed")
with tempfile.TemporaryDirectory(prefix="mission-core-vesc-check-") as temporary:
env = {"PATH":"/usr/bin:/bin", "LANG":"C.UTF-8", "QT_QPA_PLATFORM":"offscreen",
"LD_LIBRARY_PATH":str(root / "lib"), "QT_PLUGIN_PATH":str(root / "plugins"),
"XDG_CONFIG_HOME":temporary, "XDG_CACHE_HOME":temporary}
result = subprocess.run([str(root / "bin/mission-core-vesc-engine"), "--offline"],
env=env, input=b'{"id":1,"method":"engine"}\n', capture_output=True, timeout=15, check=True)
replies = [json.loads(line) for line in result.stdout.splitlines()]
if (len(replies) != 2 or not replies[0]["ready"] or not replies[1]["ok"]
or replies[1]["result"]["hardware_enabled"] or replies[1]["result"]["connected"]
or replies[1]["result"]["commit"] != manifest["upstream_commit"]):
raise RuntimeError("Native engine offline check failed")
print(json.dumps({"ok":True, "upstream_commit":manifest["upstream_commit"], "hardware_access":False}))
if __name__ == "__main__":
if os.geteuid() == 0: raise RuntimeError("Run as the VESC service account")
check(Path(__file__).resolve().parent / "native")