p11-kit uses libffi, and in turn used by mod_gnutls in apache and engine_pkcs11 in nginx. However, libffi is blocked by Fedora's SELinux policy for web servers. That is by the policy to not execute in /tmp. For example, once I start apache in Fedora with mod_gnutls using a PKCS #11 HSM I get: (p11-kit:11686) p11_kit_module_load: in: libp11clientsofthsm.so (p11-kit:11686) load_module_from_file_inlock: module path is relative, loading from: /usr/lib64/pkcs11 (p11-kit:11686) load_module_from_file_inlock: loading module from path: /usr/lib64/pkcs11/libp11clientsofthsm.so (p11-kit:11686) dlopen_and_get_function_list: opened module: /usr/lib64/pkcs11/libp11clientsofthsm.so ffi_closure_alloc failed p11-kit: shouldn't be reached at init_wrapper_funcs p11-kit: shouldn't be reached at p11_virtual_wrap p11-kit: '*module != NULL' not true at prepare_module_inlock_reentrant (p11-kit:11686) p11_kit_module_load: out: fail That issue is not there when SELinux is set to not enforcing. The SELinux warning is: "SELinux is preventing /usr/sbin/httpd from execute access on the file /tmp/ffisox7RN (deleted)." That is, libffi's temp file which is used to mmap memory for execution is blocked by SELinux's policy. I find the policy of blocking execution in tmp quite reasonable, so I think that libffi is to blame here. This is a summary of: http://lists.freedesktop.org/archives/p11-glue/2015-September/000576.html Please consider an alternative way to obtain an executable memory area by avoiding /tmp/.
Update to summarize SELinux requirements: According to [0] SELinux can allow/prevent execmem or exec in specific places (tmp dirs for example). execmem is about allowing the execution of _anonymous_ memory. The current design of libffi requires the execution of files in tmp (creates a file in tmp mmaps it, writes and executes). That is a quite dangerous permission for an http server for example. It would be very much preferable to use anonymous mapping instead. I'm not familiar with internal usage, but I've attempted a patch which attempts an anonymous map prior to trying files in /tmp. The patch is at: https://github.com/atgreen/libffi/pull/200 [0]. http://www.akkadia.org/drepper/selinux-mem.html
execmem also prevents the creation of any mmap() that is PROT_WRITE | PROT_EXEC at the same time. libffi uses a file based memory mapping to map the same pages to two different addresses in virtual memory. The first virtual address is writeable, the second is executable. For python-cryptography we ran into the same issue. I did some experiments with a developer of python-cffi. The double mmap() trick only works for mmap() that are both MAP_SHARED and backed by a file descriptor. The file must be read/writeable and reside on an executable file system, too. It might be possible to use memfd_create() to create an anonymous file in memory. But that won't fix the problem with fork(). Because both mmap() must be shared (otherwise the executable mmap won't see data written to the writeable mmap), the pages are shared with forks. Can you rewrite the code without libffi's closure API? Only closures require dynamic code creation.
Created attachment 1093610 [details] FFI demo with memfd_create()
I have modified an example script and tested it with memfd_create(). According to my tests the double mmap() trick also works with memfd_create(). $ gcc -DUSE_MEMFD -lffi -o ffi_closure_alloc_test_memfd ffi_closure_alloc_test_memfd.c && ./ffi_closure_alloc_test_memfd Using memfd 3 Before bound_put() Hello World! Result is 1 $ gcc -lffi -o ffi_closure_alloc_test_memfd ffi_closure_alloc_test_memfd.c && ./ffi_closure_alloc_test_memfd Using tempfile /tmp/.execmemy81jL4 3 Before bound_put() Hello World! Result is 1
Fedora 22 changed to end-of-life (EOL) status on 2016-07-19. Fedora 22 is no longer maintained, which means that it will not receive any further security or bug fix updates. As a result we are closing this bug. If you can reproduce this bug against a currently maintained version of Fedora please feel free to reopen this bug against that version. If you are unable to reopen this bug, please file a new report against the current release. If you experience problems, please add a comment to this bug. Thank you for reporting this bug and we are sorry it could not be fixed.
This bug appears to have been reported against 'rawhide' during the Fedora 25 development cycle. Changing version to '25'.
This bug appears to have been reported against 'rawhide' during the Fedora 26 development cycle. Changing version to '26'.
This message is a reminder that Fedora 26 is nearing its end of life. Approximately 4 (four) weeks from now Fedora will stop maintaining and issuing updates for Fedora 26. It is Fedora's policy to close all bug reports from releases that are no longer maintained. At that time this bug will be closed as EOL if it remains open with a Fedora 'version' of '26'. Package Maintainer: If you wish for this bug to remain open because you plan to fix it in a currently maintained version, simply change the 'version' to a later Fedora version. Thank you for reporting this issue and we are sorry that we were not able to fix it before Fedora 26 is end of life. If you would still like to see this bug fixed and are able to reproduce it against a later version of Fedora, you are encouraged change the 'version' to a later Fedora version prior this bug is closed as described in the policy above. Although we aim to fix as many bugs as possible during every release's lifetime, sometimes those efforts are overtaken by events. Often a more recent Fedora release includes newer upstream software that fixes bugs or makes them obsolete.
Fedora 26 changed to end-of-life (EOL) status on 2018-05-29. Fedora 26 is no longer maintained, which means that it will not receive any further security or bug fix updates. As a result we are closing this bug. If you can reproduce this bug against a currently maintained version of Fedora please feel free to reopen this bug against that version. If you are unable to reopen this bug, please file a new report against the current release. If you experience problems, please add a comment to this bug. Thank you for reporting this bug and we are sorry it could not be fixed.