Loose File Permissions High

Detects files with world write or read permissions, highlighting security risks and emphasizing the importance of restricting access for improved data protection.

Detector ID
c/loose-file-permissions@v1.0
Category
Common Weakness Enumeration (CWE) external icon
Tags
-

Noncompliant example

1#include <sys/stat.h>
2#include <fcntl.h>
3
4void looseFilePermissionsNonCompliant(){
5
6  int fd;
7
8  // Noncompliant: The process set 777 permissions to this newly created file
9  open("myfile.txt", O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); 
10
11  // Noncompliant: The process try to set 777 permissions to this newly created directory
12  mkdir("myfolder", S_IRWXU | S_IRWXG | S_IRWXO);  
13  
14  // Noncompliant: The process set 777 permissions to this file
15  chmod("myfile.txt", S_IRWXU | S_IRWXG | S_IRWXO);  
16}

Compliant example

1#include <sys/stat.h>
2#include <fcntl.h>
3
4void looseFilePermissionsCompliant(){
5  
6  int fd;
7
8  // Compliant: The O_CREAT flag indicates that the file should be created if it doesn't exist.
9  open("myfile.txt", O_CREAT, S_IRWXU | S_IRWXG); 
10  // Compliant: further created files or directories will not have permissions set for "other group"
11  umask(S_IRWXO); 
12  
13}