Skip to content

Commit

Permalink
Use memfd_create() (java-native-access#604)
Browse files Browse the repository at this point in the history
memfd_create creates a file in a memory-only filesystem that may
bypass strict security protocols in filesystem-based temporary
files.
  • Loading branch information
djdelorie authored Dec 2, 2020
1 parent cb84743 commit 5c63b46
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ EOF

AM_MAINTAINER_MODE

AC_CHECK_HEADERS(sys/memfd.h)
AC_CHECK_FUNCS([memfd_create])

AC_CHECK_HEADERS(sys/mman.h)
AC_CHECK_FUNCS([mmap mkostemp])
AC_FUNC_MMAP_BLACKLIST
Expand Down
17 changes: 17 additions & 0 deletions src/closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

#include <stddef.h>
#include <unistd.h>
#ifdef HAVE_SYS_MEMFD_H
#include <sys/memfd.h>
#endif

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

#ifdef HAVE_MEMFD_CREATE
/* Open a temporary file name, and immediately unlink it. */
static int
open_temp_exec_file_memfd (const char *name)
{
int fd;
fd = memfd_create (name, MFD_CLOEXEC);
return fd;
}
#endif

/* Open a temporary file name, and immediately unlink it. */
static int
open_temp_exec_file_name (char *name, int flags)
Expand Down Expand Up @@ -671,6 +685,9 @@ static struct
const char *arg;
int repeat;
} open_temp_exec_file_opts[] = {
#ifdef HAVE_MEMFD_CREATE
{ open_temp_exec_file_memfd, "libffi", 0 },
#endif
{ open_temp_exec_file_env, "TMPDIR", 0 },
{ open_temp_exec_file_dir, "/tmp", 0 },
{ open_temp_exec_file_dir, "/var/tmp", 0 },
Expand Down

0 comments on commit 5c63b46

Please sign in to comment.