...
This vulnerability can be prevented by including the flags O_CREAT
and O_EXCL
when calling open()
.
Code Block | ||
---|---|---|
| ||
int fd = open("/tmp/some_file", O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0600); |
...
Wiki Markup |
---|
The TR 24731-1 {{tmpnam_s()}} function generates a string that is a valid file name and that is not the same as the name of an existing file \[[ISO/IEC TR 24731-2006|AA. C References#SO/IEC TR 24731-2006]\]. The function is potentially capable of generating {{TMP_MAX_S}} different strings, but any or all of them may already be in use by existing files and thus not be suitable return values. The lengths of these strings must be less than the value of the {{L_tmpnam_s}} macro. |
Code Block | ||
---|---|---|
| ||
... FILE *file_ptr; result = tmpfile_s(&file_ptr); if (resultchar filename[L_tmpnam_s]; if (tmpnam_s(filename, L_tmpnam_s) != 0) { /* Handle Error */ } t_file = if (!fopen_s(temp_&file_nameptr, filename, "wb+"); if (!t_file) { /* Handle Error */ } ... |
...
Wiki Markup |
---|
This solution is also non-compliant because it violates \[[FIO32-C|FIO32-C. Temporary file names must be unique when the file is created]\] and \[[FI042-C|FI042-C. Temporary files must be removed before the program exits]\]. |
Non-Compliant Code Example: mktemp()
The POSIX function {{mktemp()
}} takes a given file name template and overwrites a portion of it to create a file name.
The template may be any file name with some number of 'X's appended to it, for example /tmp/temp.XXXXXX
. The trailing 'X's are replaced with the current process number and/or a unique letter combination. The number of unique file names mktemp()
can return depends on the number of 'X's provided.
...