Blame SOURCES/libffi-3.1-memfd.patch

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