When a library only takes a path and we already hold the file open, /dev/fd/<n> looks like a tidy way to hand it exactly the file we hold, and that's what the import code in my iOS reading app did for comic archives, right up until comic imports started failing on a real iPhone while every other format imported fine. It turns out /dev/fd means two different things on Apple platforms depending on where the code runs, and neither of them is what the code assumed, so I'll go through both and then through what I replaced it with.

Why a descriptor in the first place

When a comic is imported, the app copies it into a private staging folder and takes a SHA-256 of it. Every later read of that file (the Rust core computing a BLAKE3 checksum, the comic reader sniffing the archive format) then opens the staging folder and the file inside it again, checks that they're the ones that were staged, hashes the file again, and only then lets the reader at it, with another round of checks once the reader is done. A descriptor makes "the same bytes" easy to promise, because a path can be pointed at a different file between two calls and an open descriptor can't, so the path given to the Rust readers was built from the descriptor:

var descriptorURL: URL { URL(fileURLWithPath: "/dev/fd/\(leafDescriptor)") } 

On an iPhone there is no /dev/fd

The failure on the phone was a "file not found" from the Rust core while it read the checksum, which was odd for a file the app had open at that very moment. A small probe built into the app opened a real file and tried the obvious things, and this is what it printed on the device (trimmed a little):

open real fd=3 lstat /dev rc=0 mode=40555 errno=0 lstat /dev/fd rc=-1 errno=2 No such file or directory open /dev/fd/3 -> -1 errno=2 No such file or directory open /dev/fd/0 -> -1 errno=2 No such file or directory open /dev/null -> 4 errno=0 

So /dev exists and /dev/null opens fine, but /dev/fd isn't there at all, not even for stdin. This doesn't seem to be written down anywhere for iOS (the only man page around is the macOS one, which is where the second problem comes from).

On the simulator it reads nothing

My assumption was that the simulator would be fine, since it runs on the Mac's kernel and the Mac has /dev/fd, so the bug would be a device-only thing, but a new test that ran the real Rust readers over a staged comic failed on the simulator too, with "Archive contains no images". The checksum side had been failing in a way nobody noticed, because it was hashing zero bytes and producing af1349b9f5f9…, which is the BLAKE3 of empty input.

The macOS fd(4) man page explains why, since it says that opening /dev/fd/0 (with a mode the descriptor already allows) is the same as calling fcntl(0, F_DUPFD, 0). A dup'd descriptor shares the original's open file description, and with it the file offset, and the staging code had just hashed that descriptor to the end right before handing out the path. Python shows it in a few lines:

import os with open("book.cbz", "wb") as out: out.write(b"hello comic bytes") fd = os.open("book.cbz", os.O_RDONLY) os.read(fd, 1024) # b'hello comic bytes', the offset is now at EOF f = open(f"/dev/fd/{fd}", "rb") f.read() # b'' os.lseek(fd, 0, os.SEEK_SET) f.read() # b'hello comic bytes' 

Seeking the original descriptor moves the "copy" too, because there's only one offset. On Linux, as far as I know, /dev/fd points into /proc/self/fd, and opening one of those entries reopens the file with an offset of its own, which is probably where the habit of using it as "a path for this descriptor" comes from. The bad part on macOS is that nothing errors, since the checksum is a perfectly valid hash of zero bytes, and comics imported in the simulator and the Mac build while the /dev/fd path was in place were stored with exactly that checksum.

Resetting the offset with lseek before handing the path out wouldn't have been enough either, because every open of /dev/fd/<n> shares that one offset, so a reader that sniffs the first few hundred bytes and then opens the archive again starts where the sniff stopped (and the phone has no /dev/fd to open anyway).

Why not pass the descriptor

The obvious next idea is to skip paths altogether and pass the descriptor across the FFI boundary into Rust, which might work for the checksum and the ZIP reader, but the RAR library the core uses opens archives only by name, and a multi-volume RAR finds its next volume by name in the same folder, and as soon as one of the readers works like that, the app needs a real path anyway.

A real path, checked afterwards

The fix hands the readers the staged file's real path (the staging base, the folder's name, the file's name). Each reader opens its own file description, so it gets its own offset and the offset problem goes away. What the app gives up is the descriptor's guarantee, since a path can now name a different file by the time the reader opens it, so once the reader returns the staging code compares the folder and the file with what it saw before the read, and throws the result away if anything moved. Simplified, and with names that aren't the app's, the check looks like this:

// After the reader returns. Any mismatch throws the reader's result away. guard folderNow == folderBeforeRead, // fstat: device, inode, size, links, mtime, ctime fileNow == fileAtStaging, // fstat: device, inode, size, links, mtime, ctime pathNamesOpenFile(fileURL, fileNow), // lstat(path): same device and inode, one link try sha256(of: fileDescriptor) == digestAtStaging else { throw StagingError.bytesChanged } 

Each line catches a different kind of swap. Renaming the file away, or putting another file under its name, changes the file's own ctime or its link count, and fstat on the descriptor sees that, so the second line covers anything done to the file inside its folder. What the descriptors can't see is a swap one level up, where the whole folder is renamed away and a foreign folder holding a file of the same name is put in its place, so the reader opens the foreign file while the original folder and file sit untouched somewhere else. If the foreign folder is still there when the reader returns, the lstat of the path finds a different inode, and if the original was moved back before the reader returned, the folder's own times give it away, because on APFS renaming a folder changes that folder's ctime without touching anything inside it, and adding, renaming or removing an entry changes the folder's mtime and ctime (which also covers a second RAR volume planted next to the archive and removed again):

$ mkdir -p base/staged && touch base/staged/book.cbz $ stat -f '%N ino=%i mtime=%Fm ctime=%Fc' base base/staged base/staged/book.cbz base ino=…136 mtime=…599.258 ctime=…599.258 base/staged ino=…137 mtime=…599.261 ctime=…599.261 base/staged/book.cbz ino=…138 mtime=…599.260 ctime=…599.261 $ mv base/staged base/moved $ stat -f '%N ino=%i mtime=%Fm ctime=%Fc' base base/moved base/moved/book.cbz base ino=…136 mtime=…600.268 ctime=…600.268 # the parent changed base/moved ino=…137 mtime=…599.261 ctime=…600.268 # the renamed folder's ctime changed base/moved/book.cbz ino=…138 mtime=…599.260 ctime=…599.261 # its contents did not 

(I had dropped the folder's times from the check at one point, because a mutation test that removed them failed nothing, and it took a review to point out that the test only ever swapped the file, which the file line already catches.)

The digest line is the expensive one, since it re-reads and re-hashes the whole file after every reader call, which is why it goes last, after the cheap fstat and lstat comparisons have had a chance to fail.

With that in place comics import and open on a physical iPhone again, from both On My iPhone and iCloud Drive.

Paths and descriptors on Apple platforms

/dev/fd/<n> isn't a portable way to give a path to an open file, since on iOS it doesn't exist, on macOS opening it is a dup that shares the original's offset, and on Linux it reopens the file. If a library needs a path, give it the real one and check afterwards that the file and the folder holding it are the ones you checked before the read, and check the folder every time, not only for libraries that open more than one file, because swapping the folder and putting it back is invisible to every check on the file. A check like this only covers the things it compares, so everything above the folder you check is trusted, which is fine only where no other process can write there. And the simulator hid the iPhone's failure and showed a different one, because it shares the Mac's /dev, so anything that touches device files needs a run on a real device before it can be trusted.