Thread: When your code crashes

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    When your code crashes

    What happens when you write code In C and your computer crashes. I run xp and i get that msg box that tells me this program has perforemed an illegal and must shut down.

    I'm wonderign because I heard that In C when your code isn't tight, it can write to parts of the memory its not supposed to. Does this mean It can erase data in certain parts of your memory and if so which part. Can it overite parts already in use? Can you lose data. Stuff like that.

    I know In linux your compiler will tell you it has a segmentation fault and won't allow you to do continue. So I was just wondering.

    On the same note. Playing around with '\a' in a line of C code one time.
    actaully seemed to changed the voulum prompt on my box. It never went away until i installed XP again.

    So in closing. C is a dangerous language, but just how can it compromise your system???
    Thanks
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It depends on the operating system. If it runs in protected mode (XP, NT, Linux, etc) then nothing horrible will happen as the OS shuts your program down before any changes were made. In an unprotected mode OS well then just about anything could happen.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    the compiler doesn't warn you about seg faults. The OS does. In linux, the os will realize what is happening send a signal to stop the program before the bad stuff happens.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> What happens when you write code In C and your computer crashes. I run xp and i get that msg box that tells me this program has perforemed an illegal and must shut down. <<

    You might be interested to know that you can type drwtsn32 in the run box to get more information.

    >> I'm wonderign because I heard that In C when your code isn't tight, it can write to parts of the memory its not supposed to. <<

    Yep, it can do that. However, in NT/2000/XP it can typically only hit memory belonging to the current process. But see below.

    >> Does this mean It can erase data in certain parts of your memory and if so which part. Can it overite parts already in use? Can you lose data. Stuff like that. <<

    Yes, all of that can and does happen. Just typically not totally by accident. Putting crafted data in a buffer overflow can often allow a user to execute the code of their choice. This includes installing spyware, deleting your files, acting as a spam zombie, attacking other computers or simply formatting your harddisk.

    >> So in closing. C is a dangerous language, but just how can it compromise your system??? <<

    Yes C can be an extremely dangerous language. Thankfully however, it is fairly difficult to unknowingly cause damage when writing a program that you use on your own machine. But install a network enabled program on a few million machines and you can be guaranteed that buffer overruns are going to be exploited in a not so nice way.

    All of this applies to nix as much as windows. A segmentation fault or the windows equivalent, an access denied exception(code 5) should be a flag that there is a potential security issue in your program.
    Last edited by anonytmouse; 06-19-2004 at 02:46 PM.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    That is some scary stuff

    anyone have any links about info like this. Like how to write secure code, how a user can put injections of their own in your code and do damage to you os and such? I'd be really interested to know about it.

    should I back up my hd when practicing C then. Just in Case? I'm only half joking, seriously should I?
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you should start by reading this
    http://www.dwheeler.com/secure-programs/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by caroundw5h
    That is some scary stuff

    anyone have any links about info like this. Like how to write secure code, how a user can put injections of their own in your code and do damage to you os and such? I'd be really interested to know about it.

    should I back up my hd when practicing C then. Just in Case? I'm only half joking, seriously should I?
    Writing safe C applications is really hard and you can almost be sure to make a few mistakes here and there. The best protection is probably to have others look through the code.
    Most bufferoverflow attacks use forged strings that lack a terminating \0. This means the function reading in the string will continue to write data behind the buffers' reserved space on the stack. The first thing it will overwrite is the functions return address. An attacker will try to overwrite the address with one that points on the stack itself where the malicious string is located (or i.e. if it's a login function, will probably let it point to a location after login). After the data is read in the function will continue, but not return to where it should.

    Of course, in reality it is not that easy. The base stack address depends on a few other factors, i.e. environment variables, the programs path/parameterlist and others. If there is enough space on the stack, an attacker can overcome this by prefixing the malicious code with a lot of noop operations.

    Depending on your OS, it might help to use allocated memory. It's location is more random and it is unlikely to come anywhere close to the desired return address.
    Also, try not to use potentially dangerous functions like strcpy on unreliable data (i.e. data from the network). Also note that not even strncpy is safe. It only guarantees the input to be truncated after n bytes - it will however not append a terminating \0 if the input was longer than n. This cannot be exploitet to run attacker code, but it can be used to shut down the attacked network service (the service will segfault on Linux boxes, possibly eating a lot of memory before dying). This would be a DOS attack. Defense against it is not easy, but there is software available that protects your stack either by makeing sure that the memory location of the stack is marked as non-executable or by randomizing the location of the stack. So it's still possible to attack and bring down the application, but not to execute malicious code.
    Last edited by Nyda; 06-19-2004 at 03:29 PM.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Start here (more articles in the contents on the left, the article on integer manipulation should be read by all programmers).

    Use <strsafe.h> instead of str*, sprintf, etc.

    Read tutorials.

    Read security FAQs.

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    of course strsafe.h is windows specific

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks a lot for the input guys. it'll come in helpful.
    Start here (more articles in the contents on the left, the article on integer manipulation should be read by all programmers).
    Its funny to see microsoft advocate safe coding practices. I'm not bashing them I'm sure its just their haste to get their products to market faster. seriously.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  2. code crashes on Solaris
    By watcher in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 07:58 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2002, 12:28 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM