Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
If the programmer's intent is to not remove an existing destination file, the POSIX {{access()}} function can be used to check for the existence of a file \[[Open Group 042004|AA. Bibliography#Open Group 04]\]. This compliant solution renames the source file only if the destination file does not exist.

...

This code contains an unavoidable race condition between the call to access() and the call to rename() and can consequently be safely executed only when the destination file is located within a secure directory. (see See recommendation FIO15-C. Ensure that file operations are performed in a secure directory.).

On file systems where the program does not have sufficient permissions in the directory to view the file, access() may return -1 even when the file exists. In such cases, rename() will also fail because the program lacks adequate permissions to perform the operation.

In situations where the source file is supposed not to be a directory or symbolic link, an alternative solution is to use link() to link the source file to the destination file and then use unlink() (or remove()) to delete the source file. Since link() fails if the destination file exists, this avoids the need for calling access(). However, this solution has two race conditions related to the source file. Firstly, before calling link() the program must use lstat() to check that the source file is not a directory or symbolic link. Secondly, there is the time window between the link() and the unlink() during which the source file could change. Consequently, this alternative solution can be safely executed only when the source file is located within a secure directory.

...

Wiki Markup
On Windows, the [{{rename()}}|http://msdn.microsoft.com/en-us/library/zw5t957f(VS.80).aspx] function fails if \[[MSDN|AA. Bibliography#MSDN]\]:

File or directory specified by newname already exists or could not be created (invalid path).

Consequently, it is unnecessary to explicitly check for the existence of the destination file before calling rename().

...

On Windows systems, it is necessary to explicitly remove the destination file before calling rename(), if you want the file to be overwritten and the rename() operation to succeed.

...

This code contains unavoidable race conditions between the calls to _access_s(), remove() and rename() and can consequently be safely executed only within a secure directory. (see See recommendation FIO15-C. Ensure that file operations are performed in a secure directory.).

Compliant Solution (POSIX)

...

This code contains an unavoidable race condition between the call to remove() and the call to rename() and, consequently, can be safely executed only within a secure directory. (see See recommendation FIO15-C. Ensure that file operations are performed in a secure directory.).

The return value of remove() is deliberately not checked , because it is expected to fail in the case where the file does not exist. If the file exists but cannot be removed, the rename() call will also fail, and the error will be detected at that point. This is a valid exception (EXP12-EX1) to recommendation EXP12-C. Do not ignore values returned by functions.

...

This code contains an unavoidable race condition between the call to file_exists() and the call to rename() and can consequently be safely executed only within a secure directory. (see See recommendation FIO15-C. Ensure that file operations are performed in a secure directory.).

The file_exists() function is provided by the application and is not shown here because it must be implemented differently on different platforms. (On POSIX systems, it would use access(), on Windows _access_s(), and on other platforms whatever function is available to test file existence.)

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

Related Guidelines

CERT This rule appears in the C++ Secure Coding Standard as : FIO10-CPP. Take care when using the rename() function.

Bibliography

unmigrated-wiki-markup

\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 7.9.4.2, "The {{rename}} function"

Bibliography

Wiki Markup

\[[MSDN|AA. Bibliography#MSDN]\] [{{rename()}}|http://msdn.microsoft.com/en-us/library/zw5t957f(VS.80).aspx]
\[[Open Group 042004|AA. Bibliography#Open Group 04]\] [{{access()}}|http://www.opengroup.org/onlinepubs/009695399/functions/access.html]

...