summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/mktemp.3
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libc/stdlib/mktemp.3
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libc/stdlib/mktemp.3')
-rw-r--r--src/lib/libc/stdlib/mktemp.3431
1 files changed, 0 insertions, 431 deletions
diff --git a/src/lib/libc/stdlib/mktemp.3 b/src/lib/libc/stdlib/mktemp.3
deleted file mode 100644
index 83b7c9eb30..0000000000
--- a/src/lib/libc/stdlib/mktemp.3
+++ /dev/null
@@ -1,431 +0,0 @@
1.\" $OpenBSD: mktemp.3,v 1.2 2024/03/01 21:30:40 millert Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
17.\"
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
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: March 1 2024 $
31.Dt MKTEMP 3
32.Os
33.Sh NAME
34.Nm mktemp ,
35.Nm mkstemp ,
36.Nm mkostemp ,
37.Nm mkstemps ,
38.Nm mkostemps ,
39.Nm mkdtemp ,
40.Nm mkdtemps
41.Nd make temporary file name (unique)
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft char *
45.Fn mktemp "char *template"
46.Ft int
47.Fn mkstemp "char *template"
48.Ft int
49.Fn mkstemps "char *template" "int suffixlen"
50.Ft char *
51.Fn mkdtemp "char *template"
52.Ft char *
53.Fn mkdtemps "char *template" "int suffixlen"
54.In stdlib.h
55.In fcntl.h
56.Ft int
57.Fn mkostemp "char *template" "int flags"
58.Ft int
59.Fn mkostemps "char *template" "int suffixlen" "int flags"
60.Sh DESCRIPTION
61The
62.Fn mktemp
63family of functions take the given file name template and overwrite
64a portion of it to create a new file name.
65This file name is unique and suitable for use by the application.
66The template may be any file name with at least six trailing
67.Em X Ns s ,
68for example
69.Pa /tmp/temp.XXXXXXXX .
70The trailing
71.Em X Ns s
72are replaced with a unique digit and letter combination.
73The number of unique file names that can be returned
74depends on the number of
75.Em X Ns s
76provided;
77.Fn mktemp
78will try at least 2 ** 31 combinations before giving up.
79At least six
80.Em X Ns s
81must be used, though 10 is much better.
82.Pp
83The
84.Fn mktemp
85function generates a temporary file name based on a template as
86described above.
87Because
88.Fn mktemp
89does not actually create the temporary file, there is a window of
90opportunity during which another process can open the file instead.
91Because of this race condition,
92.Fn mktemp
93should not be used where
94.Fn mkstemp
95can be used instead.
96.Fn mktemp
97was marked as a legacy interface in
98.St -p1003.1-2001 .
99.Pp
100The
101.Fn mkstemp
102function makes the same replacement to the template and creates the template
103file, mode 0600, returning a file descriptor opened for reading and writing.
104This avoids the race between testing for a file's existence and opening it
105for use.
106.Pp
107The
108.Fn mkostemp
109function acts the same as
110.Fn mkstemp ,
111except that the
112.Fa flags
113argument may contain zero or more of the following flags for the underlying
114.Xr open 2
115system call:
116.Pp
117.Bl -tag -width "O_CLOEXECXX" -offset indent -compact
118.It Dv O_APPEND
119Append on each write.
120.It Dv O_CLOEXEC
121Set the close-on-exec flag on the new file descriptor.
122.It Dv O_SYNC
123Perform synchronous I/O operations.
124.El
125.Pp
126The
127.Fn mkstemps
128and
129.Fn mkostemps
130functions act the same as
131.Fn mkstemp
132and
133.Fn mkostemp ,
134except they permit a suffix to exist in the template.
135The template should be of the form
136.Pa /tmp/tmpXXXXXXXXXXsuffix .
137.Fn mkstemps
138and
139.Fn mkostemps
140are told the length of the suffix string, i.e.,
141.Li strlen("suffix") .
142.Pp
143The
144.Fn mkdtemp
145function makes the same replacement to the template as in
146.Fn mktemp
147and creates the template directory, mode 0700.
148The
149.Fn mkdtemps
150function acts the same as
151.Fn mkdtemp ,
152except that it permits a suffix to exist in the template,
153similar to
154.Fn mkstemps .
155.Sh RETURN VALUES
156The
157.Fn mktemp ,
158.Fn mkdtemp ,
159and
160.Fn mkdtemps
161functions return a pointer to the template on success and
162.Dv NULL
163on failure.
164The
165.Fn mkstemp ,
166.Fn mkostemp ,
167.Fn mkstemps ,
168and
169.Fn mkostemps
170functions return \-1 if no suitable file could be created.
171If any call fails, an error code is placed in the global variable
172.Va errno .
173.Sh EXAMPLES
174Quite often a programmer will want to replace a use of
175.Fn mktemp
176with
177.Fn mkstemp ,
178usually to avoid the problems described above.
179Doing this correctly requires a good understanding of the code in question.
180.Pp
181For instance, code of this form:
182.Bd -literal -offset indent
183char sfn[19];
184FILE *sfp;
185
186strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn));
187if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
188 warn("%s", sfn);
189 return (NULL);
190}
191return (sfp);
192.Ed
193.Pp
194should be rewritten like this:
195.Bd -literal -offset indent
196char sfn[19];
197FILE *sfp;
198int fd;
199
200strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn));
201if ((fd = mkstemp(sfn)) == -1 ||
202 (sfp = fdopen(fd, "w+")) == NULL) {
203 if (fd != -1) {
204 unlink(sfn);
205 close(fd);
206 }
207 warn("%s", sfn);
208 return (NULL);
209}
210return (sfp);
211.Ed
212.Pp
213Often one will find code which uses
214.Fn mktemp
215very early on, perhaps to globally initialize the template nicely, but the
216code which calls
217.Xr open 2
218or
219.Xr fopen 3
220on that file name will occur much later.
221(In almost all cases, the use of
222.Xr fopen 3
223will mean that the flags
224.Dv O_CREAT
225|
226.Dv O_EXCL
227are not given to
228.Xr open 2 ,
229and thus a symbolic link race becomes possible, hence making
230necessary the use of
231.Xr fdopen 3
232as seen above.)
233Furthermore, one must be careful about code which opens, closes, and then
234re-opens the file in question.
235Finally, one must ensure that upon error the temporary file is
236removed correctly.
237.Pp
238There are also cases where modifying the code to use
239.Fn mktemp ,
240in concert with
241.Xr open 2
242using the flags
243.Dv O_CREAT
244|
245.Dv O_EXCL ,
246is better, as long as the code retries a new template if
247.Xr open 2
248fails with an
249.Va errno
250of
251.Er EEXIST .
252.Sh ERRORS
253The
254.Fn mktemp ,
255.Fn mkstemp ,
256.Fn mkostemp ,
257and
258.Fn mkdtemp
259functions may set
260.Va errno
261to one of the following values:
262.Bl -tag -width Er
263.It Bq Er EINVAL
264The
265.Ar template
266argument has fewer than six trailing
267.Em X Ns s .
268.It Bq Er EEXIST
269All file names tried are already in use.
270Consider appending more
271.Em X Ns s to the
272.Ar template .
273.El
274.Pp
275The
276.Fn mkstemps
277and
278.Fn mkostemps
279functions may set
280.Va errno
281to
282.Bl -tag -width Er
283.It Bq Er EINVAL
284The
285.Ar template
286argument length is less than
287.Ar suffixlen
288or it has fewer than six
289.Em X Ns s
290before the suffix.
291.It Bq Er EEXIST
292All file names tried are already in use.
293Consider appending more
294.Em X Ns s to the
295.Ar template .
296.El
297.Pp
298In addition, the
299.Fn mkostemp
300and
301.Fn mkostemps
302functions may also set
303.Va errno
304to
305.Bl -tag -width Er
306.It Bq Er EINVAL
307.Fa flags
308is invalid.
309.El
310.Pp
311The
312.Fn mktemp
313function may also set
314.Va errno
315to any value specified by the
316.Xr lstat 2
317function.
318.Pp
319The
320.Fn mkstemp ,
321.Fn mkostemp ,
322.Fn mkstemps ,
323and
324.Fn mkostemps
325functions may also set
326.Va errno
327to any value specified by the
328.Xr open 2
329function.
330.Pp
331The
332.Fn mkdtemp
333function may also set
334.Va errno
335to any value specified by the
336.Xr mkdir 2
337function.
338.Sh SEE ALSO
339.Xr chmod 2 ,
340.Xr lstat 2 ,
341.Xr mkdir 2 ,
342.Xr open 2 ,
343.Xr tempnam 3 ,
344.Xr tmpfile 3 ,
345.Xr tmpnam 3
346.Sh STANDARDS
347The
348.Fn mkdtemp
349and
350.Fn mkstemp
351functions conform to the
352.St -p1003.1-2008
353specification.
354The ability to specify more than six
355.Em X Ns s
356is an extension to that standard.
357The
358.Fn mkostemp
359function is expected to conform to a future revision of that standard.
360.Pp
361The
362.Fn mktemp
363function conforms to
364.St -p1003.1-2001 ;
365as of
366.St -p1003.1-2008
367it is no longer a part of the standard.
368.Pp
369The
370.Fn mkstemps ,
371.Fn mkostemps ,
372and
373.Fn mkdtemps
374functions are non-standard and should not be used if portability is required.
375.Sh HISTORY
376A
377.Fn mktemp
378function appeared in
379.At v7 .
380The
381.Fn mkdtemp
382function appeared in
383.Ox 2.2 .
384The
385.Fn mkstemp
386function appeared in
387.Bx 4.3 .
388The
389.Fn mkstemps
390function appeared in
391.Ox 2.3 .
392The
393.Fn mkostemp
394and
395.Fn mkostemps
396functions appeared in
397.Ox 5.7 .
398The
399.Fn mkdtemps
400function appeared in
401.Ox 7.5 .
402.Sh BUGS
403For
404.Fn mktemp
405there is an obvious race between file name selection and file
406creation and deletion: the program is typically written to call
407.Xr tmpnam 3 ,
408.Xr tempnam 3 ,
409or
410.Fn mktemp .
411Subsequently, the program calls
412.Xr open 2
413or
414.Xr fopen 3
415and erroneously opens a file (or symbolic link, FIFO or other
416device) that the attacker has created in the expected file location.
417Hence
418.Fn mkstemp
419is recommended, since it atomically creates the file.
420An attacker can guess the file names produced by
421.Fn mktemp .
422Whenever it is possible,
423.Fn mkstemp
424or
425.Fn mkdtemp
426should be used instead.
427.Pp
428For this reason,
429.Xr ld 1
430will output a warning message whenever it links code that uses
431.Fn mktemp .