diff --git a/docs/dev/BUILD.md b/docs/dev/BUILD.md index c2fc56116029..5478fb0be2cd 100644 --- a/docs/dev/BUILD.md +++ b/docs/dev/BUILD.md @@ -377,6 +377,10 @@ Set `APS5_THREAD_PRIORITY=high` to raise the AGC driver's queue workers and the The shader recompilation logic in [core/shader/recompiler](../../core/shader/recompiler) is isolated from the rest of the project and is a pure function of its input data, designed for integration into any other project. The current CMake target also includes cache support and links a supplied runtime target, glslang, and optionally SPIRV-Tools. +## Hardened runtime probe + +`tools/macos_hardened_probe.cpp` prints the result of `mmap` with read, write, and execute, `mprotect` to read and execute, and the `mach_vm_remap` plus `mach_vm_protect` sequence used by call-trace thunks. It also prints `DYLD_INSERT_LIBRARIES`. `tools/macos_sign_guest.sh` ad-hoc signs one executable with `--options runtime` and `tools/macos_hardened_runtime.entitlements`. Those entitlements are `allow-unsigned-executable-memory`, `allow-dyld-environment-variables`, and `disable-library-validation`. This tree does not change the allocators and does not claim that Rosetta accepts any of those calls. Read the probe output on a Mac. + ## Register context tests The x86-64 register capture and restore tests can be built separately on Linux and macOS, without submodules or the prx libraries. On macOS: diff --git a/tools/macos_hardened_probe.cpp b/tools/macos_hardened_probe.cpp new file mode 100644 index 000000000000..590700df330b --- /dev/null +++ b/tools/macos_hardened_probe.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static void ReportErrno(const char* name) { + std::printf("%s: errno %d %s\n", name, errno, std::strerror(errno)); +} + +int main() { + const long pageSize = ::getpagesize(); + const auto page = static_cast(pageSize > 0 ? pageSize : 4096); + const char* inserted = std::getenv("DYLD_INSERT_LIBRARIES"); + std::printf("dyld_insert_libraries: %s\n", inserted == nullptr ? "unset" : inserted); +#if defined(__x86_64__) + std::printf("arch: x86_64\n"); +#elif defined(__aarch64__) + std::printf("arch: arm64\n"); +#else + std::printf("arch: other\n"); +#endif + + void* rwx = ::mmap(nullptr, page, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0); + if (rwx == MAP_FAILED) ReportErrno("mmap_rwx"); + else { + std::printf("mmap_rwx: ok\n"); + ::munmap(rwx, page); + } + + void* rw = ::mmap(nullptr, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + if (rw == MAP_FAILED) ReportErrno("mmap_rw"); + else { + std::printf("mmap_rw: ok\n"); + if (::mprotect(rw, page, PROT_READ | PROT_EXEC) != 0) ReportErrno("mprotect_rx"); + else std::printf("mprotect_rx: ok\n"); + ::munmap(rw, page); + } + + void* writable = ::mmap(nullptr, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + if (writable == MAP_FAILED) ReportErrno("calltrace_mmap"); + else { + mach_vm_address_t executable = 0; + vm_prot_t current = VM_PROT_NONE; + vm_prot_t maximum = VM_PROT_NONE; + const kern_return_t remapped = ::mach_vm_remap(mach_task_self(), &executable, page, 0, VM_FLAGS_ANYWHERE, + mach_task_self(), reinterpret_cast(writable), false, ¤t, &maximum, VM_INHERIT_NONE); + if (remapped != KERN_SUCCESS) std::printf("mach_vm_remap: kern %d\n", remapped); + else { + const kern_return_t protectedPages = ::mach_vm_protect(mach_task_self(), executable, page, false, VM_PROT_READ | VM_PROT_EXECUTE); + if (protectedPages != KERN_SUCCESS) std::printf("mach_vm_protect_rx: kern %d\n", protectedPages); + else std::printf("mach_vm_protect_rx: ok\n"); + ::mach_vm_deallocate(mach_task_self(), executable, page); + } + ::munmap(writable, page); + } + return 0; +} diff --git a/tools/macos_hardened_runtime.entitlements b/tools/macos_hardened_runtime.entitlements new file mode 100644 index 000000000000..5cd7ba2e7112 --- /dev/null +++ b/tools/macos_hardened_runtime.entitlements @@ -0,0 +1,12 @@ + + + + + com.apple.security.cs.allow-unsigned-executable-memory + + com.apple.security.cs.allow-dyld-environment-variables + + com.apple.security.cs.disable-library-validation + + + diff --git a/tools/macos_sign_guest.sh b/tools/macos_sign_guest.sh new file mode 100755 index 000000000000..ad078bcd78be --- /dev/null +++ b/tools/macos_sign_guest.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -euo pipefail +if [[ $# -lt 1 || $# -gt 2 ]]; then + echo "usage: tools/macos_sign_guest.sh executable [library-directory]" >&2 + exit 2 +fi +root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) +entitlements=$root/tools/macos_hardened_runtime.entitlements +identity=${CODESIGN_IDENTITY:--} +codesign --force --sign "$identity" --options runtime --timestamp=none --entitlements "$entitlements" "$1" +if [[ $# -eq 2 ]]; then + find "$2" -type f \( -name '*.prx' -o -name '*.dylib' \) -print0 | + while IFS= read -r -d '' library; do + codesign --force --sign "$identity" --timestamp=none "$library" + done +fi +codesign --display --verbose=2 --entitlements - "$1"