From 834920162766874cf806be9665c198bf3ecb1ade Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:39:07 -0500 Subject: [PATCH] fix(elf): report empty kernel module signature ranges as malformed (#12471) --- docs/kernel-modules.md | 10 +++--- scripts/verify/baselines/raw-buffer-read.json | 2 +- src/native/elf/KernelModuleParser.ts | 35 ++++++++++--------- .../boundary/filesystem/kernelModule.test.ts | 34 ++++++++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/docs/kernel-modules.md b/docs/kernel-modules.md index 90405bad34..4fa4552e14 100644 --- a/docs/kernel-modules.md +++ b/docs/kernel-modules.md @@ -64,10 +64,10 @@ limitation that the decoded size is unknown. The signature trailer is the 28-byte magic `~Module signature appended~\n` preceded by a 12-byte `module_signature` (`algo`, `hash`, `id_type`, `signer_len`, `key_id_len`, three pad bytes, and big-endian `sig_len`). When -those parameter fields are zero and `id_type` is PKCS#7 (`2`), the preceding -`sig_len` bytes are the PKCS#7 range and its SHA-256. The range is not parsed -and no certificate, signer, or digest-coverage claim is made. An absent -trailer is observed absence, not proof the module is unsigned. +those parameter fields are zero, `id_type` is PKCS#7 (`2`), and `sig_len` is +positive, the report gives the preceding PKCS#7 range and its SHA-256. +Certificate, signer, and digest coverage remain unobserved. An absent trailer +records observed absence for this input. When section names are incomplete, a missing named section is `unknown` rather than `absent`. An empty file-backed section is present and empty. @@ -119,7 +119,7 @@ entries. `SHT_NOBITS` or a declared range that leaves the file is `signature_trailer_truncated` is reserved for an appended magic with fewer than 12 preceding bytes. ELF admission requires at least 52 (ELF32) or 64 (ELF64) bytes, so this inspection never returns it for an admitted file. A -`sig_len` that does not leave at least one module byte is +`sig_len` that leaves no module bytes or names an empty PKCS#7 range is `signature_length_inconsistent`. Nonzero `algo`, `hash`, signer, key-id, or pad fields for PKCS#7 are `signature_parameters_nonzero`. A parameter block whose `id_type` is not PKCS#7 is diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index 8a5204341f..80024a3ea4 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -44,7 +44,7 @@ "src/native/elf/ElfNoteParser.ts": 27, "src/native/elf/ElfUnwindTables.ts": 6, "src/native/elf/KernelBuildConfig.ts": 7, - "src/native/elf/KernelModuleParser.ts": 5, + "src/native/elf/KernelModuleParser.ts": 4, "src/native/hardening/ElfHardening.ts": 2, "src/native/initializers/ElfInitializerSymbols.ts": 4, "src/native/initializers/MachoInitializers.ts": 22, diff --git a/src/native/elf/KernelModuleParser.ts b/src/native/elf/KernelModuleParser.ts index a7233ea294..672b39017b 100644 --- a/src/native/elf/KernelModuleParser.ts +++ b/src/native/elf/KernelModuleParser.ts @@ -13,9 +13,10 @@ import { type InspectKernelModuleInput, } from "../../domain/native/kernelModule.js"; import type { AnalysisParameterLimit } from "../../domain/analysisSettings.js"; -import type { - ImageSection, - BinaryContainer, +import { + BinaryReader, + type ImageSection, + type BinaryContainer, } from "../../domain/binaryContainer.js"; import { readElfImage } from "../../domain/native/elfImage.js"; import { kernelCompressionCodecAt } from "./KernelCompressionCodec.js"; @@ -438,21 +439,23 @@ const parseSignature = (bytes: Buffer): Report["signature"] => { reason: "signature_trailer_truncated", }; const infoOffset = magic.offset - MODULE_SIGNATURE_INFO_BYTES; + const footer = new BinaryReader( + bytes.subarray(infoOffset, magic.offset), + false, + "elf", + ); const info = { location: { offset: infoOffset, bytes: MODULE_SIGNATURE_INFO_BYTES }, - algo: bytes[infoOffset] ?? 0, - hash: bytes[infoOffset + 1] ?? 0, - id_type: bytes[infoOffset + 2] ?? 0, - signer_len: bytes[infoOffset + 3] ?? 0, - key_id_len: bytes[infoOffset + 4] ?? 0, - pad: [ - bytes[infoOffset + 5] ?? 0, - bytes[infoOffset + 6] ?? 0, - bytes[infoOffset + 7] ?? 0, - ] as [number, number, number], - sig_len: bytes.readUInt32BE(infoOffset + 8), + algo: footer.u8(0), + hash: footer.u8(1), + id_type: footer.u8(2), + signer_len: footer.u8(3), + key_id_len: footer.u8(4), + pad: [footer.u8(5), footer.u8(6), footer.u8(7)] as [number, number, number], + sig_len: footer.u32(8), }; - if (info.sig_len >= infoOffset) + const isPkcs7 = info.id_type === MODULE_SIGNATURE_PKCS7_ID_TYPE; + if (info.sig_len >= infoOffset || (isPkcs7 && info.sig_len === 0)) return { status: "malformed", magic, @@ -460,7 +463,7 @@ const parseSignature = (bytes: Buffer): Report["signature"] => { pkcs7: null, reason: "signature_length_inconsistent", }; - if (info.id_type !== MODULE_SIGNATURE_PKCS7_ID_TYPE) + if (!isPkcs7) return { status: "unsupported", magic, diff --git a/tests/boundary/filesystem/kernelModule.test.ts b/tests/boundary/filesystem/kernelModule.test.ts index 7ece8468c8..86ec5e3089 100644 --- a/tests/boundary/filesystem/kernelModule.test.ts +++ b/tests/boundary/filesystem/kernelModule.test.ts @@ -6,10 +6,44 @@ import { createKernelModuleService } from "../../../src/composition/binaryDiagno import { compressedKernelModuleContainer, kernelModuleFixture, + moduleSignatureTrailer, } from "../../../src/native/elf/KernelModule.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; describe("kernel module stable filesystem boundary", () => { + it("retains trailer fields but withholds an empty PKCS#7 range", async () => { + const root = await createTestTempDirectory("rea-kernel-module-empty-sig-"); + const path = join(root, "demo.ko"); + await writeFile( + path, + kernelModuleFixture({ + signature: moduleSignatureTrailer(Buffer.alloc(0)), + }), + ); + const service = createKernelModuleService(); + try { + const result = await service.inspect({ path }); + if (!result.ok) throw result.error; + expect(result.value.normalized_result).toMatchObject({ + coverage: { status: "partial" }, + modinfo: { + entries: expect.arrayContaining([ + expect.objectContaining({ key: "license", value: "GPL" }), + ]), + }, + signature: { + status: "malformed", + reason: "signature_length_inconsistent", + magic: { bytes: 28 }, + info: { sig_len: 0, id_type: 2 }, + pkcs7: null, + }, + }); + } finally { + await service.close(); + } + }); + it("reports non-file and malformed ELF selections as caller input errors", async () => { const root = await createTestTempDirectory("rea-kernel-module-files-"); const path = join(root, "demo.ko");