From d9537fc75c293eb9b9a721fa7cb9c506c48e57d8 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:22:25 +0800 Subject: [PATCH] fix(native): validate ELF section backing against its type (#10066) fix(native): reject contradictory ELF section backing --- .../BinaryLayoutService.test.ts | 119 ++++++++++++++++++ src/domain/native/binaryLayout.test.ts | 2 + src/domain/native/binaryLayout.ts | 11 +- 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/src/application/binaryDiagnostics/BinaryLayoutService.test.ts b/src/application/binaryDiagnostics/BinaryLayoutService.test.ts index 660165d28c..34801fbbde 100644 --- a/src/application/binaryDiagnostics/BinaryLayoutService.test.ts +++ b/src/application/binaryDiagnostics/BinaryLayoutService.test.ts @@ -218,3 +218,122 @@ it.each([ error: { _tag: "AnalysisOutputError" }, }); }); + +it.each([ + { + type: "SHT_PROGBITS", + backing: "file", + offset: "0x40", + size: "0x10", + valid: true, + }, + { + type: "SHT_PROGBITS", + backing: "none", + offset: "0x40", + size: "0x10", + valid: false, + }, + { + type: "SHT_PROGBITS", + backing: "none", + offset: "0xffffffffffffffff", + size: "0x10", + valid: false, + }, + { + type: "SHT_NOBITS", + backing: "none", + offset: "0xffffffffffffffff", + size: "0x2000", + valid: true, + }, + { + type: "SHT_NOBITS", + backing: "file", + offset: "0x40", + size: "0x10", + valid: false, + }, + { + type: "SHT_NULL", + backing: "none", + offset: "0xffffffffffffffff", + size: "0x2000", + valid: true, + }, + { + type: "SHT_NULL", + backing: "file", + offset: "0x40", + size: "0x10", + valid: false, + }, + { + type: 0x70000001, + backing: "file", + offset: "0x40", + size: "0x10", + valid: true, + }, + { + type: 0x70000001, + backing: "none", + offset: "0x40", + size: "0x10", + valid: false, + }, + { + type: "SHT_PROGBITS", + backing: "file", + offset: "0x80", + size: "0x0", + valid: true, + }, +] as const)( + "validates section $type backing=$backing offset=$offset size=$size before Evidence", + async ({ type, backing, offset, size, valid }) => { + const layout = binaryLayoutFixture(); + layout.sections.push({ + index: 0, + name: { + display: "data", + bytes_base64: "ZGF0YQ==", + location: null, + unknown_reason: null, + }, + name_offset: "0x0", + type, + header_location: { offset: "0x0", bytes: "0x40" }, + address: "0x20000000000001", + offset, + size, + alignment: "0x1", + flags: "0x3", + link: 0, + info: 0, + entry_size: "0x0", + file_backing: backing, + }); + const service = new BinaryLayoutService({ + inspectAndClose(input, options) { + return this.inspect(input, options); + }, + identity: BINARY_LAYOUT_TEST_PROVIDER, + inspect: async () => ok(layout), + }); + const result = await service.inspect({ path: layout.artifact.path }); + if (valid) { + if (!result.ok) throw result.error; + expect(parseEvidence(result.value).normalized_result).toEqual(layout); + } else { + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisOutputError", + capturedOutput: layout.diagnostics, + }, + }); + } + }, +); diff --git a/src/domain/native/binaryLayout.test.ts b/src/domain/native/binaryLayout.test.ts index 1d4e8a9410..a4115364dc 100644 --- a/src/domain/native/binaryLayout.test.ts +++ b/src/domain/native/binaryLayout.test.ts @@ -141,6 +141,7 @@ it.each([ it.each([ "file-backed-outside", + "false-no-file-backing", "header-outside", "wrong-index", "noncanonical-base64", @@ -151,6 +152,7 @@ it.each([ const section = value.sections[0]; if (section === undefined) throw new Error("section missing"); if (problem === "file-backed-outside") section.file_backing = "file"; + if (problem === "false-no-file-backing") section.type = "SHT_PROGBITS"; if (problem === "header-outside") section.header_location.offset = "0x80"; if (problem === "wrong-index") section.index = 1; if (problem === "wrong-machine") value.architecture.machine = "EM_RISCV"; diff --git a/src/domain/native/binaryLayout.ts b/src/domain/native/binaryLayout.ts index 84ac95eb50..5dd682283e 100644 --- a/src/domain/native/binaryLayout.ts +++ b/src/domain/native/binaryLayout.ts @@ -281,7 +281,16 @@ export const binaryLayoutSchema = binaryLayoutObjectSchema.superRefine( "header_location", ]); checkName(section.name, ["sections", index, "name"]); - if (section.file_backing === "file") + const hasFileBytes = + section.type !== "SHT_NOBITS" && section.type !== "SHT_NULL"; + if (section.file_backing !== (hasFileBytes ? "file" : "none")) + context.addIssue({ + code: "custom", + path: ["sections", index, "file_backing"], + message: + "File backing must follow the reported section type; NOBITS and unused headers have no file content.", + }); + if (hasFileBytes) check(section.offset, section.size, ["sections", index]); } for (const [index, segment] of value.segments.entries()) {