1 .\" $NetBSD: mktemp.3,v 1.30 2014/06/19 09:30:33 wiz Exp $
3 .\" Copyright (c) 1989, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
42 .Nd make unique temporary file or directory name
48 .Fn mktemp "char *template"
50 .Fn mkstemp "char *template"
52 .Fn mkostemp "char *template" "int oflags"
54 .Fn mkostemps "char *template" "int suffixlen" "int oflags"
56 .Fn mkdtemp "char *template"
59 .Fn mkstemps "char *template" "int suffixlen"
64 takes the given file name template and overwrites a portion of it
65 to create a file name.
66 This file name is unique and suitable for use
68 The template may be any file name with some number of
73 .Pa /tmp/temp.XXXXXX .
77 are replaced with the current process number and/or a
78 unique letter combination.
79 The number of unique file names
81 can return depends on the number of
87 implementation of the functions will accept any number of trailing
90 for portability reasons one should use only six.
96 testing roughly 26 ** 6 (308915776) combinations.
101 makes the same replacement to the template and creates the template file,
102 mode 0600, returning a file descriptor opened for reading and writing.
103 This avoids the race between testing for a file's existence and opening it
110 but allows specifying additional
114 The permitted flags are
127 functions act the same as
132 except they permit a suffix to exist in the template.
133 The template should be of the form
134 .Pa /tmp/tmpXXXXXXsuffix .
140 are told the length of the suffix string.
147 but it creates a mode 0700 directory instead and returns the path.
149 Please note that the permissions of the file or directory being created are
150 subject to the restrictions imposed by the
153 It may thus happen that the created file is unreadable and/or unwritable.
160 return a pointer to the template on success and
170 returns \-1 if no suitable file could be created.
171 If either call fails an error code is placed in the global variable
174 Quite often a programmer will want to replace a use of
178 usually to avoid the problems described above.
179 Doing this correctly requires a good understanding of the code in question.
181 For instance, code of this form:
182 .Bd -literal -offset indent
186 strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
187 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
188 fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
194 should be rewritten like this:
195 .Bd -literal -offset indent
200 strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
201 if ((fd = mkstemp(sfn)) == -1 ||
202 (sfp = fdopen(fd, "w+")) == NULL) {
207 fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
213 Often one will find code which uses
215 very early on, perhaps to globally initialize the template nicely, but the
220 on that filename will occur much later.
221 (In almost all cases, the use of
223 will mean that the flags
229 and thus a symbolic link race becomes possible, hence making
233 Furthermore, one must be careful about code which opens, closes, and then
234 re-opens the file in question.
235 Finally, one must ensure that upon error the temporary file is
238 There are also cases where modifying the code to use
246 is better, as long as the code retries a new template if
263 to one of the following values:
266 The pathname portion of the template is not an existing directory.
277 to any value specified by the
286 to any value specified by the
295 to any value specified by the
309 It was however removed from the specification in the
338 function first appeared in
348 functions appeared in
355 there is an obvious race between file name selection and file
356 creation and deletion: the program is typically written to call
361 Subsequently, the program calls
365 and erroneously opens a file (or symbolic link, fifo or other
366 device) that the attacker has created in the expected file location.
369 is recommended, since it atomically creates the file.
370 An attacker can guess the filenames produced by
372 Whenever it is possible,
376 should be used instead.
380 will output a warning message whenever it links code that uses
385 function is nonstandard and should not be used if portability is required.
386 .Sh SECURITY CONSIDERATIONS
389 should generally be avoided, as a hostile process can exploit a race
390 condition in the time between the generation of a temporary filename by
392 and the invoker's use of the temporary name.
393 A link-time warning will be issued advising the use of