Creating a file with weak access permissions may allow unintended access to that file. Although access permissions are heavily dependent on the operating file system, many file creation functions provide mechanisms to set (or at least influence) access permissions. When these functions are used to create files, appropriate access permissions should be specified to prevent unintended access.
...
The fopen()
function does not allow the programmer to explicitly specify file access permissions. In the example below, if the call to fopen()
creates a new file, the access permissions for that file will be are implementation-defined.
Code Block | ||
---|---|---|
| ||
/* ... */ FILE * fptr = fopen(file_name, "w"); if (!fptr){ /* Handle Error */ } /* ... */ |
...
Code Block |
---|
requested_permissions = 0666; actual_permissions = requested_permissions & ~umask(); |
For OpenBSD and Linux operating systems, any created files will have mode S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
(0666), as modified by the process' umask value (see fopen(3)). OpenBSD has the same rule as Linux (see fopen(3)).
Compliant Solution: fopen_s()
(ISO/IEC TR 24731-1)
...