mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(elf): share fixed-width unwind field reads (#12574)
* refactor(elf): share fixed-width unwind field reads * refactor(elf): share dynamic hash header word reads (#12577) refactor(elf): share bounded hardening hash reads
This commit is contained in:
1 parent
50a4dcf126
commit
cddaef83ae
4 files changed
+41
-39
No files matched your search
@@ -19,8 +19,6 @@
|
||||
"src/native/AppleObjcDispatchFacets.ts": 1,
|
||||
"src/native/AppleSwiftDispatchFacets.ts": 1,
|
||||
"src/native/elf/ElfNoteParser.ts": 27,
|
||||
"src/native/elf/ElfUnwindTables.ts": 6,
|
||||
"src/native/hardening/ElfHardening.ts": 2,
|
||||
"src/native/initializers/MachoInitializers.ts": 18,
|
||||
"src/native/pe/PeCertificateAsn1.ts": 2,
|
||||
"src/native/pwntools/RecordedCrashSourceBindings.ts": 6,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { admitElfLoadSegment } from "../../domain/elfProgramHeader.js";
|
||||
import { BinaryReader } from "../../domain/binaryContainer.js";
|
||||
import { readLeb128, SLEB64, ULEB64 } from "../../domain/leb128.js";
|
||||
import {
|
||||
allKeyed,
|
||||
@@ -296,8 +297,8 @@ const elfUnwindRangesFromDirectory = (
|
||||
signal?: AbortSignal,
|
||||
): ElfUnwindRanges => {
|
||||
assertArtifactReadActive(signal);
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const little = directory.little;
|
||||
const reader = new BinaryReader(bytes, little, "elf");
|
||||
const bits = directory.bits;
|
||||
const addressSize = bits === 64 ? 8 : 4;
|
||||
const limitations: string[] = [];
|
||||
@@ -474,36 +475,22 @@ const elfUnwindRangesFromDirectory = (
|
||||
size <= bytes.length - offset;
|
||||
const readU8 = (offset: number, end: number): number | null =>
|
||||
offset < end && offset < bytes.length ? (bytes[offset] ?? null) : null;
|
||||
const readU32 = (offset: number, end: number): number | null => {
|
||||
if (offset < 0 || offset + 4 > end || offset + 4 > bytes.length)
|
||||
return null;
|
||||
return view.getUint32(offset, little);
|
||||
};
|
||||
const readU64 = (offset: number, end: number): bigint | null => {
|
||||
if (offset < 0 || offset + 8 > end || offset + 8 > bytes.length)
|
||||
return null;
|
||||
return view.getBigUint64(offset, little);
|
||||
};
|
||||
const readS16 = (offset: number, end: number): number | null => {
|
||||
if (offset < 0 || offset + 2 > end || offset + 2 > bytes.length)
|
||||
return null;
|
||||
return view.getInt16(offset, little);
|
||||
};
|
||||
const readU16 = (offset: number, end: number): number | null => {
|
||||
if (offset < 0 || offset + 2 > end || offset + 2 > bytes.length)
|
||||
return null;
|
||||
return view.getUint16(offset, little);
|
||||
};
|
||||
const readS32 = (offset: number, end: number): number | null => {
|
||||
if (offset < 0 || offset + 4 > end || offset + 4 > bytes.length)
|
||||
return null;
|
||||
return view.getInt32(offset, little);
|
||||
};
|
||||
const readS64 = (offset: number, end: number): bigint | null => {
|
||||
if (offset < 0 || offset + 8 > end || offset + 8 > bytes.length)
|
||||
return null;
|
||||
return view.getBigInt64(offset, little);
|
||||
};
|
||||
// A field must fit its record as well as the file, even if another record
|
||||
// makes bytes available at the same offset.
|
||||
const fieldFits = (offset: number, size: number, end: number): boolean =>
|
||||
offset + size <= end && within(offset, size);
|
||||
const readU32 = (offset: number, end: number): number | null =>
|
||||
fieldFits(offset, 4, end) ? reader.u32(offset) : null;
|
||||
const readU64 = (offset: number, end: number): bigint | null =>
|
||||
fieldFits(offset, 8, end) ? reader.word(offset, 64) : null;
|
||||
const readS16 = (offset: number, end: number): number | null =>
|
||||
fieldFits(offset, 2, end) ? reader.i16(offset) : null;
|
||||
const readU16 = (offset: number, end: number): number | null =>
|
||||
fieldFits(offset, 2, end) ? reader.u16(offset) : null;
|
||||
const readS32 = (offset: number, end: number): number | null =>
|
||||
fieldFits(offset, 4, end) ? reader.i32(offset) : null;
|
||||
const readS64 = (offset: number, end: number): bigint | null =>
|
||||
fieldFits(offset, 8, end) ? reader.i64(offset) : null;
|
||||
const readLeb = (
|
||||
offset: number,
|
||||
end: number,
|
||||
|
||||
@@ -757,6 +757,22 @@ it.each([true, false])(
|
||||
},
|
||||
);
|
||||
|
||||
it("uses the ELF byte order when counting sectionless imports through DT_HASH", () => {
|
||||
const image = readElfHardening(
|
||||
elfHardeningFixture({
|
||||
little: false,
|
||||
sections: false,
|
||||
stackChk: true,
|
||||
fortify: true,
|
||||
}),
|
||||
);
|
||||
expect(image.byte_order).toBe("big");
|
||||
expect(states(image.rows)).toMatchObject({
|
||||
stack_protector: "enabled",
|
||||
fortify: "enabled",
|
||||
});
|
||||
});
|
||||
|
||||
it("reads sectionless GNU hash buckets with many non-owning load declarations", () => {
|
||||
const original = elfHardeningFixture({ sections: false, stackChk: true });
|
||||
const oldPhoff = Number(original.readBigUInt64LE(32));
|
||||
|
||||
@@ -60,6 +60,7 @@ import {
|
||||
import {
|
||||
BinaryFormatFailure,
|
||||
BinaryResourceFailure,
|
||||
BinaryReader,
|
||||
} from "../../domain/binaryContainer.js";
|
||||
import {
|
||||
ELF_PT_GNU_PROPERTY,
|
||||
@@ -664,10 +665,9 @@ const readSymbolNames = (
|
||||
};
|
||||
|
||||
const hashWords = (
|
||||
bytes: Buffer,
|
||||
reader: BinaryReader,
|
||||
va: bigint,
|
||||
mapVa: MapVa,
|
||||
little: boolean,
|
||||
): ElfHashWordReader => {
|
||||
const mapping = mapVa(va, 4n);
|
||||
return (relative) => {
|
||||
@@ -677,8 +677,8 @@ const hashWords = (
|
||||
if (end > mapping.backed) return err("is not file-backed in its PT_LOAD");
|
||||
if (end > mapping.available) return err("crosses another PT_LOAD mapping");
|
||||
const at = mapping.offset + relative;
|
||||
if (at > bytes.length - 4) return err("extends outside the file");
|
||||
return ok(little ? bytes.readUInt32LE(at) : bytes.readUInt32BE(at));
|
||||
if (at > reader.bytes.length - 4) return err("extends outside the file");
|
||||
return ok(reader.u32(at));
|
||||
};
|
||||
};
|
||||
|
||||
@@ -711,12 +711,13 @@ const symbolCount = (
|
||||
): number | null => {
|
||||
const hash = dynamic.entries.get(DT_HASH);
|
||||
const gnu = dynamic.entries.get(DT_GNU_HASH);
|
||||
const reader = new BinaryReader(bytes, little, "elf");
|
||||
const classic =
|
||||
hash === undefined
|
||||
? null
|
||||
: admitHashCount(
|
||||
readElfHashSymbolCount(
|
||||
hashWords(bytes, hash.value, mapVa, little),
|
||||
hashWords(reader, hash.value, mapVa),
|
||||
MAX_BINARY_HARDENING_TABLE_ENTRIES,
|
||||
),
|
||||
);
|
||||
@@ -726,7 +727,7 @@ const symbolCount = (
|
||||
: admitHashCount(
|
||||
finishGnuHashCount(
|
||||
readElfGnuHashSymbolCount(
|
||||
hashWords(bytes, gnu.value, mapVa, little),
|
||||
hashWords(reader, gnu.value, mapVa),
|
||||
bits === 64 ? 8 : 4,
|
||||
MAX_BINARY_HARDENING_TABLE_ENTRIES,
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user