Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1dacd2e

Browse files
authored
Add files via upload
1 parent 5709365 commit 1dacd2e

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
...
2+
FILE *fp = fopen(filename,"w"); // BAD
3+
...
4+
umask(S_IXUSR|S_IRWXG|S_IRWXO);
5+
FILE *fp;
6+
fp = fopen(filename,"w"); // GOOD
7+
chmod(filename,S_IRUSR|S_IWUSR);
8+
fprintf(fp,"%s\n","data to file");
9+
fclose(fp);
10+
...
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Finding for places to work with files without restrictions on access rights.</p>
7+
8+
9+
</overview>
10+
11+
<example>
12+
<p>The following example demonstrates erroneous and fixed methods for working with files.</p>
13+
<sample src="ExposureSensitiveInformationUnauthorizedActor.cpp" />
14+
15+
</example>
16+
<references>
17+
18+
<li>
19+
CERT C Coding Standard:
20+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/FIO06-C.+Create+files+with+appropriate+access+permissions">FIO06-C. Create files with appropriate access permissions</a>.
21+
</li>
22+
23+
</references>
24+
</qhelp>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @name Find work with the file without setting permissions.
3+
* @description Lack of restriction on file access rights can be unsafe.
4+
* @kind problem
5+
* @id cpp/work-with-file-without-permissions-rights
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags correctness
9+
* maintainability
10+
* security
11+
* external/cwe/cwe-200
12+
* external/cwe/cwe-264
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
17+
18+
/** Holds for a function `f` that has an argument at index `apos` used to read the file. */
19+
predicate numberArgumentRead(Function f, int apos) {
20+
f.hasGlobalOrStdName("fgets") and apos = 2
21+
or
22+
f.hasGlobalOrStdName("fread") and apos = 3
23+
or
24+
f.hasGlobalOrStdName("read") and apos = 0
25+
or
26+
f.hasGlobalOrStdName("fscanf") and apos = 0
27+
}
28+
29+
/** Holds for a function `f` that has an argument at index `apos` used to write to file */
30+
predicate numberArgumentWrite(Function f, int apos) {
31+
f.hasGlobalOrStdName("fprintf") and apos = 0
32+
or
33+
f.hasGlobalOrStdName("fputs") and apos = 1
34+
or
35+
f.hasGlobalOrStdName("write") and apos = 0
36+
or
37+
f.hasGlobalOrStdName("fwrite") and apos = 3
38+
or
39+
f.hasGlobalOrStdName("fflush") and apos = 0
40+
}
41+
42+
from FunctionCall fc
43+
where
44+
(
45+
fc.getTarget().hasGlobalOrStdName("fopen") or
46+
fc.getTarget().hasGlobalOrStdName("open")
47+
) and
48+
fc.getNumberOfArguments() = 2 and
49+
exists(FunctionCall fctmp, int i |
50+
numberArgumentWrite(fctmp.getTarget(), i) and
51+
globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i))
52+
) and
53+
not exists(FunctionCall fctmp, int i |
54+
numberArgumentRead(fctmp.getTarget(), i) and
55+
globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i))
56+
) and
57+
not exists(FunctionCall fctmp |
58+
fctmp.getTarget().hasGlobalOrStdName("umask") or
59+
fctmp.getTarget().hasGlobalOrStdName("fchmod") or
60+
fctmp.getTarget().hasGlobalOrStdName("chmod")
61+
) and
62+
not exists(FunctionCall fctmp |
63+
fctmp.getTarget().hasGlobalOrStdName("fdopen") and
64+
fctmp.getNumberOfArguments() = 3 and
65+
globalValueNumber(fctmp) = globalValueNumber(fc.getArgument(0))
66+
)
67+
select fc, "You may have forgotten to restrict access rights when working with a file."

0 commit comments

Comments
 (0)