jcs's openbsd hax
openbsd
at jcs 431 lines 9.5 kB view raw
1.\" $OpenBSD: mktemp.3,v 1.4 2025/08/04 14:11:37 schwarze 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: August 4 2025 $ 31.Dt MKTEMP 3 32.Os 33.Sh NAME 34.Nm mktemp , 35.Nm mkstemp , 36.Nm mkstemps , 37.Nm mkdtemp , 38.Nm mkdtemps , 39.Nm mkostemp , 40.Nm mkostemps 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_CLOFORK 123Set the close-on-fork flag on the new file descriptor. 124.It Dv O_SYNC 125Perform synchronous I/O operations. 126.El 127.Pp 128The 129.Fn mkstemps 130and 131.Fn mkostemps 132functions act the same as 133.Fn mkstemp 134and 135.Fn mkostemp , 136except they permit a suffix to exist in the template. 137The template should be of the form 138.Pa /tmp/tmpXXXXXXXXXXsuffix . 139.Fn mkstemps 140and 141.Fn mkostemps 142are told the length of the suffix string, i.e., 143.Li strlen("suffix") . 144.Pp 145The 146.Fn mkdtemp 147function makes the same replacement to the template as in 148.Fn mktemp 149and creates the template directory, mode 0700. 150The 151.Fn mkdtemps 152function acts the same as 153.Fn mkdtemp , 154except that it permits a suffix to exist in the template, 155similar to 156.Fn mkstemps . 157.Sh RETURN VALUES 158The 159.Fn mktemp , 160.Fn mkdtemp , 161and 162.Fn mkdtemps 163functions return a pointer to the template on success and 164.Dv NULL 165on failure. 166The 167.Fn mkstemp , 168.Fn mkstemps , 169.Fn mkostemp , 170and 171.Fn mkostemps 172functions return \-1 if no suitable file could be created. 173If any call fails, an error code is placed in the global variable 174.Va errno . 175.Sh EXAMPLES 176Quite often a programmer will want to replace a use of 177.Fn mktemp 178with 179.Fn mkstemp , 180usually to avoid the problems described above. 181Doing this correctly requires a good understanding of the code in question. 182.Pp 183For instance, code of this form: 184.Bd -literal -offset indent 185char sfn[19]; 186FILE *sfp; 187 188strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn)); 189if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) { 190 warn("%s", sfn); 191 return (NULL); 192} 193return (sfp); 194.Ed 195.Pp 196should be rewritten like this: 197.Bd -literal -offset indent 198char sfn[19]; 199FILE *sfp; 200int fd; 201 202strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn)); 203if ((fd = mkstemp(sfn)) == -1 || 204 (sfp = fdopen(fd, "w+")) == NULL) { 205 if (fd != -1) { 206 unlink(sfn); 207 close(fd); 208 } 209 warn("%s", sfn); 210 return (NULL); 211} 212return (sfp); 213.Ed 214.Pp 215Often one will find code which uses 216.Fn mktemp 217very early on, perhaps to globally initialize the template nicely, but the 218code which calls 219.Xr open 2 220or 221.Xr fopen 3 222on that file name will occur much later. 223(In almost all cases, the use of 224.Xr fopen 3 225will mean that the flags 226.Dv O_CREAT 227| 228.Dv O_EXCL 229are not given to 230.Xr open 2 , 231and thus a symbolic link race becomes possible, hence making 232necessary the use of 233.Xr fdopen 3 234as seen above.) 235Furthermore, one must be careful about code which opens, closes, and then 236re-opens the file in question. 237Finally, one must ensure that upon error the temporary file is 238removed correctly. 239.Pp 240There are also cases where modifying the code to use 241.Fn mktemp , 242in concert with 243.Xr open 2 244using the flags 245.Dv O_CREAT 246| 247.Dv O_EXCL , 248is better, as long as the code retries a new template if 249.Xr open 2 250fails with an 251.Va errno 252of 253.Er EEXIST . 254.Sh ERRORS 255The 256.Fn mktemp , 257.Fn mkstemp , 258.Fn mkdtemp , 259and 260.Fn mkostemp 261functions may set 262.Va errno 263to one of the following values: 264.Bl -tag -width Er 265.It Bq Er EINVAL 266The 267.Ar template 268argument has fewer than six trailing 269.Em X Ns s . 270.It Bq Er EEXIST 271All file names tried are already in use. 272Consider appending more 273.Em X Ns s to the 274.Ar template . 275.El 276.Pp 277The 278.Fn mkstemps 279and 280.Fn mkostemps 281functions may set 282.Va errno 283to 284.Bl -tag -width Er 285.It Bq Er EINVAL 286The 287.Ar template 288argument length is less than 289.Ar suffixlen 290or it has fewer than six 291.Em X Ns s 292before the suffix. 293.It Bq Er EEXIST 294All file names tried are already in use. 295Consider appending more 296.Em X Ns s to the 297.Ar template . 298.El 299.Pp 300In addition, the 301.Fn mkostemp 302and 303.Fn mkostemps 304functions may also set 305.Va errno 306to 307.Bl -tag -width Er 308.It Bq Er EINVAL 309.Fa flags 310is invalid. 311.El 312.Pp 313The 314.Fn mktemp 315function may also set 316.Va errno 317to any value specified by the 318.Xr lstat 2 319function. 320.Pp 321The 322.Fn mkstemp , 323.Fn mkstemps , 324.Fn mkostemp , 325and 326.Fn mkostemps 327functions may also set 328.Va errno 329to any value specified by the 330.Xr open 2 331function. 332.Pp 333The 334.Fn mkdtemp 335function may also set 336.Va errno 337to any value specified by the 338.Xr mkdir 2 339function. 340.Sh SEE ALSO 341.Xr chmod 2 , 342.Xr lstat 2 , 343.Xr mkdir 2 , 344.Xr open 2 , 345.Xr tempnam 3 , 346.Xr tmpfile 3 , 347.Xr tmpnam 3 348.Sh STANDARDS 349The 350.Fn mkstemp , 351.Fn mkdtemp , 352and 353.Fn mkostemp 354functions conform to the 355.St -p1003.1-2024 356specification. 357The ability to specify more than six 358.Em X Ns s 359is an extension to 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 mkdtemps , 372and 373.Fn mkostemps 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 mkstemp 382function appeared in 383.Bx 4.3 . 384The 385.Fn mkdtemp 386function appeared in 387.Ox 2.2 . 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 .