From 7487261a35ba30e9362be71d9c922d932f5f4674 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:45:09 -0500 Subject: [PATCH] perf(elf): bound kernel module signature digest reads (#14024) --- .../KernelModuleService.test.ts | 51 +++++++++++++++ .../binaryDiagnostics/KernelModuleService.ts | 30 +++++---- src/native/elf/KernelModuleParser.test.ts | 51 +++++++++++++++ src/native/elf/KernelModuleParser.ts | 62 ++++++++++--------- 4 files changed, 149 insertions(+), 45 deletions(-) diff --git a/src/application/binaryDiagnostics/KernelModuleService.test.ts b/src/application/binaryDiagnostics/KernelModuleService.test.ts index 837184ee7bd..a34fd598617 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.test.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.test.ts @@ -102,6 +102,57 @@ it("rejects relative paths, extra arguments and invalid field budgets before rea expect(calls).toBe(0); }); +it("requires a nonempty PKCS#7 range ending at the file trailer", async () => { + const valid = { + status: "present", + reason: null, + magic: { offset: 484, bytes: 28 }, + info: { + location: { offset: 472, bytes: 12 }, + algo: 0, + hash: 0, + id_type: 2, + signer_len: 0, + key_id_len: 0, + pad: [0, 0, 0], + sig_len: 5, + }, + pkcs7: { location: { offset: 467, bytes: 5 }, sha256: digest }, + } satisfies KernelModule["signature"]; + const empty = { + ...valid, + info: { ...valid.info, sig_len: 0 }, + pkcs7: { + location: { offset: valid.info.location.offset, bytes: 0 }, + sha256: + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }, + }; + const beforeEof = { + ...valid, + magic: { ...valid.magic, offset: 483 }, + info: { ...valid.info, location: { ...valid.info.location, offset: 471 } }, + pkcs7: { + ...valid.pkcs7, + location: { ...valid.pkcs7.location, offset: 466 }, + }, + }; + const outcomes = await Promise.all( + [valid, empty, beforeEof].map((signature) => + new KernelModuleService({ + identity, + inspect: () => Promise.resolve(ok({ ...report, signature })), + }).inspect({ path: report.artifact.path }), + ), + ); + expect(outcomes.map((outcome) => outcome.ok)).toEqual([true, false, false]); + for (const outcome of outcomes.slice(1)) { + if (outcome.ok) + throw new Error("Expected inconsistent signature rejection"); + expect(outcome.error).toBeInstanceOf(AnalysisOutputError); + } +}); + it("rejects a report whose entry count does not match the examined sections", async () => { const result = await new KernelModuleService({ identity, diff --git a/src/application/binaryDiagnostics/KernelModuleService.ts b/src/application/binaryDiagnostics/KernelModuleService.ts index b4c211f1067..ae913cfeafb 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.ts @@ -194,6 +194,7 @@ const versionsConsistent = ( const signatureConsistent = ( signature: NonNullable, + fileBytes: number, ): boolean => { if (signature.status === "absent") return ( @@ -202,18 +203,16 @@ const signatureConsistent = ( signature.info === null && signature.pkcs7 === null ); - if (signature.magic === null || signature.reason === null) { - if (signature.status !== "present") return false; - } + if ( + signature.magic === null || + signature.magic.bytes !== MODULE_SIGNATURE_MAGIC.length || + signature.magic.offset !== fileBytes - MODULE_SIGNATURE_MAGIC.length + ) + return false; if (signature.status === "present") { const info = signature.info; const pkcs7 = signature.pkcs7; - if ( - signature.reason !== null || - signature.magic === null || - info === null || - pkcs7 === null - ) + if (signature.reason !== null || info === null || pkcs7 === null) return false; return ( info.id_type === MODULE_SIGNATURE_PKCS7_ID_TYPE && @@ -222,10 +221,10 @@ const signatureConsistent = ( info.signer_len === 0 && info.key_id_len === 0 && info.pad.every((value) => value === 0) && + info.sig_len > 0 && info.sig_len === pkcs7.location.bytes && pkcs7.location.offset + pkcs7.location.bytes === info.location.offset && signature.magic.offset === info.location.offset + info.location.bytes && - signature.magic.bytes === MODULE_SIGNATURE_MAGIC.length && info.location.bytes === MODULE_SIGNATURE_INFO_BYTES && info.sig_len < info.location.offset ); @@ -234,12 +233,11 @@ const signatureConsistent = ( if (signature.status === "unsupported") return ( signature.reason === "signature_id_type_not_pkcs7" && - signature.info !== null && - signature.magic !== null + signature.info !== null ); - if (signature.reason === "signature_trailer_truncated") - return signature.info === null && signature.magic !== null; - return signature.info !== null && signature.magic !== null; + return signature.reason === "signature_trailer_truncated" + ? signature.info === null + : signature.info !== null; }; const sizedConsistent = ( @@ -337,7 +335,7 @@ const validReport = ( return ( modinfoConsistent(report.modinfo, input) && versionsConsistent(report.versions) && - signatureConsistent(report.signature) && + signatureConsistent(report.signature, report.artifact.bytes) && sizedConsistent(report.init_text) && sizedConsistent(report.exit_text) ); diff --git a/src/native/elf/KernelModuleParser.test.ts b/src/native/elf/KernelModuleParser.test.ts index 231aefca278..a38395a2b30 100644 --- a/src/native/elf/KernelModuleParser.test.ts +++ b/src/native/elf/KernelModuleParser.test.ts @@ -242,6 +242,57 @@ it("observes a version name with nonzero padding and rejects a missing NUL", asy }); }); +it("hashes the entire appended signature through bounded source reads", async () => { + const pkcs7 = Buffer.alloc((2 << 20) + 17, 0x6a); + pkcs7[0] = 0x31; + pkcs7[pkcs7.length - 1] = 0x7f; + const trailer = moduleSignatureTrailer(pkcs7); + const bytes = kernelModuleFixture({ signature: trailer }); + const signatureOffset = bytes.length - trailer.length; + const end = signatureOffset + pkcs7.length; + const reads: Array<{ offset: number; length: number }> = []; + const source = { + size: bytes.length, + prefix: bytes.subarray(0, 4096), + readRange: async (offset: number, length: number) => { + if (offset < end && offset + length > signatureOffset) + reads.push({ offset, length }); + return bytes.subarray(offset, offset + length); + }, + }; + const artifact = { + path: artifactPath, + sha256: "ab".repeat(32), + bytes: bytes.length, + }; + const input = inspectKernelModuleInputSchema.parse({ path: artifactPath }); + const report = await parseKernelModuleFromHandle(source, artifact, input); + expect(report.signature?.pkcs7).toEqual({ + location: { offset: signatureOffset, bytes: pkcs7.length }, + sha256: createHash("sha256").update(pkcs7).digest("hex"), + }); + let nextOffset = signatureOffset; + for (const { offset, length } of reads) { + expect(offset).toBe(nextOffset); + expect(length).toBeLessThanOrEqual(1 << 20); + nextOffset += length; + } + expect(nextOffset).toBe(end); + await expect( + parseKernelModuleFromHandle( + { + ...source, + readRange: async (offset, length) => + offset + length === end + ? bytes.subarray(offset, end - 1) + : source.readRange(offset, length), + }, + artifact, + input, + ), + ).rejects.toMatchObject({ reason: "format" }); +}); + it("reports an inconsistent signature length without a PKCS#7 range", async () => { const report = await parse( kernelModuleFixture({ diff --git a/src/native/elf/KernelModuleParser.ts b/src/native/elf/KernelModuleParser.ts index 9b92a8b1671..13464d600f7 100644 --- a/src/native/elf/KernelModuleParser.ts +++ b/src/native/elf/KernelModuleParser.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { ArtifactReaderFailure, assertArtifactReadActive, @@ -32,6 +33,7 @@ type Report = KernelModule; type Limitations = string[]; const MAGIC = Buffer.from(MODULE_SIGNATURE_MAGIC, "latin1"); +const SIGNATURE_HASH_CHUNK_BYTES = 1 << 20; const MODULE_CODECS = ["gzip", "xz", "zstd"] as const; const exceeds = (examined: number, maxEntries: number): void => { @@ -482,38 +484,51 @@ const parseSignatureTrailer = ( }; }; +const readSourceRange = async ( + source: NativeByteSource, + offset: number, + length: number, +): Promise => { + if (length === 0) return Buffer.alloc(0); + const { prefix } = source; + if (prefix !== undefined && offset + length <= prefix.length) + return prefix.subarray(offset, offset + length); + const bytes = await source.readRange(offset, length); + if (bytes.length !== length) + throw new ArtifactReaderFailure( + "format", + "Kernel module ended before the admitted file range.", + ); + return bytes; +}; + const parseSignatureFromSource = async ( source: NativeByteSource, + signal: AbortSignal | undefined, ): Promise => { const tailLen = Math.min( source.size, MAGIC.length + MODULE_SIGNATURE_INFO_BYTES, ); - const tailOffset = source.size - tailLen; - const tail = - tailLen === 0 - ? Buffer.alloc(0) - : source.prefix !== undefined && - tailOffset + tailLen <= source.prefix.length - ? source.prefix.subarray(tailOffset, tailOffset + tailLen) - : await source.readRange(tailOffset, tailLen); + const tail = await readSourceRange(source, source.size - tailLen, tailLen); const trailer = parseSignatureTrailer(source.size, tail); if (trailer.done) return trailer.signature; - const pkcs7 = - source.prefix !== undefined && - trailer.pkcs7Offset + trailer.pkcs7Bytes <= source.prefix.length - ? source.prefix.subarray( - trailer.pkcs7Offset, - trailer.pkcs7Offset + trailer.pkcs7Bytes, - ) - : await source.readRange(trailer.pkcs7Offset, trailer.pkcs7Bytes); + + const hash = createHash("sha256"); + const end = trailer.pkcs7Offset + trailer.pkcs7Bytes; + for (let offset = trailer.pkcs7Offset; offset < end;) { + assertArtifactReadActive(signal); + const length = Math.min(end - offset, SIGNATURE_HASH_CHUNK_BYTES); + hash.update(await readSourceRange(source, offset, length)); + offset += length; + } return { status: "present", magic: trailer.magic, info: trailer.info, pkcs7: { location: { offset: trailer.pkcs7Offset, bytes: trailer.pkcs7Bytes }, - sha256: sha256Hex(pkcs7), + sha256: hash.digest("hex"), }, reason: null, }; @@ -609,17 +624,6 @@ const versionsSectionLocation = ( return { ready: true, location, compressed: section.compressed }; }; -const readSourceRange = async ( - source: NativeByteSource, - offset: number, - length: number, -): Promise => - length === 0 - ? Buffer.alloc(0) - : source.prefix !== undefined && offset + length <= source.prefix.length - ? source.prefix.subarray(offset, offset + length) - : source.readRange(offset, length); - interface KernelModulePrepared { readonly facts: ReturnType; readonly sections: readonly ElfSection[]; @@ -858,7 +862,7 @@ export const parseKernelModuleFromHandle = async ( input.max_entries, ) : versionsLoc.result; - const signature = await parseSignatureFromSource(source); + const signature = await parseSignatureFromSource(source, signal); return finalizeKernelModule( source.size, artifact,