Submitted By: Robert Connolly (ashes) Date: 2010-02-22 Initial Package Version: 2.6.1 Upstream Status: Submitted to bug-gnu-utils@gnu.org and rejected http://lists.gnu.org/archive/html/bug-gnu-utils/2006-11/msg00128.html Origin: Radoslaw Krahl http://www.linuxfromscratch.org/pipermail/hlfs-dev/2004-January/000290.html Description: This patch adds the use of mkstemp(3). diff -Naur patch-2.6.1.orig//config.hin patch-2.6.1/config.hin --- patch-2.6.1.orig//config.hin 2009-12-30 15:32:08.000000000 +0000 +++ patch-2.6.1/config.hin 2010-02-22 22:28:09.000000000 +0000 @@ -145,6 +145,9 @@ /* Define to 1 if you have the `mkdir' function. */ #undef HAVE_MKDIR +/* Define to 1 if you have the `mkstemp' function. */ +#undef HAVE_MKSTEMP + /* Define to 1 if you have the `mktemp' function. */ #undef HAVE_MKTEMP diff -Naur patch-2.6.1.orig//configure patch-2.6.1/configure --- patch-2.6.1.orig//configure 2009-12-30 15:31:48.000000000 +0000 +++ patch-2.6.1/configure 2010-02-22 22:28:09.000000000 +0000 @@ -8434,7 +8434,8 @@ -for ac_func in geteuid getuid mktemp raise sigaction sigprocmask sigsetmask + +for ac_func in geteuid getuid mktemp mkstemp raise sigaction sigprocmask sigsetmask do as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` { $as_echo "$as_me:$LINENO: checking for $ac_func" >&5 diff -Naur patch-2.6.1.orig//configure.ac patch-2.6.1/configure.ac --- patch-2.6.1.orig//configure.ac 2009-12-30 12:56:30.000000000 +0000 +++ patch-2.6.1/configure.ac 2010-02-22 22:28:04.000000000 +0000 @@ -77,7 +77,7 @@ AC_LIBOBJ([error]) AC_CHECK_DECLS([mktemp]) -AC_CHECK_FUNCS(geteuid getuid mktemp raise sigaction sigprocmask sigsetmask) +AC_CHECK_FUNCS(geteuid getuid mktemp mkstemp raise sigaction sigprocmask sigsetmask) AC_FUNC_FSEEKO gl_FUNC_GLIBC_UNLOCKED_IO gl_FUNC_MALLOC_POSIX diff -Naur patch-2.6.1.orig//src/patch.c patch-2.6.1/src/patch.c --- patch-2.6.1.orig//src/patch.c 2009-12-30 12:56:30.000000000 +0000 +++ patch-2.6.1/src/patch.c 2010-02-22 22:28:04.000000000 +0000 @@ -49,7 +49,9 @@ static void init_output (char const *, int, struct outstate *); static void init_reject (void); static void reinitialize_almost_everything (void); +#if ! HAVE_MKSTEMP static void remove_if_needed (char const *, int volatile *); +#endif static void usage (FILE *, int) __attribute__((noreturn)); static void abort_hunk (bool, bool); @@ -1350,9 +1352,13 @@ static FILE * create_output_file (char const *name, int open_flags) { +#if HAVE_MKSTEMP + FILE *f = fopen (name, binary_transput ? "wb" : "w"); +#else int fd = create_file (name, O_WRONLY | binary_transput | open_flags, instat.st_mode, true); FILE *f = fdopen (fd, binary_transput ? "wb" : "w"); +#endif if (! f) pfatal ("Can't create file %s", quotearg (name)); return f; @@ -1526,14 +1532,34 @@ make_temp (char letter) { char *r; -#if HAVE_MKTEMP +#if defined(HAVE_MKTEMP) || defined(HAVE_MKSTEMP) char const *tmpdir = getenv ("TMPDIR"); /* Unix tradition */ if (!tmpdir) tmpdir = getenv ("TMP"); /* DOS tradition */ if (!tmpdir) tmpdir = getenv ("TEMP"); /* another DOS tradition */ if (!tmpdir) tmpdir = TMPDIR; r = xmalloc (strlen (tmpdir) + 10); sprintf (r, "%s/p%cXXXXXX", tmpdir, letter); +#endif +#if HAVE_MKSTEMP + int tfd; /* temporary file descriptor */ + mode_t old_umask; + + /* man page for mkstemp says: + "The old behaviour (creating a file with mode 0666) may be a security + risk, especially since other Unix flavours use 0600, and somebody might + overlook this detail when porting programs. + More generally, the POSIX specification does not say anything about + file modes, so the application should make sure its umask is set appro- + priately before calling mkstemp." + So we save old umask and set mode 0600 before calling mkstemp. */ + old_umask = umask (0177); + tfd = mkstemp (r); + umask (old_umask); + if (tfd == -1) + pfatal ("mkstemp"); + +#elif HAVE_MKTEMP /* It is OK to use mktemp here, since the rest of the code always opens temp files with O_EXCL. It might be better to use mkstemp to avoid some DoS problems, but simply substituting mkstemp for @@ -1564,6 +1590,7 @@ exit (2); } +#if ! HAVE_MKSTEMP static void remove_if_needed (char const *name, int volatile *needs_removal) { @@ -1573,12 +1600,22 @@ *needs_removal = 0; } } +#endif static void cleanup (void) { +#if HAVE_MKSTEMP + /* Remove files created by mkstemp. we don't want to end up with ziliards + temporary patch files in /tmp */ + unlink (TMPINNAME); + unlink (TMPOUTNAME); + unlink (TMPPATNAME); + unlink (TMPREJNAME); +#else remove_if_needed (TMPINNAME, &TMPINNAME_needs_removal); remove_if_needed (TMPOUTNAME, &TMPOUTNAME_needs_removal); remove_if_needed (TMPPATNAME, &TMPPATNAME_needs_removal); remove_if_needed (TMPREJNAME, &TMPREJNAME_needs_removal); +#endif } diff -Naur patch-2.6.1.orig//src/pch.c patch-2.6.1/src/pch.c --- patch-2.6.1.orig//src/pch.c 2009-12-30 12:56:30.000000000 +0000 +++ patch-2.6.1/src/pch.c 2010-02-22 22:28:04.000000000 +0000 @@ -126,12 +126,16 @@ else { size_t charsread; +#if HAVE_MKSTEMP + pfp = fopen (TMPPATNAME, "w+b"); +#else int exclusive = TMPPATNAME_needs_removal ? 0 : O_EXCL; TMPPATNAME_needs_removal = 1; pfp = fdopen (create_file (TMPPATNAME, O_RDWR | O_BINARY | exclusive, (mode_t) 0, true), "w+b"); +#endif if (!pfp) pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME)); for (st.st_size = 0;