Skip to content

Commit 1005a89

Browse files
committed
Updating test cases.
1 parent 8ab22fe commit 1005a89

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class BufferAccess extends Expr {
3030
* - 1 = buffer range [0, getSize) is accessed entirely.
3131
* - 2 = buffer range [0, getSize) may be accessed partially or entirely.
3232
* - 3 = buffer is accessed at offset getSize - 1.
33+
* - 4 = buffer is accessed with null terminator read protections
34+
* (does not read past null terminator, regardless of access size)
3335
*/
3436
abstract Expr getBuffer(string bufferDesc, int accessType);
3537

@@ -129,7 +131,10 @@ class StrncpyBA extends BufferAccess {
129131
result = this.(FunctionCall).getArgument(0) and
130132
bufferDesc = "destination buffer" and
131133
accessType = 2
132-
// NOTE, ignoring getting the source buffer (arg 1) since reading past the the source null terminator is not the behavior of strncpy
134+
or
135+
result = this.(FunctionCall).getArgument(1) and
136+
bufferDesc = "source buffer" and
137+
accessType = 4
133138
}
134139

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

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class BufferAccess extends ArrayExpr {
4242
not exists(Macro m |
4343
m.getName() = "strcmp" and
4444
m.getAnInvocation().getAnExpandedElement() = this
45-
)
45+
) and
46+
//A buffer access must be reachable (not in dead code)
47+
reachable(this)
4648
}
4749

4850
int bufferSize() { staticBuffer(this.getArrayBase(), _, result) }

cpp/ql/src/Security/CWE/CWE-119/OverflowBuffer.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ from
2626
BufferAccess ba, string bufferDesc, int accessSize, int accessType, Element bufferAlloc,
2727
int bufferSize, string message
2828
where
29+
accessType != 4 and
2930
accessSize = ba.getSize() and
3031
bufferSize = getBufferSize(ba.getBuffer(bufferDesc, accessType), bufferAlloc) and
3132
(

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

Lines changed: 12 additions & 12 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:688:32:688:35 | **argv | provenance | |
35-
| main.cpp:10:20:10:23 | *argv | tests.cpp:688:32:688:35 | **argv | provenance | |
36-
| main.cpp:10:20:10:23 | *argv | tests.cpp:688:32:688:35 | *argv | provenance | |
34+
| main.cpp:10:20:10:23 | **argv | tests.cpp:689:32:689:35 | **argv | provenance | |
35+
| main.cpp:10:20:10:23 | *argv | tests.cpp:689:32:689:35 | **argv | provenance | |
36+
| main.cpp:10:20:10:23 | *argv | tests.cpp:689:32:689: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:688:32:688:35 | **argv | tests.cpp:713:9:713:15 | *access to array | provenance | |
50-
| tests.cpp:688:32:688:35 | **argv | tests.cpp:714:9:714:15 | *access to array | provenance | |
51-
| tests.cpp:688:32:688:35 | *argv | tests.cpp:713:9:713:15 | *access to array | provenance | |
52-
| tests.cpp:688:32:688:35 | *argv | tests.cpp:714:9:714:15 | *access to array | provenance | |
53-
| tests.cpp:713:9:713:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54-
| tests.cpp:714:9:714:15 | *access to array | tests.cpp:622:19:622:24 | *source | provenance | |
49+
| tests.cpp:689:32:689:35 | **argv | tests.cpp:714:9:714:15 | *access to array | provenance | |
50+
| tests.cpp:689:32:689:35 | **argv | tests.cpp:715:9:715:15 | *access to array | provenance | |
51+
| tests.cpp:689:32:689:35 | *argv | tests.cpp:714:9:714:15 | *access to array | provenance | |
52+
| tests.cpp:689:32:689:35 | *argv | tests.cpp:715:9:715:15 | *access to array | provenance | |
53+
| tests.cpp:714:9:714:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54+
| tests.cpp:715:9:715: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:688:32:688:35 | **argv | semmle.label | **argv |
89-
| tests.cpp:688:32:688:35 | *argv | semmle.label | *argv |
90-
| tests.cpp:713:9:713:15 | *access to array | semmle.label | *access to array |
88+
| tests.cpp:689:32:689:35 | **argv | semmle.label | **argv |
89+
| tests.cpp:689:32:689:35 | *argv | semmle.label | *argv |
9190
| tests.cpp:714:9:714:15 | *access to array | semmle.label | *access to array |
91+
| tests.cpp:715:9:715: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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,17 +654,18 @@ void test26(bool cond)
654654
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
655655
}
656656

657+
#define IND 100
658+
#define MAX_SIZE 100
657659
void test27(){
658660
char *src = "";
659661
char *dest = "abcdefgh";
660-
const int size = 100;
661662
int ind = 100;
662-
char buffer[size];
663+
char buffer[MAX_SIZE];
663664

664-
strncpy(dest, src, strlen(dest)); // GOOD, strncpy will not read past null terminator of source
665+
strncpy(dest, src, 8); // GOOD, strncpy will not read past null terminator of source
665666

666-
if(ind < size){
667-
buffer[ind] = 0; // GOOD: out of bounds, but inaccessible code
667+
if(IND < MAX_SIZE){
668+
buffer[IND] = 0; // GOOD: out of bounds, but inaccessible code
668669
}
669670
}
670671

0 commit comments

Comments
 (0)