Bug 1265106 - libffi using applications cannot run in Fedora under SELinux
Summary: libffi using applications cannot run in Fedora under SELinux
Keywords:
Status: CLOSED EOL
Alias: None
Product: Fedora
Classification: Fedora
Component: libffi
Version: 26
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Anthony Green
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks: 1271501
TreeView+ depends on / blocked
 
Reported: 2015-09-22 07:12 UTC by Nikos Mavrogiannopoulos
Modified: 2019-05-21 11:03 UTC (History)
8 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-05-29 11:58:02 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
FFI demo with memfd_create() (2.58 KB, text/plain)
2015-11-13 12:33 UTC, Christian Heimes
no flags Details


Links
System ID Private Priority Status Summary Last Updated
Red Hat Bugzilla 1249685 0 high CLOSED python-cffi should not require execmem when selinux is enabled 2021-02-22 00:41:40 UTC
Red Hat Bugzilla 1277224 0 unspecified CLOSED SELinux 'execmem' denials with FreeIPA on Fedora 23 2021-02-22 00:41:40 UTC

Internal Links: 1249685 1277224

Description Nikos Mavrogiannopoulos 2015-09-22 07:12:01 UTC
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/.

Comment 1 Nikos Mavrogiannopoulos 2015-09-23 09:06:13 UTC
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

Comment 2 Christian Heimes 2015-11-13 10:57:28 UTC
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.

Comment 3 Christian Heimes 2015-11-13 12:33:20 UTC
Created attachment 1093610 [details]
FFI demo with memfd_create()

Comment 4 Christian Heimes 2015-11-13 12:36:01 UTC
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

Comment 5 Fedora End Of Life 2016-07-19 17:58:48 UTC
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.

Comment 6 Jan Kurik 2016-07-26 04:19:05 UTC
This bug appears to have been reported against 'rawhide' during the Fedora 25 development cycle.
Changing version to '25'.

Comment 7 Fedora End Of Life 2017-02-28 09:49:20 UTC
This bug appears to have been reported against 'rawhide' during the Fedora 26 development cycle.
Changing version to '26'.

Comment 8 Fedora End Of Life 2018-05-03 08:47:42 UTC
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.

Comment 9 Fedora End Of Life 2018-05-29 11:58:02 UTC
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.


Note You need to log in before you can comment on or make changes to this bug.