mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(elf): read IKCONFIG gzip and boot fields through BinaryReader (#12525)
This commit is contained in:
3 files changed
+39
-13
No files matched your search
@@ -35,7 +35,6 @@
|
||||
"src/native/AppleSwiftDispatchFacets.ts": 1,
|
||||
"src/native/elf/ElfNoteParser.ts": 27,
|
||||
"src/native/elf/ElfUnwindTables.ts": 6,
|
||||
"src/native/elf/KernelBuildConfig.ts": 7,
|
||||
"src/native/elf/KernelModuleParser.ts": 4,
|
||||
"src/native/hardening/ElfHardening.ts": 2,
|
||||
"src/native/initializers/ElfInitializerSymbols.ts": 4,
|
||||
|
||||
@@ -222,6 +222,32 @@ it("reports a framed member with a bad CRC-32 as malformed, not absent", async (
|
||||
expect(report.limitations.some((line) => line.includes("CRC-32"))).toBe(true);
|
||||
});
|
||||
|
||||
it("reads the full little-endian gzip extra field before the config", async () => {
|
||||
const member = gzipSync(Buffer.from(SAMPLE_KERNEL_CONFIG, "latin1"));
|
||||
const header = Buffer.from(member.subarray(0, 10));
|
||||
header[3] = 0x04;
|
||||
// XLEN = 260, containing one 256-byte subfield.
|
||||
const extra = Buffer.concat([
|
||||
Buffer.from([4, 1, 0x52, 0x45, 0, 1]),
|
||||
Buffer.alloc(256),
|
||||
]);
|
||||
const frame = (payload: Buffer) =>
|
||||
Buffer.concat([
|
||||
Buffer.from("IKCFG_ST"),
|
||||
header,
|
||||
extra,
|
||||
payload,
|
||||
Buffer.from("IKCFG_ED"),
|
||||
]);
|
||||
expect((await parse(frame(member.subarray(10)))).config).toMatchObject({
|
||||
status: "present",
|
||||
option_count: 9,
|
||||
});
|
||||
expect((await parse(frame(Buffer.alloc(0)))).config.status).not.toBe(
|
||||
"present",
|
||||
);
|
||||
});
|
||||
|
||||
it("checks the optional gzip header CRC before accepting a member", async () => {
|
||||
const plain = Buffer.from(SAMPLE_KERNEL_CONFIG, "latin1");
|
||||
const frame = (member: Buffer): Buffer =>
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
type KernelBuildConfigEntry,
|
||||
} from "../../domain/native/kernelBuildConfig.js";
|
||||
import { kernelCompressionCodecAt } from "./KernelCompressionCodec.js";
|
||||
import { BinaryReader } from "../../domain/binaryContainer.js";
|
||||
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
|
||||
|
||||
type Report = KernelBuildConfig;
|
||||
@@ -150,9 +151,10 @@ const FCOMMENT = 0x10;
|
||||
* `invalid` when it is not deflate, sets reserved flags, or fails its FHCRC.
|
||||
*/
|
||||
const gzipHeaderEnd = (
|
||||
bytes: Buffer,
|
||||
reader: BinaryReader,
|
||||
start: number,
|
||||
): number | "invalid" | null => {
|
||||
const { bytes } = reader;
|
||||
if (start + 10 > bytes.length) return null;
|
||||
if (bytes[start + 2] !== CM_DEFLATE) return "invalid";
|
||||
const flags = bytes[start + 3] ?? 0;
|
||||
@@ -160,7 +162,7 @@ const gzipHeaderEnd = (
|
||||
let cursor = start + 10;
|
||||
if ((flags & FEXTRA) !== 0) {
|
||||
if (cursor + 2 > bytes.length) return null;
|
||||
cursor += 2 + bytes.readUInt16LE(cursor);
|
||||
cursor += 2 + reader.u16(cursor);
|
||||
}
|
||||
for (const flag of [FNAME, FCOMMENT]) {
|
||||
if ((flags & flag) === 0) continue;
|
||||
@@ -171,10 +173,7 @@ const gzipHeaderEnd = (
|
||||
if ((flags & FHCRC) === 0) return cursor > bytes.length ? null : cursor;
|
||||
if (cursor + 2 > bytes.length) return null;
|
||||
// FHCRC is the low 16 bits of the CRC-32 of every header byte before it.
|
||||
if (
|
||||
bytes.readUInt16LE(cursor) !==
|
||||
(crc32(bytes.subarray(start, cursor)) & 0xffff)
|
||||
)
|
||||
if (reader.u16(cursor) !== (crc32(bytes.subarray(start, cursor)) & 0xffff))
|
||||
return "invalid";
|
||||
return cursor + 2;
|
||||
};
|
||||
@@ -207,7 +206,8 @@ const inflateGzipMember = async (
|
||||
maxOutput: number,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<GzipMember> => {
|
||||
const body = gzipHeaderEnd(bytes, start);
|
||||
const reader = new BinaryReader(bytes, true, "gzip");
|
||||
const body = gzipHeaderEnd(reader, start);
|
||||
if (body === null) return { kind: "truncated", spent: 0 };
|
||||
if (body === "invalid") return { kind: "invalid", spent: 0 };
|
||||
let output: Buffer;
|
||||
@@ -230,8 +230,8 @@ const inflateGzipMember = async (
|
||||
if (trailer + 8 > bytes.length)
|
||||
return { kind: "truncated", spent: output.length };
|
||||
if (
|
||||
bytes.readUInt32LE(trailer) !== crc32(output) ||
|
||||
bytes.readUInt32LE(trailer + 4) !== output.length >>> 0
|
||||
reader.u32(trailer) !== crc32(output) ||
|
||||
reader.u32(trailer + 4) !== output.length >>> 0
|
||||
)
|
||||
return { kind: "corrupt", end: trailer + 8, inflated: output.length };
|
||||
return { kind: "member", output, end: trailer + 8 };
|
||||
@@ -826,12 +826,13 @@ const readBzImage = (
|
||||
| { kind: "payload"; payload: BzImagePayload } => {
|
||||
if (bytes.length < 0x206 || bytes.toString("latin1", 0x202, 0x206) !== "HdrS")
|
||||
return { kind: "none" };
|
||||
if (bytes.length < 0x250 || bytes.readUInt16LE(0x206) < 0x0208)
|
||||
const reader = new BinaryReader(bytes, true, "bzImage");
|
||||
if (bytes.length < 0x250 || reader.u16(0x206) < 0x0208)
|
||||
return { kind: "unavailable" };
|
||||
const setupSects = bytes[0x1f1] === 0 ? 4 : (bytes[0x1f1] ?? 4);
|
||||
const payload = {
|
||||
offset: (setupSects + 1) * 512 + bytes.readUInt32LE(0x248),
|
||||
bytes: bytes.readUInt32LE(0x24c),
|
||||
offset: (setupSects + 1) * 512 + reader.u32(0x248),
|
||||
bytes: reader.u32(0x24c),
|
||||
};
|
||||
if (payload.bytes === 0) return { kind: "unavailable" };
|
||||
return payload.offset > bytes.length ||
|
||||
|
||||
Reference in new issue
Block a user