Skip to content

Commit

Permalink
Validate gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Jan 18, 2025
1 parent 987661d commit 439fd16
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
1 change: 1 addition & 0 deletions const.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// deno-lint-ignore-file camelcase

export const UINT32_MAX = 0xffffffff;
export const PAGE_SIZE = 0x4000;

// FAT mach magic numbers:
export const FAT_MAGIC = 0xcafebabe;
Expand Down
67 changes: 62 additions & 5 deletions macho/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MH_CIGAM_64,
MH_MAGIC,
MH_MAGIC_64,
PAGE_SIZE,
} from '../const.ts';
import { FatHeader } from '../mach/fatheader.ts';
import { MachHeader } from '../mach/machheader.ts';
Expand All @@ -15,6 +16,11 @@ import { FatArch } from '../mach/fatarch.ts';
import { CPU_ARCH_ABI64 } from '../const.ts';
import { CPU_TYPE_ARM } from '../const.ts';

/**
* Maximum power of 2 alignment amount.
*/
const MAX_ALIGN = 30;

/**
* A universal binary over a readable.
* Works for fat binaries and also thin binaries.
Expand Down Expand Up @@ -79,7 +85,7 @@ export class Universal {
this.mBase = offset;
this.mLength = length;
this.mMachType = 0;
this.mSuspicious = false;
let mSuspicious = this.mSuspicious = false;
this.mArchList = null;
this.mArchCount = 0;
this.mThinArch = null;
Expand Down Expand Up @@ -124,25 +130,76 @@ export class Universal {
this.mArchCount = mArchCount = ++mArchCount;
}

// Padding between header and slices should all be zeroed out.
const sortedList = [];
for (let i = 0; i < mArchCount; i++) {
sortedList.push(mArchList[i]);
}
sortedList.sort((a, b) => a.offset - b.offset);

for (const { offset, size } of sortedList) {
const universalHeaderEnd = offset + header.byteLength +
(FatArch.BYTE_LENGTH * mArchCount);
let prevHeaderEnd = universalHeaderEnd;
let prevArchSize = 0;
let prevArchStart = 0;

for (const { offset, size, align } of sortedList) {
if (mSizes.has(offset)) {
throw new RangeError(
`Two architectures have the same offset: ${offset}`,
);
}
mSizes.set(offset, size);

// TODO
}
const gapSize = offset - prevHeaderEnd;
if (
prevHeaderEnd !== universalHeaderEnd &&
(align > MAX_ALIGN || gapSize >= (1 << align))
) {
this.mSuspicious = mSuspicious = true;
break;
}

// TODO
let off = 0;
GAPS: while (off < gapSize) {
const want = Math.min(gapSize - off, PAGE_SIZE);
const readOffset = prevHeaderEnd + off;
// deno-lint-ignore no-await-in-loop
const read = await reader
.slice(readOffset, readOffset + want)
.arrayBuffer();
const got = read.byteLength;
if (!got) {
this.mSuspicious = mSuspicious = true;
break;
}
off += got;
const gapBytes = new Uint8Array(got);
for (let x = 0; x < got; x++) {
if (gapBytes[x]) {
this.mSuspicious = mSuspicious = true;
break GAPS;
}
}
}
if (off !== gapSize) {
this.mSuspicious = mSuspicious = true;
}
if (mSuspicious) {
break;
}

prevHeaderEnd = offset + size;
prevArchSize = size;
prevArchStart = offset;
}

if (
!mSuspicious &&
(prevArchStart + prevArchSize !== reader.size)
) {
this.mSuspicious = true;
}
break;
}
case MH_CIGAM:
Expand Down

0 comments on commit 439fd16

Please sign in to comment.