mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(bun): admit Mach-O graph segments within their image (#12275)
This commit is contained in:
1 parent
3acc952925
commit
a9ad92001d
2 files changed
+68
-15
No files matched your search
@@ -6,6 +6,10 @@ import { open, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
CPU_ARM64,
|
||||
universalMacho,
|
||||
} from "../../../tests/fixtures/universalMacho.js";
|
||||
import { probeBunStandaloneGraph } from "./BunModuleGraphLayout.js";
|
||||
import { decodeBunModuleGraph } from "./BunModuleGraphMembers.js";
|
||||
import {
|
||||
@@ -289,6 +293,43 @@ it.each([
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["thin", "universal"] as const)(
|
||||
"requires a Bun segment to fit its %s Mach-O image",
|
||||
async (format) => {
|
||||
const payload = bunPrefixedPayload(
|
||||
bunModuleGraphPayload({
|
||||
modules: [{ name: "main.js", contents: Buffer.from("body") }],
|
||||
}),
|
||||
);
|
||||
const wrap = (thin: Buffer) =>
|
||||
format === "thin"
|
||||
? thin
|
||||
: universalMacho([{ bytes: thin, cpuType: CPU_ARM64, cpuSubtype: 0 }])
|
||||
.bytes;
|
||||
const thin = machoBunSection(payload);
|
||||
const control = await probe(wrap(thin));
|
||||
expect(control.result).toMatchObject({
|
||||
status: "present",
|
||||
claim: { carrier: "mach-o-section" },
|
||||
});
|
||||
const malformed = Buffer.from(thin);
|
||||
const segment = 32;
|
||||
const size = BigInt(payload.length) + 1n;
|
||||
malformed.writeBigUInt64LE(size, segment + 32);
|
||||
malformed.writeBigUInt64LE(size, segment + 48);
|
||||
// The universal fixture pads after the slice. The excess byte is outside
|
||||
// the slice even when the outer file has bytes at that offset.
|
||||
const { result } = await probe(wrap(malformed));
|
||||
expect(result).toMatchObject({
|
||||
status: "present",
|
||||
failure: {
|
||||
reason: "format",
|
||||
message: expect.stringContaining("segment range is outside the file"),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("probes a section graph without reading its payload data", async () => {
|
||||
const contents = Buffer.alloc(1024 * 1024, 0x61);
|
||||
const bytes = machoBunSection(
|
||||
|
||||
@@ -17,7 +17,10 @@ import {
|
||||
machoSectionBacking,
|
||||
readMachoSectionHeader,
|
||||
} from "../../domain/apple/machoSectionHeader.js";
|
||||
import { readMachoSegmentHeader } from "../../domain/apple/machoSegmentHeader.js";
|
||||
import {
|
||||
admitMachoSegmentCommand,
|
||||
readMachoSegmentHeader,
|
||||
} from "../../domain/apple/machoSegmentHeader.js";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
|
||||
import {
|
||||
@@ -494,7 +497,9 @@ const machOClaims = async (
|
||||
): Promise<readonly SectionClaim[]> => {
|
||||
const { handle, fileSize, signal, limits } = source;
|
||||
if (imageOffset < 0 || imageOffset >= fileSize) return [];
|
||||
const header = await tryRead(handle, imageOffset, 12, fileSize, signal);
|
||||
const imageSize = Math.min(fileSize - imageOffset, imageLimit);
|
||||
const imageEnd = imageOffset + imageSize;
|
||||
const header = await tryRead(handle, imageOffset, 12, imageEnd, signal);
|
||||
if (header === null || header.length < 4) return [];
|
||||
const facts = readMachoHeaderPrefixFacts(header);
|
||||
if (facts.identity === null)
|
||||
@@ -502,12 +507,12 @@ const machOClaims = async (
|
||||
const full = await tryRead(
|
||||
handle,
|
||||
imageOffset,
|
||||
Math.min(32, imageLimit),
|
||||
fileSize,
|
||||
Math.min(32, imageSize),
|
||||
imageEnd,
|
||||
signal,
|
||||
);
|
||||
if (full === null) return [];
|
||||
const parsed = readMachoLoadCommandTableHeader(full, imageLimit);
|
||||
const parsed = readMachoLoadCommandTableHeader(full, imageSize);
|
||||
if (!parsed.ok || parsed.value === null) return [];
|
||||
const table = parsed.value;
|
||||
const {
|
||||
@@ -526,7 +531,7 @@ const machOClaims = async (
|
||||
handle,
|
||||
imageOffset + headerBytes,
|
||||
sizeofcmds,
|
||||
Math.min(fileSize, imageOffset + imageLimit),
|
||||
imageEnd,
|
||||
signal,
|
||||
);
|
||||
if (commands === null || commands.length !== sizeofcmds) return [];
|
||||
@@ -547,7 +552,7 @@ const machOClaims = async (
|
||||
littleEndian,
|
||||
cmd === LC.SEGMENT_64,
|
||||
imageOffset,
|
||||
Math.min(fileSize, imageOffset + imageLimit),
|
||||
imageSize,
|
||||
))
|
||||
claims.push(claim);
|
||||
}
|
||||
@@ -567,7 +572,7 @@ const segmentClaims = (
|
||||
littleEndian: boolean,
|
||||
wide: boolean,
|
||||
imageOffset: number,
|
||||
fileSize: number,
|
||||
imageSize: number,
|
||||
): readonly SectionClaim[] => {
|
||||
const parsedSegment = readMachoSegmentHeader(command, wide, littleEndian);
|
||||
if (!parsedSegment.ok) return [];
|
||||
@@ -575,6 +580,12 @@ const segmentClaims = (
|
||||
const { headerSize, sectionCount } = segment;
|
||||
const sectionBytes = wide ? SECTION_64_BYTES : SECTION_32_BYTES;
|
||||
if (headerSize + sectionCount * sectionBytes > command.length) return [];
|
||||
const admission = admitMachoSegmentCommand(
|
||||
segment,
|
||||
command.length,
|
||||
wide ? 64 : 32,
|
||||
imageSize,
|
||||
);
|
||||
const claims: SectionClaim[] = [];
|
||||
for (let index = 0; index < sectionCount; index += 1) {
|
||||
const start = headerSize + index * sectionBytes;
|
||||
@@ -599,14 +610,15 @@ const segmentClaims = (
|
||||
});
|
||||
continue;
|
||||
}
|
||||
claims.push(
|
||||
windowFor(
|
||||
"mach-o-section",
|
||||
imageOffset + section.offset,
|
||||
Number(section.size),
|
||||
fileSize,
|
||||
),
|
||||
const window = windowFor(
|
||||
"mach-o-section",
|
||||
imageOffset + section.offset,
|
||||
Number(section.size),
|
||||
imageOffset + imageSize,
|
||||
);
|
||||
// A named section with a missing tail already carries its own failure.
|
||||
if (admission.ok || window.truncated) claims.push(window);
|
||||
else claims.push({ failure: bunFormatFailure(admission.error.message) });
|
||||
}
|
||||
return claims;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user