I am testing a simple buffer overflow in c++. The example is a test where given that checks are not in place, a malicious user could overwrite variables using a buffer overflow.
The example defines a buffer and then a variable, this means that space should be allocated for the buffer, and then space for the variable. The example reads from cin
to a buffer of length 5, and then checks if the admin variable is set to something other that 0, if it is, the user conceptually gained admin access.
#include <iostream>
using namespace std;
int main()
{
char buffer[5];
int admin = 0;
cin>>buffer;
if(strcmp(buffer,"in") == 0)
{
admin = 1;
cout<<"Correct"<<endl;
}
if(admin != 0)
cout << "Access" << endl;
return 0;
}
I have 3 machines, 1 Windows and 2 Linux systems.
When I test this on windows (CodeBlocks) it works (logically)
entering more than 5 characters overflows and rewrites the the admin
variable's bytes
Now my first linux system also works but only when I enter 13 characters, is this to do with different compilers and how they allocate memory to the program?
My second linux machine can't overflow at all. It will give a dump error only after the 13th character.
Why do they differ that much?