Question

Consider the following function which is called from main(). The meaning is that a substring of...

Consider the following function which is called from main(). The meaning is that a substring of str will be copied in buf. The substring is between locations [start, end] of str. Does this program contain a buffer overflow vulnerability? Explain why or why not.

int func(char ∗str, int start, int end) {

   ...

   char   buf [1024];

    if (end−start > 1024) {

       printf (“Error - Substring too big\n”);

       exit (-1);

   }

    strcpy (buf,   str);

   ...

   return 1;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Going by the function definition, it is safe to assume that the original string (pointed by str) is of length 1024 and the function tries to copy a substring of the original string in char buff[]. If the original string size is not 1024, please let me know.

Now does this program has a buffer overflow vulnerabiliity ? Yes.

Reason is we created the buffer char buff[] to be size 1024. That is good and ok as original string is also of length 1024. But the problem lies in the fact that we are only checking the difference between the starting and ending index of substring to be copied i.e if end-start <= 1024 we copy it, otherwise not.

What if the start index is 5 and end index is 1029 (for example). In this case end-start = 1029-5 = 1024 which is within the limits defined and the copy operation strcpy(...) will take place. But is this allowed? The end index here is 1029. But str has index range only from 0 to 1023. We cannot access and copy the characters at indexes after 1024 because this is unauthorized or not intended by the programmer.

Hence we need to perform additional checks in addition to checking the difference between end-start > 1024 i.e

If (start <0 || end>1023)

printf("Error - Index out of bounds\n");

exit(-1); //abnormal termination

Please do let me know if you have any questions, queries/need clarifications. Thanks

Add a comment
Know the answer?
Add Answer to:
Consider the following function which is called from main(). The meaning is that a substring of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Consider the following function://To compile this code by itself, naturally you need a main method. #include...

    Consider the following function://To compile this code by itself, naturally you need a main method. #include <stdio.h> #include <sys/stat.h> void printFileIndexNumber(char *path){ struct stat statbuf; if (stat(path, &statbuf) == -1) perror("Failed to get file status"); else printf("%s inode number is %d", path, &statbuf.st_ino);} It uses the stat system call to get information about the file indicated by the parameter *path. It puts the information about the file in the structure struct stat statbuf and prints out the file index number/inode...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT