diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index db36dfb8d2..89ebb026ce 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -67,7 +67,7 @@ "src/ghidra/extensions/GhidraExtensions.ts": 1, "src/go/GoStringLiterals.ts": 4, "src/gpu/AmdGpuMetadata.ts": 4, - "src/gpu/ClangOffloadBundle.ts": 14, + "src/gpu/ClangOffloadBundle.ts": 10, "src/gpu/CudaFatbinary.ts": 13, "src/gpu/GpuInventory.ts": 3, "src/hopper/HopperAnalysisProfile.ts": 1, diff --git a/src/gpu/ClangOffloadBundle.ts b/src/gpu/ClangOffloadBundle.ts index e0bad8f2e6..5d32b1921c 100644 --- a/src/gpu/ClangOffloadBundle.ts +++ b/src/gpu/ClangOffloadBundle.ts @@ -3,6 +3,7 @@ import { scheduler } from "node:timers/promises"; import type { GpuObject } from "../domain/gpu/gpuCodeObjects.js"; import { BinaryFormatFailure, + BinaryReader, BinaryResourceFailure, } from "../domain/binaryContainer.js"; import { assertArtifactReadActive } from "../resources/index.js"; @@ -21,6 +22,8 @@ import { } from "./AmdGpuTarget.js"; export const CLANG_BUNDLE_MAGIC = Buffer.from("__CLANG_OFFLOAD_BUNDLE__"); export const CCOB_MAGIC = Buffer.from("CCOB"); +const CLANG_BUNDLE_HEADER_BYTES = 32; +const CLANG_BUNDLE_ENTRY_BYTES = 24; export const readClangBundleDirectory = async ( source: GpuSource, base: number, @@ -29,14 +32,21 @@ export const readClangBundleDirectory = async ( declarations: { object: GpuObject; amdTarget: AmdGpuTargetIdentity | null }[]; end: number; }> => { - const header = await source.read(base, 32); - if (!header.subarray(0, 24).equals(CLANG_BUNDLE_MAGIC)) + const header = await source.read(base, CLANG_BUNDLE_HEADER_BYTES); + if (!header.subarray(0, CLANG_BUNDLE_MAGIC.length).equals(CLANG_BUNDLE_MAGIC)) rejectGpu("Invalid Clang offload bundle magic"); - const declaredCount = header.readBigUInt64LE(24); - // Every entry needs three 64-bit words before its variable-length ID. - // Compare before numeric conversion so even a uint64 maximum stays a - // malformed extent rather than a capacity or host-precision refusal. - if (declaredCount > BigInt(Math.floor((source.size - base - 32) / 24))) + const declaredCount = new BinaryReader( + header, + true, + "clang offload bundle", + ).word(CLANG_BUNDLE_MAGIC.length, 64); + // Each directory entry needs three 64-bit words before its variable ID. + const sourceEnd = BigInt(source.size); + const firstEntry = base + CLANG_BUNDLE_HEADER_BYTES; + if ( + declaredCount * BigInt(CLANG_BUNDLE_ENTRY_BYTES) > + sourceEnd - BigInt(firstEntry) + ) rejectGpu("Clang offload bundle directory is truncated"); const count = gpuCount(declaredCount); if (count > 65536) @@ -52,17 +62,41 @@ export const readClangBundleDirectory = async ( object: GpuObject; amdTarget: AmdGpuTargetIdentity | null; }[] = []; - let position = base + 32; + let position = firstEntry; + let firstPayload = sourceEnd; + let lastPayloadEnd = firstEntry; for (let index = 0; index < count; index++) { if (index % 256 === 0) { await scheduler.yield(); assertArtifactReadActive(signal); } - const h = await source.read(position, 24), - offset = gpuCount(BigInt(base) + h.readBigUInt64LE(0)), - size = gpuCount(h.readBigUInt64LE(8)), - nameSize = gpuCount(h.readBigUInt64LE(16)); - if (nameSize > 65536 || position - base + 24 + nameSize > GPU_REPORT_BYTES) + const entry = new BinaryReader( + await source.read(position, CLANG_BUNDLE_ENTRY_BYTES), + true, + "clang offload bundle", + ); + const rawOffset = BigInt(base) + entry.word(0, 64); + const rawSize = entry.word(8, 64); + const rawNameSize = entry.word(16, 64); + if (rawOffset > sourceEnd || rawSize > sourceEnd - rawOffset) + rejectGpu("HIP entry range is outside its bundle"); + const offset = Number(rawOffset); + const size = Number(rawSize); + if (rawSize > 0n) { + if (rawOffset < firstPayload) firstPayload = rawOffset; + lastPayloadEnd = Math.max(lastPayloadEnd, offset + size); + } + + const nameOffset = position + CLANG_BUNDLE_ENTRY_BYTES; + const minimumDirectoryEnd = + BigInt(nameOffset + (count - index - 1) * CLANG_BUNDLE_ENTRY_BYTES) + + rawNameSize; + if (minimumDirectoryEnd > sourceEnd) + rejectGpu("HIP bundle ID or following entry is outside its bundle"); + if (minimumDirectoryEnd > firstPayload) + rejectGpu("HIP entry overlaps its bundle directory"); + const nameSize = Number(rawNameSize); + if (nameSize > 65536 || nameOffset - base + nameSize > GPU_REPORT_BYTES) throw new BinaryResourceFailure( "structure", GPU_REPORT_BYTES, @@ -71,7 +105,7 @@ export const readClangBundleDirectory = async ( let name: string; try { name = decodeStrictUtf8( - await source.read(position + 24, nameSize), + await source.read(nameOffset, nameSize), "preserve", ); } catch (cause) { @@ -79,8 +113,7 @@ export const readClangBundleDirectory = async ( return rejectGpu("HIP bundle ID is not valid UTF-8"); throw cause; } - if (offset > source.size || size > source.size - offset) - rejectGpu("HIP entry range is outside its bundle"); + const row = gpuObject(source, { offset, size, @@ -89,22 +122,15 @@ export const readClangBundleDirectory = async ( : isAmdGpuBundleId(name) ? "amdgpu" : "unknown", - metadata: { offset: position, size: 24 + nameSize }, + metadata: { offset: position, size: CLANG_BUNDLE_ENTRY_BYTES + nameSize }, }); row.bundle_id = name; const amdTarget = readAmdGpuBundleTarget(name); row.target = amdTarget?.key ?? null; declarations.push({ object: row, amdTarget }); - position += 24 + nameSize; + position = nameOffset + nameSize; } - let end = position; - for (const { object: row } of declarations) { - if (row.encoded_size === 0) continue; - if (row.location.offset < position) - rejectGpu("HIP entry overlaps its bundle directory"); - end = Math.max(end, row.location.offset + row.encoded_size); - } - return { declarations, end }; + return { declarations, end: Math.max(position, lastPayloadEnd) }; }; export const readCcobHeader = (bytes: Buffer, remaining: number) => { if (bytes.length < 20) rejectGpu("CCOB header is truncated"); diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index 324ac34b19..c939c37c24 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -1347,3 +1347,31 @@ it("distinguishes impossible Clang bundle counts from a framed over-capacity dir if (result.ok) throw new Error("Expected directory capacity failure"); expect(result.error._tag).toBe("AnalysisResourceConstraintError"); }); + +it("checks Clang bundle ID bounds before the retained directory allowance", async () => { + const name = "host-" + "x".repeat(65_537 - 5); + const header = Buffer.alloc(56); + header.write("__CLANG_OFFLOAD_BUNDLE__"); + header.writeBigUInt64LE(1n, 24); + header.writeBigUInt64LE(BigInt(header.length + name.length), 32); + header.writeBigUInt64LE(1n, 40); + header.writeBigUInt64LE(BigInt(name.length), 48); + const framed = Buffer.concat([header, Buffer.from(name), Buffer.alloc(1)]); + const limited = await inspect(framed, { analysis: "inventory" }); + if (limited.ok) throw new Error("Expected a directory capacity failure"); + expect(limited.error._tag).toBe("AnalysisResourceConstraintError"); + + const short = Buffer.from(header); + short.writeBigUInt64LE(BigInt(short.length), 32); + short.writeBigUInt64LE(0n, 40); + const overlaps = Buffer.from(framed); + overlaps.writeBigUInt64LE(BigInt(header.length + 1), 32); + for (const bytes of [short, overlaps]) { + const result = await inspect(bytes, { analysis: "inventory" }); + if (result.ok) throw new Error("Expected a malformed bundle directory"); + expect(result.error).toMatchObject({ + _tag: "AnalysisInputError", + issues: [{ path: ["path"], reason: "invalid_format" }], + }); + } +});