8000 [RFC WIP] Apple whole dylib trampoline by jeremyhu · Pull Request #621 · libffi/libffi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

[RFC WIP] Apple whole dylib trampoline #621

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeremyhu
Copy link
Contributor

This pull request follows up upon #565 to finish providing support for Apple Silicon. These changes are from Apple's libffi-27 (with some additional cleanup).

TODO:
1. Don't hard code the path to the trampoline dylib
2. Build the trampoline dylib

/* Helpers for writing assembly compatible with arm ptr auth */
#ifdef LIBFFI_ASM

#ifdef HAVE_PTRAUTH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be for generic code not just for apple which this is wrong:

  • return address signing requires cfi annotation (toggle pac state)
  • b key requires cfi annotation
  • libffi should use nop-space instructions. armv8.3-a only asm can be added conditionally, but that's not relevant to most users: no li 8000 nux distro would deploy binaries with armv8.3-a baseline any time soon.
  • pauth and bti should go together in gcc target libraries: branch-protection requires both return address and indirect jump/call protection. at least that's the intended usage model. however bti support requires significant changes in the jump table as bti j does not fit there.

so if apple requires this then it should be apple only or changed such that it works for other targets too (at least aarch64*-linux-gnu), libitm aarch64/sjlj.S is an example how this was done in other target libraries.

@jeremyhu
Copy link
Contributor Author

I'll rebase this again after #565 and #620 are merged.

sbogolepov added a commit to JetBrains/kotlin that referenced this pull request Mar 17, 2021
Currently, upstream libffi doesn't support Apple Silicon.
However, there are several PRs from Apple employees:
* libffi/libffi#565
* libffi/libffi#621

Homebrew formulae for libffi contains patch that is based
on these pull requests. We use this patch here to build our
own version of libffi.
@gpakosz
Copy link
Contributor
gpakosz commented Mar 29, 2021

Will libffi have a 3.4 release with Apple M1 support? Or should we plan to integrate master?

@dcolascione
Copy link
dcolascione commented May 15, 2021

See #633. Wouldn't it be cleaner to just use libffi itself as the trampoline dylib? That'll work on every platform too.

@jeremyhu jeremyhu force-pushed the apple-whole-dylib-trampoline branch from d03a732 to 8b612c3 Compare June 27, 2021 00:26
It is no longer allowed to simply vm_remap code pages willy-nilly.  Only entire
signed executable segment of a dylib may be remapped.  Accordingly, we need a
helper dylib (called libffi-trampolines.dylib) to support our use of vm_remap
for trampolines.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
@jeremyhu jeremyhu force-pushed the apple-whole-dylib-trampoline branch from 8b612c3 to 4b7be51 Compare June 27, 2021 00:28
@gpakosz
Copy link
Contributor
gpakosz commented Nov 8, 2021

Any news about upstreaming Apple Silicon support please?

@jeremyhu
Copy link
Contributor Author
jeremyhu commented Nov 9, 2021

Sorry, no. I haven't had much time to circle back to this. Most of the work is here on this branch... mainly just need to get the build system changes in place to build the trampoline dylib. If you have time to help with that, it'd be great. The Xcode project can serve as reference.

@tjni
Copy link
tjni commented Aug 18, 2022

I would like to try getting this across the finish line. However, this is a little outside my comfort zone, and so I would like some help in creating a consistent failing reproducer that I can use to gauge how I am doing.

/cc @jeremyhu

@kkharji
Copy link
kkharji commented Aug 20, 2022

@tjni 🎉

* trampoline dylib mach-o header (if FFI_TRAMPOLINE_WHOLE_DYLIB)
* trampoline page
*/
kt = vm_allocate (mach_task_self (), &config_page, FFI_TRAMPOLINE_ALLOCATION_PAGE_COUNT * PAGE_MAX_SIZE,
VM_FLAGS_ANYWHERE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that ffi_trampoline_table_free() needs to be updated to avoid leaking memory, as it deallocates PAGE_MAX_SIZE * 2.

Copy link
Contributor
@oleavr oleavr Aug 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremyhu Just took a quick look at ffi_closure_free() on macOS 12.5 using r2, and macOS' libffi does indeed have this memory leak:

Screenshot 2022-08-25 at 20 53 46

@charafsalmi
Copy link

Is there any news about this?

oleavr added a commit to frida/libffi that referenced this pull request Nov 1, 2022
Based on libffi#621 by @jeremyhu.

So we can support iOS 15 and beyond.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 1, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 1, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 1, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 8, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 9, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
oleavr added a commit to frida/libffi that referenced this pull request Nov 9, 2022
So we can support iOS 15 and beyond.

Based on libffi#621 by @jeremyhu.

Co-authored-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
github-actions bot pushed a commit to NixOS/nixpkgs that referenced this pull request Nov 28, 2022
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.

(cherry picked from commit 4fc97dc)
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this pull request Nov 30, 2023
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this pull request Dec 7, 2023
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this pull request Dec 14, 2023
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's 
9E88
fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this pull request Dec 22, 2023
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this pull request Jan 13, 2024
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
tm-drtina pushed a commit to awakesecurity/nixpkgs that referenced this pull request Feb 27, 2024
Trusts the libffi library inside of nixpkgs on Apple devices.

When Apple's fork of libffi is not detected, cffi assumes that libffi
uses a strategy for creating closures (i.e. callbacks) that is in
certain cases susceptible to a security exploit.

Based on some analysis I did:

  https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk

I believe that libffi already contains the code from Apple's fork that
is deemed safe to trust in cffi.

It uses a more sophisticated strategy for creating trampolines to
support closures that works on Apple Silicon, while the simple approach
that cffi falls back on does not, so this patch enables code that uses
closures on M1 Macs again.

Notably, pyOpenSSL is impacted and will be fixed by this, reported in

  pyca/pyopenssl#873

Note that libffi closures still will not work on signed apps without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement while

  libffi/libffi#621

is still open (which I haven't tested but is my best guess from reading).

I am hopeful that all of these changes will be upstreamed back into cffi
and libffi, and that this comment provides enough breadcrumbs for future
maintainers to track and clean this up.
reckenrode added a commit to NixOS/nixpkgs that referenced this pull request Feb 17, 2025
bengsparks pushed a commit to bengsparks/nixpkgs that referenced this pull request Mar 5, 2025
bengsparks pushed a commit to bengsparks/nixpkgs that referenced this pull request Mar 5, 2025
shymega pushed a commit to shymega/nixpkgs that referenced this pull request Mar 5, 2025
sandptel pushed a commit to sandptel/nixpkgs that referenced this pull request Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0