Skip to content

Commit d4c0c40

Browse files
authored
Brodes/overflow buffer fixes (#79)
* Addreessing false positive due to incorrect use of getType * Addressing false positive with strncpy. * BufferAccess must be reachable. False positives observed where accesses occur in dead code. * Formatting and updating tests.
1 parent fd512d2 commit d4c0c40

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Buffer.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private int isSource(Expr bufferExpr, Element why) {
5757
exists(Type bufferType |
5858
// buffer is the address of a variable
5959
why = bufferExpr.(AddressOfExpr).getAddressable() and
60-
bufferType = why.(Variable).getType() and
60+
bufferType = why.(Variable).getUnspecifiedType() and
6161
result = bufferType.getSize() and
6262
not bufferType instanceof ReferenceType and
6363
not any(Union u).getAMemberVariable() = why

cpp/ql/lib/semmle/code/cpp/security/BufferAccess.qll

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ int getPointedSize(Type t) {
1414
* BufferWrite differ.
1515
*/
1616
abstract class BufferAccess extends Expr {
17-
BufferAccess() { not this.isUnevaluated() }
17+
BufferAccess() {
18+
not this.isUnevaluated() and
19+
//A buffer access must be reachable (not in dead code)
20+
reachable(this)
21+
}
1822

1923
abstract string getName();
2024

@@ -125,10 +129,11 @@ class StrncpyBA extends BufferAccess {
125129
result = this.(FunctionCall).getArgument(0) and
126130
bufferDesc = "destination buffer" and
127131
accessType = 2
128-
or
129-
result = this.(FunctionCall).getArgument(1) and
130-
bufferDesc = "source buffer" and
131-
accessType = 2
132+
// Ignore this case as reading past the source null terminator is not the behavior of strncpy
133+
// or
134+
// result = this.(FunctionCall).getArgument(1) and
135+
// bufferDesc = "source buffer" and
136+
// accessType = 2
132137
}
133138

134139
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/UnboundedWrite.expected

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ edges
3131
| main.cpp:9:29:9:32 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | |
3232
| main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | **argv | provenance | |
3333
| main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | *argv | provenance | |
34-
| main.cpp:10:20:10:23 | **argv | tests.cpp:657:32:657:35 | **argv | provenance | |
35-
| main.cpp:10:20:10:23 | *argv | tests.cpp:657:32:657:35 | **argv | provenance | |
36-
| main.cpp:10:20:10:23 | *argv | tests.cpp:657:32:657:35 | *argv | provenance | |
34+
| main.cpp:10:20:10:23 | **argv | tests.cpp:672:32:672:35 | **argv | provenance | |
35+
| main.cpp:10:20:10:23 | *argv | tests.cpp:672:32:672:35 | **argv | provenance | |
36+
| main.cpp:10:20:10:23 | *argv | tests.cpp:672:32:672:35 | *argv | provenance | |
3737
| overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | provenance | |
3838
| overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | *argv | provenance | |
3939
| test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | provenance | |
@@ -46,12 +46,12 @@ edges
4646
| tests.cpp:628:14:628:14 | *s [*home] | tests.cpp:628:14:628:19 | *home | provenance | |
4747
| tests.cpp:628:14:628:14 | *s [*home] | tests.cpp:628:16:628:19 | *home | provenance | |
4848
| tests.cpp:628:16:628:19 | *home | tests.cpp:628:14:628:19 | *home | provenance | |
49-
| tests.cpp:657:32:657:35 | **argv | tests.cpp:682:9:682:15 | *access to array | provenance | |
50-
| tests.cpp:657:32:657:35 | **argv | tests.cpp:683:9:683:15 | *access to array | provenance | |
51-
| tests.cpp:657:32:657:35 | *argv | tests.cpp:682:9:682:15 | *access to array | provenance | |
52-
| tests.cpp:657:32:657:35 | *argv | tests.cpp:683:9:683:15 | *access to array | provenance | |
53-
| tests.cpp:682:9:682:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54-
| tests.cpp:683:9:683:15 | *access to array | tests.cpp:622:19:622:24 | *source | provenance | |
49+
| tests.cpp:672:32:672:35 | **argv | tests.cpp:697:9:697:15 | *access to array | provenance | |
50+
| tests.cpp:672:32:672:35 | **argv | tests.cpp:698:9:698:15 | *access to array | provenance | |
51+
| tests.cpp:672:32:672:35 | *argv | tests.cpp:697:9:697:15 | *access to array | provenance | |
52+
| tests.cpp:672:32:672:35 | *argv | tests.cpp:698:9:698:15 | *access to array | provenance | |
53+
| tests.cpp:697:9:697:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54+
| tests.cpp:698:9:698:15 | *access to array | tests.cpp:622:19:622:24 | *source | provenance | |
5555
| tests_restrict.c:15:41:15:44 | **argv | tests_restrict.c:15:41:15:44 | **argv | provenance | |
5656
| tests_restrict.c:15:41:15:44 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | |
5757
nodes
@@ -85,10 +85,10 @@ nodes
8585
| tests.cpp:628:14:628:14 | *s [*home] | semmle.label | *s [*home] |
8686
| tests.cpp:628:14:628:19 | *home | semmle.label | *home |
8787
| tests.cpp:628:16:628:19 | *home | semmle.label | *home |
88-
| tests.cpp:657:32:657:35 | **argv | semmle.label | **argv |
89-
| tests.cpp:657:32:657:35 | *argv | semmle.label | *argv |
90-
| tests.cpp:682:9:682:15 | *access to array | semmle.label | *access to array |
91-
| tests.cpp:683:9:683:15 | *access to array | semmle.label | *access to array |
88+
| tests.cpp:672:32:672:35 | **argv | semmle.label | **argv |
89+
| tests.cpp:672:32:672:35 | *argv | semmle.label | *argv |
90+
| tests.cpp:697:9:697:15 | *access to array | semmle.label | *access to array |
91+
| tests.cpp:698:9:698:15 | *access to array | semmle.label | *access to array |
9292
| tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv |
9393
| tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv |
9494
| tests_restrict.c:15:41:15:44 | *argv | semmle.label | *argv |

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,21 @@ void test26(bool cond)
654654
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
655655
}
656656

657+
void test27(){
658+
char *src = "";
659+
char *dest = "abcdefgh";
660+
const int size = 100;
661+
int ind = 100;
662+
char buffer[size];
663+
664+
strncpy(dest, src, strlen(dest)); // GOOD, strncpy will not read past null terminator of source
665+
666+
if(ind < size){
667+
buffer[ind] = 0; // GOOD: out of bounds, but inaccessible code
668+
}
669+
}
670+
671+
657672
int tests_main(int argc, char *argv[])
658673
{
659674
long long arr17[19];

0 commit comments

Comments
 (0)