🐛 Give the arm64 signature hook XNU's ten-argument mpo shape

RgfxGate's arm64 wrapVSig sits in AMFI's vnode_check_signature slot, so it is
called as mpo_vnode_check_signature_t: (vp, label, cpu_type, cs_blob, cs_flags,
signer_type, flags, platform, fatal_failure_desc, fatal_failure_desc_len), as
XNU's mac_policy.h declares it. It was declared with eight arguments and no
cpu_type, so it took the slice's CPU type for the blob, and wherever AMFI
rejected a signature anywhere in the guest it called csblob_get_cdhash on that
integer, which can panic; and AMFI, given eight arguments, read the two
fatal-failure out-parameters from whatever the stack held and could write
through them. The type also failed the slot's own check: under -arch arm64e
-mkernel its type discriminator is 0xe9c6, against the slot's 0xcbd2 (graft-31,
with the public prototypes reproducing file_check_mmap's 0x438d and
file_check_library_validation's 0x9e37, which our other two wrappers match).

wrapVSig now takes and forwards all ten and reads the blob at arg4. Each
wrapper's type is named after its mpo, and a static_assert in the arm64e build
ties each one's __builtin_ptrauth_type_discriminator to its slot's
(rgfx_amfi.h), so a wrapper of another shape fails the build. The x86 path
hooks the mac_* dispatchers and is unchanged.
This commit is contained in:
Anees Iqbal committed 2026-11-09 20:59:10 -08:00
1 parent e381098623
commit 618f3644db
1 file changed
+50 -23
+50 -23
View File
@@ -11,6 +11,7 @@
#include "RgfxGatePayload.h"
#if defined(__arm64e__)
#include <mach/machine.h>
#include <ptrauth.h>
#include "rgfx_amfi.h"
#endif
@@ -73,17 +74,20 @@ static bool vnodeUnderBundles(vnode_t vp) {
// directly in the operation's slot, so these wrappers are called as the raw
// policy ops (mpo_*), not the x86 dispatchers below. Only the three admission
// ops are routed: the install writes go below the MAC layer on arm64
// (RgfxGateVnop.hpp), so there is no create/open/truncate route. The mpo
// signatures insert a `struct label *` (file_check_mmap arg3,
// vnode_check_signature arg2, library_validation none; Ambrosia's disasm of the
// five config-2 KCs, 2026-11-08), forwarded unchanged. Each saved original is
// AMFI's own mpo pointer, signed key IA under the operation's constant
// discriminator with no address diversity (rgfx_amfi.h), so it is declared as a
// matching __ptrauth function pointer and called through directly.
// (RgfxGateVnop.hpp), so there is no create/open/truncate route. Each wrapper
// has its operation's mpo shape, as XNU's mac_policy.h declares it, and forwards
// every argument unchanged. Each saved original is AMFI's own mpo pointer,
// signed key IA under the operation's constant discriminator with no address
// diversity (rgfx_amfi.h), so it is declared as a matching __ptrauth function
// pointer and called through directly. That discriminator is the mpo type's own
// (function-type discrimination), so each wrapper's type is checked against its
// slot's at compile time: a wrapper of another shape reads the wrong arguments,
// and fails the build instead.
// library validation (wall B): no label, the dispatcher's shape.
static int (*__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscLibraryValidation) orgLibVal)(
void *proc, void *fg, off_t slice_offset, uint64_t error_message, size_t error_message_size);
// library validation (wall B): mpo_file_check_library_validation_t, no label.
using MpoLibVal = int(void *proc, void *fg, off_t slice_offset, uint64_t error_message,
size_t error_message_size);
static MpoLibVal *__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscLibraryValidation) orgLibVal;
static int wrapLibVal(void *proc, void *fg, off_t slice_offset, uint64_t error_message,
size_t error_message_size) {
if (cdhashIsOurs(fg, (uint64_t)slice_offset))
@@ -91,10 +95,12 @@ static int wrapLibVal(void *proc, void *fg, off_t slice_offset, uint64_t error_m
return orgLibVal(proc, fg, slice_offset, error_message, error_message_size);
}
// file-map-executable (wall C): label at arg3 (NULL from the dispatcher; never
// dereferenced). The *maxprot clamp is the x86 wrapper's, for the same reason.
static int (*__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscFileCheckMmap) orgMmap)(
void *cred, void *fg, void *label, int prot, int flags, uint64_t file_pos, int *maxprot);
// file-map-executable (wall C): mpo_file_check_mmap_t, the label at arg3 (NULL
// from the dispatcher; never dereferenced). The *maxprot clamp is the x86
// wrapper's, for the same reason.
using MpoMmap = int(void *cred, void *fg, void *label, int prot, int flags, uint64_t file_pos,
int *maxprot);
static MpoMmap *__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscFileCheckMmap) orgMmap;
static int wrapMmap(void *cred, void *fg, void *label, int prot, int flags, uint64_t file_pos,
int *maxprot) {
if ((prot & kVmProtExecute) && cdhashIsOurs(fg, file_pos)) {
@@ -105,16 +111,26 @@ static int wrapMmap(void *cred, void *fg, void *label, int prot, int flags, uint
return orgMmap(cred, fg, label, prot, flags, file_pos, maxprot);
}
// code-signature trust (wall B, before library validation): label at arg2
// (real, from the vnode). The x86 wrapVSig's comment holds for this one: the
// real policies run first, and only our bundle's own executable under
// code-signature trust (wall B, before library validation):
// mpo_vnode_check_signature_t, (vp, label, cpu_type, cs_blob, cs_flags,
// signer_type, flags, platform, fatal_failure_desc, fatal_failure_desc_len):
// the label (real, from the vnode) at arg2, the slice's CPU type at arg3, the
// blob at arg4, and the two out-parameters of a fatal failure's description
// last, all forwarded. The x86 wrapVSig's comment holds for this one: the real
// policies run first, and only our bundle's own executable under
// /Library/GPUBundles is trusted when they reject it.
static int (*__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscVnodeCheckSignature) orgVSig)(
vnode_t vp, void *label, void *cs_blob, void *imgp, unsigned int *cs_flags,
unsigned int *signer_type, int flags, unsigned int platform);
static int wrapVSig(vnode_t vp, void *label, void *cs_blob, void *imgp, unsigned int *cs_flags,
unsigned int *signer_type, int flags, unsigned int platform) {
int r = orgVSig(vp, label, cs_blob, imgp, cs_flags, signer_type, flags, platform);
using MpoVnodeCheckSignature = int(vnode_t vp, void *label, cpu_type_t cpu_type, void *cs_blob,
unsigned int *cs_flags, unsigned int *signer_type, int flags,
unsigned int platform, char **fatal_failure_desc,
size_t *fatal_failure_desc_len);
static MpoVnodeCheckSignature *__ptrauth(ptrauth_key_asia, 0, kRgfxAmfiDiscVnodeCheckSignature)
orgVSig;
static int wrapVSig(vnode_t vp, void *label, cpu_type_t cpu_type, void *cs_blob,
unsigned int *cs_flags, unsigned int *signer_type, int flags,
unsigned int platform, char **fatal_failure_desc,
size_t *fatal_failure_desc_len) {
int r = orgVSig(vp, label, cpu_type, cs_blob, cs_flags, signer_type, flags, platform,
fatal_failure_desc, fatal_failure_desc_len);
if (r == 0)
return 0; // already trusted; leave everything to the real policies
const uint8_t *cd = (gCsblobGetCdhash && cs_blob) ? gCsblobGetCdhash(cs_blob) : nullptr;
@@ -124,6 +140,17 @@ static int wrapVSig(vnode_t vp, void *label, void *cs_blob, void *imgp, unsigned
return r;
}
// Each wrapper's type, and so its mpo type, is signed under its slot's discriminator.
static_assert(__builtin_ptrauth_type_discriminator(decltype(wrapLibVal)) ==
kRgfxAmfiDiscLibraryValidation,
"wrapLibVal has file_check_library_validation's mpo shape");
static_assert(__builtin_ptrauth_type_discriminator(decltype(wrapMmap)) ==
kRgfxAmfiDiscFileCheckMmap,
"wrapMmap has file_check_mmap's mpo shape");
static_assert(__builtin_ptrauth_type_discriminator(decltype(wrapVSig)) ==
kRgfxAmfiDiscVnodeCheckSignature,
"wrapVSig has vnode_check_signature's mpo shape");
bool installHooks() {
uint64_t csfg = 0, csblob = 0;
if (!graft_kernel_symbol(kClientID, "_csfg_get_cdhash", &csfg) ||