Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

Fix out-of-bounds reads and undefined behavior in the resolver

NotProtonNotc2bee67a38ca
diff --git a/dylib/core/macho.c b/dylib/core/macho.c
index e511c18..ab509e8 100644
--- a/dylib/core/macho.c
+++ b/dylib/core/macho.c
@@ -1,5 +1,6 @@
#include "macho.h"
#include <mach-o/dyld.h>
+#include <mach-o/nlist.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
@@ -44,6 +45,18 @@ int np_await_image(const char *name, int timeout_ms,
return -1;
}
+static const struct load_command *lc_at(const struct mach_header_64 *mh,
+ const uint8_t *cursor) {
+ const uint8_t *lc_end = (const uint8_t *)(mh + 1) + mh->sizeofcmds;
+ if (cursor > lc_end || (size_t)(lc_end - cursor) < sizeof(struct load_command))
+ return NULL;
+
+ const struct load_command *lc = (const struct load_command *)cursor;
+ if (lc->cmdsize < sizeof(*lc) || lc->cmdsize > (size_t)(lc_end - cursor))
+ return NULL;
+ return lc;
+}
+
int np_find_segment(const struct mach_header_64 *mh, intptr_t slide,
const char *segname, uintptr_t *out_base, size_t *out_size) {
if (!mh || !segname) return -1;
@@ -52,10 +65,10 @@ int np_find_segment(const struct mach_header_64 *mh, intptr_t slide,
uint32_t remaining = mh->ncmds;
while (remaining--) {
- const struct load_command *lc = (const struct load_command *)cursor;
- if (lc->cmdsize < sizeof(*lc))
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc)
return -1; // corrupt or truncated
- if (lc->cmd == LC_SEGMENT_64) {
+ if (lc->cmd == LC_SEGMENT_64 && lc->cmdsize >= sizeof(struct segment_command_64)) {
const struct segment_command_64 *sc = (const struct segment_command_64 *)cursor;
if (strncmp(sc->segname, segname, sizeof(sc->segname)) == 0) {
*out_base = (uintptr_t)sc->vmaddr + (uintptr_t)slide;
@@ -77,13 +90,9 @@ static const uint8_t *fn_starts_table(const struct mach_header_64 *mh,
uint64_t le_vmaddr = 0, le_fileoff = 0, le_filesize = 0;
int have_le = 0;
- const uint8_t *lc_end = cursor + mh->sizeofcmds;
-
while (remaining--) {
- if ((size_t)(lc_end - cursor) < sizeof(struct load_command)) return NULL;
- const struct load_command *lc = (const struct load_command *)cursor;
- if (lc->cmdsize < sizeof(*lc) || lc->cmdsize > (size_t)(lc_end - cursor))
- return NULL;
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc) return NULL;
if (lc->cmd == LC_FUNCTION_STARTS && lc->cmdsize >= sizeof(*fs)) {
fs = (const struct linkedit_data_command *)cursor;
@@ -159,14 +168,14 @@ int np_get_section_containing(const struct mach_header_64 *mh, intptr_t slide,
uint32_t remaining = mh->ncmds;
while (remaining--) {
- const struct load_command *lc = (const struct load_command *)cursor;
- if (lc->cmdsize < sizeof(*lc))
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc)
return -1; // corrupt or truncated
- if (lc->cmd == LC_SEGMENT_64) {
+ if (lc->cmd == LC_SEGMENT_64 && lc->cmdsize >= sizeof(struct segment_command_64)) {
const struct segment_command_64 *sc =
(const struct segment_command_64 *)cursor;
- if (lc->cmdsize < sizeof(*sc) + sc->nsects * sizeof(struct section_64))
+ if (lc->cmdsize - sizeof(*sc) < (uint64_t)sc->nsects * sizeof(struct section_64))
return -1;
const struct section_64 *sect = (const struct section_64 *)(sc + 1);
@@ -183,3 +192,189 @@ int np_get_section_containing(const struct mach_header_64 *mh, intptr_t slide,
}
return -1;
}
+
+int np_find_section(const struct mach_header_64 *mh, intptr_t slide,
+ const char *segname, const char *sectname,
+ uintptr_t *out_base, size_t *out_size) {
+ if (!mh || !segname || !sectname || !out_base || !out_size) return -1;
+
+ const uint8_t *cursor = (const uint8_t *)(mh + 1);
+ uint32_t remaining = mh->ncmds;
+
+ while (remaining--) {
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc)
+ return -1;
+ if (lc->cmd == LC_SEGMENT_64 && lc->cmdsize >= sizeof(struct segment_command_64)) {
+ const struct segment_command_64 *sc =
+ (const struct segment_command_64 *)cursor;
+
+ if (lc->cmdsize - sizeof(*sc) < (uint64_t)sc->nsects * sizeof(struct section_64))
+ return -1;
+
+ if (strncmp(sc->segname, segname, sizeof(sc->segname)) == 0) {
+ const struct section_64 *sect = (const struct section_64 *)(sc + 1);
+ for (uint32_t i = 0; i < sc->nsects; i++) {
+ if (strncmp(sect[i].sectname, sectname, sizeof(sect[i].sectname)) != 0)
+ continue;
+ *out_base = (uintptr_t)sect[i].addr + (uintptr_t)slide;
+ *out_size = (size_t)sect[i].size;
+ return 0;
+ }
+ }
+ }
+ cursor += lc->cmdsize;
+ }
+ return -1;
+}
+
+typedef struct {
+ const struct nlist_64 *syms;
+ uint32_t nsyms;
+ const char *strings;
+ uint32_t strsize;
+ const uint32_t *indirect;
+ uint32_t nindirect;
+} symbols_t;
+
+static int in_linkedit(const struct segment_command_64 *le,
+ uint64_t fileoff, uint64_t len) {
+ if (fileoff < le->fileoff) return 0;
+ uint64_t rel = fileoff - le->fileoff;
+ return rel <= le->filesize && len <= le->filesize - rel;
+}
+
+static int load_symbols(const struct mach_header_64 *mh, intptr_t slide,
+ symbols_t *out) {
+ const struct symtab_command *symtab = NULL;
+ const struct dysymtab_command *dysymtab = NULL;
+ const struct segment_command_64 *linkedit = NULL;
+ const uint8_t *cursor = (const uint8_t *)(mh + 1);
+
+ for (uint32_t i = 0; i < mh->ncmds; i++) {
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc)
+ return -1;
+ if (lc->cmd == LC_SYMTAB && lc->cmdsize >= sizeof(struct symtab_command))
+ symtab = (const struct symtab_command *)cursor;
+ else if (lc->cmd == LC_DYSYMTAB && lc->cmdsize >= sizeof(struct dysymtab_command))
+ dysymtab = (const struct dysymtab_command *)cursor;
+ else if (lc->cmd == LC_SEGMENT_64
+ && lc->cmdsize >= sizeof(struct segment_command_64)
+ && strcmp(((const struct segment_command_64 *)cursor)->segname, SEG_LINKEDIT) == 0)
+ linkedit = (const struct segment_command_64 *)cursor;
+ cursor += lc->cmdsize;
+ }
+ if (!symtab || !dysymtab || !linkedit || !dysymtab->nindirectsyms)
+ return -1;
+
+ if (!in_linkedit(linkedit, symtab->symoff, (uint64_t)symtab->nsyms * sizeof(struct nlist_64)) ||
+ !in_linkedit(linkedit, symtab->stroff, symtab->strsize) ||
+ !in_linkedit(linkedit, dysymtab->indirectsymoff,
+ (uint64_t)dysymtab->nindirectsyms * sizeof(uint32_t)))
+ return -1;
+
+ uintptr_t base = (uintptr_t)slide + linkedit->vmaddr - linkedit->fileoff;
+ out->syms = (const struct nlist_64 *)(base + symtab->symoff);
+ out->nsyms = symtab->nsyms;
+ out->strings = (const char *)(base + symtab->stroff);
+ out->strsize = symtab->strsize;
+ out->indirect = (const uint32_t *)(base + dysymtab->indirectsymoff);
+ out->nindirect = dysymtab->nindirectsyms;
+ return 0;
+}
+
+static const char *indirect_symbol_name(const symbols_t *syms, uint64_t slot) {
+ if (slot >= syms->nindirect) return NULL;
+
+ uint32_t index = syms->indirect[slot];
+ if (index & (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) return NULL;
+ if (index >= syms->nsyms) return NULL;
+
+ uint32_t strx = syms->syms[index].n_un.n_strx;
+ if (strx >= syms->strsize) return NULL;
+ return syms->strings + strx;
+}
+
+// reserved2 is the stub stride, reserved1 the first indirect-symbol index.
+static int is_stub_section(const struct section_64 *sect) {
+ return (sect->flags & SECTION_TYPE) == S_SYMBOL_STUBS && sect->reserved2 != 0;
+}
+
+static const struct section_64 *segment_sections(const struct load_command *lc,
+ uint32_t *out_count) {
+ if (lc->cmdsize < sizeof(struct segment_command_64))
+ return NULL;
+
+ const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
+ if (lc->cmdsize - sizeof(*seg) < (uint64_t)seg->nsects * sizeof(struct section_64))
+ return NULL;
+ *out_count = seg->nsects;
+ return (const struct section_64 *)(seg + 1);
+}
+
+const char *np_import_stub_symbol(const struct mach_header_64 *mh, intptr_t slide,
+ uintptr_t stub) {
+ symbols_t syms;
+ if (!mh || !stub || load_symbols(mh, slide, &syms) != 0) return NULL;
+
+ const uint8_t *cursor = (const uint8_t *)(mh + 1);
+ for (uint32_t i = 0; i < mh->ncmds; i++) {
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc) return NULL;
+ cursor += lc->cmdsize;
+ if (lc->cmd != LC_SEGMENT_64) continue;
+
+ uint32_t nsects = 0;
+ const struct section_64 *sect = segment_sections(lc, &nsects);
+ if (!sect) return NULL;
+
+ for (uint32_t j = 0; j < nsects; j++, sect++) {
+ if (!is_stub_section(sect)) continue;
+
+ uintptr_t base = (uintptr_t)sect->addr + (uintptr_t)slide;
+ if (stub < base || stub >= base + (uintptr_t)sect->size) continue;
+
+ uintptr_t offset = stub - base;
+ if (offset % sect->reserved2) return NULL;
+ return indirect_symbol_name(&syms,
+ (uint64_t)sect->reserved1 +
+ offset / sect->reserved2);
+ }
+ }
+ return NULL;
+}
+
+uintptr_t np_import_stub_for_symbol(const struct mach_header_64 *mh, intptr_t slide,
+ const char *symbol) {
+ symbols_t syms;
+ if (!mh || !symbol || !*symbol || load_symbols(mh, slide, &syms) != 0) return 0;
+
+ uintptr_t found = 0;
+ const uint8_t *cursor = (const uint8_t *)(mh + 1);
+ for (uint32_t i = 0; i < mh->ncmds; i++) {
+ const struct load_command *lc = lc_at(mh, cursor);
+ if (!lc) return 0;
+ cursor += lc->cmdsize;
+ if (lc->cmd != LC_SEGMENT_64) continue;
+
+ uint32_t nsects = 0;
+ const struct section_64 *sect = segment_sections(lc, &nsects);
+ if (!sect) return 0;
+
+ for (uint32_t j = 0; j < nsects; j++, sect++) {
+ if (!is_stub_section(sect)) continue;
+
+ uintptr_t base = (uintptr_t)sect->addr + (uintptr_t)slide;
+ uint64_t count = sect->size / sect->reserved2;
+ for (uint64_t k = 0; k < count; k++) {
+ const char *name = indirect_symbol_name(&syms,
+ (uint64_t)sect->reserved1 + k);
+ if (!name || strcmp(name, symbol) != 0) continue;
+ if (found) return 0;
+ found = base + (uintptr_t)(k * sect->reserved2);
+ }
+ }
+ }
+ return found;
+}
diff --git a/dylib/core/macho.h b/dylib/core/macho.h
index 9fc8641..ee6d7ca 100644
--- a/dylib/core/macho.h
+++ b/dylib/core/macho.h
@@ -21,4 +21,15 @@ int np_get_section_containing(const struct mach_header_64 *mh, intptr_t slide,
int np_function_bounds(const struct mach_header_64 *mh, intptr_t slide,
uintptr_t addr, uintptr_t *out_start, uintptr_t *out_end);
+int np_find_section(const struct mach_header_64 *mh, intptr_t slide,
+ const char *segname, const char *sectname,
+ uintptr_t *out_base, size_t *out_size);
+
+// The indirect symbol table maps __stubs entries to imports.
+const char *np_import_stub_symbol(const struct mach_header_64 *mh, intptr_t slide,
+ uintptr_t stub);
+
+uintptr_t np_import_stub_for_symbol(const struct mach_header_64 *mh, intptr_t slide,
+ const char *symbol);
+
#endif // NOTPROTON_CORE_MACHO_H
diff --git a/dylib/resolver/anchor.c b/dylib/resolver/anchor.c
index d513207..1d6feb9 100644
--- a/dylib/resolver/anchor.c
+++ b/dylib/resolver/anchor.c
@@ -21,11 +21,12 @@ static const char *const data_segs[] = {"__DATA_CONST", "__DATA"};
static uintptr_t adrp_target(uintptr_t pc, uint32_t w, int *reg) {
if ((w & 0x9F000000u) != 0x90000000u) return 0;
*reg = (int)(w & 0x1Fu);
- uint32_t lo = (w >> 29) & 3u;
- uint32_t hi = (w >> 5) & 0x7FFFFu;
- int64_t imm = (int64_t)((hi << 2) | lo);
- imm = (imm << 43) >> 43;
- return (pc & ~(uintptr_t)0xFFF) + (uintptr_t)(imm << 12);
+ uint32_t lo = (w >> 29) & 3u;
+ uint32_t hi = (w >> 5) & 0x7FFFFu;
+ uint32_t raw = (hi << 2) | lo;
+ // Sign-extend 21 bits without signed left-shift overflow.
+ int64_t imm = (int64_t)(raw ^ 0x100000u) - 0x100000;
+ return (pc & ~(uintptr_t)0xFFF) + (uintptr_t)(imm * 0x1000);
}
// Decode ADD (immediate) with shift=0 whose Rn matches `expected_rn`.
@@ -49,8 +50,9 @@ static uintptr_t ldr_slot_addr(uint32_t w, int expected_rn, uintptr_t page) {
static uintptr_t branch26_target(uintptr_t pc, uint32_t w, uint32_t opcode) {
if ((w & 0xFC000000u) != opcode) return 0;
- int64_t imm = (int64_t)(w & 0x03FFFFFFu);
- imm = (imm << 38) >> 38;
+ uint32_t raw = w & 0x03FFFFFFu;
+ // Sign-extend 26 bits without signed left-shift overflow.
+ int64_t imm = (int64_t)(raw ^ 0x2000000u) - 0x2000000;
return pc + (uintptr_t)(imm * 4);
}
@@ -237,6 +239,12 @@ static uintptr_t scan_for_insn(uintptr_t text, size_t text_sz,
return 0;
}
+static int is_fn_start(const struct mach_header_64 *mh, intptr_t slide, uintptr_t fn) {
+ uintptr_t start, end;
+ if (np_function_bounds(mh, slide, fn, &start, &end) != 0) return 0;
+ return start == fn;
+}
+
// The sole matching instruction pair inside one function body
static uintptr_t body_end(const struct mach_header_64 *mh, intptr_t slide,
uintptr_t fn, uintptr_t text, size_t text_sz) {
@@ -377,7 +385,7 @@ uintptr_t np_locate_anchor(const struct mach_header_64 *mh, intptr_t slide,
} else {
fn = enclosing_fn(mh, slide, text_base, ref);
}
- if (!fn) return 0;
+ if (!fn || !is_fn_start(mh, slide, fn)) return 0;
if (anchor->kind == NP_MATCH_INSN_PAIR_IN_FN)
return sole_insn_pair(mh, slide, fn, text_base, text_size, &anchor->pair);