Skip to main content
coreydaley.dev

Taking libarchive's security fixes without losing my patches

libarchive 3.8.11 came out on October 30. Two of its fixes were for bugs that could crash Purrchiver’s archive engine on a crafted file, just from opening it: no extraction, no click past a warning. Purrchiver 1.1.0 was on the App Store with both, and 1.1.1 was waiting in App Review with both. I pulled that build before Apple reviewed it, updated libarchive, and resubmitted. 1.1.1, with the fixes, is on the App Store now.

Updating should have been a download and a copy. It wasn’t, because Purrchiver doesn’t use libarchive as shipped. It compiles in its own copy and has changed about a dozen things in it. Copy a new release over that and the changes are gone. Only some of them have a test that would notice.

This is how the update went: how I knew to do it, the record that says what to keep, the one place the merge conflicted, three workarounds it let me delete, and a patch the record had missed.

How I knew to update

I was already waiting for 3.8.11. It would carry my fix for a cpio bug and the maintainers’ fix for a multi-part RAR bug, both below, and I’d opened an issue to update once a release had both.

Nothing in Purrchiver failed. The release page says only that 3.8.11 is a security and bugfix release and links the changelog, so I read the pull requests in it, about fifty, and noted which touched code Purrchiver uses.

Two of them reproduced against Purrchiver’s engine on 3.8.10. I used upstream’s own test files from the 3.8.11 tag and a small test program linked against the engine, 30 runs per file in each of listing and full-read modes:

  • A crafted XAR, the container format inside .pkg installers: a memory error that crashed in 27 of 60 runs. The other runs gave a misleading error or, once, a list of entries. Upstream’s fix is libarchive#3748.
  • A crafted ISO whose Joliet directories form a cycle: the path builder had no depth limit and recursed until the stack ran out, in 60 of 60 runs. Fixed by libarchive#3736.

With the fixes, both ended in a clean error every time. A third, a RAR symlink claiming an enormous target (libarchive#3709), didn’t crash in the two runs I tried in a release build.

That turned “update when it’s convenient” into “update before this build ships.”

The record that guides the update

Next to the vendored sources sits a file I’d now call the most important one in that directory. It pins the exact release (version, tarball hash, the key the signature was checked with) and lists every change Purrchiver makes to an upstream file, one row each: what the change is, why it exists, and whether it’s been reported upstream. Rows for patches sent upstream end with when to drop them, like “On the next bump, drop this patch if libarchive#3714 has merged.”

Going into this update, the table had eleven rows across nine upstream files: 7z encryption on read and write, the XAR parser’s network lockdown, checksum checks on stored RAR files, several ZIP fixes, a cpio fix and the build configuration. Purrchiver’s own files, like its Deflate64 decoder, are listed separately, since an update never touches them.

The checklist in the same file comes down to: verify the tarball’s hash and signature, three-way merge every file in the table, copy every other file straight from the new release, then run the tests, including the damaged-archive collections under AddressSanitizer.

“Three-way” is the important word. For each patched file there are three versions: the base (the old release, untouched), ours (the old release with Purrchiver’s changes), and theirs (the new release). The merge applies upstream’s base-to-theirs changes on top of ours. Where both sides changed the same lines, it stops and asks.

The merge, and its one conflict

Most of the patched files merged cleanly. The 7z, XAR, RAR and ZIP readers all changed upstream, and the merge applied those changes around Purrchiver’s. The XAR network lockdown came through, and a test that reads the vendored source would have failed if it hadn’t.

The one conflict was in the ZIP reader’s streaming skip: the code that runs when you read a ZIP front to back and move past an entry whose size isn’t known until a trailer after its data. Purrchiver had added a case to it for Deflate64, the method Windows Explorer uses for large ZIPs, next to upstream’s case for ordinary Deflate (trimmed):

switch (zip->entry->compression) {
case 8: /* Deflate compression. */
    while (!zip->end_of_entry) {
        r = zip_read_data_deflate(a, &buff, &size, &offset);
        if (r != ARCHIVE_OK)
            return (r);
    }
    return ARCHIVE_OK;
case 9: /* Deflate64 (Purrchiver). */
    while (!zip->end_of_entry) {
        r = zip_read_data_deflate64(a, &buff, &size, &offset);
        if (r != ARCHIVE_OK)
            return (r);
    }
    return ARCHIVE_OK;
default: /* Uncompressed or unknown. */
    /* Scan for a PK\007\010 signature. */

Upstream (libarchive#3657) replaced the whole switch with one loop through the normal data path, so the trailer is checked against the decoded data:

/* Validate the descriptor against decoded CRC and sizes. */
while (!zip->end_of_entry) {
    r = archive_read_format_zip_read_data(a, &buff, &size, &offset);
    if (r == ARCHIVE_EOF)
        return (ARCHIVE_OK);
    if (r != ARCHIVE_OK)
        return (r);
}

I took upstream’s side. Purrchiver’s Deflate64 support already lives in that data path, so the skip now reaches it without a second copy of the patch. The merge left Purrchiver’s code smaller.

That’s the pattern I’d repeat. A patch in one place that all the data flows through survives upstream refactoring. A patch copied into each place that needs it collides with every one of them.

The workarounds it let me delete

Every update is also a chance to stop carrying things. This one let three go.

The cpio fix. An audit of Purrchiver before 1.1 found that libarchive’s cpio reader could skip past the next header after a few bytes of junk and lose every file after it. I sent the fix upstream as libarchive#3745, it merged in October, and it shipped in 3.8.11. Because the patch was the same four lines, the merge produced upstream’s file exactly. All that was left was to delete the row.

The missing uu filter. This workaround wasn’t in libarchive’s C at all. In 3.8.10, libarchive’s uudecode detector started reading ahead through binary data. When libarchive opened a multi-part set as one stream, that look-ahead crossed into the next part, and during the 3.8.10 update a three-part RAR5 set stopped reading partway through. Purrchiver stopped registering that filter, I reported it, and the maintainers fixed it within a day.

A workaround outside the vendored code is the kind a merge never conflicts on. The record’s note on it said what to check before turning the filter back on: that the multi-part RAR5 test sets still read with it registered. They did. One new test makes sure a tar that happens to contain a uuencoded file still opens as a tar.

The split compressed-tar reader. Purrchiver had a 170-line reader that stitched the parts of a split .tar.gz back together itself, because libarchive’s own multi-file open failed when switching parts under a compression filter. This one wasn’t in the record. An issue tracked it. 3.8.11 handles the case, so the reader is gone, and a new test reads all six compressed tar types from 1,024-byte parts, more than a hundred of them, which failed on 3.8.10.

Tests that pin the update

The update raised one test and added a set.

The raised one is a version floor, added with the update before this one. It reads the linked library’s version and now fails below 3.8.11:

#expect(numbers.lexicographicallyPrecedes([3, 8, 11]) == false,
        "\(ArchiveKit.libarchiveVersion)")

It’s crude, and the tests below would also fail on an older copy, but this one names the cause in a single line.

The set is upstream’s crafted files, kept as fixtures with a note of which upstream test file and pull request each came from. The XAR and ISO tests check for the error each fix adds, not just “no crash.” That matters for the XAR file: on the old library it sometimes doesn’t crash, so a test that only checked for survival could pass on the bug. Checking for the fix’s own message, “Too many bytes to skip”, can’t.

What I got wrong

The record was wrong, then incomplete. Before the previous update, it gave the release date as “verify against upstream”, pointed to the wrong directory for Purrchiver’s patches, and said the 7z encryption used the LZMA SDK’s AES. It uses Apple’s CommonCrypto. That update rewrote it as a table. But going into this one, the table was missing a patch: a gzip check I’d added two weeks earlier, which makes libarchive verify a .gz file’s checksum and length. libarchive left that as a to-do. 3.8.11 didn’t change the gzip reader, so the patch survived, and this update added its row. If upstream had changed that file, the checklist says copy it straight from the new release, and the merge would never have seen the patch. What would have caught it is a test that a .gz with a wrong checksum fails to read. The record is only as complete as I remember to make it. The test isn’t.

One of the new tests proves less than its commit says. The commit says the crafted files are “read by tests that crash or fail on 3.8.10.” The RAR symlink test only checks that some error comes back, and 3.8.10 already returned one (“Failed to read link”) in a release build. It passes on both versions.

Some of it is untested, or unrecorded.

  • The ISO-cycle test is turned off under AddressSanitizer. ASan’s bigger stack frames run a Swift task out of stack at about 850 levels, before the fix’s limit of 1,000 kicks in.
  • The crashes were reproduced with a test program linked to the archive engine, not through the app. Quick Look previews of ISO files use the same read, so a crafted ISO should have crashed the preview extension too, but I didn’t test that.
  • The conflict resolution rests on reasoning, not a test. The Deflate64 tests read every entry’s data, so none of them skips a Deflate64 entry in a streamed ZIP, which is the exact path the conflict was in.
  • The checklist’s AddressSanitizer run isn’t recorded for this update.

What I’d tell anyone vendoring a patched library

  • Keep one record of every patch, with why, where it’s reported and when to drop it, and give every patch a test, because the record misses things.
  • Merge with the pristine old release as the base. Never copy over.
  • Put a patch where the data flows through once, not in every caller.

If one of your patches vanished in the next library update, what would tell you: a record, a test, or nothing at all?

Related posts