-
Notifications
You must be signed in to change notification settings - Fork 745
[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
base: master
Are you sure you want to change the base?
Conversation
fedfc7e
to
d03a732
Compare
src/aarch64/internal.h
Outdated
/* Helpers for writing assembly compatible with arm ptr auth */ | ||
#ifdef LIBFFI_ASM | ||
|
||
#ifdef HAVE_PTRAUTH |
There was a problem hiding this comment.
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.
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.
Will libffi have a |
See #633. Wouldn't it be cleaner to just use libffi itself as the trampoline dylib? That'll work on every platform too. |
d03a732
to
8b612c3
Compare
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>
8b612c3
to
4b7be51
Compare
Any news about upstreaming Apple Silicon support please? |
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. |
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 |
@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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any news about this? |
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>
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>
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>
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>
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>
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>
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>
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)
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.
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.
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.
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.
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.
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.
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