Skip to content

Commit ee185ea

Browse files
author
Robert Marsh
authored
Merge pull request #2273 from geoffw0/ntohl
CPP: Add tests of NtohlArrayNoBoundOpenSource.ql.
2 parents c5396d9 + f9feb05 commit ee185ea

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| test.cpp:12:25:12:29 | call to ntohl | Unchecked use of data from network function $@ | test.cpp:12:25:12:29 | call to ntohl | call to ntohl |
2+
| test.cpp:21:26:21:29 | len2 | Unchecked use of data from network function $@ | test.cpp:10:16:10:20 | call to ntohl | call to ntohl |
3+
| test.cpp:31:26:31:29 | len2 | Unchecked use of data from network function $@ | test.cpp:10:16:10:20 | call to ntohl | call to ntohl |
4+
| test.cpp:61:26:61:29 | len2 | Unchecked use of data from network function $@ | test.cpp:10:16:10:20 | call to ntohl | call to ntohl |
5+
| test.cpp:64:9:64:12 | len2 | Unchecked use of data from network function $@ | test.cpp:10:16:10:20 | call to ntohl | call to ntohl |
6+
| test.cpp:73:10:73:13 | lens | Unchecked use of data from network function $@ | test.cpp:10:16:10:20 | call to ntohl | call to ntohl |
7+
| test.cpp:86:10:86:13 | len3 | Unchecked use of data from network function $@ | test.cpp:85:10:85:14 | call to ntohl | call to ntohl |
8+
| test.cpp:94:9:94:11 | len | Unchecked use of data from network function $@ | test.cpp:99:8:99:12 | call to ntohl | call to ntohl |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Memory Management/Buffer Overflow/NtohlArrayNoBoundOpenSource.ql
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
typedef unsigned int size_t;
3+
void *memcpy(void *s1, const void *s2, size_t n);
4+
size_t strlen(const char *s);
5+
int ntohl(int x);
6+
7+
void test1(const char *source, size_t len)
8+
{
9+
char buffer[256];
10+
size_t len2 = ntohl(len);
11+
12+
memcpy(buffer, source, ntohl(len)); // BAD
13+
14+
if (len2 < 256)
15+
{
16+
memcpy(buffer, source, len2); // GOOD
17+
}
18+
19+
if (source != 0)
20+
{
21+
memcpy(buffer, source, len2); // BAD
22+
}
23+
24+
if ((len2 < 256) && (source != 0))
25+
{
26+
memcpy(buffer, source, len2); // GOOD
27+
}
28+
29+
if ((len2 < 256) || (source != 0))
30+
{
31+
memcpy(buffer, source, len2); // BAD
32+
}
33+
34+
if (len2 < 256)
35+
{
36+
if (source != 0)
37+
{
38+
memcpy(buffer, source, len2); // GOOD
39+
}
40+
}
41+
42+
if (len2 >= 256)
43+
{
44+
// fail
45+
} else {
46+
memcpy(buffer, source, len2); // GOOD
47+
}
48+
49+
if (len2 + 1 < 256)
50+
{
51+
memcpy(buffer, source, len2 + 1); // GOOD
52+
}
53+
54+
if (strlen(source) < 256)
55+
{
56+
memcpy(buffer, source, strlen(source)); // GOOD
57+
}
58+
59+
if (strlen(source) < 256)
60+
{
61+
memcpy(buffer, source, len2); // BAD
62+
}
63+
64+
buffer[len2] = 0; // BAD
65+
66+
if (len2 < 256)
67+
{
68+
buffer[len2] = 0; // GOOD
69+
}
70+
71+
{
72+
unsigned short lens = len2;
73+
buffer[lens] = 0; // BAD
74+
}
75+
76+
if (len2 < 256)
77+
{
78+
unsigned short lens = len2;
79+
buffer[lens] = 0; // GOOD
80+
}
81+
82+
size_t len3 = 0;
83+
if (len3 < 256)
84+
{
85+
len3 = ntohl(len);
86+
buffer[len3] = 0; // BAD
87+
}
88+
}
89+
90+
void test2(size_t len)
91+
{
92+
char buffer[256];
93+
94+
buffer[len] = 0; // BAD
95+
}
96+
97+
void test3(size_t len)
98+
{
99+
test2(ntohl(len));
100+
}

0 commit comments

Comments
 (0)