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