Format:Mach-O/Code signature
The embedded code signature of Mach-O files (LC_CODE_SIGNATURE): a big-endian SuperBlob with magic FA DE 0C C0 containing the CodeDirectory (FA DE 0C 02), requirements, entitlements and CMS signature blobs, with a hash per page of the file.
Location
LC_CODE_SIGNATURE (0x1D) is a linkedit_data_command {cmd, cmdsize, dataoff, datasize}; the blob sits at the end of __LINKEDIT. All fields in signature blobs are big-endian, even in little-endian Mach-O files.
Blob magics
| Magic | Blob |
|---|---|
| 0xFADE0CC0 | CSMAGIC_EMBEDDED_SIGNATURE: SuperBlob {magic, length, count} then count BlobIndex {type (slot), offset}
|
| 0xFADE0CC1 | CSMAGIC_DETACHED_SIGNATURE (multi-arch detached) |
| 0xFADE0C02 | CSMAGIC_CODEDIRECTORY |
| 0xFADE0C00 | CSMAGIC_REQUIREMENT (one) |
| 0xFADE0C01 | CSMAGIC_REQUIREMENTS (vector) |
| 0xFADE7171 | CSMAGIC_EMBEDDED_ENTITLEMENTS (XML plist) |
| 0xFADE7172 | CSMAGIC_EMBEDDED_DER_ENTITLEMENTS |
| 0xFADE0B01 | CSMAGIC_BLOBWRAPPER (CMS signature) |
| 0xFADE8181 | CSMAGIC_EMBEDDED_LAUNCH_CONSTRAINT |
| 0xFADE0B02 | legacy embedded signature |
Slots
0 CodeDirectory, 1 Info.plist hash, 2 Requirements, 3 ResourceDir, 4 Application, 5 Entitlements, 7 DER entitlements, 8-11 launch/library constraints, 0x1000-0x1004 alternate CodeDirectories (for example SHA-256 next to SHA-1), 0x10000 CMS signature, 0x10001 identification, 0x10002 ticket.
CodeDirectory
Fields: magic, length, version, flags, hashOffset, identOffset, nSpecialSlots, nCodeSlots, codeLimit, hashSize (u8), hashType (u8), platform (u8), pageSize (u8, log2; 12 = 4 KiB, 0 = infinite), spare2; then by version: 0x20100 scatterOffset; 0x20200 teamOffset; 0x20300 spare3 and codeLimit64; 0x20400 execSegBase, execSegLimit, execSegFlags; 0x20500 runtime, preEncryptOffset; 0x20600 linkage fields. Hash types: 1 SHA-1 (20 bytes), 2 SHA-256 (32), 3 SHA-256 truncated to 20, 4 SHA-384 (48). The cdhash is the hash of the whole CodeDirectory, truncated to 20 bytes. Special slots are stored at negative indices before hash slot 0 (the hash of the Info.plist, requirements and entitlements).
Version history and offsets (xnu cs_blobs.h)
The fixed part is 0x2C bytes up to and including spare2 (magic 0x0, length 0x4, version 0x8, flags 0xC, hashOffset 0x10, identOffset 0x14, nSpecialSlots 0x18, nCodeSlots 0x1C, codeLimit 0x20, hashSize 0x24, hashType 0x25, platform 0x26, pageSize 0x27, spare2 0x28). Fields are added by version, each only present when version is at least the value (offsets computed from the struct in osfmk/kern/cs_blobs.h):
| Version | Constant | Adds |
|---|---|---|
| 0x20100 | CS_SUPPORTSSCATTER | scatterOffset u32 at 0x2C |
| 0x20200 | CS_SUPPORTSTEAMID | teamOffset u32 at 0x30 (offset of the team identifier string) |
| 0x20300 | CS_SUPPORTSCODELIMIT64 | spare3 u32 at 0x34, codeLimit64 u64 at 0x38 |
| 0x20400 | CS_SUPPORTSEXECSEG | execSegBase, execSegLimit, execSegFlags (u64 each) at 0x40, 0x48, 0x50 |
| 0x20500 | CS_SUPPORTSRUNTIME | runtime u32 at 0x58, preEncryptOffset u32 at 0x5C |
| 0x20600 | CS_SUPPORTSLINKAGE | linkageHashType u8 at 0x60, linkageApplicationType u8 at 0x61, linkageApplicationSubType u16 at 0x62, linkageOffset u32 at 0x64, linkageSize u32 at 0x68 |
The header comment calls identOffset the identifier string and hashOffset the position of hash slot element zero; special slots lie before it, so the hash for special slot n is at hashOffset - n*hashSize. Unverified: that last formula is the usual reading of the header comments, not checked against a signed file.
Executable segment flags (execSegFlags): CS_EXECSEG_MAIN_BINARY 0x1, ALLOW_UNSIGNED 0x10, DEBUGGER 0x20, JIT 0x40, SKIP_LV 0x80 (obsolete), CAN_LOAD_CDHASH 0x100, CAN_EXEC_CDHASH 0x200.
The header marks CS_ALLOWED_MACHO (ADHOC 0x2, HARD 0x100, KILL 0x200, CHECK_EXPIRATION 0x400, RESTRICT 0x800, ENFORCEMENT 0x1000, REQUIRE_LV 0x2000, RUNTIME 0x10000, LINKER_SIGNED 0x20000) as the flag bits a Mach-O's CodeDirectory flags field may carry; the other CS_* bits (VALID, GET_TASK_ALLOW, PLATFORM_BINARY 0x4000000, SIGNED 0x20000000, ...) are kernel process state, not file content. ADHOC (0x2) plus LINKER_SIGNED (0x20000) identifies the signature the linker adds automatically to arm64 binaries.
The alternate CodeDirectory slots run from CSSLOT_ALTERNATE_CODEDIRECTORIES 0x1000 with at most 5 (CSSLOT_ALTERNATE_CODEDIRECTORY_MAX), so 0x1000-0x1004. The CMS signature is the CSMAGIC_BLOBWRAPPER blob (0xFADE0B01) in slot 0x10000: a generic blob (magic u32, length u32, then the DER CMS data). Launch constraints use slots 8 (self), 9 (parent), 10 (responsible) and 11 (library constraint), each a blob with magic 0xFADE8181.
Reversing notes
Patching any byte of a signed binary invalidates the page hash; on Apple silicon unsigned or invalid arm64 code is killed, and an ad-hoc signature (CodeDirectory with no CMS blob) is enough for local binaries. Entitlements are readable as plain XML in the blob. Circumventing platform protections is out of scope for this wiki.
Parent: Format:Mach-O.
Code & repositories
- https://github.com/apple-oss-distributions/xnu @
f6217f891ac0bb64f3d375211650a4c1ff8ca1ea· license: APSL-2.0 (declared by the contributor; GitHub's detector did not recognize a license file)
XNU source: EXTERNAL_HEADERS/mach-o/loader.h (MH_FILESET, fileset_entry_command), libkern/libkern/prelink.h (__PRELINK_* names) and osfmk/kern/cs_blobs.h (code signature blobs and CodeDirectory)
Tested on: source read, not run: source read, not run: headers read only · checked 2026-10-19; last activity 2026-06-18