Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dab1bba

Browse files
committed
CPP: Add a test of TaintedAllocationSize.
1 parent 581e765 commit dab1bba

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:43:38:43:63 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
2+
| test.cpp:52:35:52:60 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-190/TaintedAllocationSize.ql
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Associated with CWE-190: Integer Overflow or Wraparound. http://cwe.mitre.org/data/definitions/190.html
2+
3+
typedef unsigned long size_t;
4+
typedef struct {} FILE;
5+
6+
void *malloc(size_t size);
7+
void *realloc(void *ptr, size_t size);
8+
int atoi(const char *nptr);
9+
10+
struct MyStruct
11+
{
12+
char data[256];
13+
};
14+
15+
namespace std
16+
{
17+
template<class charT> struct char_traits;
18+
19+
template <class charT, class traits = char_traits<charT> >
20+
class basic_istream /*: virtual public basic_ios<charT,traits> - not needed for this test */ {
21+
public:
22+
basic_istream<charT,traits>& operator>>(int& n);
23+
};
24+
25+
typedef basic_istream<char> istream;
26+
27+
extern istream cin;
28+
}
29+
30+
int getTainted() {
31+
int i;
32+
33+
std::cin >> i;
34+
35+
return i;
36+
}
37+
38+
int main(int argc, char **argv) {
39+
int tainted = atoi(argv[1]);
40+
41+
MyStruct *arr1 = (MyStruct *)malloc(sizeof(MyStruct)); // GOOD
42+
MyStruct *arr2 = (MyStruct *)malloc(tainted); // BAD [NOT DETECTED]
43+
MyStruct *arr3 = (MyStruct *)malloc(tainted * sizeof(MyStruct)); // BAD
44+
MyStruct *arr4 = (MyStruct *)malloc(getTainted() * sizeof(MyStruct)); // BAD [NOT DETECTED]
45+
MyStruct *arr5 = (MyStruct *)malloc(sizeof(MyStruct) + tainted); // BAD [NOT DETECTED]
46+
47+
int size = tainted * 8;
48+
char *chars1 = (char *)malloc(size); // BAD [NOT DETECTED]
49+
char *chars2 = new char[size]; // BAD [NOT DETECTED]
50+
char *chars3 = new char[8]; // GOOD
51+
52+
arr1 = (MyStruct *)realloc(arr1, sizeof(MyStruct) * tainted); // BAD
53+
54+
size = 8;
55+
chars3 = new char[size]; // GOOD
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)