Skip to content

Commit 80af5b7

Browse files
committed
C++: Add a third example for cpp/world-writable-file-creation.
1 parent 4f0d725 commit 80af5b7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

cpp/ql/src/Security/CWE/CWE-732/DoNotCreateWorldWritable.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ void write_default_config_good() {
99
int out = creat(OUTFILE, S_IWUSR | S_IRUSR);
1010
dprintf(out, DEFAULT_CONFIG);
1111
}
12+
13+
void write_default_config_good_2() {
14+
// GOOD - this allows only the current user to modify the file
15+
int out = open(OUTFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
16+
FILE *fd = fdopen(out, "w");
17+
fprintf(fd, DEFAULT_CONFIG);
18+
}

cpp/ql/src/Security/CWE/CWE-732/DoNotCreateWorldWritable.qhelp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ so it is important that they cannot be controlled by an attacker.
2929
</p>
3030

3131
<p>
32-
The first example creates the default configuration file with the usual "default" Unix permissions, <code>0666</code>. This makes the
32+
The first example creates the default configuration file with the usual "default" Unix permissions, <code>0666</code>. This makes the
3333
file world-writable, so that an attacker could write in their own configuration that would be read by the program. The second example uses
3434
more restrictive permissions: a combination of the standard Unix constants <code>S_IWUSR</code> and <code>S_IRUSR</code> which means that
35-
only the current user will have read and write access to the file.
35+
only the current user will have read and write access to the file. The third example shows another way to create a file with more restrictive
36+
permissions if a <code>FILE *</code> stream pointer is required rather than a file descriptor.
3637
</p>
3738

3839
<sample src="DoNotCreateWorldWritable.c" />

0 commit comments

Comments
 (0)