diff --git a/src/artifacts/CpioArtifactReader.ts b/src/artifacts/CpioArtifactReader.ts index ec05e9532..19bfe2d18 100644 --- a/src/artifacts/CpioArtifactReader.ts +++ b/src/artifacts/CpioArtifactReader.ts @@ -228,6 +228,20 @@ const drain = async (chunks: AsyncIterable): Promise => { for await (const chunk of chunks) void chunk; }; +/** Human-readable limitation for a collected (or skipped) symlink target. */ +const symlinkTargetLimitation = ( + header: CpioHeader, + target: Buffer | undefined, +): string => { + if (target === undefined) + return `Symlink target of ${header.fileSize} bytes exceeds ${MAX_SYMLINK_TARGET_BYTES} bytes and was not read.`; + try { + return `Symlink target: ${new TextDecoder("utf-8", { fatal: true }).decode(target)}`; + } catch { + return "Symlink target is not valid UTF-8 and was not decoded; the archived bytes are preserved only as a byte count."; + } +}; + /** * Sequential reader for a gzip-compressed cpio archive, such as an installer * package's Scripts or Payload. Members are decompressed once, in order: open @@ -357,31 +371,7 @@ export class CpioArtifactReader implements ArtifactReader { const type = header.mode & S_IFMT; const path = memberPath(raw); if (type === S_IFLNK) { - // A hostile header can declare a multi-gigabyte target; stream it past. - let target: Buffer | undefined; - if (header.fileSize > MAX_SYMLINK_TARGET_BYTES) - await drain(this.#verified(source, header, raw)); - else target = await this.#collect(source, header, raw); - if (path !== undefined) { - let limitation: string; - if (target === undefined) - limitation = `Symlink target of ${header.fileSize} bytes exceeds ${MAX_SYMLINK_TARGET_BYTES} bytes and was not read.`; - else { - try { - limitation = `Symlink target: ${new TextDecoder("utf-8", { fatal: true }).decode(target)}`; - } catch { - limitation = - "Symlink target is not valid UTF-8 and was not decoded; the archived bytes are preserved only as a byte count."; - } - } - yield entryOf({ - path, - kind: "symlink", - key, - header, - limitations: [limitation], - }); - } + yield* this.#symlink(source, { header, raw, key }, path); return; } if ( @@ -453,6 +443,59 @@ export class CpioArtifactReader implements ArtifactReader { }); } + /** + * One symlink member. A hostile header can declare a multi-gigabyte target, + * so oversized targets stream past. CRC failures surface before the entry + * is yielded, outside the scanner's per-entry recovery: under + * record-and-continue the symlink becomes an unavailable occurrence so + * later siblings still inventory. + */ + async *#symlink( + source: ByteSource, + member: { + readonly header: CpioHeader; + readonly raw: string; + readonly key: string; + }, + path: string | undefined, + ): AsyncGenerator { + const { header, raw, key } = member; + let target: Buffer | undefined; + let crcLimitation: string | undefined; + try { + if (header.fileSize > MAX_SYMLINK_TARGET_BYTES) + await drain(this.#verified(source, header, raw)); + else target = await this.#collect(source, header, raw); + } catch (cause: unknown) { + if ( + this.integrity !== "record-and-continue" || + !(cause instanceof ArtifactReaderFailure) || + cause.reason !== "integrity" + ) + throw cause; + crcLimitation = cause.message; + } + if (path === undefined) return; + if (crcLimitation !== undefined) { + yield entryOf({ + path, + kind: "symlink", + key, + header, + limitations: [crcLimitation], + contentUnavailable: true, + }); + return; + } + yield entryOf({ + path, + kind: "symlink", + key, + header, + limitations: [symlinkTargetLimitation(header, target)], + }); + } + #alias( path: string, key: string, diff --git a/src/artifacts/XarArtifactReader.ts b/src/artifacts/XarArtifactReader.ts index 1c4bb20b2..e58977a93 100644 --- a/src/artifacts/XarArtifactReader.ts +++ b/src/artifacts/XarArtifactReader.ts @@ -55,6 +55,9 @@ interface XarData { readonly archivedChecksum: | { readonly algorithm: string; readonly value: string } | undefined; + readonly unsupportedArchivedChecksum: + | { readonly style: string; readonly value: string } + | undefined; } interface XarMember { @@ -131,6 +134,10 @@ const parseData = (file: Element): XarData | undefined => { archived === undefined || "unsupported" in archived ? undefined : { algorithm: archived.algorithm, value: archived.value }, + unsupportedArchivedChecksum: + archived !== undefined && "unsupported" in archived + ? { style: archived.unsupported, value: archived.value } + : undefined, }; }; @@ -154,6 +161,7 @@ const assertRawSize = (member: XarMember, data: XarData | undefined): void => { const memberLimitations = ( member: XarMember, unsupportedChecksum: XarData["unsupportedChecksum"], + unsupportedArchived: XarData["unsupportedArchivedChecksum"], classified: { readonly limitations: readonly string[] }, ): string[] => [ ...(member.kind === "symlink" && member.link !== undefined @@ -164,6 +172,11 @@ const memberLimitations = ( : [ `Member declares unsupported checksum ${unsupportedChecksum.style}; integrity not verified.`, ]), + ...(unsupportedArchived === undefined + ? [] + : [ + `Member declares unsupported archived checksum ${unsupportedArchived.style}; stored-byte integrity not verified.`, + ]), ...classified.limitations, ]; @@ -237,15 +250,23 @@ const declaredDigest = ( : { declaredSha256: null }; }; +/** + * Absolute decoded-byte ceiling for one gzip member. The declared size is + * attacker-controlled, so it cannot be the only inflation bound. + */ +const MAX_DECODED_MEMBER_BYTES = 256 * 1024 * 1024; + /** Bound decoded output to the TOC-declared extracted size. */ class SizeBound extends Transform { #seen = 0; + #cap: number; constructor( private readonly path: string, private readonly expected: number, ) { super(); + this.#cap = Math.min(expected, MAX_DECODED_MEMBER_BYTES); } override _transform( @@ -254,11 +275,13 @@ class SizeBound extends Transform { done: TransformCallback, ): void { this.#seen += chunk.length; - if (this.#seen > this.expected) { + if (this.#seen > this.#cap) { done( new ArtifactReaderFailure( "limit", - `xar member ${this.path} decodes beyond its declared ${this.expected} bytes`, + this.expected > MAX_DECODED_MEMBER_BYTES + ? `xar member ${this.path} decodes beyond the ${MAX_DECODED_MEMBER_BYTES}-byte member ceiling` + : `xar member ${this.path} decodes beyond its declared ${this.expected} bytes`, ), ); return; @@ -309,7 +332,7 @@ class ChecksumVerifier extends Transform { ? null : new ArtifactReaderFailure( "integrity", - `xar ${this.expected.algorithm} checksum disagrees with content: ${this.path}`, + `xar ${this.expected.algorithm} checksum disagrees with content: ${this.path} (declared ${this.expected.value}, observed ${observed})`, ), ); } @@ -424,9 +447,26 @@ export class XarArtifactReader implements ArtifactReader { compressed: Buffer, headerAlg: number, ): Promise { - const checksum = childElement(toc, "checksum"); + // Fixed-header checksum IDs follow libarchive's XAR reader: 0 is none, + // 1 is SHA-1, 2 is MD5, 3 is SHA-256, 4 is SHA-512. const expectedStyle = - headerAlg === 1 ? "sha1" : headerAlg === 2 ? "md5" : undefined; + headerAlg === 0 + ? null + : headerAlg === 1 + ? "sha1" + : headerAlg === 2 + ? "md5" + : headerAlg === 3 + ? "sha256" + : headerAlg === 4 + ? "sha512" + : undefined; + if (expectedStyle === undefined) + throw new ArtifactReaderFailure( + "format", + `xar header declares unsupported TOC checksum algorithm ${headerAlg}`, + ); + const checksum = childElement(toc, "checksum"); if (checksum === undefined) { if (headerAlg !== 0) throw new ArtifactReaderFailure( @@ -436,10 +476,10 @@ export class XarArtifactReader implements ArtifactReader { return; } const style = checksum.getAttribute("style")?.toLowerCase() ?? ""; - if (expectedStyle !== undefined && style !== expectedStyle) + if (expectedStyle !== null && style !== expectedStyle) throw new ArtifactReaderFailure( "format", - `xar TOC checksum style ${style || "(missing)"} does not match header algorithm ${expectedStyle}`, + `xar TOC checksum style ${style || "(missing)"} does not match header algorithm ${expectedStyle ?? "none"}`, ); const algorithm = CHECKSUM_ALGORITHMS[style]; if (algorithm === undefined) @@ -552,8 +592,11 @@ export class XarArtifactReader implements ArtifactReader { const data = member.data; assertRawSize(member, data); const unsupportedChecksum = data?.unsupportedChecksum; + const unsupportedArchived = data?.unsupportedArchivedChecksum; const classified = - member.kind === "file" && unsupportedChecksum === undefined + member.kind === "file" && + unsupportedChecksum === undefined && + unsupportedArchived === undefined ? await this.#nestedArchive(member) : { nested: undefined as XarNestedArchive | undefined, @@ -572,7 +615,12 @@ export class XarArtifactReader implements ArtifactReader { : null, ...declaredDigest(member.data?.extractedChecksum), unpacked: false, - limitations: memberLimitations(member, unsupportedChecksum, classified), + limitations: memberLimitations( + member, + unsupportedChecksum, + unsupportedArchived, + classified, + ), adapterKey: member.path, ...(classified.nested === undefined ? {}