Skip to content

Commit 87b6495

Browse files
committed
add zlib tests with stubs :)
1 parent 361ad6b commit 87b6495

2 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/query-tests/Security/CWE/CWE-409/DecompressionBombs.ql
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
2+
#define Z_NULL 0
3+
# define FAR
4+
typedef unsigned char Byte;
5+
typedef Byte FAR Bytef;
6+
typedef unsigned int uInt;
7+
#define Z_BEST_COMPRESSION 9
8+
#define Z_FINISH 4
9+
#define Z_NO_FLUSH 0
10+
11+
12+
typedef struct {
13+
int *zalloc;
14+
int *zfree;
15+
Bytef *next_in;
16+
Bytef *next_out;
17+
int *opaque;
18+
uInt avail_out;
19+
uInt avail_in;
20+
} z_stream;
21+
22+
23+
void deflateInit(z_stream *defstream, int i);
24+
25+
void deflate(z_stream *defstream, int i);
26+
27+
void deflateEnd(z_stream *defstream);
28+
29+
void inflateInit(z_stream *infstream);
30+
31+
void inflate(z_stream *infstream, int i);
32+
33+
void inflateEnd(z_stream *infstream);
34+
35+
namespace std {
36+
template<class charT>
37+
struct char_traits;
38+
39+
template<class charT, class traits = char_traits<charT> >
40+
class basic_ostream {
41+
public:
42+
typedef charT char_type;
43+
};
44+
45+
template<class charT, class traits>
46+
basic_ostream<charT, traits> &operator<<(basic_ostream<charT, traits> &, const charT *);
47+
48+
typedef basic_ostream<char> ostream;
49+
50+
extern ostream cout;
51+
}
52+
53+
int UnsafeInflate(int argc, char *argv[]) {
54+
// original string len = 36
55+
char a[50] = "Hello Hello Hello Hello Hello Hello!";
56+
// placeholder for the compressed (deflated) version of "a"
57+
char b[50];
58+
// placeholder for the Uncompressed (inflated) version of "b"
59+
char c[50];
60+
61+
62+
// STEP 1.
63+
// zlib struct
64+
z_stream defstream;
65+
defstream.zalloc = Z_NULL;
66+
defstream.zfree = Z_NULL;
67+
defstream.opaque = Z_NULL;
68+
// setup "a" as the input and "b" as the compressed output
69+
defstream.avail_in = (uInt) 50 + 1; // size of input, string + terminator
70+
defstream.next_in = (Bytef *) a; // input char array
71+
defstream.avail_out = (uInt) sizeof(b); // size of output
72+
defstream.next_out = (Bytef *) b; // output char array
73+
74+
// the actual compression work.
75+
deflateInit(&defstream, Z_BEST_COMPRESSION);
76+
deflate(&defstream, Z_FINISH);
77+
deflateEnd(&defstream);
78+
79+
// This is one way of getting the size of the output
80+
// STEP 2.
81+
// inflate b into c
82+
// zlib struct
83+
z_stream infstream;
84+
infstream.zalloc = Z_NULL;
85+
infstream.zfree = Z_NULL;
86+
infstream.opaque = Z_NULL;
87+
// setup "b" as the input and "c" as the compressed output
88+
// TOTHINK: Here we can add additional step from Right operand to z_stream variable access
89+
infstream.avail_in = (uInt) ((char *) defstream.next_out - b); // size of input
90+
infstream.next_in = (Bytef *) b; // input char array
91+
infstream.avail_out = (uInt) sizeof(c); // size of output
92+
infstream.next_out = (Bytef *) c; // output char array
93+
94+
// uLong total_out; /* total number of bytes output so far */
95+
// the actual DE-compression work.
96+
inflateInit(&infstream);
97+
inflate(&infstream, Z_NO_FLUSH);
98+
inflateEnd(&infstream);
99+
100+
101+
return 0;
102+
}
103+
104+
105+
typedef struct {
106+
} gzFile;
107+
108+
gzFile gzopen(char *str, const char *rb);
109+
110+
111+
void exit(int i);
112+
113+
unsigned int gzread(gzFile gz_file, unsigned char *str, int i);
114+
115+
void gzclose(gzFile gz_file);
116+
117+
std::ostream operator<<(const std::ostream &lhs, unsigned char rhs);
118+
119+
120+
int send(int, const void *, int, int);
121+
122+
123+
int UnsafeGzread(char **argv) {
124+
char *fileName;
125+
send(0, fileName, 0, 0);
126+
gzFile inFileZ = gzopen(fileName, "rb");
127+
if (&inFileZ == nullptr) {
128+
exit(0);
129+
}
130+
unsigned char unzipBuffer[8192];
131+
unsigned int unzippedBytes;
132+
while (true) {
133+
unzippedBytes = gzread(inFileZ, unzipBuffer, 8192);
134+
if (unzippedBytes > 0) {
135+
std::cout << unzippedBytes;
136+
} else {
137+
break;
138+
}
139+
}
140+
gzclose(inFileZ);
141+
return 0;
142+
}
143+
144+
bool gzfread(char *str, int i, int i1, gzFile gz_file);
145+
146+
int UnsafeGzfread(char **argv) {
147+
char *fileName;
148+
send(0, fileName, 0, 0);
149+
gzFile inFileZ = gzopen(fileName, "rb");
150+
if (&inFileZ == nullptr) {
151+
exit(0);
152+
}
153+
while (true) {
154+
char buffer[1000];
155+
if (!gzfread(buffer, 999, 1, inFileZ)) {
156+
break;
157+
}
158+
}
159+
gzclose(inFileZ);
160+
return 0;
161+
}
162+
163+
char *gzgets(gzFile gz_file, char *buffer, int i);
164+
165+
int UnsafeGzgets(char **argv) {
166+
char *fileName;
167+
send(0, fileName, 0, 0);
168+
gzFile inFileZ = gzopen(fileName, "rb");
169+
if (&inFileZ == nullptr) {
170+
exit(0);
171+
}
172+
char *buffer = new char[4000000000];
173+
char *result;
174+
result = gzgets(inFileZ, buffer, 1000000000);
175+
while (true) {
176+
result = gzgets(inFileZ, buffer, 1000000000);
177+
if (result == nullptr) {
178+
break;
179+
}
180+
}
181+
return 0;
182+
}

0 commit comments

Comments
 (0)