Skip to content

Commit 7684796

Browse files
committed
C++: Fix handling of the 'stat' pointer argument.
1 parent 0c02989 commit 7684796

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

cpp/ql/src/Security/CWE/CWE-367/TOCTOUFilesystemRace.ql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ where
111111
// access to a member variable on the stat buf
112112
// (morally, this should be a use-use pair, but it seems unlikely
113113
// that this variable will get reused in practice)
114-
exists(Variable buf | exists(stat(checkPath, buf.getAnAccess())) |
115-
check.(VariableAccess).getQualifier() = buf.getAnAccess()
114+
exists(Expr call, Expr e, Variable v |
115+
call = stat(checkPath, e) and
116+
e.getAChild*().(VariableAccess).getTarget() = v and
117+
check.(VariableAccess).getTarget() = v and
118+
not e.getAChild*() = check // the call that writes to the pointer is not where the pointer is checked.
116119
)
117120
) and
118121
// `checkPath` and `usePath` refer to the same SSA variable

cpp/ql/test/query-tests/Security/CWE/CWE-367/semmle/TOCTOUFilesystemRace.expected

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
| test2.cpp:39:7:39:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:39:13:39:16 | path | filename | test2.cpp:34:6:34:10 | call to fopen | checked |
22
| test2.cpp:52:7:52:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:52:13:52:16 | path | filename | test2.cpp:52:7:52:11 | call to fopen | checked |
33
| test2.cpp:69:7:69:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:69:13:69:16 | path | filename | test2.cpp:67:6:67:9 | call to stat | checked |
4-
| test2.cpp:98:7:98:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:98:13:98:16 | path | filename | test2.cpp:96:15:96:17 | foo | checked |
4+
| test2.cpp:83:7:83:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:83:13:83:16 | path | filename | test2.cpp:81:6:81:8 | buf | checked |
5+
| test2.cpp:98:7:98:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:98:13:98:16 | path | filename | test2.cpp:96:6:96:12 | buf_ptr | checked |
6+
| test2.cpp:115:7:115:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:115:13:115:16 | path | filename | test2.cpp:113:22:113:24 | buf | checked |
7+
| test2.cpp:130:7:130:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:130:13:130:16 | path | filename | test2.cpp:128:21:128:27 | buf_ptr | checked |
58
| test2.cpp:157:7:157:10 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:157:12:157:15 | path | filename | test2.cpp:155:6:155:9 | call to stat | checked |
69
| test2.cpp:170:7:170:10 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:170:12:170:15 | path | filename | test2.cpp:168:6:168:10 | call to lstat | checked |
710
| test2.cpp:245:3:245:7 | call to chmod | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:245:9:245:12 | path | filename | test2.cpp:238:6:238:10 | call to fopen | checked |

cpp/ql/test/query-tests/Security/CWE/CWE-367/semmle/test2.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void test2_2(const char *path)
8080
stat(path, &buf);
8181
if (buf.foo > 0)
8282
{
83-
f = fopen(path, "r"); // BAD [NOT DETECTED]
83+
f = fopen(path, "r"); // BAD
8484
}
8585

8686
// ...
@@ -112,7 +112,7 @@ void test2_4(const char *path)
112112
stat(path, &buf);
113113
if (stat_condition(&buf))
114114
{
115-
f = fopen(path, "r"); // BAD [NOT DETECTED]
115+
f = fopen(path, "r"); // BAD
116116
}
117117

118118
// ...
@@ -127,7 +127,7 @@ void test2_5(const char *path)
127127
stat(path, buf_ptr);
128128
if (stat_condition(buf_ptr))
129129
{
130-
f = fopen(path, "r"); // BAD [NOT DETECTED]
130+
f = fopen(path, "r"); // BAD
131131
}
132132

133133
// ...

0 commit comments

Comments
 (0)