codesign reads the cdhash of the slice it picks, the host's native arch by default. The guest runs x86_64 and the kernel gates on the x86_64 slice's cdhash. Today the bundle is a thin x86_64 build so they agree, but a fat bundle packed on an arm64 Mac would embed the arm64 slice's hash and the gate would reject the guest's x86_64 slice -- a silent "bundle won't load". Pass --arch x86_64 so the packer always reads the slice the guest loads.
103 lines
4.2 KiB
Python
Executable File
103 lines
4.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Pack a signed reims-graphics.bundle directory into a C header RgfxGate embeds.
|
|
|
|
Emits the bundle's files as byte arrays, the directory list (parents first), and
|
|
the executable's 20-byte code-directory hash (cdhash) that the kext gates
|
|
library-validation and file-map-executable on. Run on a Mac (uses codesign to
|
|
read the cdhash). Output is build output, never committed.
|
|
|
|
pack-payload.py BUNDLE_DIR OUT_HEADER
|
|
"""
|
|
import os, sys, subprocess
|
|
|
|
def die(m):
|
|
sys.stderr.write("pack-payload: " + m + "\n"); sys.exit(2)
|
|
|
|
if len(sys.argv) != 3:
|
|
die("usage: pack-payload.py BUNDLE_DIR OUT_HEADER")
|
|
bundle, out = sys.argv[1], sys.argv[2]
|
|
if not os.path.isdir(bundle):
|
|
die("no bundle dir " + bundle)
|
|
name = os.path.basename(bundle.rstrip("/"))
|
|
if not name.endswith(".bundle"):
|
|
die("BUNDLE_DIR must be a .bundle directory")
|
|
|
|
# The executable is Contents/MacOS/<stem>, the wire-version marker is
|
|
# Contents/Resources/wire-version-*. Both are named relative to the bundle root.
|
|
stem = name[:-len(".bundle")]
|
|
exec_rel = "Contents/MacOS/" + stem
|
|
|
|
# cdhash of the executable, from codesign (truncated to CS_CDHASH_LEN = 20 bytes).
|
|
# Pin the x86_64 slice: the guest runs x86_64, and the kernel's csfg_get_cdhash
|
|
# gates on that slice's cdhash. A fat bundle packed on an arm64 Mac would
|
|
# otherwise embed the arm64 slice's hash, and the gate would reject the guest's
|
|
# x86_64 slice -- a silent "bundle won't load". codesign's default --arch is the
|
|
# host's; --arch x86_64 reads the slice the guest actually loads.
|
|
cd = subprocess.run(["codesign", "--arch", "x86_64", "-dvvv", bundle],
|
|
capture_output=True, text=True)
|
|
cdhash = None
|
|
for line in (cd.stdout + cd.stderr).splitlines():
|
|
if line.startswith("CDHash="):
|
|
cdhash = line.split("=", 1)[1].strip()
|
|
break
|
|
if not cdhash:
|
|
die("could not read CDHash from codesign of " + bundle)
|
|
cdbytes = bytes.fromhex(cdhash)[:20]
|
|
if len(cdbytes) != 20:
|
|
die("cdhash is not 20 bytes: " + cdhash)
|
|
|
|
files, dirs, marker_rel = [], set(), None
|
|
for root, _, fnames in os.walk(bundle):
|
|
for fn in fnames:
|
|
full = os.path.join(root, fn)
|
|
rel = os.path.relpath(full, bundle)
|
|
if os.path.islink(full):
|
|
die("symlink in bundle not supported: " + rel)
|
|
d = os.path.dirname(rel)
|
|
while d:
|
|
dirs.add(d); d = os.path.dirname(d)
|
|
with open(full, "rb") as f:
|
|
files.append((rel, f.read()))
|
|
if rel.startswith("Contents/Resources/wire-version-"):
|
|
marker_rel = rel
|
|
if not marker_rel:
|
|
die("no Contents/Resources/wire-version-* marker in bundle")
|
|
if not any(rel == exec_rel for rel, _ in files):
|
|
die("no executable at " + exec_rel)
|
|
|
|
files.sort(key=lambda rf: rf[0])
|
|
# Parents before children.
|
|
dir_list = sorted(dirs, key=lambda d: (d.count("/"), d))
|
|
|
|
def carr(b):
|
|
return ",".join(str(x) for x in b)
|
|
|
|
with open(out, "w") as o:
|
|
w = o.write
|
|
w("// GENERATED by tools/pack-payload.py from a signed reims-graphics.bundle.\n")
|
|
w("// Build output: never committed. Our own bundle bytes only.\n")
|
|
w("#pragma once\n#include <stdint.h>\n\nnamespace rgfx_payload {\n\n")
|
|
w('static const char kBundlePath[] = "/Library/GPUBundles/%s";\n' % name)
|
|
w('static const char kExecRel[] = "%s";\n' % exec_rel)
|
|
w('static const char kMarkerRel[] = "%s";\n\n' % marker_rel)
|
|
w("static const uint8_t kExecCDHash[20] = {%s};\n\n" % carr(cdbytes))
|
|
w("static const char *const kDirs[] = {\n")
|
|
for d in dir_list:
|
|
w(' "%s",\n' % d)
|
|
w("};\n")
|
|
w("static const uint32_t kDirCount = %d;\n\n" % len(dir_list))
|
|
for i, (rel, data) in enumerate(files):
|
|
w("static const uint8_t f%d[] = {%s};\n" % (i, carr(data)))
|
|
w("\nstruct PayloadFile { const char *rel; const uint8_t *data; uint32_t size; uint8_t is_exec; uint8_t is_marker; };\n")
|
|
w("static const PayloadFile kFiles[] = {\n")
|
|
for i, (rel, data) in enumerate(files):
|
|
w(' {"%s", f%d, %d, %d, %d},\n' % (
|
|
rel, i, len(data),
|
|
1 if rel == exec_rel else 0,
|
|
1 if rel == marker_rel else 0))
|
|
w("};\n")
|
|
w("static const uint32_t kFileCount = %d;\n\n" % len(files))
|
|
w("} // namespace rgfx_payload\n")
|
|
|
|
print("pack-payload: %s -> %s (%d files, cdhash %s)" % (bundle, out, len(files), cdhash))
|