Skip to content

Commit

Permalink
linux use MFD_NOEXEC_SEAL for shared memory
Browse files Browse the repository at this point in the history
ref: https://lore.kernel.org/lkml/[email protected]/

The new MFD_NOEXEC_SEAL and MFD_EXEC flags allows application to set
executable bit at creation time (memfd_create).

When MFD_NOEXEC_SEAL is set, memfd is created without executable bit
(mode:0666), and sealed with F_SEAL_EXEC, so it can't be chmod to be
executable (mode: 0777) after creation.

When MFD_EXEC flag is set, memfd is created with executable bit
(mode:0777), this is the same as the old behavior of memfd_create.

Signed-off-by: Rudi Heitbaum <[email protected]>
  • Loading branch information
heitbaum committed Aug 13, 2023
1 parent 43bf471 commit 43a9ffe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions xbmc/platform/posix/utils/SharedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#if defined(HAVE_LINUX_MEMFD)
#include <linux/memfd.h>
#include <sys/syscall.h>
#ifndef MFD_NOEXEC_SEAL
#define MFD_NOEXEC_SEAL 0x0008U
#endif
#endif

#include <cerrno>
Expand Down Expand Up @@ -63,7 +66,7 @@ CFileHandle CSharedMemory::OpenMemfd()
#if defined(SYS_memfd_create) && defined(HAVE_LINUX_MEMFD)
// This is specific to Linux >= 3.17, but preferred over shm_create if available
// because it is race-free
int fd = syscall(SYS_memfd_create, "kodi", MFD_CLOEXEC);
int fd = syscall(SYS_memfd_create, "kodi", MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
if (fd < 0)
{
throw std::system_error(errno, std::generic_category(), "memfd_create");
Expand Down Expand Up @@ -115,4 +118,4 @@ CFileHandle CSharedMemory::OpenShm()
unlink(tmpFilename.c_str());

return fd;
}
}
6 changes: 5 additions & 1 deletion xbmc/utils/UDMABufferObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include "PlatformDefs.h"

#ifndef MFD_NOEXEC_SEAL
#define MFD_NOEXEC_SEAL 0x0008U
#endif

namespace
{

Expand Down Expand Up @@ -95,7 +99,7 @@ bool CUDMABufferObject::CreateBufferObject(uint64_t size)
// Must be rounded to the system page size
m_size = RoundUp(size, PAGESIZE);

m_memfd = memfd_create("kodi", MFD_CLOEXEC | MFD_ALLOW_SEALING);
m_memfd = memfd_create("kodi", MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
if (m_memfd < 0)
{
CLog::Log(LOGERROR, "CUDMABufferObject::{} - memfd_create failed: {}", __FUNCTION__,
Expand Down

0 comments on commit 43a9ffe

Please sign in to comment.