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