Coder Profile - Show off your skills, get a coder profile.
 
 
 
Buffer Overflow Protection
Software Development
Code :: Vunerable Snippet One Copy / Restore
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc,char **argv){
  5.           char file_name[100];
  6.  
  7.           if(!argv[1]) {
  8.                              printf("File Name: ");
  9.            gets(file_name);
  10.                       }else strcpy(file_name,argv[1]);
  11.  
  12.           printf("\nFile Name: %s\n",file_name);
  13.                       return 0;
  14. }
The above code is vunerable to Buffer Overflow in two locations

Unfortunatly you see it now in lots of code.

The program gets a file name and prints it to the screen. If the file name is given in the first arguement it copys that into the buffer and prints the buffer, if not it asks the user for it.
Code Copy / Restore
  1. gets(file_name);
Is the first vunerability.

This does not take into account size of the buffer and which just write everything it gets into there. So if the user types in more than 100 charachters the buffer will overflow and the program may crash.
Code Copy / Restore
  1. }else strcpy(file_name,argv[1]);
strcpy also does not take into account the size of a buffer.

The Following code is not vunerable to buffer overflow:
Code :: Non Vunerable Code Copy / Restore
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc,char **argv){
  5.           char file_name[100];
  6.  
  7.           if(!argv[1]) {
  8.                              printf("File Name: ");
  9.            fgets(file_name,sizeof(file_name)-1,stdin);
  10.                              if(*file_name) file_name[strlen(file_name)-1]=0;
  11.                              else{
  12.                                   puts("No text entered");
  13.                                   return 0;
  14.                               }
  15.                       }else strncpy(file_name,argv[1],sizeof(file_name)-1);
  16.  
  17.           printf("\nFile Name: %s\n",file_name);
  18.                       return 0;
  19. }
My Tips for avoiding buffer overflows.

-Protect access to variables. Free access is asking for exploits
-If you can't protect access, (servers & clients etc...) monitior what goes n and out
-Modify the size of your buffers depending on what your going to put in them, set sized buffers are asking for problems.
-Make sure you know the functions your using and what they do to buffers and exactly what each arguement it for, some functions will go one over the paramater for maximum bytes to write to a buffer so that they can null terminate it.

Notice how i used "sizeof(buffer)-1" instead of "sizeof(buffer)"

In general I would always give functions a byte in leway each time

Also use your common sense.

EDIT.
Sorry for indentation, codeboxes screwed up


Posted By Uranium-239
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Arcube     Posted 257 Days Ago
 
 
Well done, this articles explains and shows clearly what are buffer overflows, and
you even show how to protect them, that's the kind of articles we need!
 
VBAssassin     Posted 260 Days Ago
 
 
Nice article! Clearly explains and shows what buffer overflows are ;-)

I think you could improve the article though by adding an example of an exploit of
a buffer overflow. Just a suggestion.

Overall though i like it ;-)

Kind regards,
Scott
Page 1 of 1
More Articles By This Author
Buffer Overflow Protection
Recently Posted "Software Development" Articles
Software engineering <:: before we start::>
Planning: The First Step Towards Good Codeing
Use Cases
Buffer Overflow Protection
Versioning Your Application
Recently Rated "Software Development" Articles
Software engineering <:: before we start::>
Use Cases
Buffer Overflow Protection
Planning: The First Step Towards Good Codeing
Versioning Your Application
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Software Development
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved