Skip to content

Commit ae944b2

Browse files
committed
C++: Restrict the 'check' to stat / access only as these are by far the more reliable results.
1 parent ab4b2c2 commit ae944b2

File tree

4 files changed

+8
-19
lines changed

4 files changed

+8
-19
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ where
8787
// a stat
8888
check = stat(checkPath, _)
8989
or
90-
// another filename operation (null pointers can indicate errors)
91-
check = filenameOperation(checkPath)
92-
or
9390
// access to a member variable on the stat buf
9491
// (morally, this should be a use-use pair, but it seems unlikely
9592
// that this variable will get reused in practice)
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
| 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 |
2-
| 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 |
31
| 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 |
42
| 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 |
53
| 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 |
64
| 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 |
75
| 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 |
86
| 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 |
97
| 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 |
10-
| 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 |
11-
| test2.cpp:255:3:255:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:255:10:255:14 | path1 | filename | test2.cpp:253:6:253:11 | call to rename | checked |
12-
| test2.cpp:265:7:265:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:265:13:265:17 | path2 | filename | test2.cpp:263:7:263:12 | call to rename | checked |
138
| test2.cpp:277:7:277:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:277:13:277:16 | path | filename | test2.cpp:275:6:275:11 | call to access | checked |
149
| test2.cpp:303:7:303:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:303:13:303:16 | path | filename | test2.cpp:301:7:301:12 | call to access | checked |
1510
| test2.cpp:317:7:317:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:317:13:317:16 | path | filename | test2.cpp:313:6:313:11 | call to access | checked |
16-
| test.cpp:21:3:21:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:21:10:21:14 | file1 | filename | test.cpp:19:7:19:12 | call to rename | checked |
17-
| test.cpp:35:3:35:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:35:10:35:14 | file1 | filename | test.cpp:32:7:32:12 | call to rename | checked |
18-
| test.cpp:49:3:49:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:49:10:49:14 | file1 | filename | test.cpp:47:7:47:12 | call to rename | checked |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void test1()
1818
create(file1);
1919
if (!rename(file1, file2))
2020
{
21-
remove(file1); // DUBIOUS (bad but perhaps not exploitable) [REPORTED]
21+
remove(file1); // DUBIOUS (bad but perhaps not exploitable)
2222
}
2323
}
2424

@@ -32,7 +32,7 @@ void test2()
3232
if (!rename(file1, file2))
3333
{
3434
file1.set("d.txt");
35-
remove(file1); // GOOD [FALSE POSITIVE]
35+
remove(file1); // GOOD
3636
}
3737
}
3838

@@ -46,6 +46,6 @@ void test3()
4646
create(file1);
4747
if (!rename(file1, file2))
4848
{
49-
remove(file1); // DUBIOUS (bad but perhaps not exploitable) [REPORTED]
49+
remove(file1); // DUBIOUS (bad but perhaps not exploitable)
5050
}
5151
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void test1_1(const char *path)
3636
if (f == NULL)
3737
{
3838
// retry
39-
f = fopen(path, "r"); // GOOD (this is just trying again) [FALSE POSITIVE]
39+
f = fopen(path, "r"); // GOOD (this is just trying again)
4040
}
4141

4242
// ...
@@ -49,7 +49,7 @@ void test1_2(const char *path)
4949
// try until we succeed
5050
while (f == NULL)
5151
{
52-
f = fopen(path, "r"); // GOOD (this is just trying again) [FALSE POSITIVE]
52+
f = fopen(path, "r"); // GOOD (this is just trying again)
5353

5454
// ...
5555
}
@@ -242,7 +242,7 @@ void test4_1(const char *path)
242242

243243
fclose(f);
244244

245-
chmod(path, 0); // DUBIOUS (bad but perhaps not exploitable) [REPORTED]
245+
chmod(path, 0); // DUBIOUS (bad but perhaps not exploitable)
246246
}
247247
}
248248

@@ -252,7 +252,7 @@ void test5_1(const char *path1, const char *path2)
252252
{
253253
if (rename(path1, path2))
254254
{
255-
remove(path1); // DUBIOUS (bad but perhaps not exploitable) [REPORTED]
255+
remove(path1); // DUBIOUS (bad but perhaps not exploitable)
256256
}
257257
}
258258

@@ -262,7 +262,7 @@ void test5_2(const char *path1, const char *path2)
262262

263263
if (!rename(path1, path2))
264264
{
265-
f = fopen(path2, "r"); // BAD
265+
f = fopen(path2, "r"); // BAD [NOT DETECTED]
266266
}
267267
}
268268

0 commit comments

Comments
 (0)