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

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