Coder Profile - Show off your skills, get a coder profile.
 
 
 
Buffer Overflow Protection
Software Development
CODE: Copy / Restore  ::  Remove Scroll Bars
  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  ::  Remove Scroll Bars
  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  ::  Remove Scroll Bars
  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: Copy / Restore  ::  Remove Scroll Bars
  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 2.50 Years 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 2.51 Years 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
Planning: The First Step Towards Good Codeing
Software engineering <:: before we start::>
Versioning Your Application
Use Cases
Buffer Overflow Protection
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (2)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (5)
Other (2)
Programming (47)
Security (10)
Software Development (5)
Web Development (14)
search Search Inside
Software Development
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
1.67 Year Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2010, Scott Thompson, All Rights Reserved