Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:Kernelcache

From ReverseEngineering.wiki
Format
Vendor Apple
Platforms iOS, macOS
Magic CF FA ED FE (Mach-O) after any IM4P wrapper
On this wiki Facts · Code · Notes · Topics

The Apple kernel as shipped on iOS, iPadOS, tvOS and macOS: a Mach-O (MH_FILESET on current systems, with one LC_FILESET_ENTRY per kernel and kext; older kernelcaches use __PRELINK_* segments with a plist of kext info), wrapped in an IMG4/IM4P on iOS firmware.

Overview

A kernelcache holds the XNU kernel and (almost all of) the kernel extensions in one file, linked together so that no kext has to be loaded separately at boot. Two generations exist:

  • Prelinked kernel (older): an ordinary Format:Mach-O executable with extra segments named __PRELINK_TEXT, __PRELINK_DATA and __PRELINK_INFO; the __info section of __PRELINK_INFO holds an XML property list describing each kext. The segment, section and dictionary key names are #defined in xnu libkern/libkern/prelink.h (kPrelinkTextSegment, kPrelinkInfoSegment, kPrelinkBundlePathKey = _PrelinkBundlePath, kPrelinkExecutableLoadKey = _PrelinkExecutableLoadAddr, kPrelinkKmodInfoKey = _PrelinkKmodInfo, and so on). Kernel-collection builds add _PrelinkKCID, _BootKCID and _PageableKCID keys, a __RECEIPT_INFO segment with an __aux_kc_receipt section, and a __BRANCH_STUBS name (same header).
  • Kernel collection / fileset (newer): the file type is MH_FILESET (0xC, "set of mach-o's"), and the load commands include one LC_FILESET_ENTRY (0x80000035) per member.

LC_FILESET_ENTRY

struct fileset_entry_command {
    uint32_t cmd;        // LC_FILESET_ENTRY
    uint32_t cmdsize;    // includes the id string
    uint64_t vmaddr;     // memory address of the member dylib
    uint64_t fileoff;    // file offset of the member dylib
    union lc_str entry_id; // offset of the entry id string inside the command
    uint32_t reserved;   // padding after the 32-bit lc_str
};

Each member is a complete Mach-O header at fileoff (and vmaddr once mapped), so a loader or disassembler can treat every kext as its own image inside the shared segments. Unverified: entry ids are bundle identifiers such as com.apple.kernel and com.apple.driver.* (not read in a sample; check by listing the LC_FILESET_ENTRY commands of a kernelcache).

Pointers

Kernel collections use Format:Mach-O/Chained fixups formats DYLD_CHAINED_PTR_ARM64E_KERNEL (7), DYLD_CHAINED_PTR_64_KERNEL_CACHE (8) and DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE (11); the cacheLevel field of format 8/11 distinguishes the kernel collection levels. Unverified: which load command or section carries the chains for each collection (the firmware-style __TEXT,__chain_starts section with dyld_chained_starts_offsets is described on the chained fixups page).

On iOS firmware

In an IPSW or OTA the kernelcache is not a bare Mach-O but an IM4P (Format:IMG4) whose data is usually compressed; extract and decompress it first. ipsw (MIT) has a kernelcache package with a NOTES file pointing to a write-up on iOS 12 tagged pointers in kernelcaches. Unverified: the compression algorithm is reported in the IM4P itself (see Format:IMG4).

Reversing notes

  • Identify the layout by filetype in the mach_header: 0xC means fileset, anything else with a __PRELINK_INFO segment is the old prelinked layout.
  • Kexts inside a kernelcache are listed by LC_FILESET_ENTRY (new) or by the plist in __PRELINK_INFO,__info (old).
  • The system call tables, sysent, and the XNU symbols are not in this page: see Platform:IOS and Platform:MacOS.

Code & repositories