Skip to content

Commit 6173b11

Browse files
authored
Add files via upload
1 parent 5709365 commit 6173b11

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
...
2+
umask(0); // BAD
3+
...
4+
cmusk = umask(S_IRWXG | S_IRWXO); // GOOD
5+
...
6+
fchmod(fileno(fp), 0555 - cmusk); // BAD
7+
...
8+
fchmod(fileno(fp), 0555 & ~curumsk); // GOOD
9+
...
10+
umask(0666);
11+
chmod(0666); // BAD
12+
...
13+
umask(0022);
14+
chmod(0666); // GOOD
15+
...
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Finding for function calls that set file permissions that may have errors in use. Incorrect arithmetic for calculating the resolution mask, using the same mask in opposite functions, using a mask that is too wide.</p>
7+
8+
</overview>
9+
10+
<example>
11+
<p>The following example demonstrates erroneous and fixed ways to use functions.</p>
12+
<sample src="IncorrectPrivilegeAssignment.cpp" />
13+
14+
</example>
15+
<references>
16+
17+
<li>
18+
CERT C Coding Standard:
19+
<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>.
20+
</li>
21+
22+
</references>
23+
</qhelp>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @name Find the wrong use of the umask function.
3+
* @description Incorrectly evaluated argument to the umask function may have security implications.
4+
* @kind problem
5+
* @id cpp/wrong-use-of-the-umask
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags correctness
9+
* maintainability
10+
* security
11+
* external/cwe/cwe-266
12+
* external/cwe/cwe-264
13+
* external/cwe/cwe-200
14+
* external/cwe/cwe-560
15+
* external/cwe/cwe-687
16+
*/
17+
18+
import cpp
19+
import semmle.code.cpp.exprs.BitwiseOperation
20+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
21+
22+
/** Holds for a function `f` that has an argument at index `apos` used to set file permissions. */
23+
predicate numberArgumentModFunctions(Function f, int apos) {
24+
f.hasGlobalOrStdName("umask") and apos = 0
25+
or
26+
f.hasGlobalOrStdName("fchmod") and apos = 1
27+
or
28+
f.hasGlobalOrStdName("chmod") and apos = 1
29+
}
30+
31+
from FunctionCall fc, string msg
32+
where
33+
fc.getTarget().hasGlobalOrStdName("umask") and
34+
fc.getArgument(0).getValue() = "0" and
35+
not exists(FunctionCall fctmp |
36+
fctmp.getTarget().hasGlobalOrStdName("umask") and
37+
globalValueNumber(fctmp.getArgument(0)) != globalValueNumber(fc.getArgument(0))
38+
) and
39+
exists(FunctionCall fctmp |
40+
(
41+
fctmp.getTarget().hasGlobalOrStdName("fopen") or
42+
fctmp.getTarget().hasGlobalOrStdName("open")
43+
) and
44+
fctmp.getNumberOfArguments() = 2 and
45+
fctmp.getArgument(0).getValue() != "/dev/null"
46+
) and
47+
not exists(FunctionCall fctmp |
48+
fctmp.getTarget().hasGlobalOrStdName("chmod") or
49+
fctmp.getTarget().hasGlobalOrStdName("fchmod")
50+
) and
51+
msg = "Using umask (0) may not be safe."
52+
or
53+
fc.getTarget().hasGlobalOrStdName("umask") and
54+
exists(FunctionCall fctmp |
55+
(
56+
fctmp.getTarget().hasGlobalOrStdName("chmod") or
57+
fctmp.getTarget().hasGlobalOrStdName("fchmod")
58+
) and
59+
(
60+
globalValueNumber(fc.getArgument(0)) = globalValueNumber(fctmp.getArgument(1)) and
61+
fc.getArgument(0).getValue() != "0"
62+
) and
63+
msg = "not use equal argument in umask and " + fctmp.getTarget().getName() + " functions"
64+
)
65+
or
66+
exists(Expr exptmp, int i |
67+
numberArgumentModFunctions(fc.getTarget(), i) and
68+
not exptmp.getAChild*() instanceof FunctionCall and
69+
not exists(SizeofOperator so | exptmp.getAChild*() = so) and
70+
not exists(ArrayExpr aetmp | aetmp.getArrayOffset() = exptmp.getAChild*()) and
71+
exptmp.getAChild*() instanceof BinaryArithmeticOperation and
72+
not exptmp.getAChild*() instanceof BinaryBitwiseOperation and
73+
globalValueNumber(exptmp) = globalValueNumber(fc.getArgument(i)) and
74+
not exptmp.isConstant() and
75+
msg = "Using arithmetic to compute the mask may not be safe."
76+
)
77+
select fc, msg

0 commit comments

Comments
 (0)