CWE Rule 362
Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
Since R2023a
Description
Rule Description
The program contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
Polyspace Implementation
The rule checker checks for File descriptor exposure to child process.
Examples
File descriptor exposure to child process
This issue occurs when a process is forked and the child process uses file descriptors inherited from the parent process.
When you fork a child process, file descriptors are copied from the parent process, which means that you can have concurrent operations on the same file. Use of the same file descriptor in the parent and child processes can lead to race conditions that may not be caught during standard debugging. If you do not properly manage the file descriptor permissions and privileges, the file content is vulnerable to attacks targeting the child process.
Check that the file has not been modified before forking the process. Close all inherited file descriptors and reopen them with stricter permissions and privileges, such as read-only permission.
# include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # include <fcntl.h> # include <sys/types.h> # include <sys/stat.h> const char *test_file="/home/user/test.txt"; void func(void) { char c; pid_t pid; /* create file descriptor in read and write mode */ int fd = open(test_file, O_RDWR); if (fd == -1) { /* Handle error */ abort(); } /* fork process */ pid = fork(); if (pid == -1) { /* Handle error */ abort(); } else if (pid == 0) { /* Child process accesses file descriptor inherited from parent process */ (void)read(fd, &c, 1); //Noncompliant } else { /* Parent process access same file descriptor as child process */ (void)read(fd, &c, 1); } }
In this example, a file descriptor fd
is created in read and write
mode. The process is then forked. The child process inherits and accesses
fd
with the same permissions as the parent process. A race condition
exists between the parent and child processes. The contents of the file is vulnerable to
attacks through the child process.
After you create the file descriptor, check the file for tampering. Then, close the inherited file descriptor in the child process and reopen it in read-only mode.
# include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # include <fcntl.h> # include <sys/types.h> # include <sys/stat.h> const char *test_file="/home/user/test.txt"; void func(void) { char c; pid_t pid; /* Get the state of file for further file tampering checking */ /* create file descriptor in read and write mode */ int fd = open(test_file, O_RDWR); if (fd == -1) { /* Handle error */ abort(); } /* Be sure the file was not tampered with while opening */ /* fork process */ pid = fork(); if (pid == -1) { /* Handle error */ (void)close(fd); abort(); } else if (pid == 0) { /* Close file descriptor in child process and repoen it in read only mode */ (void)close(fd); fd = open(test_file, O_RDONLY); if (fd == -1) { /* Handle error */ abort(); } (void)read(fd, &c, 1); (void)close(fd); } else { /* Parent acceses original file descriptor */ (void)read(fd, &c, 1); (void)close(fd); } }
Check Information
Category: Others |
Version History
Introduced in R2023a
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)