Skip to content

Commit 5c63b46

Browse files
authored
Use memfd_create() (java-native-access#604)
memfd_create creates a file in a memory-only filesystem that may bypass strict security protocols in filesystem-based temporary files.
1 parent cb84743 commit 5c63b46

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ EOF
6363

6464
AM_MAINTAINER_MODE
6565

66+
AC_CHECK_HEADERS(sys/memfd.h)
67+
AC_CHECK_FUNCS([memfd_create])
68+
6669
AC_CHECK_HEADERS(sys/mman.h)
6770
AC_CHECK_FUNCS([mmap mkostemp])
6871
AC_FUNC_MMAP_BLACKLIST

src/closures.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

4646
#include <stddef.h>
4747
#include <unistd.h>
48+
#ifdef HAVE_SYS_MEMFD_H
49+
#include <sys/memfd.h>
50+
#endif
4851

4952
static const size_t overhead =
5053
(sizeof(max_align_t) > sizeof(void *) + sizeof(size_t)) ?
@@ -544,6 +547,17 @@ static int execfd = -1;
544547
/* The amount of space already allocated from the temporary file. */
545548
static size_t execsize = 0;
546549

550+
#ifdef HAVE_MEMFD_CREATE
551+
/* Open a temporary file name, and immediately unlink it. */
552+
static int
553+
open_temp_exec_file_memfd (const char *name)
554+
{
555+
int fd;
556+
fd = memfd_create (name, MFD_CLOEXEC);
557+
return fd;
558+
}
559+
#endif
560+
547561
/* Open a temporary file name, and immediately unlink it. */
548562
static int
549563
open_temp_exec_file_name (char *name, int flags)
@@ -671,6 +685,9 @@ static struct
671685
const char *arg;
672686
int repeat;
673687
} open_temp_exec_file_opts[] = {
688+
#ifdef HAVE_MEMFD_CREATE
689+
{ open_temp_exec_file_memfd, "libffi", 0 },
690+
#endif
674691
{ open_temp_exec_file_env, "TMPDIR", 0 },
675692
{ open_temp_exec_file_dir, "/tmp", 0 },
676693
{ open_temp_exec_file_dir, "/var/tmp", 0 },

0 commit comments

Comments
 (0)