307589
From 5c63b463b87d3c06102a4a7f05f395929d9ea79b Mon Sep 17 00:00:00 2001
307589
From: DJ Delorie <dj@delorie.com>
307589
Date: Wed, 2 Dec 2020 16:14:27 -0500
307589
Subject: Use memfd_create() (#604)
307589
307589
memfd_create creates a file in a memory-only filesystem that may
307589
bypass strict security protocols in filesystem-based temporary
307589
files.
307589
307589
diff -rup a/configure.ac b/configure.ac
307589
--- a/configure.ac	2014-05-11 09:57:49.000000000 -0400
307589
+++ b/configure.ac	2021-11-03 17:41:31.935391831 -0400
307589
@@ -63,6 +63,9 @@ EOF
307589
 
307589
 AM_MAINTAINER_MODE
307589
 
307589
+AC_CHECK_HEADERS(sys/memfd.h)
307589
+AC_CHECK_FUNCS([memfd_create])
307589
+
307589
 AC_CHECK_HEADERS(sys/mman.h)
307589
 AC_CHECK_FUNCS(mmap)
307589
 AC_FUNC_MMAP_BLACKLIST
307589
diff -rup a/configure b/configure
307589
--- a/configure	2014-05-19 09:44:03.000000000 -0400
307589
+++ b/configure	2021-11-18 17:29:45.484951520 -0500
307589
@@ -16976,6 +16976,30 @@ fi
307589
 
307589
 
307589
 
307589
+for ac_header in sys/memfd.h
307589
+do :
307589
+  ac_fn_c_check_header_mongrel "$LINENO" "sys/memfd.h" "ac_cv_header_sys_memfd_h" "$ac_includes_default"
307589
+if test "x$ac_cv_header_sys_memfd_h" = xyes; then :
307589
+  cat >>confdefs.h <<_ACEOF
307589
+#define HAVE_SYS_MEMFD_H 1
307589
+_ACEOF
307589
+
307589
+fi
307589
+
307589
+done
307589
+
307589
+for ac_func in memfd_create
307589
+do :
307589
+  ac_fn_c_check_func "$LINENO" "memfd_create" "ac_cv_func_memfd_create"
307589
+if test "x$ac_cv_func_memfd_create" = xyes; then :
307589
+  cat >>confdefs.h <<_ACEOF
307589
+#define HAVE_MEMFD_CREATE 1
307589
+_ACEOF
307589
+
307589
+fi
307589
+done
307589
+
307589
+
307589
 for ac_header in sys/mman.h
307589
 do :
307589
   ac_fn_c_check_header_mongrel "$LINENO" "sys/mman.h" "ac_cv_header_sys_mman_h" "$ac_includes_default"
307589
diff -rup a/fficonfig.h.in b/fficonfig.h.in
307589
--- a/fficonfig.h.in	2014-05-19 09:44:04.000000000 -0400
307589
+++ b/fficonfig.h.in	2021-11-18 17:45:39.000000000 -0500
307589
@@ -79,6 +79,9 @@
307589
 /* Define to 1 if you have the `memcpy' function. */
307589
 #undef HAVE_MEMCPY
307589
 
307589
+/* Define to 1 if you have the `memfd_create' function. */
307589
+#undef HAVE_MEMFD_CREATE
307589
+
307589
 /* Define to 1 if you have the <memory.h> header file. */
307589
 #undef HAVE_MEMORY_H
307589
 
307589
@@ -109,6 +112,9 @@
307589
 /* Define to 1 if you have the <string.h> header file. */
307589
 #undef HAVE_STRING_H
307589
 
307589
+/* Define to 1 if you have the <sys/memfd.h> header file. */
307589
+#undef HAVE_SYS_MEMFD_H
307589
+
307589
 /* Define to 1 if you have the <sys/mman.h> header file. */
307589
 #undef HAVE_SYS_MMAN_H
307589
 
307589
diff -rup a/src/closures.c b/src/closures.c
307589
--- a/src/closures.c	2021-11-03 17:37:37.841416436 -0400
307589
+++ b/src/closures.c	2021-11-03 17:43:19.027498783 -0400
307589
@@ -117,6 +117,9 @@
307589
 #endif /* HAVE_MNTENT */
307589
 #include <sys/param.h>
307589
 #include <pthread.h>
307589
+#ifdef  HAVE_SYS_MEMFD_H
307589
+#include <sys/memfd.h>
307589
+#endif
307589
 
307589
 /* We don't want sys/mman.h to be included after we redefine mmap and
307589
    dlmunmap.  */
307589
@@ -263,6 +266,17 @@ static int execfd = -1;
307589
 /* The amount of space already allocated from the temporary file.  */
307589
 static size_t execsize = 0;
307589
 
307589
+#ifdef HAVE_MEMFD_CREATE
307589
+/* Open a temporary file name, and immediately unlink it.  */
307589
+static int
307589
+open_temp_exec_file_memfd (const char *name)
307589
+{
307589
+  int fd;
307589
+  fd = memfd_create (name, MFD_CLOEXEC);
307589
+  return fd;
307589
+}
307589
+#endif
307589
+
307589
 /* Open a temporary file name, and immediately unlink it.  */
307589
 static int
307589
 open_temp_exec_file_name (char *name, int flags)
307589
@@ -382,6 +396,9 @@ static struct
307589
   const char *arg;
307589
   int repeat;
307589
 } open_temp_exec_file_opts[] = {
307589
+#ifdef HAVE_MEMFD_CREATE
307589
+  { open_temp_exec_file_memfd, "libffi", 0 },
307589
+#endif
307589
   { open_temp_exec_file_env, "LIBFFI_TMPDIR", 0 },
307589
   { open_temp_exec_file_env, "TMPDIR", 0 },
307589
   { open_temp_exec_file_dir, "/tmp", 0 },