libarchive never checked stored RAR files against their CRC
New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.
Picture a RAR that has no password and was damaged on the way to you. You open it, and the app asks for a password. You don’t have one, because there isn’t one. You’re stuck at a prompt that can never succeed, when what you needed was “this file is damaged, here’s what can be recovered.”
Purrchiver could do that. I found it while writing the
help page about RAR checksum errors,
reading the code to describe exactly what it does. While confirming it, I
found a second bug underneath. The first one was mine. The second was in
libarchive, the open-source library under most archive tools, including the
tar that ships with macOS (which is why that tar can
open an ordinary RAR on its own),
and it had been there for years.
Bug one: every checksum error looked like a wrong password
When you read an encrypted archive with the wrong password, the decrypted bytes are garbage, and the first sign is usually a failed checksum. So a “checksum error” really can mean a wrong password.
Purrchiver’s error classifier took that one step too far. It had “checksum error” on its list of messages that always mean a wrong password, whether you’d given a password or not. So plain damage in an archive with no password at all was reported as a wrong password, and recovery stopped instead of saving what it could.
That bug alone was enough to cause the prompt. The fix is to read a checksum error as a wrong password only when a password was actually given. With no password, it’s damage, and Purrchiver offers to recover what’s readable. I’d rather call a wrong password “damaged” than trap someone in a password prompt for an archive that doesn’t have one.
Bug two: stored files were never checked
The second bug is quieter. For some RAR entries, damage produced no error at all. The damaged file just read back, wrong, as if it were fine.
RAR can store a file without compressing it. These “stored” entries are common for things that don’t compress anyway, like photos and video. And RAR records a checksum for every file, stored or not, so a reader can confirm the data is intact.
libarchive’s RAR readers calculated that checksum for stored entries as the data went by. They just never compared it with the one in the archive:
- RAR4: the comparison only happened on one more read call after all the data had been delivered. But once the data was delivered, an earlier check returned “end of file” first, so that call never came.
- RAR5: the stored-file path returned “end of file” without calling the function that verifies checksums. The compressed path calls it at the end, gated on a pointer that stored entries never move.
Compressed entries were always checked. Only stored ones slipped through.
That made the first bug harder to see, not easier. Damage in a stored file produced no error at all, just wrong data. Whatever error did surface came later, from a different file in the archive, and pointed the wrong way.
Proving it
The simplest reproduction uses libarchive’s own test files with one byte changed. In the RAR5 test archive, “hello” becomes “hejlo”. In the RAR4 one, “test text” becomes “Test text”. Then extract to the screen:
bsdtar -xOf damaged.rarOn libarchive 3.8.10, on its development branch, and with the bsdtar that
macOS ships (libarchive 3.7.4), this prints the corrupted text and exits
successfully. unrar t, RarLab’s own tool, reports a checksum error for both.
So it isn’t a regression. It’s been this way for a long time.
The fix, in three passes
RAR5 was nearly a one-liner: verify the checksums when a stored entry reaches its end. Then one more wrinkle. RAR5 can use BLAKE2sp instead of CRC32, and a BLAKE2sp hash can only be finalized once, so reading past the end a second time reported a checksum error on an intact file. The check now runs once per entry.
RAR4 checks when a stored entry reaches its end, unless the file continues
in the next part of a multi-part set. My first version skipped every file split
across parts, on the theory that each part carried its own CRC. That was wrong,
and I found out while fact-checking the same help page: the last part’s header
holds the CRC for the whole file, and the running CRC already carries across
parts. So split files are now checked on their last part. Big downloads split
into .part01.rar, .part02.rar and so on are the common case for stored
files, so this mattered.
Seeking needed one more rule. If you jump around inside a stored entry instead of reading it start to finish, the running CRC no longer describes the whole file, so the check is skipped for that entry. Checking anyway broke three of libarchive’s own multi-part seek tests, which is how I found out.
With the patch, the same bsdtar command prints Checksum error: CRC32 or
File CRC error and exits with an error. libarchive’s full test suite passes.
The tests on my side
Purrchiver’s tests flip a byte in each kind of stored entry and check that the damage is caught: a plain RAR5 with CRC32, a RAR5 with BLAKE2sp (including reading it twice), a plain RAR4, and a ten-part RAR4 set with the damage in the middle of a file that spans three parts. Where there’s more than one file, recovery has to mark only the damaged one and bring the rest back intact. The classifier has its own test: a checksum error with a password is a wrong password, and without one it isn’t.
Reporting it upstream
Purrchiver carries the fix in its copy of libarchive, but the bug is in libarchive, so the fix belongs there. I reported it with the reproduction above, then corrected the RAR4 part publicly the next day when the split-file case turned up. A maintainer said a pull request would be welcome, with the tests in a separate commit first, so they can be seen failing before the fix.
The pull request does that: the tests come first and fail on the current code, then the fix, and reverting any part of the fix on its own makes specific tests fail again. It’s waiting for review. As with everything I send upstream, it says that it was prepared with help from an AI assistant.
When a fix lands upstream, whether it’s this pull request or another one, Purrchiver drops its own patch at the next libarchive update.
What it still can’t tell apart
One case is still ambiguous. Older RAR4 archives have no separate password check, so a wrong password and damaged data look the same. Purrchiver reports that as damage rather than trapping you in a password prompt. If recovery marks every file in a password-protected RAR4 as damaged, check the password first.
The lesson
A checksum that’s calculated but never compared is worse than no checksum, because everything downstream assumes it was checked. And an error message is evidence about where something failed, not why: the error that finally surfaced came from a different file than the damage.
Have you ever found a check in a library you depend on that was quietly never running?