Introduction
Part 1 described the objects a buffer passes through, and it ended on a mapping that can change between a check and a use. This part follows one such case through the XNU code.
CVE-2025-43520 is a time-of-check to time-of-use bug in the XNU cluster layer, the part of the kernel that moves file data between a user buffer and a file. When an uncached read or write targets a buffer that the kernel believes is one unbroken run of physical memory, the cluster layer switches to a strategy that addresses that buffer by physical address and walks it by addition. The belief comes from a query about the virtual memory object behind the buffer. The query is made once, under the map lock, and the lock is released before the transfer builds its own view of the buffer. Nothing in between ties the two views together. When they disagree, the transfer reads from or writes to physical pages that are not the buffer’s.
Apple lists the fix in iOS 18.7.2 and iOS 26.1, component Kernel, with the one-line description “A memory corruption issue was addressed with improved memory handling” (Apple’s advisory pages support.apple.com/en-us/125633 for iOS 18.7.2, released November 5, 2025, and support.apple.com/en-us/125632 for iOS 26.1, released November 3, 2025; on both pages the CVE entry carries the note “Entry added December 12, 2025”). Google’s Threat Intelligence Group (GTIG) identifies the bug as the kernel stage of the DarkSword exploit kit and calls it “a kernel-mode race condition in XNU’s virtual filesystem (VFS) implementation”. GTIG states that the kit supports iOS 18.4 through 18.7, and that the UNC6353 activity it observed supported only iOS 18.4 through 18.6 (GTIG’s DarkSword analysis, cloud.google.com, March 18, 2026). The code is shared with macOS. Which macOS release carries a fix, if any, is not stated in the sources used here (not verified in the tree).
This post explains the bug: the background, the code path to the vulnerable function, the decision and the interval during which it goes stale, and what the transfer does when the decision is wrong. It does not explain how to reach the mismatch, and it does not touch exploitation; part 3 covers the lab work and, at the level of stages only, what an exploit has to accomplish. File and line references are to the public XNU source at tag xnu-10002.41.9, which predates the fix. The ClearSword write-up by Clarity (“Into the Dark - DarkSword Kernel Exploit Writeup”, therealclarity.github.io, last updated July 25, 2026) reached the same root cause independently and is cited where it is the source.
Background: contiguity and the page list
A user buffer is a range of virtual addresses. The process sees one block, but the memory behind it is a set of physical pages that are, in general, scattered: virtual page 0 might sit at one physical frame and virtual page 1 far away. The VM layer describes the address space as a map, a sorted set of entries, each pointing at a VM object that holds or describes the pages.
A buffer is physically contiguous when its pages are consecutive frames: frame N for page 0, frame N plus 1 for page 1, and so on. Ordinary anonymous memory makes no such promise. Device and graphics memory often does, because a driver carves it out as one physical range, and the VM object that wraps such a range carries the phys_contiguous bit. A related bit, private, marks objects whose pages the VM layer does not manage itself. The cluster layer will ask about these two bits.
Why would file I/O care? Because the cheapest transfer is one where the kernel never looks up a page. If the buffer is one run of frames, the physical address of any byte is the address of the first byte plus the distance from it. The transfer computes one base and advances, and storage can be handed one physical range instead of a scatter list.
The kernel describes the pages of a buffer for I/O with a universal page list, or UPL. A UPL is created over a range of an object and records, for each page, its physical frame and some state bits; creating one usually wires the pages so they stay put during I/O. For a phys_contiguous object the creation path takes a shortcut. It marks the UPL as device memory, records the highest page, and fills exactly one record: the frame of the first page, tagged as a device page (osfmk/vm/vm_pageout.c:9334-9352). Page N is defined as the first frame plus N. That definition, adjacency as a property of the object, is the premise every later step depends on.
With that in place, the next question is how a write reaches the code that relies on the premise.
The write path to the strategy decision
A write system call ends up in vn_write, the vnode layer’s file operation (bsd/vfs/vfs_vnops.c:1202). Among its bookkeeping, vn_write translates flags of the open file description into I/O flags for the operation. Two translations matter here. If the open file has FNOCACHE set, or the vnode reports itself as no-cache, the operation carries IO_NOCACHE (bsd/vfs/vfs_vnops.c:1265-1266). If the open file has FNODIRECT set, the operation carries IO_NODIRECT (bsd/vfs/vfs_vnops.c:1268-1269). vn_write then calls VNOP_WRITE (bsd/vfs/vfs_vnops.c:1379), which dispatches to the filesystem’s write handler.
The filesystem hands the data movement to the cluster layer through cluster_write and its extended form, cluster_write_ext. Whether the shipping iOS filesystem, which is not open source, routes its writes this way is not known (not verified in the tree). The ClearSword write-up shows the vulnerable function on a backtrace from a device.
cluster_write_ext chooses the strategy (bsd/vfs/vfs_cluster.c:2505). It also adds IO_NOCACHE when the vnode carries VNOCACHE_DATA (bsd/vfs/vfs_cluster.c:2525-2527). By default a write is copied into the file’s pages in the unified buffer cache and written to storage later. The function considers anything else only when three conditions hold together: IO_NOCACHE is set, IO_NODIRECT is not, and the uio, the kernel’s cursor over the user’s buffers, targets user space (bsd/vfs/vfs_cluster.c:2545). When they hold, it calls cluster_io_type to classify the first buffer in the request (bsd/vfs/vfs_cluster.c:2554).
There are three strategies for a user buffer. IO_COPY is the cached copy. IO_DIRECT wires the buffer’s ordinary pages and hands their individual frames to storage, bypassing the cache. IO_CONTIG is the physically contiguous strategy, implemented by cluster_write_contig for writes and cluster_read_contig for reads. A short note on zero-fill: if the request asks for zero-filling at its head or tail and the chosen strategy is IO_DIRECT, the code falls back to IO_COPY (bsd/vfs/vfs_cluster.c:2556-2561). IO_CONTIG is not demoted; its case in the dispatch loop performs any head zero-fill through the cached path first and then calls cluster_write_contig (bsd/vfs/vfs_cluster.c:2595-2611).
The read side has the same shape: cluster_read_ext classifies with the same function (bsd/vfs/vfs_cluster.c:4078) and dispatches to cluster_read_contig. This post follows the write side and notes where the read side differs. Next comes the classification itself.
The decision, taken once
cluster_io_type (bsd/vfs/vfs_cluster.c:5624) does not build anything to classify the buffer. It asks the VM layer a question. It calls vm_map_get_upl on the map the uio targets, the current task’s map for a user buffer, with a single flag, UPL_QUERY_OBJECT_TYPE (bsd/vfs/vfs_cluster.c:5655-5662). vm_map_get_upl is a thin wrapper around vm_map_create_upl (osfmk/vm/vm_user.c:2524-2547), and in that function the query flag selects a short path:
if (caller_flags & UPL_QUERY_OBJECT_TYPE) {
*flags = 0;
if (!entry->is_sub_map &&
VME_OBJECT(entry) != VM_OBJECT_NULL) {
if (VME_OBJECT(entry)->private) {
*flags = UPL_DEV_MEMORY;
}
if (VME_OBJECT(entry)->phys_contiguous) {
(osfmk/vm/vm_pageout.c:6712-6721, blank lines removed.) Before reaching this block the function has taken the map lock for reading and looked up the entry that contains the buffer’s first page (osfmk/vm/vm_pageout.c:6690). The query reads the two object bits off that entry, translates them into the return flags UPL_DEV_MEMORY and UPL_PHYS_CONTIG, drops the read lock and returns (osfmk/vm/vm_pageout.c:6725). No UPL is created, nothing is wired, and no reference to the object is kept. The header groups the two return flags under the comment “flags for return of state from vm_map_get_upl” (osfmk/mach/memory_object_types.h:671-674). In the tree at this tag, cluster_io_type is the only caller that passes the query flag.
Back in cluster_io_type, the answer becomes a strategy in three comparisons:
if (upl_flags & UPL_PHYS_CONTIG) {
*io_type = IO_CONTIG;
} else if (iov_len >= min_length) {
*io_type = IO_DIRECT;
} else {
*io_type = IO_COPY;
}
(bsd/vfs/vfs_cluster.c:5673-5679.) A contiguous object wins outright. Otherwise a buffer at least as long as the direct threshold, 16 KiB by default (MIN_DIRECT_WRITE_SIZE, bsd/vfs/vfs_cluster.c:278), goes direct, and the rest is copied. The function also stores how many bytes the decision covers, clipped by the query to the end of the measured map entry (osfmk/vm/vm_pageout.c:6706-6711).
The answer leaves cluster_io_type as an integer in the caller’s write_type variable. From here on it is a premise. The dispatch loop switches on it, and IO_CONTIG leads to cluster_write_contig (bsd/vfs/vfs_cluster.c:2611).
Why only once? Economy is one reason: the query is the lightest call the UPL interface offers. The other is an assumption about lifetime: for a driver’s contiguous range, the object’s type does not change while it is mapped, so a second ask would be wasted. The code does ask again, but only for the next buffer of a multi-buffer request, after the current one has been transferred (bsd/vfs/vfs_cluster.c:3248). For any single buffer there is one measurement and one use. What sits between them is the subject of the next section.
The unlocked interval
The measurement and the use happen in two different calls to vm_map_create_upl, in two different lock scopes.
The first scope is the query just described: read lock, lookup, two bits, unlock, return. The second begins inside cluster_write_contig (bsd/vfs/vfs_cluster.c:3086), which calls vm_map_get_upl again on the same map and the same page-aligned address, this time with creation flags that ask for a real UPL with wired pages (bsd/vfs/vfs_cluster.c:3135-3143). That call takes the read lock again, looks up the entry at the address again, records the object it finds, takes a reference on it, and drops the lock before building the page list (osfmk/vm/vm_pageout.c:6690, 7059-7071, 7128-7130).
Between the query’s unlock and the creation call’s lookup, the write path holds no lock on the map. The interval spans the return from cluster_io_type, the dispatch in cluster_write_ext, and the opening of cluster_write_contig up to its UPL call. This is a fact of the code’s structure. The following can be checked against the tree:
- The map lock is not held across the interval. Both lock acquisitions are local to vm_map_create_upl and both are released before it returns.
- No reference to the measured object or entry is kept.
- No version or timestamp of the map is recorded by the query and compared by the creation call. The VM layer has such tools for code that drops and retakes the map lock, but this path does not use them.
- No entry is marked as in transition or otherwise protected against change during the interval.
- The creation call looks the entry up afresh and has no way to know which object the query saw; only the strategy integer survives, and it stays in the caller.
Taken together, these mean the code does not guarantee that the entry it measured is the entry it uses. The map belongs to the task, and the task’s own address space operations take the map lock for writing as a matter of course; nothing on this path excludes them. If the entry at the buffer’s address points at a different object when the second lookup runs, the creation call will build its UPL from that object, correctly and completely, and return.
The VM code knows this class of problem. A comment in the creation path notes that once the map is unlocked “anything could happen to that mapping”, and the code prepares the object so that the page list keeps pointing at the same memory as the mapping (osfmk/vm/vm_pageout.c:7100-7106). That protection covers the creation call’s own unlock, not the earlier query.
Each call is correct on its own terms: the query reports on the object it saw, the creation call describes the object it sees. The defect is that the caller treats the first answer as a description of the second object. The next section follows how the transfer uses that premise.
What the transfer assumes
cluster_write_contig opens with a comment that states the premise: on entry, it says, the target address is physically contiguous (bsd/vfs/vfs_cluster.c:3110-3114). The code that follows does not test it.
After the creation call returns, the function checks two things about the result: that the call succeeded, and that the UPL covers at least as many bytes as the request needs (bsd/vfs/vfs_cluster.c:3145-3163). It does not look at the flags the call wrote back. Then it derives the physical base of the whole transfer from a single page record:
pl = ubc_upl_pageinfo(upl[cur_upl]);
src_paddr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + (addr64_t)upl_offset;
(bsd/vfs/vfs_cluster.c:3164-3166.) The frame of page 0 as an address, plus the buffer’s offset within its first page: that is all the function ever reads from the page list. For a genuinely contiguous object it is the only option, since the UPL holds one record and every later address must be computed from it. The function then moves data in three parts.
The head is handled first. While the file offset is not aligned to the device block size, or fewer bytes than a block remain, the loop moves one piece at a time through cluster_align_phys_io (bsd/vfs/vfs_cluster.c:3168-3188). After each piece, the physical address, the UPL offset, the remaining size and the virtual address iov_base all advance by the piece’s size. The physical address is a running sum, never re-derived from the map or the page list.
The middle, the block-aligned bulk, is issued to cluster_io with the flag CL_DEV_MEMORY, passing the UPL and an offset into it rather than the computed physical address (bsd/vfs/vfs_cluster.c:3232-3233). Inside cluster_io, a request with that flag is treated, in the words of a code comment, as one giant page (bsd/vfs/vfs_cluster.c:1729-1733). How the storage stack below turns such a request into physical addresses is outside this file (not verified in the tree).
The tail, any partial block left after the bulk, goes through cluster_align_phys_io again with the running physical address, after the cleanup label (bsd/vfs/vfs_cluster.c:3271-3272).
The helper is where bytes move. cluster_align_phys_io (bsd/vfs/vfs_cluster.c:6663) creates a one-page UPL over the file’s cache page at the current offset, reads it from storage if needed, and calls copypv to copy between two physical addresses (bsd/vfs/vfs_cluster.c:6735-6741). One is the file’s cache page, looked up properly. The other is the caller’s computed address, received as a number and used as given. On arm64, copypv hands both addresses to a physical copy routine that decides how to reach each address, through the kernel’s direct physical mapping or a temporary window, but does not ask who owns the page (osfmk/arm64/loose_ends.c:812-830, osfmk/arm64/loose_ends.c:66-125). After the copy, a write updates the file page on storage.
So the transfer assumes two things it never verifies: that the frame of page 0 belongs to a contiguous object, and that therefore every address reached by adding to it still lies inside the buffer. The next section looks at what happens when the first assumption is false.
What a mismatch does to the transfer
Suppose the object the creation call finds is not the one the query measured, and the new object is ordinary memory without the phys_contiguous bit. The creation call takes its normal path for such an object: it wires the pages and fills a page list with one record per page, each holding that page’s own frame. Those frames can be anywhere. The list is correct; record 1 holds the real frame of the buffer’s second page.
cluster_write_contig reads record 0 and nothing else. The base it computes is the true physical address of the buffer’s first byte under the new object, so the first bytes the transfer touches do belong to the buffer. As soon as the running sum crosses the end of that first physical page, it points at the next frame in physical memory, which is not the frame recorded for the buffer’s second page except by coincidence. From there on, the addresses the transfer computes are the frames that follow the first frame in physical memory, and those frames are not the buffer’s pages. The correct frames sit unused in the page list.
What lies at those frames is whatever the system put there: pages of other processes, of the kernel, or memory not in use. The transfer does not look. The direction of the damage depends on the system call.
On the write side, in cluster_write_contig, the user buffer is the source. The helper copies from the computed physical address into the file’s cache page and then writes that page to storage (bsd/vfs/vfs_cluster.c:6739-6740, 6742-6748). A mismatch therefore reads physical memory the buffer does not own and places it in the file. This discloses the foreign memory without changing it.
On the read side, in cluster_read_contig (bsd/vfs/vfs_cluster.c:5403), the same arithmetic produces the destination. The helper is called with CL_READ, and copypv copies from the file’s cache page to the computed physical address (bsd/vfs/vfs_cluster.c:5515, 6735-6737). A mismatch therefore writes file data into physical memory the buffer does not own. That is corruption of whatever occupies those pages.
The function has the information needed to notice the mismatch: the creation call wrote the new object’s type bits back through the flags pointer cluster_write_contig passed in, and for an ordinary object UPL_PHYS_CONTIG is absent (osfmk/vm/vm_pageout.c:7059-7067). The function never reads that variable after the call.
The bulk middle is less clear. Its UPL is now an ordinary multi-record one, but the request carries CL_DEV_MEMORY and cluster_io treats it as one giant page; whether the layers below translate it by the first record alone is not known (not verified in the tree). This post makes claims only about the head and tail copies, where the source shows the walk directly. One more consequence of the order of operations remains, and it concerns the error code.
Why the error comes back after the transfer
The error that matters here comes from a check between the head loop and the bulk transfer:
if ((u_int32_t)iov_base & mem_alignment_mask) {
/*
* request doesn't set up on a memory boundary
* the underlying DMA engine can handle...
(bsd/vfs/vfs_cluster.c:3189-3192.) If the virtual address reached after the head loop is not aligned to the mount’s memory alignment mask, the function sets EINVAL and jumps to its cleanup label (bsd/vfs/vfs_cluster.c:3189-3197). The rest of the comment gives the intent: this path is for direct I/O from device memory, so an unsuitable address fails instead of falling back to the slow copy.
The alignment check has three parts. First, it tests a virtual address against a DMA alignment requirement. Its inputs are iov_base, which the head loop advanced by plain addition, and mnt_alignmentmask from the mount (bsd/vfs/vfs_cluster.c:3117-3118). Neither depends on which object backs the buffer, so the check cannot detect a mismatch.
Second, it runs after the head loop, so any head pieces have already been moved by copypv. If it fires, the function waits for outstanding asynchronous I/O, releases the UPLs and returns the error. None of that undoes a completed copy: on the write side the file page has been written, on the read side the destination bytes have been stored.
Third, when the check passes, nothing else in the function signals the mismatch. The bulk is issued, the tail is copied, and the call returns success.
The error code therefore reports the alignment of the request and says nothing about the bug. The kernel records no anomaly in either case. The ClearSword write-up attributes the error returned in the cases it describes to this alignment branch (last updated July 25, 2026); the branch and its position are in the tree, the attribution is the write-up’s.
At this point the root cause is complete: a premise measured in one lock scope, consumed in another, and a transfer whose arithmetic leaves the buffer as soon as the premise is false. The remaining question is what Apple changed.
The fix
Apple’s advisories say little. For both iOS 18.7.2 and iOS 26.1 the entry reads “A memory corruption issue was addressed with improved memory handling”, component Kernel, with no function named and no diff (support.apple.com/en-us/125633, iOS 18.7.2, released November 5, 2025; support.apple.com/en-us/125632, iOS 26.1, released November 3, 2025; both entries carry “Entry added December 12, 2025”). GTIG gives only versions: the bug was patched in iOS 18.7.2 and 26.1 (GTIG, cloud.google.com, March 18, 2026).
The shape of the patch comes from the ClearSword write-up (therealclarity.github.io/blog/clearsword/, last updated July 25, 2026). According to it, the fix adds a check directly after the creation call in the contiguous functions. If the flags written back by vm_map_get_upl do not include UPL_PHYS_CONTIG, the function sets EINVAL and goes to its cleanup label before computing the physical base or moving any data. The write-up shows this change on the read path, cluster_read_contig, and places the same check after the creation call in cluster_write_contig. No fixed XNU release was diffed for this post, so the exact patched code is unconfirmed (not verified in the tree).
The unfixed source makes the shape plausible, and this paragraph is inference. The creation call already computes the type bits of the object it actually used and writes them back through the caller’s flags pointer (osfmk/vm/vm_pageout.c:7059-7067). The page list and those bits describe the same object, taken from the same entry under the same lock acquisition, so testing the bits right after the call validates the premise against the object the transfer will really use. It needs no new locking. Re-running the query would not help, since its answer could again be stale by the time of use. Testing at the point of use closes the gap regardless of ordering. Whether the fix does more elsewhere cannot be inferred from the advisory or the write-up.
Several questions remain open: whether other users of the contiguous strategy were reviewed, whether the query itself changed, and which macOS releases received the equivalent change (not verified in the tree). The last section collects what is established and what is not.
What is known, what is not, and what post 3 covers
Verified in the tree at xnu-10002.41.9, with the references given above:
- The uncached paths classify the buffer with a query of vm_map_get_upl that reads the object’s type bits under the map read lock and releases it before returning; the result travels as a strategy integer to cluster_write_contig or cluster_read_contig.
- Those functions call vm_map_get_upl again, which looks the entry up afresh. No lock, reference, timestamp or transition mark connects the two calls.
- The second call writes back the type bits of the object it used, and the contiguous functions do not read them.
- The transfer base comes from page 0 of the UPL alone, and the head and tail copies advance it by addition and copy physical to physical through copypv.
- The write direction reads from the computed address into the file; the read direction writes file data to the computed address.
- The alignment check tests a virtual address against the mount’s mask, runs after the head copies, and is independent of the backing object.
Known from documents, not from the tree: Apple’s advisory text, versions and dates; GTIG’s attribution to DarkSword, its version ranges and its statement of the patched versions; and ClearSword’s description of the patch, its observation of the vulnerable function on a device, and its attribution of the returned error to the alignment branch.
Not known or not verified: the exact patched code; whether the bulk transfer through cluster_io with CL_DEV_MEMORY also strays when the UPL is not contiguous; whether and how the shipping iOS filesystem reaches cluster_write_ext; how the closed iOS kernels of the affected releases differ from this open tree; and the macOS status (all not verified in the tree).
Part 3 moves from the source to a lab. It shows what was done to reproduce the mechanism described here, on a development kernel built from this tree and booted under a hypervisor. It describes the approach only at the level of stages and publishes no exploit code. The reproduction covers one kernel build on one device model.