The arm64 Metal bundle is universal, arm64e and arm64, and a process maps the slice of its own architecture: platform processes such as WindowServer map the arm64e slice, and third-party apps, built arm64, map the arm64 slice. Each slice has a code-directory hash of its own, but RgfxGate admitted only the guest arch's (arm64e), so a hardened, library-validated third-party app would have had our bundle refused and no Metal device. pack-payload.py now also writes kExecCDHashes, every slice's cdhash with the guest's first, and the arm64 hooks admit all of them. kExecCDHash stays as it was, and the x86 hooks keep admitting it alone; the x86 bundle has one slice. payload_test.sh packs a fat arm64e+arm64 bundle of our own for an arm64 guest and checks that the hooks' decision (rgfx_cdhash_allowed over the payload's allowed set) admits both slices' hashes and no other; it fails on the payload pack-payload.py wrote before this change.
134 lines
6.1 KiB
Python
Executable File
134 lines
6.1 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 code-directory hashes (cdhashes) the kext gates library validation,
|
|
file-map-executable and the signature check on: kExecCDHash, the guest's slice
|
|
of the executable, and kExecCDHashes, every slice's, the guest's first. Run on
|
|
a Mac (uses lipo and codesign to read them). Output is build output, never
|
|
committed.
|
|
|
|
pack-payload.py BUNDLE_DIR OUT_HEADER [ARCH]
|
|
"""
|
|
import os, sys, subprocess
|
|
|
|
def die(m):
|
|
sys.stderr.write("pack-payload: " + m + "\n"); sys.exit(2)
|
|
|
|
if len(sys.argv) not in (3, 4):
|
|
die("usage: pack-payload.py BUNDLE_DIR OUT_HEADER [ARCH]")
|
|
bundle, out = sys.argv[1], sys.argv[2]
|
|
# The slice the guest's kernel gates on: x86_64 for an x86-64 guest, arm64e for an arm64 one.
|
|
arch = sys.argv[3] if len(sys.argv) == 4 else "x86_64"
|
|
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 guest's slice (arch: x86_64 for an x86-64 guest, arm64e for an arm64 one): the
|
|
# kernel's csfg_get_cdhash gates on that slice's cdhash. A fat bundle read without --arch
|
|
# would embed the host's slice's hash, and the gate would reject the guest's slice -- a
|
|
# silent "bundle won't load". codesign's default --arch is the host's.
|
|
cd = subprocess.run(["codesign", "--arch", arch, "-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)
|
|
|
|
# Every slice of the executable, each with a cdhash of its own: a process maps the slice of its
|
|
# own architecture, so on an arm64 guest the platform processes (arm64e, WindowServer among them)
|
|
# map the arm64e slice and third-party ones (arm64) the arm64 slice. The kext admits each slice's
|
|
# hash (kExecCDHashes), the guest's slice first; the x86-64 bundle has one.
|
|
exec_path = os.path.join(bundle, exec_rel)
|
|
lipo = subprocess.run(["lipo", "-archs", exec_path], capture_output=True, text=True)
|
|
if lipo.returncode != 0:
|
|
die("could not read the slices of " + exec_path + ": " + lipo.stderr.strip())
|
|
slices = lipo.stdout.split()
|
|
if arch not in slices:
|
|
die("the executable has no %s slice (it has %s)" % (arch, " ".join(slices)))
|
|
slice_hashes = []
|
|
for a in [arch] + sorted(s for s in slices if s != arch):
|
|
sd = subprocess.run(["codesign", "--arch", a, "-dvvv", bundle], capture_output=True, text=True)
|
|
h = next((l.split("=", 1)[1].strip() for l in (sd.stdout + sd.stderr).splitlines()
|
|
if l.startswith("CDHash=")), None)
|
|
if not h or len(bytes.fromhex(h)[:20]) != 20:
|
|
die("could not read the %s slice's CDHash from codesign of %s" % (a, bundle))
|
|
slice_hashes.append(bytes.fromhex(h)[:20])
|
|
if slice_hashes[0] != cdbytes:
|
|
die("the %s slice's cdhash differs between two reads" % arch)
|
|
|
|
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 uint8_t kExecCDHashes[%d][20] = {\n" % len(slice_hashes))
|
|
for h in slice_hashes:
|
|
w(" {%s},\n" % carr(h))
|
|
w("};\n")
|
|
w("static const uint32_t kExecCDHashCount = %d;\n\n" % len(slice_hashes))
|
|
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, %d slices)" % (bundle, out, len(files), cdhash,
|
|
len(slice_hashes)))
|