Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Apple M Series CPUs & OSX Hardened Runtime #1

Open
Sewer56 opened this issue Aug 6, 2023 · 2 comments
Open

Support Apple M Series CPUs & OSX Hardened Runtime #1

Sewer56 opened this issue Aug 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@Sewer56
Copy link
Member

Sewer56 commented Aug 6, 2023

Up for grabs.

I do not have access to the relevant hardware here to test on Apple Series M chips; and no way to emulate them either.

Motivations

Although this library is no_std and already supports ARM64 (aarch64); some operating systems have security measures in place which make hooking difficult.

For example, macOS enforces W^X on Apple hardware; where a region of memory can either only be writable or executable at any time. This is a bit problematic...

Solution

Some fiddling with JIT Entitlement might be necessary to acquire the desired outcome here.

The execute protection appears to function on a per thread basis, as described in the article, so simply calling pthread_jit_write_protect_np(false) before the code is written and pthread_jit_write_protect_np(true) after it is written, should be sufficient.

Additional Context

I do not have a way to simulate this hardware or test under hardened runtime.
All current testing of OSX was done in a veeeeeryyyy slowwwwww VM (Intel based macOS), and CI/CD.

To implement this, it might also be necessary to change the allocate perms on the Rust port Reloaded.Memory.Buffers which is the memory buffer provider; current perms allocate as RWX using mach_vm_allocate, the behaviour of that is unclear.

@Sewer56 Sewer56 added the enhancement New feature or request label Aug 6, 2023
@Sewer56 Sewer56 changed the title Support Apple M Series CPUs & Hardened Runtime Support Apple M Series CPUs & OSX Hardened Runtime Aug 6, 2023
@CryoMyst
Copy link

I can setup hardware access or am able to test code for this ticket.

@Sewer56
Copy link
Member Author

Sewer56 commented Apr 20, 2024

For reference.

The function https://github.com/Reloaded-Project/Reloaded.Memory.Buffers/blob/8d96107b4ddf8e5c5f190278814dcf217597f8b5/src-rust/src/buffers.rs#L142 overwrite_allocated_code in Buffers is used to 'install' the hooks. And the requirement is that they are atomic (for code execution).

On x86 this is already the case, and that's also the true for AArch64 on non-macOS, but due to W^X, this isn't possible on macOS M Series. The goal would be getting that function to be atomic.

I was thinking of the double mmap trick, where you map a certain page twice with different permissions. Maybe might work. Needs someone with hardware to find out :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants