CWE Rule 377
Description
Rule Description
Creating and using insecure temporary files can leave application and system data vulnerable to attack.
Polyspace Implementation
The rule checker checks for Use of non-secure temporary file.
Examples
This issue occurs when you use temporary file routines that are not secure.
If an attacker guesses the file name generated by a standard temporary file routine, the attacker can:
Cause a race condition when you generate the file name.
Precreate a file of the same name, filled with malicious content. If your program reads the file, the attacker’s file can inject the malicious code.
Create a symbolic link to a file storing sensitive data. When your program writes to the temporary file, the sensitive data is deleted.
To create temporary files, use a more secure
standard temporary file routine, such as mkstemp
from
POSIX.1-2001.
Also, when creating temporary files with routines that allow
flags, such as mkostemp
, use the exclusion flag O_EXCL
to
avoid race conditions.
#define _BSD_SOURCE
#define _XOPEN_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int test_temp()
{
char tpl[] = "abcXXXXXX";
char suff_tpl[] = "abcXXXXXXsuff";
char *filename = NULL;
int fd;
filename = tempnam("/var/tmp", "foo_");
if (filename != NULL)
{
printf("generated tmp name (%s) in (%s:%s:%s)\n",
filename, getenv("TMPDIR") ? getenv("TMPDIR") : "$TMPDIR",
"/var/tmp", P_tmpdir);
fd = open(filename, O_CREAT, S_IRWXU|S_IRUSR); //Noncompliant
if (fd != -1)
{
close(fd);
unlink(filename);
return 1;
}
}
return 0;
}
In this example, Bug Finder flags open
because
it tries to use an unsecure temporary file. The file is opened without
exclusive privileges. An attacker can access the file causing various risks.
O_EXCL
FlagOne possible correction is to add the O_EXCL
flag
when you open the temporary file.
#define _BSD_SOURCE
#define _XOPEN_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int test_temp()
{
char tpl[] = "abcXXXXXX";
char suff_tpl[] = "abcXXXXXXsuff";
char *filename = NULL;
int fd;
filename = tempnam("/var/tmp", "foo_");
if (filename != NULL)
{
printf("generated tmp name (%s) in (%s:%s:%s)\n",
filename, getenv("TMPDIR") ? getenv("TMPDIR") : "$TMPDIR",
"/var/tmp", P_tmpdir);
fd = open(filename, O_CREAT|O_EXCL, S_IRWXU|S_IRUSR);
if (fd != -1)
{
close(fd);
unlink(filename);
return 1;
}
}
return 0;
}
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)