Skip to content

Commit 2e338cc

Browse files
authored
Merge pull request github#13929 from jketema/buffer
C++: Only consider the maximum buffer size for badly bounded write
2 parents 51a0528 + e04d30a commit 2e338cc

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

cpp/ql/src/Security/CWE/CWE-120/BadlyBoundedWrite.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import semmle.code.cpp.security.BufferWrite
2424
from BufferWrite bw, int destSize
2525
where
2626
bw.hasExplicitLimit() and // has an explicit size limit
27-
destSize = getBufferSize(bw.getDest(), _) and
27+
destSize = max(getBufferSize(bw.getDest(), _)) and
2828
bw.getExplicitLimit() > destSize // but it's larger than the destination
2929
select bw,
3030
"This '" + bw.getBWDesc() + "' operation is limited to " + bw.getExplicitLimit() +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/badly-bounded-write` query could report false positives when a pointer was first initialized with a literal and later assigned a dynamically allocated array. These false positives now no longer occur.

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/VeryLikelyOverrunWrite.expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
| tests2.cpp:17:3:17:8 | call to wcscpy | This 'call to wcscpy' operation requires 12 bytes but the destination is only 8 bytes. |
2-
| tests2.cpp:22:3:22:8 | call to wcscpy | This 'call to wcscpy' operation requires 16 bytes but the destination is only 12 bytes. |
3-
| tests2.cpp:27:3:27:8 | call to wcscpy | This 'call to wcscpy' operation requires 20 bytes but the destination is only 16 bytes. |
4-
| tests2.cpp:31:3:31:8 | call to wcscpy | This 'call to wcscpy' operation requires 24 bytes but the destination is only 20 bytes. |
5-
| tests2.cpp:36:3:36:8 | call to wcscpy | This 'call to wcscpy' operation requires 28 bytes but the destination is only 24 bytes. |
6-
| tests2.cpp:41:3:41:8 | call to wcscpy | This 'call to wcscpy' operation requires 32 bytes but the destination is only 28 bytes. |
7-
| tests2.cpp:46:3:46:8 | call to wcscpy | This 'call to wcscpy' operation requires 36 bytes but the destination is only 32 bytes. |
1+
| tests2.cpp:18:3:18:8 | call to wcscpy | This 'call to wcscpy' operation requires 12 bytes but the destination is only 8 bytes. |
2+
| tests2.cpp:23:3:23:8 | call to wcscpy | This 'call to wcscpy' operation requires 16 bytes but the destination is only 12 bytes. |
3+
| tests2.cpp:28:3:28:8 | call to wcscpy | This 'call to wcscpy' operation requires 20 bytes but the destination is only 16 bytes. |
4+
| tests2.cpp:32:3:32:8 | call to wcscpy | This 'call to wcscpy' operation requires 24 bytes but the destination is only 20 bytes. |
5+
| tests2.cpp:37:3:37:8 | call to wcscpy | This 'call to wcscpy' operation requires 28 bytes but the destination is only 24 bytes. |
6+
| tests2.cpp:42:3:42:8 | call to wcscpy | This 'call to wcscpy' operation requires 32 bytes but the destination is only 28 bytes. |
7+
| tests2.cpp:47:3:47:8 | call to wcscpy | This 'call to wcscpy' operation requires 36 bytes but the destination is only 32 bytes. |
88
| tests.c:54:3:54:9 | call to sprintf | This 'call to sprintf' operation requires 11 bytes but the destination is only 10 bytes. |
99
| tests.c:58:3:58:9 | call to sprintf | This 'call to sprintf' operation requires 11 bytes but the destination is only 10 bytes. |
1010
| tests.c:62:17:62:24 | buffer10 | This 'scanf string argument' operation requires 11 bytes but the destination is only 10 bytes. |

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/tests2.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void *realloc(void *ptr, size_t size);
66
void *calloc(size_t nmemb, size_t size);
77
void free(void *ptr);
88
wchar_t *wcscpy(wchar_t *s1, const wchar_t *s2);
9+
int snprintf(char *s, size_t n, const char *format, ...);
910

1011
// --- Semmle tests ---
1112

@@ -46,3 +47,18 @@ void tests2() {
4647
wcscpy(buffer, L"12345678"); // BAD: buffer overflow
4748
delete [] buffer;
4849
}
50+
51+
char* dest1 = "a";
52+
char* dest2 = "abcdefghijklmnopqrstuvwxyz";
53+
54+
void test3() {
55+
const char src[] = "abcdefghijkl";
56+
dest1 = (char*)malloc(sizeof(src));
57+
if (!dest1)
58+
return;
59+
snprintf(dest1, sizeof(src), "%s", src); // GOOD
60+
dest2 = (char*)malloc(3);
61+
if (!dest2)
62+
return;
63+
snprintf(dest2, sizeof(src), "%s", src); // BAD [NOT DETECTED]: buffer overflow
64+
}

0 commit comments

Comments
 (0)