1 .\" $NetBSD: mktemp.3,v 1.24 2003/10/15 19:44:51 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
39 .Nd make unique temporary file or directory name
45 .Fn mktemp "char *template"
47 .Fn mkstemp "char *template"
49 .Fn mkdtemp "char *template"
54 takes the given file name template and overwrites a portion of it
55 to create a file name.
56 This file name is unique and suitable for use
58 The template may be any file name with some number of
63 .Pa /tmp/temp.XXXXXX .
67 are replaced with the current process number and/or a
68 unique letter combination.
69 The number of unique file names
71 can return depends on the number of
77 implementation of the functions will accept any number of trailing
80 for portability reasons one should use only six.
86 testing roughly 26 ** 6 (308915776) combinations.
91 makes the same replacement to the template and creates the template file,
92 mode 0600, returning a file descriptor opened for reading and writing.
93 This avoids the race between testing for a file's existence and opening it
101 but it creates a mode 0700 directory instead and returns the path.
103 Please note that the permissions of the file or directory being created are
104 subject to the restrictions imposed by the
107 It may thus happen that the created file is unreadable and/or unwritable.
114 return a pointer to the template on success and
120 returns \-1 if no suitable file could be created.
121 If either call fails an error code is placed in the global variable
124 Quite often a programmer will want to replace a use of
128 usually to avoid the problems described above.
129 Doing this correctly requires a good understanding of the code in question.
131 For instance, code of this form:
132 .Bd -literal -offset indent
136 strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
137 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
138 fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
144 should be rewritten like this:
145 .Bd -literal -offset indent
150 strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
151 if ((fd = mkstemp(sfn)) == -1 ||
152 (sfp = fdopen(fd, "w+")) == NULL) {
157 fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
163 Often one will find code which uses
165 very early on, perhaps to globally initialize the template nicely, but the
170 on that filename will occur much later.
171 (In almost all cases, the use of
173 will mean that the flags
179 and thus a symbolic link race becomes possible, hence making
183 Furthermore, one must be careful about code which opens, closes, and then
184 re-opens the file in question.
185 Finally, one must ensure that upon error the temporary file is
188 There are also cases where modifying the code to use
196 is better, as long as the code retries a new template if
211 to one of the following values:
214 The pathname portion of the template is not an existing directory.
225 to any value specified by the
234 to any value specified by the
243 to any value specified by the
270 there is an obvious race between file name selection and file
271 creation and deletion: the program is typically written to call
276 Subsequently, the program calls
280 and erroneously opens a file (or symbolic link, fifo or other
281 device) that the attacker has created in the expected file location.
284 is recommended, since it atomically creates the file.
285 An attacker can guess the filenames produced by
287 Whenever it is possible,
291 should be used instead.
295 will output a warning message whenever it links code that uses
300 function is nonstandard and should not be used if portability is required.
301 .Sh SECURITY CONSIDERATIONS
304 should generally be avoided, as a hostile process can exploit a race
305 condition in the time between the generation of a temporary filename by
307 and the invoker's use of the temporary name.
308 A link-time warning will be issued advising the use of
314 .\" .St -p1003.1-2003