Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What C/C++ functions are most often used incorrectly and can lead to buffer overflows?

I've been asked to maintain a large C++ codebase full of memory leaks. While poking around, I found out that we have a lot of buffer overflows that lead to the leaks (how it got this bad, I don't ever want to know).

I've decided to removing the buffer overflows first, starting with the dangerous functions. What C/C++ functions that are most often used incorrectly and can lead to buffer overflow?

For compiler and/or tools used to help look for buffer overrun, I've created another question that deals with this

like image 860
MrValdez Avatar asked Oct 03 '08 14:10

MrValdez


People also ask

Which C function can cause buffer overflow?

That is why the safest basic method in C is to avoid the following five unsafe functions that can lead to a buffer overflow vulnerability: printf , sprintf , strcat , strcpy , and gets .

What types of programming languages are vulnerable to buffer overflows?

Some coding languages ​​are more sensitive to buffer overflows than others. C and C ++ are two common languages ​​with high vulnerabilities because they do not include built-in protection against access or overwriting of memory data. Code is written in one or both of these languages ​​in Windows, Mac OSX, and Linux.

Is C vulnerable to buffer overflow?

C and C++ are more susceptible to buffer overflow. Secure development practices should include regular testing to detect and fix buffer overflows. These practices include automatic protection at the language level and bounds-checking at run-time.


1 Answers

In general, any function that does not check bounds in the arguments. A list would be

  • gets()
  • scanf()
  • strcpy()
  • strcat()

You should use size limited versions like stncpy, strncat, fgets, etc. Then be careful while giving the size limit; take into consideration the '\0' terminating the string.

Also, arrays are NOT bound checked in C or C++. The following example would cause errors. See off by one error

int foo[3];
foo[3] = WALKED_OFF_END_OF_ARRAY;

edit: Copied answers of @MrValdez , @Denton Gentry

like image 178
hayalci Avatar answered Sep 18 '22 17:09

hayalci