authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-02-17 16:18:33-08:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-02-18 09:01:10+01:00
log0779e847f79851419dfeb39595b1817ce72ea9fa
tree82ece10dcab4917af58ac4b0063a29f52f50de52
parentd2e70ef84a912947f6485f6fc480030c2d6a8c8f

Skip empty/invalid records/certs in MacOS keychain files

In the original PR that implemented this (https://github.com/ziglang/zig/pull/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). Fixes #22870

1 files changed, 6 insertions(+), 0 deletions(-)

lib/std/crypto/Certificate/Bundle/macos.zig+6
......@@ -61,10 +61,16 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void {
6161 }
6262
6363 for (record_list) |record_offset| {
64 // An offset of zero means that the record is not present.
65 // An offset that is not 4-byte-aligned is invalid.
66 if (record_offset == 0 or record_offset % 4 != 0) continue;
67
6468 try stream.seekTo(db_header.schema_offset + table_offset + record_offset);
6569
6670 const cert_header = try reader.readStructEndian(X509CertHeader, .big);
6771
72 if (cert_header.cert_size == 0) continue;
73
6874 try cb.bytes.ensureUnusedCapacity(gpa, cert_header.cert_size);
6975
7076 const cert_start = @as(u32, @intCast(cb.bytes.items.len));