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