Skip to content

Commit baec186

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

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
...
2+
chroot("/myFold/myTmp"); // BAD
3+
...
4+
chdir("/myFold/myTmp"); // BAD
5+
...
6+
int fd = open("/myFold/myTmp", O_RDONLY | O_DIRECTORY);
7+
fchdir(fd); // BAD
8+
...
9+
if (chdir("/myFold/myTmp") == -1) {
10+
exit(-1);
11+
}
12+
if (chroot("/myFold/myTmp") == -1) { // GOOD
13+
exit(-1);
14+
}
15+
...
16+
if (chdir("/myFold/myTmp") == -1) { // GOOD
17+
exit(-1);
18+
}
19+
...
20+
int fd = open("/myFold/myTmp", O_RDONLY | O_DIRECTORY);
21+
if(fchdir(fd) == -1) { // GOOD
22+
exit(-1);
23+
}
24+
...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Working with changing directories, without checking the return value or pinning the directory, may not be safe. Requires the attention of developers.</p>
7+
8+
</recommendation>
9+
<example>
10+
<p>The following example demonstrates erroneous and corrected work with changing working directories.</p>
11+
<sample src="IncorrectChangingWorkingDirectory.cpp" />
12+
13+
</example>
14+
<references>
15+
16+
<li>
17+
CERT C Coding Standard:
18+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/POS05-C.+Limit+access+to+files+by+creating+a+jail">POS05-C. Limit access to files by creating a jail</a>.
19+
</li>
20+
21+
</references>
22+
</qhelp>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @name Find work with changing working directories, with security errors.
3+
* @description Not validating the return value or pinning the directory can be unsafe.
4+
* @kind problem
5+
* @id cpp/work-with-changing-working-directories
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags correctness
9+
* security
10+
* external/cwe/cwe-243
11+
* external/cwe/cwe-252
12+
*/
13+
14+
import cpp
15+
16+
/** Holds if a `fc` function call is available before or before a `chdir` function call. */
17+
predicate inExistsChdir(FunctionCall fcp) {
18+
exists(FunctionCall fctmp |
19+
(
20+
fctmp.getTarget().hasGlobalOrStdName("chdir") or
21+
fctmp.getTarget().hasGlobalOrStdName("fchdir")
22+
) and
23+
(
24+
fctmp.getASuccessor*() = fcp or
25+
fcp.getASuccessor*() = fctmp
26+
)
27+
)
28+
}
29+
30+
/** Holds if a `fc` function call is available before or before a function call containing a `chdir` call. */
31+
predicate outExistsChdir(FunctionCall fcp) {
32+
exists(FunctionCall fctmp |
33+
exists(FunctionCall fctmp2 |
34+
(
35+
fctmp2.getTarget().hasGlobalOrStdName("chdir") or
36+
fctmp2.getTarget().hasGlobalOrStdName("fchdir")
37+
) and
38+
fctmp2.getEnclosingStmt().getParentStmt*() = fctmp.getTarget().getEntryPoint().getChildStmt*()
39+
) and
40+
(
41+
fctmp.getASuccessor*() = fcp or
42+
fcp.getASuccessor*() = fctmp
43+
)
44+
)
45+
}
46+
47+
from FunctionCall fc, string msg
48+
where
49+
fc.getTarget().hasGlobalOrStdName("chroot") and
50+
not inExistsChdir(fc) and
51+
not outExistsChdir(fc) and
52+
exists(FunctionCall fctmp |
53+
fc.getEnclosingStmt().getParentStmt*() = fctmp.getTarget().getEntryPoint().getChildStmt*() and
54+
not inExistsChdir(fctmp) and
55+
not outExistsChdir(fctmp)
56+
) and
57+
msg = "Creation of chroot Jail Without Changing Working Directory out"
58+
or
59+
(
60+
fc.getTarget().hasGlobalOrStdName("chdir") or
61+
fc.getTarget().hasGlobalOrStdName("fchdir")
62+
) and
63+
not exists(ConditionalStmt cotmp | cotmp.getControllingExpr().getAChild*() = fc) and
64+
not exists(Loop lptmp | lptmp.getCondition().getAChild*() = fc) and
65+
not exists(ReturnStmt rttmp | rttmp.getExpr().getAChild*() = fc) and
66+
not exists(Assignment astmp | astmp.getAChild*() = fc) and
67+
not exists(Initializer ittmp | ittmp.getExpr().getAChild*() = fc) and
68+
not fc.isInMacroExpansion() and
69+
msg = fc.getTarget().getName() + " unchecked return value."
70+
select fc, msg

0 commit comments

Comments
 (0)