Blame SOURCES/libffi-3.1-closures-Create-temporary-file-with-O_TMPFILE-and-O_.patch

e65318
From 8daeed9570af72eb135c8ded460d2888f05b2e68 Mon Sep 17 00:00:00 2001
e65318
From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= <mic@digikod.net>
e65318
Date: Sun, 11 May 2014 22:54:58 +0200
e65318
Subject: [PATCH 626/627] closures: Create temporary file with O_TMPFILE and
e65318
 O_CLOEXEC when available
e65318
e65318
The open_temp_exec_file_dir function can create a temporary file without
e65318
file system accessible link. If the O_TMPFILE flag is not defined (old
e65318
Linux kernel or libc) the behavior is unchanged.
e65318
e65318
The open_temp_exec_file_name function now need a new argument "flags"
e65318
(like O_CLOEXEC) used for temporary file creation.
e65318
e65318
The O_TMPFILE flag allow temporary file creation without race condition.
e65318
This feature/fix prevent another process to access the (future)
e65318
executable file from the file system.
e65318
e65318
The O_CLOEXEC flag automatically close the temporary file for any
e65318
execve. This avoid transmitting (executable) file descriptor to a child
e65318
process.
e65318
---
e65318
 src/closures.c | 29 ++++++++++++++++++++++++-----
e65318
 1 file changed, 24 insertions(+), 5 deletions(-)
e65318
e65318
diff -rup a/src/closures.c b/src/closures.c
e65318
--- a/src/closures.c	2019-06-14 14:24:01.510844835 -0400
e65318
+++ b/src/closures.c	2019-06-14 14:26:48.248924350 -0400
e65318
@@ -265,9 +265,9 @@ static size_t execsize = 0;
e65318
 
e65318
 /* Open a temporary file name, and immediately unlink it.  */
e65318
 static int
e65318
-open_temp_exec_file_name (char *name)
e65318
+open_temp_exec_file_name (char *name, int flags)
e65318
 {
e65318
-  int fd = mkstemp (name);
e65318
+  int fd = mkostemp (name, flags);
e65318
 
e65318
   if (fd != -1)
e65318
     unlink (name);
e65318
@@ -280,8 +280,28 @@ static int
e65318
 open_temp_exec_file_dir (const char *dir)
e65318
 {
e65318
   static const char suffix[] = "/ffiXXXXXX";
e65318
-  size_t lendir = strlen (dir);
e65318
-  char *tempname = __builtin_alloca (lendir + sizeof (suffix));
e65318
+  size_t lendir;
e65318
+  int flags, fd;
e65318
+  char *tempname;
e65318
+
e65318
+#ifdef O_CLOEXEC
e65318
+  flags = O_CLOEXEC;
e65318
+#else
e65318
+  flags = 0;
e65318
+#endif
e65318
+
e65318
+#ifdef O_TMPFILE
e65318
+  fd = open (dir, flags | O_RDWR | O_EXCL | O_TMPFILE, 0700);
e65318
+  /* If the running system does not support the O_TMPFILE flag then retry without it. */
e65318
+  if (fd != -1 || (errno != EINVAL && errno != EISDIR && errno != EOPNOTSUPP)) {
e65318
+    return fd;
e65318
+  } else {
e65318
+    errno = 0;
e65318
+  }
e65318
+#endif
e65318
+
e65318
+  lendir = strlen (dir);
e65318
+  tempname = __builtin_alloca (lendir + sizeof (suffix));
e65318
 
e65318
   if (!tempname)
e65318
     return -1;
e65318
@@ -289,7 +309,7 @@ open_temp_exec_file_dir (const char *dir
e65318
   memcpy (tempname, dir, lendir);
e65318
   memcpy (tempname + lendir, suffix, sizeof (suffix));
e65318
 
e65318
-  return open_temp_exec_file_name (tempname);
e65318
+  return open_temp_exec_file_name (tempname, flags);
e65318
 }
e65318
 
e65318
 /* Open a temporary file in the directory in the named environment