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

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