Skip to content

Commit 42bcd52

Browse files
authored
Merge pull request #13 from zer0h-bb/master
Trying to fix some buffer overflows
2 parents 17738d4 + 354aa49 commit 42bcd52

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

main.cpp

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <iostream>
1+
#include <iostream>
22
#include <vector>
33
#include <fstream>
44
#include <cstddef>
@@ -204,10 +204,26 @@ void process_append()
204204

205205
// First level
206206
char a[1024] = {};
207+
if (location >= sizeof(a)-1) // the array should contain at least contain a '-' and a null byte
208+
{
209+
cout << "Target " << targ << endl << "too long : Overflow" << endl;
210+
return;
211+
}
207212
strncpy(a, targ.c_str(), location);
208213
strcat(a, "-");
209214
char temp[1024] = {};
215+
if(appnd.length() >= sizeof(temp))
216+
{
217+
cout << "Append value " << appnd << endl << "too long : Overflow" << endl;
218+
return;
219+
}
210220
strncpy(temp, appnd.c_str(), appnd.length());
221+
if((strlen(a)+strlen(temp) >=sizeof(a)) || (strlen(a)+targ.substr(location, targ.length() - location).length() >=sizeof(a)))
222+
{
223+
cout << "Can't concatenate target and append, strings too long : Overflow" << endl;
224+
return;
225+
}
226+
211227
strcat(a, temp);
212228
strcat(a, targ.substr(location, targ.length() - location).c_str());
213229
print_domain(string(a));
@@ -323,7 +339,22 @@ void process_prepend()
323339
string targ = *target;
324340
// First one
325341
char a[1024] = {};
342+
if (prep.length() >= sizeof(a)-1)
343+
{
344+
cout << "Prepend " << prep << endl << "too long : Overflow" << endl;
345+
return;
346+
}
326347
strncpy(a, prep.c_str(), prep.length());
348+
if(targ.length()> 1024) // this is to prevent integer overflow in the check below
349+
{
350+
cout << "Target " << targ << endl << "is too long : Overflow" << endl;
351+
return;
352+
}
353+
if(strlen(a)+targ.length() >= sizeof(a)-1)
354+
{
355+
cout << "Can't concatenate target and prepend, strings too long : Overflow" << endl;
356+
return;
357+
}
327358
strcat(a, targ.c_str());
328359

329360
print_domain(string(a));

0 commit comments

Comments
 (0)