Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created May 13, 2022 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonetheman/06fc42576692a8b404d13c53cc00e37f to your computer and use it in GitHub Desktop.
Save tonetheman/06fc42576692a8b404d13c53cc00e37f to your computer and use it in GitHub Desktop.
shows an overwrite in c (not enough space in the array greeting)
# needed to include the no-stack-protector to make gcc
# be fast and loose with the stack
junk : test.c
gcc -o junk -g -fno-stack-protector test.c
clean :
rm -f ./junk
#include <stdio.h>
int main() {
// Example 1: An array with not enough space?
// step 1
// run this and enter ab for the greeting and everything is good
// step 2
// run this and enter abc and one of the variables will be over written (on my computer)
// step 3
// run this and enter abcd and BOTH! of the variables will be over written
// step 4
// run this and enter a string of 12 or 13 characters and you will get a segmentation fault
char a;
char greeting[3];
char b;
a = 0xff;
b = 0xff;
printf("value of a b before %d %d\n",a,b);
printf("Enter a greeting: ");
scanf("%s", greeting);
printf("value of greeting is %s\n", greeting);
printf("value of a b after %d %d\n",a,b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment