summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2013-03-12 15:07:12 +0000
committermillert <millert@openbsd.org>2013-03-12 15:07:12 +0000
commit0bc28667d0d1fccd3ae9e90402b3533107fc30a8 (patch)
treee29262251347c62edd7891de32b31a1760674c67
parentAdd check that mkstemp() fails with EINVAL if the string has fewer than (diff)
downloadwireguard-openbsd-0bc28667d0d1fccd3ae9e90402b3533107fc30a8.tar.xz
wireguard-openbsd-0bc28667d0d1fccd3ae9e90402b3533107fc30a8.zip
Require that the template include at least 6 trailing Xs to match
POSIX mkstemp/mkdtemp. Check before the call to mkstemp/mkdtemp so we can give a better error message than "invalid argument". OK deraadt@ jmc@
-rw-r--r--usr.bin/mktemp/mktemp.145
-rw-r--r--usr.bin/mktemp/mktemp.c26
2 files changed, 58 insertions, 13 deletions
diff --git a/usr.bin/mktemp/mktemp.1 b/usr.bin/mktemp/mktemp.1
index 90094ce3ebe..8f46cea2023 100644
--- a/usr.bin/mktemp/mktemp.1
+++ b/usr.bin/mktemp/mktemp.1
@@ -1,6 +1,7 @@
-.\" $OpenBSD: mktemp.1,v 1.26 2010/12/27 21:18:44 millert Exp $
+.\" $OpenBSD: mktemp.1,v 1.27 2013/03/12 15:07:12 millert Exp $
.\"
-.\" Copyright (c) 1996, 2000, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
+.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013
+.\" Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -14,7 +15,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: December 27 2010 $
+.Dd $Mdocdate: March 12 2013 $
.Dt MKTEMP 1
.Os
.Sh NAME
@@ -33,7 +34,7 @@ utility takes the given filename
and overwrites a portion of it to create a unique filename.
The
.Ar template
-may be any filename with some number of
+may be any filename with at least six
.Ql X Ns s
appended
to it, for example
@@ -196,7 +197,7 @@ TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
echo "program output" >> $TMPFILE
.Ed
.Pp
-In some cases, we want the script to catch the error.
+In other cases, we want the script to catch the error.
For instance, if we attempt to create two temporary files and
the second one fails we need to remove the first before exiting.
.Bd -literal -offset indent
@@ -220,6 +221,40 @@ TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
rm -f $TMPFILE
}
.Ed
+.Sh DIAGNOSTICS
+One of the following error messages may be displayed if
+.Nm
+does not succeed and the
+.Fl q
+option was not specified:
+.Bl -tag -width indent
+.It Li "insufficient number of Xs in template"
+The specified
+.Ar template
+contained fewer than six
+.Ql X Ns s
+at the end.
+.It Li "template must not contain directory separators in -t mode"
+The
+.Ar template
+contained one or more directory components and the
+.Fl t
+option was specified.
+.It Li "cannot make temp dir"
+.Nm
+was unable to create the temporary directory for any of the reasons
+specified by
+.Xr mkdir 2 .
+.It Li "cannot make temp file"
+.Nm
+was unable to create the temporary file for any of the reasons
+specified by
+.Xr open 2 .
+.It Li "cannot allocate memory"
+.Nm
+was unable to allocate memory for any of the reasons specified by
+.Xr malloc 3 .
+.El
.Sh SEE ALSO
.Xr mktemp 3
.Sh HISTORY
diff --git a/usr.bin/mktemp/mktemp.c b/usr.bin/mktemp/mktemp.c
index 414a0960e73..e23ebfe46a0 100644
--- a/usr.bin/mktemp/mktemp.c
+++ b/usr.bin/mktemp/mktemp.c
@@ -1,7 +1,8 @@
-/* $OpenBSD: mktemp.c,v 1.15 2009/10/27 23:59:40 deraadt Exp $ */
+/* $OpenBSD: mktemp.c,v 1.16 2013/03/12 15:07:12 millert Exp $ */
/*
- * Copyright (c) 1996, 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1996, 1997, 2001-2003, 2013
+ * Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -30,7 +31,7 @@ main(int argc, char *argv[])
{
int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
char *cp, *template, *tempfile, *prefix = _PATH_TMP;
- int plen;
+ size_t len;
while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
switch(ch) {
@@ -78,14 +79,23 @@ main(int argc, char *argv[])
cp = getenv("TMPDIR");
if (cp != NULL && *cp != '\0')
prefix = cp;
- plen = strlen(prefix);
- while (plen != 0 && prefix[plen - 1] == '/')
- plen--;
+ len = strlen(prefix);
+ while (len != 0 && prefix[len - 1] == '/')
+ len--;
- if (asprintf(&tempfile, "%.*s/%s", plen, prefix, template) < 0)
+ if (asprintf(&tempfile, "%.*s/%s", (int)len, prefix, template) < 0)
tempfile = NULL;
- } else
+ } else {
+ len = strlen(template);
+ if (len < 6 || strcmp(&template[len - 6], "XXXXXX")) {
+ if (!quiet) {
+ warningx("insufficient number of Xs in template `%s'",
+ template);
+ }
+ exit(1);
+ }
tempfile = strdup(template);
+ }
if (tempfile == NULL) {
if (!quiet)
warnx("cannot allocate memory");