xref: /openbsd-src/lib/libc/stdio/tmpnam.3 (revision 289c5a0e2ce8e7c07d6347887bfb6bb811938d3a)
1*289c5a0eSderaadt.\"	$OpenBSD: tmpnam.3,v 1.23 2019/08/30 23:33:45 deraadt Exp $
2df930be7Sderaadt.\"
3df930be7Sderaadt.\" Copyright (c) 1988, 1991, 1993
4df930be7Sderaadt.\"	The Regents of the University of California.  All rights reserved.
5df930be7Sderaadt.\"
6df930be7Sderaadt.\" This code is derived from software contributed to Berkeley by
7df930be7Sderaadt.\" the American National Standards Committee X3, on Information
8df930be7Sderaadt.\" Processing Systems.
9df930be7Sderaadt.\"
10df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt.\" modification, are permitted provided that the following conditions
12df930be7Sderaadt.\" are met:
13df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt.\"    documentation and/or other materials provided with the distribution.
186580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
19df930be7Sderaadt.\"    may be used to endorse or promote products derived from this software
20df930be7Sderaadt.\"    without specific prior written permission.
21df930be7Sderaadt.\"
22df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df930be7Sderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df930be7Sderaadt.\" SUCH DAMAGE.
33df930be7Sderaadt.\"
34*289c5a0eSderaadt.Dd $Mdocdate: August 30 2019 $
35*289c5a0eSderaadt.Dt TMPNAM 3
36df930be7Sderaadt.Os
37df930be7Sderaadt.Sh NAME
38df930be7Sderaadt.Nm tempnam ,
39df930be7Sderaadt.Nm tmpfile ,
40df930be7Sderaadt.Nm tmpnam
41df930be7Sderaadt.Nd temporary file routines
42df930be7Sderaadt.Sh SYNOPSIS
4364d4e987Stedu.In stdio.h
44df930be7Sderaadt.Ft FILE *
45df930be7Sderaadt.Fn tmpfile void
46df930be7Sderaadt.Ft char *
47df930be7Sderaadt.Fn tmpnam "char *str"
48df930be7Sderaadt.Ft char *
49df930be7Sderaadt.Fn tempnam "const char *tmpdir" "const char *prefix"
50df930be7Sderaadt.Sh DESCRIPTION
51df930be7SderaadtThe
52df930be7Sderaadt.Fn tmpfile
53aa86bf2dSaaronfunction returns a pointer to a stream associated with a file descriptor
54aa86bf2dSaaronreturned by the routine
55df930be7Sderaadt.Xr mkstemp 3 .
56df930be7SderaadtThe created file is unlinked before
57df930be7Sderaadt.Fn tmpfile
58df930be7Sderaadtreturns, causing the file to be automatically deleted when the last
59df930be7Sderaadtreference to it is closed.
60df930be7SderaadtThe file is opened with the access value
61df930be7Sderaadt.Ql w+ .
62df930be7Sderaadt.Pp
63df930be7SderaadtThe
64df930be7Sderaadt.Fn tmpnam
65aa86bf2dSaaronfunction returns a pointer to a file name, in the
66df930be7Sderaadt.Dv P_tmpdir
67aa86bf2dSaarondirectory, which did not reference an existing file at some
68aa86bf2dSaaronindeterminate point in the past.
69df930be7Sderaadt.Dv P_tmpdir
70df930be7Sderaadtis defined in the include file
71369bef3aSschwarze.In stdio.h .
72df930be7SderaadtIf the argument
7315a52ba3Sderaadt.Fa str
74aa86bf2dSaaronis non-null, the file name is copied to the buffer it references.
75df930be7SderaadtOtherwise, the file name is copied to a static buffer.
76df930be7SderaadtIn either case,
77df930be7Sderaadt.Fn tmpnam
78df930be7Sderaadtreturns a pointer to the file name.
79df930be7Sderaadt.Pp
80df930be7SderaadtThe buffer referenced by
8115a52ba3Sderaadt.Fa str
82df930be7Sderaadtis expected to be at least
83df930be7Sderaadt.Dv L_tmpnam
84df930be7Sderaadtbytes in length.
85df930be7Sderaadt.Dv L_tmpnam
86df930be7Sderaadtis defined in the include file
87369bef3aSschwarze.In stdio.h .
88df930be7Sderaadt.Pp
89df930be7SderaadtThe
90df930be7Sderaadt.Fn tempnam
91aa86bf2dSaaronfunction is similar to
92df930be7Sderaadt.Fn tmpnam ,
93df930be7Sderaadtbut provides the ability to specify the directory which will
94df930be7Sderaadtcontain the temporary file and the file name prefix.
95df930be7Sderaadt.Pp
96df930be7SderaadtThe environment variable
97df930be7Sderaadt.Ev TMPDIR
98df930be7Sderaadt(if set), the argument
99df930be7Sderaadt.Fa tmpdir
100aa86bf2dSaaron(if non-null),
101df930be7Sderaadtthe directory
102df930be7Sderaadt.Dv P_tmpdir ,
103df930be7Sderaadtand the directory
104df930be7Sderaadt.Pa /tmp
105df930be7Sderaadtare tried, in the listed order, as directories in which to store the
106df930be7Sderaadttemporary file.
107df930be7Sderaadt.Pp
108df930be7SderaadtThe argument
109df930be7Sderaadt.Fa prefix ,
110aa86bf2dSaaronif non-null, is used to specify a file name prefix, which will be the
111df930be7Sderaadtfirst part of the created file name.
112960f8fbdSderaadt.Fn tempnam
113df930be7Sderaadtallocates memory in which to store the file name; the returned pointer
114df930be7Sderaadtmay be used as a subsequent argument to
115df930be7Sderaadt.Xr free 3 .
116df930be7Sderaadt.Sh RETURN VALUES
117df930be7SderaadtThe
118df930be7Sderaadt.Fn tmpfile
119aa86bf2dSaaronfunction returns a pointer to an open file stream on success, and a null
120aa86bf2dSaaronpointer on error.
121df930be7Sderaadt.Pp
122df930be7SderaadtThe
123df930be7Sderaadt.Fn tmpnam
124df930be7Sderaadtand
1259a898c0bSheko.Fn tempnam
126aa86bf2dSaaronfunctions return a pointer to a file name on success, and a null pointer
127df930be7Sderaadton error.
12866436dedSjmc.Sh ENVIRONMENT
12966436dedSjmc.Bl -tag -width Ds
13066436dedSjmc.It Ev TMPDIR
13166436dedSjmc.Pf [ Fn tempnam
13266436dedSjmconly]
13366436dedSjmcIf set,
13466436dedSjmcthe directory in which the temporary file is stored.
13566436dedSjmc.Ev TMPDIR
13666436dedSjmcis ignored for processes
13766436dedSjmcfor which
13866436dedSjmc.Xr issetugid 2
13966436dedSjmcis true.
14066436dedSjmc.El
141df930be7Sderaadt.Sh ERRORS
142df930be7SderaadtThe
143df930be7Sderaadt.Fn tmpfile
144aa86bf2dSaaronfunction may fail and set the global variable
145df930be7Sderaadt.Va errno
146df930be7Sderaadtfor any of the errors specified for the library functions
147df930be7Sderaadt.Xr fdopen 3
148df930be7Sderaadtor
149df930be7Sderaadt.Xr mkstemp 3 .
150df930be7Sderaadt.Pp
151df930be7SderaadtThe
152df930be7Sderaadt.Fn tmpnam
153aa86bf2dSaaronfunction may fail and set
154df930be7Sderaadt.Va errno
155df930be7Sderaadtfor any of the errors specified for the library function
156df930be7Sderaadt.Xr mktemp 3 .
157df930be7Sderaadt.Pp
158df930be7SderaadtThe
159df930be7Sderaadt.Fn tempnam
160aa86bf2dSaaronfunction may fail and set
161df930be7Sderaadt.Va errno
162df930be7Sderaadtfor any of the errors specified for the library functions
163df930be7Sderaadt.Xr malloc 3
164df930be7Sderaadtor
165df930be7Sderaadt.Xr mktemp 3 .
166df930be7Sderaadt.Sh SEE ALSO
16766436dedSjmc.Xr issetugid 2 ,
168df930be7Sderaadt.Xr mkstemp 3 ,
169df930be7Sderaadt.Xr mktemp 3
170df930be7Sderaadt.Sh STANDARDS
171df930be7SderaadtThe
172df930be7Sderaadt.Fn tmpfile
173df930be7Sderaadtand
174df930be7Sderaadt.Fn tmpnam
175aa86bf2dSaaronfunctions conform to
176df930be7Sderaadt.St -ansiC .
177df930be7Sderaadt.Sh BUGS
178fe1a9c7cSderaadt.Fn tmpnam
179fe1a9c7cSderaadtand
180fe1a9c7cSderaadt.Fn tempnam
18153ce3bb9Sschwarzeare provided for System V and ANSI compatibility only.
182fe1a9c7cSderaadtThese interfaces are typically not used in safe ways.
183df930be7SderaadtThe
184df930be7Sderaadt.Xr mkstemp 3
185df930be7Sderaadtinterface is strongly preferred.
186df930be7Sderaadt.Pp
187df930be7SderaadtThere are four important problems with these interfaces (as well as
188df930be7Sderaadtwith the historic
189df930be7Sderaadt.Xr mktemp 3
190df930be7Sderaadtinterface).
191df930be7SderaadtFirst, there is an obvious race between file name selection and file
192fe1a9c7cSderaadtcreation and deletion: the program is typically written to call
193244c8eddSjmc.Fn tmpnam ,
19469093b27Sjmc.Fn tempnam ,
195244c8eddSjmcor
196fe1a9c7cSderaadt.Xr mktemp 3 .
197fe1a9c7cSderaadtSubsequently, the program calls
198fe1a9c7cSderaadt.Xr open 2
199fe1a9c7cSderaadtor
200fe1a9c7cSderaadt.Xr fopen 3
2012ead7391Sjmcand erroneously opens a file (or symbolic link, or FIFO or other
202fe1a9c7cSderaadtdevice) that the attacker has placed in the expected file location.
203fe1a9c7cSderaadtHence
204fe1a9c7cSderaadt.Xr mkstemp 3
205fe1a9c7cSderaadtis recommended, since it atomically creates the file.
206fe1a9c7cSderaadt.Pp
207df930be7SderaadtSecond, most historic implementations provide only a limited number
208df930be7Sderaadtof possible temporary file names (usually 26) before file names will
209df930be7Sderaadtstart being recycled.
210df930be7SderaadtThird, the System V implementations of these functions (and of
211244c8eddSjmc.Xr mktemp 3 )
212df930be7Sderaadtuse the
213df930be7Sderaadt.Xr access 2
214df930be7Sderaadtfunction to determine whether or not the temporary file may be created.
215fe1a9c7cSderaadtThis has obvious ramifications for daemons or setuid/setgid programs,
216fe1a9c7cSderaadtcomplicating the portable use of these interfaces in such programs.
217df930be7SderaadtFinally, there is no specification of the permissions with which the
218df930be7Sderaadttemporary files are created.
219df930be7Sderaadt.Pp
220df930be7SderaadtThis implementation does not have these flaws, but portable software
221df930be7Sderaadtcannot depend on that.
222fe1a9c7cSderaadt.Pp
223fe1a9c7cSderaadtFor these reasons,
224a119fbc5Sjmc.Xr ld 1
225aa86bf2dSaaronwill output a warning message whenever it links code that uses the functions
226fe1a9c7cSderaadt.Fn tmpnam
227fe1a9c7cSderaadtor
228fe1a9c7cSderaadt.Fn tempnam .
229