Versions Compared

Key

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

...

There are many different IPC mechanisms, ; some of which require the use of temporary files, and others of which do not. An example of an IPC mechanism that uses temporary files is the POSIX mmap() function. Berkeley Sockets, POSIX Local IPC Sockets, and System V Shared Memory do not require temporary files. Because the multiuser nature of shared directories poses an inherent security risk, the use of shared temporary files for IPC is discouraged.

...

  1. created unpredictable file names,
  2. created with unique names,
  3. opened only if the file doesn't already exist (atomic open),
  4. opened with exclusive access,
  5. opened with appropriate permissions, and
  6. removed before the program exits.

The following table lists common temporary file functions and their respective conformance to these criteria:

...

 

tmpnam
(C)

tmpnam_s
(Annex K)

tmpfile
(C)

tmpfile_s
(Annex K)

mktemp
(POSIX)

mkstemp
(POSIX)

Unpredictable Name

not Not portably

yesYes

not Not portably

yesYes

not Not portablynot

Not portably

Unique Name

yesYesyes

Yes

yesYesyes

Yes

yesYesyes

Yes

Atomic open

noNono

No

yesYesyes

Yes

noNo

yesYes

Exclusive Access

possiblePossiblepossible

Possible

noNo

if If supported by OS

possiblePossible

if If supported by OS

Appropriate Permissions

possiblePossiblepossible

Possible

noNo

if If supported by OS

possiblePossible

not Not portably

File Removed

noNono

No

yesYes*yes

Yes*

noNono

No

* If the program terminates abnormally, this behavior is implementation-defined.

...

The following noncompliant code example attempts to remedy the problem by generating the file name at runtime using tmpnam(). The C tmpnam() 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 9899:2011]. Files created using strings generated by the tmpnam() function are temporary in that their names should not collide with those generated by conventional naming rules for the implementation. The function is potentially capable of generating TMP_MAX different strings, but any or all of them may already be in use by existing files.

...

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-1:2007]. It is almost identical to the tmpnam() function , except for an added maxsize argument for the supplied buffer.

...

Nonnormative text in TR 24731-1 [ISO/IEC TR 24731-1:2007] also recommends the following:

...

It should be possible to open at least TMP_MAX temporary files during the lifetime of the program. (This limit may be shared with tmpnam().) Section 7.21.4.4 of the C standard Standard [ISO/IEC 9899:2011] allows for the value of the macro TMP_MAX to be as small as 25.

...

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 only 25 [ISO/IEC TR 24731-1:2007].

...

This compliant solution invokes the user-defined function secure_dir()} (such as the one defined in FIO15-C. Ensure that file operations are performed in a secure directory) to ensure the temporary file resides in a secure directory.

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Can detect violations of this recommendation. Specifically, Rose reports use of tmpnam(), tmpnam_s(), tmpfile(), and mktemp().

Coverity6.5SECURE_TEMPFully Implemented.

LDRA tool suite

Include Page
LDRA_V
LDRA_V

489 S

Partially implemented.

PRQA QA-C
Include Page
PRQA_V
PRQA_V
warncall tmpnam, tmpfile, mktemp, tmpnam_sPartially implemented

...

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

Related Guidelines

...

...

...

...

ISO/IEC PDTR 24772 "EWR Path traversal"

...

TR 24731-1:2007Section 6.5.1.1, "The tmpfile_s Function,"
Section 6.5.1.2, "The tmpnam_s

...

Function"
Section 6.5.2.1, "The fopen_s

...

Function"
ISO/IEC TR 24772:2013Path Traversal [EWR]
MITRE CWE

...

...

...

Creation of temporary file in directory with insecure permissions

...

Bibliography

 

...

...


open()
[Seacord 2005a]Chapter 3, "Pointer Subterfuge

...

"

...


Chapter 7, "File I/O"
[Viega 2003]Section 2.1, "Creating

...

Files for

...

...