Advertisement
Bobita

b_overflow.c

Jan 7th, 2025
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | Source Code | 0 0
  1. /* Vunlerable program: stack.c */
  2. /* You can get this program from the lab’s website */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. /* Changing this size will change the layout of the stack.
  8.  * Instructors can change this value each year, so students
  9.  * won’t be able to use the solutions from the past.
  10.  * Suggested value: between 0 and 400 */
  11. #ifndef BUF_SIZE
  12. #define BUF_SIZE 24
  13. #endif
  14.  
  15. int bof(char * str)
  16. {
  17.   char buffer[BUF_SIZE];
  18.  
  19.   /* The following statement has a buffer overflow problem */
  20.   strcpy(buffer, str);
  21.  
  22.   return 1;
  23. }
  24. int main(int argc, char ** argv)
  25. {
  26.   char str[517];
  27.   FILE * badfile;
  28.   /* Change the size of the dummy array to randomize the parameters
  29.   for this lab. Need to use the array at least once */
  30.   char dummy[BUF_SIZE];
  31.   memset(dummy, 0, BUF_SIZE);
  32.  
  33.   badfile = fopen("badfile", "r");
  34.   fread(str, sizeof(char), 517, badfile);
  35.   bof(str);
  36.   printf("Returned Properly\n");
  37.   return 1;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement