Skip empty/invalid records/certs in MacOS keychain files #22927
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the original PR that implemented
rescanMac
(#14325), it included a list of references for the keychain format. Multiple of those references include the checks that are added in this commit, and empirically this fixes the loading of a real keychain file that was previously failing (it had both a record with offset 0 and a record with cert_size 0).To give more detail: when not skipping a record with offset zero, it would attempt to read the base of the table as a
X509CertHeader
, but the base of the table is actually aTableHeader
. It would then take this misinterpretedX509CertHeader
and read N bytes that follow it (where N is equal to the misinterpretedcert_size
), and then try to parse those bytes as a DER cert. These bytes are just some arbitrary bytes in the keychain file, though, not the actual start of a cert, so it would fail at some arbitrary point inCertificate.parse
.What this means is that #22701 didn't directly cause a regression, but instead exposed a latent bug, and that these zero-record-offset/zero-cert-size records are seemingly much more common in
/Library/Keychains/System.keychain
.Fixes #22870
Specifically, this has fixed the regression for @nurpax detailed in https://ziggit.dev/t/zig-fetch-failing-with-discover-remote-git-server-capabilities-certificatebundleloadfailure/8608
However, this fix does not explain/account for the 'swap the ordering' workaround from #22870 (comment), so this may not be a full solution. Would appreciate @jedisct1 checking to see that this fixes the regression for them.