bdf625
diff -up cups-2.2.7/cups/tempfile.c.str3382 cups-2.2.7/cups/tempfile.c
bdf625
--- cups-2.2.7/cups/tempfile.c.str3382	2018-03-23 04:48:36.000000000 +0100
bdf625
+++ cups-2.2.7/cups/tempfile.c	2018-04-03 14:21:54.567221492 +0200
bdf625
@@ -25,6 +25,7 @@
e9876b
 #  include <io.h>
e9876b
 #else
e9876b
 #  include <unistd.h>
e9876b
+#  include <sys types.h="">
e9876b
 #endif /* WIN32 || __EMX__ */
e9876b
 
e9876b
 
6dd074
@@ -48,7 +49,7 @@ cupsTempFd(char *filename,		/* I - Point
bdf625
 #ifdef WIN32
e9876b
   DWORD		curtime;		/* Current time */
e9876b
 #else
e9876b
-  struct timeval curtime;		/* Current time */
e9876b
+  mode_t	old_umask;		/* Old umask before using mkstemp() */
e9876b
 #endif /* WIN32 */
e9876b
 
e9876b
 
bdf625
@@ -114,32 +115,24 @@ cupsTempFd(char *filename,		/* I - Point
6dd074
     */
e9876b
 
6dd074
     snprintf(filename, (size_t)len - 1, "%s/%05lx%08lx", tmpdir, GetCurrentProcessId(), curtime);
e9876b
-#else
e9876b
-   /*
e9876b
-    * Get the current time of day...
e9876b
-    */
e9876b
-
e9876b
-    gettimeofday(&curtime, NULL);
e9876b
-
e9876b
-   /*
e9876b
-    * Format a string using the hex time values...
e9876b
-    */
e9876b
-
6dd074
-    snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
e9876b
-#endif /* WIN32 */
e9876b
 
e9876b
    /*
e9876b
     * Open the file in "exclusive" mode, making sure that we don't
e9876b
     * stomp on an existing file or someone's symlink crack...
e9876b
     */
e9876b
 
e9876b
-#ifdef WIN32
e9876b
     fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
e9876b
               _S_IREAD | _S_IWRITE);
e9876b
-#elif defined(O_NOFOLLOW)
e9876b
-    fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
e9876b
 #else
e9876b
-    fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
e9876b
+   /*
e9876b
+    * Use the standard mkstemp() call to make a temporary filename
e9876b
+    * securely.  -- andrew.wood@jdplc.com
e9876b
+    */
e9876b
+    snprintf(filename, len - 1, "%s/cupsXXXXXX", tmpdir);
e9876b
+
e9876b
+    old_umask = umask(0077);
e9876b
+    fd = mkstemp(filename);
e9876b
+    umask(old_umask);
e9876b
 #endif /* WIN32 */
e9876b
 
e9876b
     if (fd < 0 && errno != EEXIST)