...
Code Block | ||
---|---|---|
| ||
/* ... */ int fd; char temp_name[] = "/tmp/temp-XXXXXX"; if (mktemp(temp_name) == NULL) { /* Handle Error */ } if ((fd = open(temp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0600)) == -1) { /* Handle Error */ } /* ... */ |
The mktemp()
function was has been marked LEGACY in the Open Group Base Specifications Issue 6.
...
It should be possible to open at least TMP_MAX
temporary files during the lifetime of the program (this limit may be shared with tmpfile()
). The value of the macro TMP_MAX
is only required to be 25 by the C99 standard.
...
Wiki Markup |
---|
The Open Group Base Specification Issue 6 \[[Open Group 04|AA. C References#Open Group 04]\] does not specify the permissions the file is created with, so these are [implementation-defined|BB. Definitions#implementation-defined behavior]. However, Issue 7 (akathat is, POSIX.1-2008) will specifyspecifies them as {{S_IRUSR\|S_IWUSR}} (0600). |
Implementation Details
...
Wiki Markup |
---|
In many older [implementations|BB. Definitions#implementation], the name is a function of process ID and time--, so it is possible for the attacker to predict guessthe itname and create a decoy in advance. FreeBSD has recently changed the {{mk*temp()}} family to geteliminate rid of the PID component of the filename and replace the entire field with base-62 encoded randomness. This raises the number of possible temporary files for the typical use of 6 Xs significantly, meaning that even {{mktemp()}} with 6 Xs is reasonably (probabilistically) secure against guessing, except under very frequent usage \[[Kennaway 00|AA. C References#Kennaway 00]\] . |
...
The ISO/IEC TR 24731-1 function tmpfile_s()
creates a temporary binary file that is different from any other existing file and that is automatically removed when it is closed or at program termination. If the program terminates abnormally, whether an open temporary file is removed is implementation-defined.
...
It should be possible to open at least TMP_MAX_S
temporary files during the lifetime of the program (this limit may be shared with tmpnam_s()
). The value of the macro TMP_MAX_S
is only required to be 25 by ISO/IEC TR 24731-1.
The tmpfile_s()
function is available on systems that support ISO/IEC TR 24731-1 (e.g., Microsoft Visual Studio 2005).
Code Block | ||
---|---|---|
| ||
/* ... */ if (tmpfile_s(&file_ptr)) { /* Handle Error */ } /* ... */ |
Wiki Markup |
---|
The {{tmpfile_s()}} function may not be compliant with \[[TMP33-C. Temporary files must be removed before the program exits]\] for implementations where the temporary file is not removed if the program terminates abnormally. |
Risk Assessment
A protected system file to which the symbolic link points can be overwritten when a vulnerable program is executedNon-unique or predictable temporary file names can be exploited to access or modify privileged files.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
TMP30-C | 3 (high) | 2 (probable) | 1 (high) | P6 | L2 |
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Sections 7.19.4.4, "The {{tmpnam}} function," 7.19.4.3, "The {{tmpfile}} function," and 7.19.5.3, "The {{fopen}} function" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "EWR Path Traversal" \[[ISO/IEC TR 24731-1-2007|AA. C References#ISO/IEC TR 24731-1-2007]\] Sections 6.5.1.2, "The {{tmpnam_s}} function," 6.5.1.1, "The {{tmpfile_s}} function," and 6.5.2.1, "The {{fopen_s}} function" \[[Open Group 04|AA. C References#Open Group 04]\] [{{mktemp()}}|http://www.opengroup.org/onlinepubs/000095399/functions/mktemp.html], [{{mkstemp()}}|http://www.opengroup.org/onlinepubs/009695399/functions/mkstemp.html], [{{open()}}|http://www.opengroup.org/onlinepubs/009695399/functions/open.html] \[[Seacord 05a|AA. C References#Seacord 05a]\] Chapter 3, "File I/O" \[[Wheeler 03|AA. C References#Wheeler 03]\] [Chapter 7, "Structure Program Internals and Approach"|http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html#TEMPORARY-FILES] \[[Viega 03|AA. C References#Viega 03]\] Section 2.1, "Creating Files for Temporary Use" \[[Kennaway 00|AA. C References#Kennaway 00]\] \[[HP 03|AA. C References#HP 03]\] |
...