Skip to content

Potential time of check time of use vulnerability (chmod)

Description

Usage of the `chmod` function call hints at a potential Time Of Check Time Of Use (TOCTOU) vulnerability. An attacker may be able to modify the file being specified by the `chmod` function prior to the `chmod` function being called. Since `chmod` will resolve symbolic links, an attacker may be able to exploit this fact to have files outside of their control modified. It is recommended that the `fchmod` function be used instead since this function takes a file descriptor instead of a file.

Examples

Insecure Code

c
chmod("example.txt", 0644);

Secure Code

c
int fd = open("example.txt", O_RDWR); fchmod(fd, 0644);

Remediation

Replace `chmod` with `fchmod` and ensure the opened file descriptor is pointing to the correct file or directory prior to executing `fchmod` or any other file-based operations.

Rule Details

FieldValue
IDCODE-0607
CategoryInsecureConfig
SeverityMEDIUM
CWECWE-362
ConfidenceHIGH
ImpactMEDIUM
LikelihoodMEDIUM
ExploitabilityMODERATE
TagsTOCTOU, file permissions
OWASPA5:2017-Broken Access Control, A01:2021-Broken Access Control