From e0fdb6943066032db294720915c3bd644bf2bcd1 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 4 Aug 2013 09:56:56 -0700 Subject: [PATCH] Fix some minor races in hosts lacking mkostemp. Gnulib's emulation of mkostemp doesn't have races that Emacs's does. * configure.ac (mkostemp): Remove check for this function; gnulib does the check now. (mkstemp): Remove check for this no-longer-used function. * lib/mkostemp.c, lib/secure_getenv.c, lib/tempname.c, lib/tempname.h: * m4/mkostemp.m4, m4/secure_getenv.m4, m4/tempname.m4: New files, copied from Gnulib. * lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate. * admin/merge-gnulib (GNULIB_MODULES): Add mkostemp. * lib-src/movemail.c (main): * lib-src/update-game-score.c (write_scores): Use mkostemp (which now works on all platforms, due to changes in the portability layer) rather than mktemp (which has a race) or mkstemp (which we no longer bother with). * src/callproc.c (create_temp_file): * src/filelock.c (create_lock_file): Assume mkostemp, since it's now provided by Gnulib. Fixes: debbugs:15015 --- ChangeLog | 12 ++ admin/ChangeLog | 5 + admin/merge-gnulib | 2 +- configure.ac | 2 +- lib-src/ChangeLog | 9 ++ lib-src/movemail.c | 17 +- lib-src/update-game-score.c | 12 +- lib/gnulib.mk | 32 +++- lib/mkostemp.c | 46 ++++++ lib/secure_getenv.c | 41 +++++ lib/tempname.c | 306 ++++++++++++++++++++++++++++++++++++ lib/tempname.h | 50 ++++++ m4/gnulib-comp.m4 | 44 ++++++ m4/mkostemp.m4 | 23 +++ m4/secure_getenv.m4 | 25 +++ m4/tempname.m4 | 19 +++ src/ChangeLog | 7 + src/callproc.c | 14 +- src/filelock.c | 18 +-- 19 files changed, 632 insertions(+), 52 deletions(-) create mode 100644 lib/mkostemp.c create mode 100644 lib/secure_getenv.c create mode 100644 lib/tempname.c create mode 100644 lib/tempname.h create mode 100644 m4/mkostemp.m4 create mode 100644 m4/secure_getenv.m4 create mode 100644 m4/tempname.m4 diff --git a/ChangeLog b/ChangeLog index bc70b1e444..cd8ce1a25e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2013-08-04 Paul Eggert + + Fix some minor races in hosts lacking mkostemp (Bug#15015). + Gnulib's emulation of mkostemp doesn't have races that Emacs's does. + * configure.ac (mkostemp): Remove check for this function; + gnulib does the check now. + (mkstemp): Remove check for this no-longer-used function. + * lib/mkostemp.c, lib/secure_getenv.c, lib/tempname.c, lib/tempname.h: + * m4/mkostemp.m4, m4/secure_getenv.m4, m4/tempname.m4: + New files, copied from Gnulib. + * lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate. + 2013-07-29 Michael Albinus * INSTALL (DETAILED BUILDING AND INSTALLATION): Add diff --git a/admin/ChangeLog b/admin/ChangeLog index cbcd698e8b..5373a9d20b 100644 --- a/admin/ChangeLog +++ b/admin/ChangeLog @@ -1,3 +1,8 @@ +2013-08-04 Paul Eggert + + Fix some minor races in hosts lacking mkostemp (Bug#15015). + * merge-gnulib (GNULIB_MODULES): Add mkostemp. + 2013-07-12 Glenn Morris * admin.el (manual-style-string): Use non-abbreviated url. diff --git a/admin/merge-gnulib b/admin/merge-gnulib index 82e0cd77fc..97126704d1 100755 --- a/admin/merge-gnulib +++ b/admin/merge-gnulib @@ -32,7 +32,7 @@ GNULIB_MODULES=' fcntl fcntl-h fdatasync fdopendir filemode fstatat fsync getloadavg getopt-gnu gettime gettimeofday intprops largefile lstat - manywarnings memrchr mktime + manywarnings memrchr mkostemp mktime pipe2 pselect pthread_sigmask putenv qacl readlink readlinkat sig2str socklen stat-time stdalign stdarg stdbool stdio strftime strtoimax strtoumax symlink sys_stat diff --git a/configure.ac b/configure.ac index d8557f2989..89aba556bb 100644 --- a/configure.ac +++ b/configure.ac @@ -3277,7 +3277,7 @@ select getpagesize setlocale \ getrlimit setrlimit shutdown getaddrinfo \ strsignal setitimer \ sendto recvfrom getsockname getpeername getifaddrs freeifaddrs \ -gai_strerror mkostemp mkstemp getline getdelim sync \ +gai_strerror getline getdelim sync \ difftime posix_memalign \ getpwent endpwent getgrent endgrent \ touchlock \ diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index f41c23df5d..2cfe46f535 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog @@ -1,3 +1,12 @@ +2013-08-04 Paul Eggert + + Fix some minor races in hosts lacking mkostemp (Bug#15015). + * movemail.c (main): + * update-game-score.c (write_scores): + Use mkostemp (which now works on all platforms, due to changes + in the portability layer) rather than mktemp (which has a race) + or mkstemp (which we no longer bother with). + 2013-07-10 Paul Eggert Port to C89. diff --git a/lib-src/movemail.c b/lib-src/movemail.c index 81ac8aa187..9434782cb1 100644 --- a/lib-src/movemail.c +++ b/lib-src/movemail.c @@ -304,24 +304,13 @@ main (int argc, char **argv) memcpy (tempname, inname, inname_dirlen); strcpy (tempname + inname_dirlen, "EXXXXXX"); -#ifdef HAVE_MKSTEMP - desc = mkstemp (tempname); -#else - mktemp (tempname); - if (!*tempname) - desc = -1; - else - { - unlink (tempname); - desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600); - } -#endif + desc = mkostemp (tempname, 0); if (desc < 0) { - int mkstemp_errno = errno; + int mkostemp_errno = errno; error ("error while creating what would become the lock file", 0, 0); - errno = mkstemp_errno; + errno = mkostemp_errno; pfatal_with_name (tempname); } close (desc); diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index ec8b431777..1699e305c8 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c @@ -383,6 +383,7 @@ sort_scores (struct score_entry *scores, int count, int reverse) static int write_scores (const char *filename, const struct score_entry *scores, int count) { + int fd; FILE *f; int i; char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1); @@ -390,12 +391,11 @@ write_scores (const char *filename, const struct score_entry *scores, int count) return -1; strcpy (tempfile, filename); strcat (tempfile, ".tempXXXXXX"); -#ifdef HAVE_MKSTEMP - if (mkstemp (tempfile) < 0 -#else - if (mktemp (tempfile) != tempfile -#endif - || !(f = fopen (tempfile, "w"))) + fd = mkostemp (tempfile, 0); + if (fd < 0) + return -1; + f = fdopen (fd, "w"); + if (! f) return -1; for (i = 0; i < count; i++) if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username, diff --git a/lib/gnulib.mk b/lib/gnulib.mk index d053e15efc..32255181fb 100644 --- a/lib/gnulib.mk +++ b/lib/gnulib.mk @@ -21,7 +21,7 @@ # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. -# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=open --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=sigprocmask --avoid=sys_types --avoid=threadlib --makefile-name=gnulib.mk --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt c-ctype c-strcase careadlinkat close-stream crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 dtoastr dtotimespec dup2 environ execinfo faccessat fcntl fcntl-h fdatasync fdopendir filemode fstatat fsync getloadavg getopt-gnu gettime gettimeofday intprops largefile lstat manywarnings memrchr mktime pipe2 pselect pthread_sigmask putenv qacl readlink readlinkat sig2str socklen stat-time stdalign stdarg stdbool stdio strftime strtoimax strtoumax symlink sys_stat sys_time time timer-time timespec-add timespec-sub unsetenv utimens warnings +# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=open --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=sigprocmask --avoid=sys_types --avoid=threadlib --makefile-name=gnulib.mk --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt c-ctype c-strcase careadlinkat close-stream crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 dtoastr dtotimespec dup2 environ execinfo faccessat fcntl fcntl-h fdatasync fdopendir filemode fstatat fsync getloadavg getopt-gnu gettime gettimeofday intprops largefile lstat manywarnings memrchr mkostemp mktime pipe2 pselect pthread_sigmask putenv qacl readlink readlinkat sig2str socklen stat-time stdalign stdarg stdbool stdio strftime strtoimax strtoumax symlink sys_stat sys_time time timer-time timespec-add timespec-sub unsetenv utimens warnings MOSTLYCLEANFILES += core *.stackdump @@ -560,6 +560,15 @@ EXTRA_libgnu_a_SOURCES += memrchr.c ## end gnulib module memrchr +## begin gnulib module mkostemp + + +EXTRA_DIST += mkostemp.c + +EXTRA_libgnu_a_SOURCES += mkostemp.c + +## end gnulib module mkostemp + ## begin gnulib module mktime @@ -657,6 +666,17 @@ EXTRA_DIST += root-uid.h ## end gnulib module root-uid +## begin gnulib module secure_getenv + +if gl_GNULIB_ENABLED_secure_getenv + +endif +EXTRA_DIST += secure_getenv.c + +EXTRA_libgnu_a_SOURCES += secure_getenv.c + +## end gnulib module secure_getenv + ## begin gnulib module sig2str @@ -1480,6 +1500,16 @@ EXTRA_DIST += sys_time.in.h ## end gnulib module sys_time +## begin gnulib module tempname + +if gl_GNULIB_ENABLED_tempname +libgnu_a_SOURCES += tempname.c + +endif +EXTRA_DIST += tempname.h + +## end gnulib module tempname + ## begin gnulib module time BUILT_SOURCES += time.h diff --git a/lib/mkostemp.c b/lib/mkostemp.c new file mode 100644 index 0000000000..2188ff5dd6 --- /dev/null +++ b/lib/mkostemp.c @@ -0,0 +1,46 @@ +/* Copyright (C) 1998-1999, 2001, 2005-2007, 2009-2013 Free Software + Foundation, Inc. + This file is derived from the one in the GNU C Library. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#if !_LIBC +# include +#endif + +#include + +#if !_LIBC +# include "tempname.h" +# define __gen_tempname gen_tempname +# ifndef __GTFILE +# define __GT_FILE GT_FILE +# endif +#endif + +#include + +#ifndef __GT_FILE +# define __GT_FILE 0 +#endif + +/* Generate a unique temporary file name from XTEMPLATE. + The last six characters of XTEMPLATE must be "XXXXXX"; + they are replaced with a string that makes the file name unique. + Then open the file and return a fd. */ +int +mkostemp (char *xtemplate, int flags) +{ + return __gen_tempname (xtemplate, 0, flags, __GT_FILE); +} diff --git a/lib/secure_getenv.c b/lib/secure_getenv.c new file mode 100644 index 0000000000..6c11c5e0ed --- /dev/null +++ b/lib/secure_getenv.c @@ -0,0 +1,41 @@ +/* Look up an environment variable more securely. + + Copyright 2013 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include + +#if !HAVE___SECURE_GETENV +# if HAVE_ISSETUGID +# include +# else +# undef issetugid +# define issetugid() 1 +# endif +#endif + +char * +secure_getenv (char const *name) +{ +#if HAVE___SECURE_GETENV + return __secure_getenv (name); +#else + if (issetugid ()) + return 0; + return getenv (name); +#endif +} diff --git a/lib/tempname.c b/lib/tempname.c new file mode 100644 index 0000000000..087b79b31f --- /dev/null +++ b/lib/tempname.c @@ -0,0 +1,306 @@ +/* tempname.c - generate the name of a temporary file. + + Copyright (C) 1991-2003, 2005-2007, 2009-2013 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Extracted from glibc sysdeps/posix/tempname.c. See also tmpdir.c. */ + +#if !_LIBC +# include +# include "tempname.h" +#endif + +#include +#include + +#include +#ifndef __set_errno +# define __set_errno(Val) errno = (Val) +#endif + +#include +#ifndef P_tmpdir +# define P_tmpdir "/tmp" +#endif +#ifndef TMP_MAX +# define TMP_MAX 238328 +#endif +#ifndef __GT_FILE +# define __GT_FILE 0 +# define __GT_DIR 1 +# define __GT_NOCREATE 2 +#endif +#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \ + || GT_NOCREATE != __GT_NOCREATE) +# error report this to bug-gnulib@gnu.org +#endif + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#if _LIBC +# define struct_stat64 struct stat64 +#else +# define struct_stat64 struct stat +# define __gen_tempname gen_tempname +# define __getpid getpid +# define __gettimeofday gettimeofday +# define __mkdir mkdir +# define __open open +# define __lxstat64(version, file, buf) lstat (file, buf) +# define __secure_getenv secure_getenv +#endif + +#ifdef _LIBC +# include +# if HP_TIMING_AVAIL +# define RANDOM_BITS(Var) \ + if (__builtin_expect (value == UINT64_C (0), 0)) \ + { \ + /* If this is the first time this function is used initialize \ + the variable we accumulate the value in to some somewhat \ + random value. If we'd not do this programs at startup time \ + might have a reduced set of possible names, at least on slow \ + machines. */ \ + struct timeval tv; \ + __gettimeofday (&tv, NULL); \ + value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \ + } \ + HP_TIMING_NOW (Var) +# endif +#endif + +/* Use the widest available unsigned type if uint64_t is not + available. The algorithm below extracts a number less than 62**6 + (approximately 2**35.725) from uint64_t, so ancient hosts where + uintmax_t is only 32 bits lose about 3.725 bits of randomness, + which is better than not having mkstemp at all. */ +#if !defined UINT64_MAX && !defined uint64_t +# define uint64_t uintmax_t +#endif + +#if _LIBC +/* Return nonzero if DIR is an existent directory. */ +static int +direxists (const char *dir) +{ + struct_stat64 buf; + return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode); +} + +/* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is + non-null and exists, uses it; otherwise uses the first of $TMPDIR, + P_tmpdir, /tmp that exists. Copies into TMPL a template suitable + for use with mk[s]temp. Will fail (-1) if DIR is non-null and + doesn't exist, none of the searched dirs exists, or there's not + enough space in TMPL. */ +int +__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, + int try_tmpdir) +{ + const char *d; + size_t dlen, plen; + + if (!pfx || !pfx[0]) + { + pfx = "file"; + plen = 4; + } + else + { + plen = strlen (pfx); + if (plen > 5) + plen = 5; + } + + if (try_tmpdir) + { + d = __secure_getenv ("TMPDIR"); + if (d != NULL && direxists (d)) + dir = d; + else if (dir != NULL && direxists (dir)) + /* nothing */ ; + else + dir = NULL; + } + if (dir == NULL) + { + if (direxists (P_tmpdir)) + dir = P_tmpdir; + else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) + dir = "/tmp"; + else + { + __set_errno (ENOENT); + return -1; + } + } + + dlen = strlen (dir); + while (dlen > 1 && dir[dlen - 1] == '/') + dlen--; /* remove trailing slashes */ + + /* check we have room for "${dir}/${pfx}XXXXXX\0" */ + if (tmpl_len < dlen + 1 + plen + 6 + 1) + { + __set_errno (EINVAL); + return -1; + } + + sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx); + return 0; +} +#endif /* _LIBC */ + +/* These are the characters used in temporary file names. */ +static const char letters[] = +"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +/* Generate a temporary file name based on TMPL. TMPL must match the + rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). + The name constructed does not exist at the time of the call to + __gen_tempname. TMPL is overwritten with the result. + + KIND may be one of: + __GT_NOCREATE: simply verify that the name does not exist + at the time of the call. + __GT_FILE: create the file using open(O_CREAT|O_EXCL) + and return a read-write fd. The file is mode 0600. + __GT_DIR: create a directory, which will be mode 0700. + + We use a clever algorithm to get hard-to-predict names. */ +int +__gen_tempname (char *tmpl, int suffixlen, int flags, int kind) +{ + int len; + char *XXXXXX; + static uint64_t value; + uint64_t random_time_bits; + unsigned int count; + int fd = -1; + int save_errno = errno; + struct_stat64 st; + + /* A lower bound on the number of temporary files to attempt to + generate. The maximum total number of temporary file names that + can exist for a given template is 62**6. It should never be + necessary to try all of these combinations. Instead if a reasonable + number of names is tried (we define reasonable as 62**3) fail to + give the system administrator the chance to remove the problems. */ +#define ATTEMPTS_MIN (62 * 62 * 62) + + /* The number of times to attempt to generate a temporary file. To + conform to POSIX, this must be no smaller than TMP_MAX. */ +#if ATTEMPTS_MIN < TMP_MAX + unsigned int attempts = TMP_MAX; +#else + unsigned int attempts = ATTEMPTS_MIN; +#endif + + len = strlen (tmpl); + if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6)) + { + __set_errno (EINVAL); + return -1; + } + + /* This is where the Xs start. */ + XXXXXX = &tmpl[len - 6 - suffixlen]; + + /* Get some more or less random data. */ +#ifdef RANDOM_BITS + RANDOM_BITS (random_time_bits); +#else + { + struct timeval tv; + __gettimeofday (&tv, NULL); + random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; + } +#endif + value += random_time_bits ^ __getpid (); + + for (count = 0; count < attempts; value += 7777, ++count) + { + uint64_t v = value; + + /* Fill in the random bits. */ + XXXXXX[0] = letters[v % 62]; + v /= 62; + XXXXXX[1] = letters[v % 62]; + v /= 62; + XXXXXX[2] = letters[v % 62]; + v /= 62; + XXXXXX[3] = letters[v % 62]; + v /= 62; + XXXXXX[4] = letters[v % 62]; + v /= 62; + XXXXXX[5] = letters[v % 62]; + + switch (kind) + { + case __GT_FILE: + fd = __open (tmpl, + (flags & ~O_ACCMODE) + | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); + break; + + case __GT_DIR: + fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR); + break; + + case __GT_NOCREATE: + /* This case is backward from the other three. __gen_tempname + succeeds if __xstat fails because the name does not exist. + Note the continue to bypass the common logic at the bottom + of the loop. */ + if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) + { + if (errno == ENOENT) + { + __set_errno (save_errno); + return 0; + } + else + /* Give up now. */ + return -1; + } + continue; + + default: + assert (! "invalid KIND in __gen_tempname"); + abort (); + } + + if (fd >= 0) + { + __set_errno (save_errno); + return fd; + } + else if (errno != EEXIST) + return -1; + } + + /* We got out of the loop because we ran out of combinations to try. */ + __set_errno (EEXIST); + return -1; +} diff --git a/lib/tempname.h b/lib/tempname.h new file mode 100644 index 0000000000..333267dfd0 --- /dev/null +++ b/lib/tempname.h @@ -0,0 +1,50 @@ +/* Create a temporary file or directory. + + Copyright (C) 2006, 2009-2013 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* header written by Eric Blake */ + +#ifndef GL_TEMPNAME_H +# define GL_TEMPNAME_H + +# include + +# ifdef __GT_FILE +# define GT_FILE __GT_FILE +# define GT_DIR __GT_DIR +# define GT_NOCREATE __GT_NOCREATE +# else +# define GT_FILE 0 +# define GT_DIR 1 +# define GT_NOCREATE 2 +# endif + +/* Generate a temporary file name based on TMPL. TMPL must match the + rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). + The name constructed does not exist at the time of the call to + gen_tempname. TMPL is overwritten with the result. + + KIND may be one of: + GT_NOCREATE: simply verify that the name does not exist + at the time of the call. + GT_FILE: create a large file using open(O_CREAT|O_EXCL) + and return a read-write fd. The file is mode 0600. + GT_DIR: create a directory, which will be mode 0700. + + We use a clever algorithm to get hard-to-predict names. */ +extern int gen_tempname (char *tmpl, int suffixlen, int flags, int kind); + +#endif /* GL_TEMPNAME_H */ diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4 index d7062e862d..af9a9ca3ae 100644 --- a/m4/gnulib-comp.m4 +++ b/m4/gnulib-comp.m4 @@ -89,6 +89,7 @@ AC_DEFUN([gl_EARLY], # Code from module lstat: # Code from module manywarnings: # Code from module memrchr: + # Code from module mkostemp: # Code from module mktime: # Code from module multiarch: # Code from module nocrash: @@ -102,6 +103,7 @@ AC_DEFUN([gl_EARLY], # Code from module readlink: # Code from module readlinkat: # Code from module root-uid: + # Code from module secure_getenv: # Code from module sig2str: # Code from module signal-h: # Code from module snippet/_Noreturn: @@ -134,6 +136,7 @@ AC_DEFUN([gl_EARLY], # Code from module sys_select: # Code from module sys_stat: # Code from module sys_time: + # Code from module tempname: # Code from module time: # Code from module time_r: # Code from module timer-time: @@ -274,6 +277,13 @@ AC_DEFUN([gl_INIT], gl_PREREQ_MEMRCHR fi gl_STRING_MODULE_INDICATOR([memrchr]) + gl_FUNC_MKOSTEMP + if test $HAVE_MKOSTEMP = 0; then + AC_LIBOBJ([mkostemp]) + gl_PREREQ_MKOSTEMP + fi + gl_MODULE_INDICATOR([mkostemp]) + gl_STDLIB_MODULE_INDICATOR([mkostemp]) gl_FUNC_MKTIME if test $REPLACE_MKTIME = 1; then AC_LIBOBJ([mktime]) @@ -381,9 +391,11 @@ AC_DEFUN([gl_INIT], gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false gl_gnulib_enabled_pathmax=false gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false + gl_gnulib_enabled_secure_getenv=false gl_gnulib_enabled_stat=false gl_gnulib_enabled_strtoll=false gl_gnulib_enabled_strtoull=false + gl_gnulib_enabled_tempname=false gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec=false func_gl_gnulib_m4code_260941c0e5dc67ec9e87d1fb321c300b () { @@ -485,6 +497,18 @@ AC_DEFUN([gl_INIT], gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=true fi } + func_gl_gnulib_m4code_secure_getenv () + { + if ! $gl_gnulib_enabled_secure_getenv; then + gl_FUNC_SECURE_GETENV + if test $HAVE_SECURE_GETENV = 0; then + AC_LIBOBJ([secure_getenv]) + gl_PREREQ_SECURE_GETENV + fi + gl_STDLIB_MODULE_INDICATOR([secure_getenv]) + gl_gnulib_enabled_secure_getenv=true + fi + } func_gl_gnulib_m4code_stat () { if ! $gl_gnulib_enabled_stat; then @@ -527,6 +551,14 @@ AC_DEFUN([gl_INIT], gl_gnulib_enabled_strtoull=true fi } + func_gl_gnulib_m4code_tempname () + { + if ! $gl_gnulib_enabled_tempname; then + gl_FUNC_GEN_TEMPNAME + gl_gnulib_enabled_tempname=true + func_gl_gnulib_m4code_secure_getenv + fi + } func_gl_gnulib_m4code_682e609604ccaac6be382e4ee3a4eaec () { if ! $gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec; then @@ -569,6 +601,9 @@ AC_DEFUN([gl_INIT], if test $REPLACE_LSTAT = 1; then func_gl_gnulib_m4code_stat fi + if test $HAVE_MKOSTEMP = 0; then + func_gl_gnulib_m4code_tempname + fi if test $HAVE_READLINK = 0 || test $REPLACE_READLINK = 1; then func_gl_gnulib_m4code_stat fi @@ -598,9 +633,11 @@ AC_DEFUN([gl_INIT], AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7]) AM_CONDITIONAL([gl_GNULIB_ENABLED_pathmax], [$gl_gnulib_enabled_pathmax]) AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c]) + AM_CONDITIONAL([gl_GNULIB_ENABLED_secure_getenv], [$gl_gnulib_enabled_secure_getenv]) AM_CONDITIONAL([gl_GNULIB_ENABLED_stat], [$gl_gnulib_enabled_stat]) AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll]) AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoull], [$gl_gnulib_enabled_strtoull]) + AM_CONDITIONAL([gl_GNULIB_ENABLED_tempname], [$gl_gnulib_enabled_tempname]) AM_CONDITIONAL([gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec], [$gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec]) # End of code from modules m4_ifval(gl_LIBSOURCES_LIST, [ @@ -806,6 +843,7 @@ AC_DEFUN([gl_FILE_LIST], [ lib/md5.c lib/md5.h lib/memrchr.c + lib/mkostemp.c lib/mktime-internal.h lib/mktime.c lib/openat-priv.h @@ -821,6 +859,7 @@ AC_DEFUN([gl_FILE_LIST], [ lib/readlink.c lib/readlinkat.c lib/root-uid.h + lib/secure_getenv.c lib/sha1.c lib/sha1.h lib/sha256.c @@ -853,6 +892,8 @@ AC_DEFUN([gl_FILE_LIST], [ lib/sys_select.in.h lib/sys_stat.in.h lib/sys_time.in.h + lib/tempname.c + lib/tempname.h lib/time.in.h lib/time_r.c lib/timespec-add.c @@ -908,6 +949,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/manywarnings.m4 m4/md5.m4 m4/memrchr.m4 + m4/mkostemp.m4 m4/mktime.m4 m4/multiarch.m4 m4/nocrash.m4 @@ -919,6 +961,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/putenv.m4 m4/readlink.m4 m4/readlinkat.m4 + m4/secure_getenv.m4 m4/setenv.m4 m4/sha1.m4 m4/sha256.m4 @@ -948,6 +991,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/sys_socket_h.m4 m4/sys_stat_h.m4 m4/sys_time_h.m4 + m4/tempname.m4 m4/time_h.m4 m4/time_r.m4 m4/timer_time.m4 diff --git a/m4/mkostemp.m4 b/m4/mkostemp.m4 new file mode 100644 index 0000000000..6d101eeda3 --- /dev/null +++ b/m4/mkostemp.m4 @@ -0,0 +1,23 @@ +# mkostemp.m4 serial 2 +dnl Copyright (C) 2009-2013 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_MKOSTEMP], +[ + AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) + + dnl Persuade glibc to declare mkostemp(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_CHECK_FUNCS_ONCE([mkostemp]) + if test $ac_cv_func_mkostemp != yes; then + HAVE_MKOSTEMP=0 + fi +]) + +# Prerequisites of lib/mkostemp.c. +AC_DEFUN([gl_PREREQ_MKOSTEMP], +[ +]) diff --git a/m4/secure_getenv.m4 b/m4/secure_getenv.m4 new file mode 100644 index 0000000000..5da5298f66 --- /dev/null +++ b/m4/secure_getenv.m4 @@ -0,0 +1,25 @@ +# Look up an environment variable more securely. +dnl Copyright 2013 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_SECURE_GETENV], +[ + dnl Persuade glibc to declare secure_getenv(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) + AC_CHECK_FUNCS_ONCE([secure_getenv]) + if test $ac_cv_func_secure_getenv = no; then + HAVE_SECURE_GETENV=0 + fi +]) + +# Prerequisites of lib/secure_getenv.c. +AC_DEFUN([gl_PREREQ_SECURE_GETENV], [ + AC_CHECK_FUNCS([__secure_getenv]) + if test $ac_cv_func___secure_getenv = no; then + AC_CHECK_FUNCS([issetugid]) + fi +]) diff --git a/m4/tempname.m4 b/m4/tempname.m4 new file mode 100644 index 0000000000..a6e42dcea8 --- /dev/null +++ b/m4/tempname.m4 @@ -0,0 +1,19 @@ +#serial 5 + +# Copyright (C) 2006-2007, 2009-2013 Free Software Foundation, Inc. +# This file is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# glibc provides __gen_tempname as a wrapper for mk[ds]temp. Expose +# it as a public API, and provide it on systems that are lacking. +AC_DEFUN([gl_FUNC_GEN_TEMPNAME], +[ + gl_PREREQ_TEMPNAME +]) + +# Prerequisites of lib/tempname.c. +AC_DEFUN([gl_PREREQ_TEMPNAME], +[ + : +]) diff --git a/src/ChangeLog b/src/ChangeLog index c29c4c8da5..f8f2fa4708 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2013-08-04 Paul Eggert + + Fix some minor races in hosts lacking mkostemp (Bug#15015). + * callproc.c (create_temp_file): + * filelock.c (create_lock_file): + Assume mkostemp, since it's now provided by Gnulib. + 2013-08-04 Eli Zaretskii * w32.c (mkostemp): New function. diff --git a/src/callproc.c b/src/callproc.c index 450fc57f92..54bc5cd9dc 100644 --- a/src/callproc.c +++ b/src/callproc.c @@ -1009,23 +1009,11 @@ create_temp_file (ptrdiff_t nargs, Lisp_Object *args) tempfile = SSDATA (filename_string); { - int fd; - -#ifdef HAVE_MKOSTEMP - fd = mkostemp (tempfile, O_CLOEXEC); -#elif defined HAVE_MKSTEMP - fd = mkstemp (tempfile); -#else - errno = EEXIST; - mktemp (tempfile); - fd = *tempfile ? 0 : -1; -#endif + int fd = mkostemp (tempfile, O_CLOEXEC); if (fd < 0) report_file_error ("Failed to open temporary file using pattern", pattern); -#if defined HAVE_MKOSTEMP || defined HAVE_MKSTEMP emacs_close (fd); -#endif } record_unwind_protect (delete_temp_file, filename_string); diff --git a/src/filelock.c b/src/filelock.c index b9c991e4ba..0f31b7d4de 100644 --- a/src/filelock.c +++ b/src/filelock.c @@ -411,28 +411,14 @@ create_lock_file (char *lfname, char *lock_info_str, bool force) memcpy (nonce, lfname, lfdirlen); strcpy (nonce + lfdirlen, nonce_base); -#if HAVE_MKOSTEMP - /* Prefer mkostemp to mkstemp, as it avoids a window where FD is - temporarily open without close-on-exec. */ fd = mkostemp (nonce, O_BINARY | O_CLOEXEC); -#elif HAVE_MKSTEMP - /* Prefer mkstemp to mktemp, as it avoids a race between - mktemp and emacs_open. */ - fd = mkstemp (nonce); -#else - mktemp (nonce); - fd = emacs_open (nonce, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, - S_IRUSR | S_IWUSR); -#endif - if (fd < 0) err = errno; else { ptrdiff_t lock_info_len; -#if ! (HAVE_MKOSTEMP && O_CLOEXEC) - fcntl (fd, F_SETFD, FD_CLOEXEC); -#endif + if (! O_CLOEXEC) + fcntl (fd, F_SETFD, FD_CLOEXEC); lock_info_len = strlen (lock_info_str); err = 0; /* Use 'write', not 'emacs_write', as garbage collection -- 2.20.1