jcs's openbsd hax
openbsd

Clarify which part of the path name is considered for substitution. Also use "name" instead of "file name" is some places since the result could be either a file or a directory. Work done with jmc@

millert 80249bc8 43503d76

+24 -24
+24 -24
usr.bin/mktemp/mktemp.1
··· 1 - .\" $OpenBSD: mktemp.1,v 1.33 2024/03/01 23:37:42 millert Exp $ 1 + .\" $OpenBSD: mktemp.1,v 1.34 2024/03/03 15:24:45 millert Exp $ 2 2 .\" 3 3 .\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013, 2024 4 4 .\" Todd C. Miller <millert@openbsd.org> ··· 15 15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 16 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 17 .\" 18 - .Dd $Mdocdate: March 1 2024 $ 18 + .Dd $Mdocdate: March 3 2024 $ 19 19 .Dt MKTEMP 1 20 20 .Os 21 21 .Sh NAME 22 22 .Nm mktemp 23 - .Nd make temporary filename (unique) 23 + .Nd make temporary file or directory (unique) 24 24 .Sh SYNOPSIS 25 25 .Nm mktemp 26 26 .Op Fl dqtu ··· 29 29 .Sh DESCRIPTION 30 30 The 31 31 .Nm mktemp 32 - utility takes the given filename 32 + utility takes the specified file name 33 33 .Ar template 34 - and overwrites a portion of it to create a unique filename. 34 + and overwrites a portion of it to create a unique file name. 35 35 The 36 36 .Ar template 37 - may be any filename with at least six 37 + may be any file name containing at least six 38 38 .Ql X Ns s 39 - in the last component of the filename, for example 39 + in the last component of the path, for example 40 40 .Pa /tmp/tfile.XXXXXXXXXX 41 41 or 42 42 .Pa /tmp/editor.XXXXXXXXXX.txt . ··· 47 47 only the last one will be considered. 48 48 If no 49 49 .Ar template 50 - is specified, a default of 50 + is specified, a default value of 51 51 .Pa tmp.XXXXXXXXXX 52 52 is used and the 53 53 .Fl t ··· 60 60 .Ql X Ns s 61 61 in the 62 62 .Ar template 63 - and the number of collisions with pre-existing files. 64 - The number of unique filenames 63 + and the number of collisions with pre-existing entries. 64 + The number of unique names 65 65 .Nm 66 66 can return depends on the number of 67 67 .Ql X Ns s ··· 74 74 .Pp 75 75 If 76 76 .Nm 77 - can successfully generate a unique filename, the file (or directory) 78 - is created with file permissions such that it is only readable and writable 77 + successfully generates a unique name, the file (or directory) 78 + is created with permissions such that it is only readable and writable 79 79 by its owner (unless the 80 80 .Fl u 81 - flag is given) and the filename is printed to standard output. 81 + flag is given) and the name is printed to the standard output. 82 82 .Pp 83 83 .Nm mktemp 84 84 is provided to allow shell scripts to safely use temporary files. 85 85 Traditionally, many shell scripts take the name of the program with 86 - the PID as a suffix and use that as a temporary filename. 86 + the PID as a suffix and use that as a temporary file name. 87 87 This kind of naming scheme is predictable and the race condition it creates 88 88 is easy for an attacker to win. 89 - A safer, though still inferior approach 89 + A safer, though still inferior approach, 90 90 is to make a temporary directory using the same naming scheme. 91 91 While this does allow one to guarantee that a temporary file will not be 92 92 subverted, it still allows a simple denial of service attack. ··· 101 101 .It Fl p Ar directory 102 102 Use the specified 103 103 .Ar directory 104 - as a prefix when generating the temporary filename. 104 + as a prefix when generating the temporary name. 105 105 The 106 106 .Ar directory 107 107 will be overridden by the user's ··· 163 163 fragment illustrates a simple use of 164 164 .Nm 165 165 where the script should quit if it cannot get a safe 166 - temporary file. 166 + temporary file: 167 167 .Bd -literal -offset indent 168 168 TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1 169 169 echo "program output" >> $TMPFILE ··· 171 171 .Pp 172 172 The same fragment with support for a user's 173 173 .Ev TMPDIR 174 - environment variable can be written as follows. 174 + environment variable can be written as follows: 175 175 .Bd -literal -offset indent 176 176 TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1 177 177 echo "program output" >> $TMPFILE ··· 181 181 the temporary file. 182 182 In this case the 183 183 .Fl t 184 - flag is implied. 184 + flag is implied: 185 185 .Bd -literal -offset indent 186 186 TMPFILE=`mktemp` || exit 1 187 187 echo "program output" >> $TMPFILE ··· 194 194 .Pa /extra/tmp 195 195 unless the user's 196 196 .Ev TMPDIR 197 - environment variable specifies otherwise. 197 + environment variable specifies otherwise: 198 198 .Bd -literal -offset indent 199 199 TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1 200 200 echo "program output" >> $TMPFILE ··· 202 202 .Pp 203 203 In other cases, we want the script to catch the error. 204 204 For instance, if we attempt to create two temporary files and 205 - the second one fails we need to remove the first before exiting. 205 + the second one fails we need to remove the first before exiting: 206 206 .Bd -literal -offset indent 207 - TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1 208 - TMP2=`mktemp -t example.2.XXXXXXXXXX` 207 + TMP1=`mktemp -t example.XXXXXXXXXX.1` || exit 1 208 + TMP2=`mktemp -t example.XXXXXXXXXX.2` 209 209 if [ $? -ne 0 ]; then 210 210 rm -f $TMP1 211 211 exit 1 ··· 215 215 Or perhaps you don't want to exit if 216 216 .Nm 217 217 is unable to create the file. 218 - In this case you can protect that part of the script thusly. 218 + In this case you can protect that part of the script thusly: 219 219 .Bd -literal -offset indent 220 220 TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && { 221 221 # Safe to use $TMPFILE in this block