Re: safe strcpy()?

From: Crispin Cowan (crispin@wirex.com)
Date: 01/28/03

  • Next message: Jason Coombs: "RE: malicious code"
    Date: Mon, 27 Jan 2003 23:57:37 -0800
    From: Crispin Cowan <crispin@wirex.com>
    To: Ed Carp <erc@pobox.com>
    
    
    

    Ed Carp wrote:

    >start looking for a way to figure out how to determine how
    >much space was allocated to a string. I was rather surprised to find that
    >I couldn't find such an animal! Am I missing something here?
    >
    >Consider the simple case of:
    >
    >my_strcpy (char *to, char *from)
    >{
    >while(*from)
    >{
    >*to = *from;
    >to++;
    >from++;
    >}
    >return (from);
    >}
    >
    >The problem arises if the memory allocation for from exceeds that for to.
    >
    No, there is no way for the string copy function itself to determine the
    size of the destination buffer. All such computation has to be done at
    the call site, not in the copy function. This is an inherent safety
    limitation of the C language.

    There are two compiler enhancements for GCC that provide full bounds
    checking on arrays (Bounded Pointers
    <http://gcc.gnu.org/projects/bp/main.html>, and the other nameless
    project by Jones&Kelly
    <http://www.doc.ic.ac.uk/%7Ephjk/BoundsChecking.html>, and ten Bruggee
    <http://web.inter.nl.net/hcc/Haj.Ten.Brugge/>). However, IIRC, both of
    these enhancements just cause the program to die if the buffer
    overflows. The other response would be to just ignore writes to arrays
    beyond the bounds of the array, which is very likely to cause
    "surprising" incorrect behavior.

    More generally, you can read my survey of buffer overflow attacks and
    defenses here:

        "Buffer Overflows: Attacks and Defenses for the Vulnerability of
        the Decade". Crispin Cowan, Perry Wagle, Calton Pu, Steve Beattie,
        and Jonathan Walpole. DARPA Information Survivability Conference and
        Expo (DISCEX) <http://schafercorp-ballston.com/discex/>, Hilton Head
        Island SC, January 2000. Also presented as an invited talk at SANS
        2000 <http://www.sans.org/sans2000/sans2000.htm>, Orlando FL, March
        2000. PDF <http://wirex.com/%7Ecrispin/discex00.pdf>.

    It's now a little dated, in that PAX <http://pageexec.virtualave.net/>,
    libsafe <http://www.research.avayalabs.com/project/libsafe/>, and
    StackGhost <http://stackghost.cerias.purdue.edu/> came out since I wrote
    that paper. A more recent and comprehensive survey of open source
    security will appear shortly in the new IEEE Security&Privacy Magazine
    <http://www.computer.org/security/>.

    Oh yeah, and there's StackGuard <http://immunix.org/stackguard.html> :-)

    Crispin

    -- 
    Crispin Cowan, Ph.D.
    Chief Scientist, WireX                      http://wirex.com/~crispin/
    Security Hardened Linux Distribution:       http://immunix.org
    Available for purchase: http://wirex.com/Products/Immunix/purchase.html
    			    Just say ".Nyet"
    
    




    Relevant Pages

    • Re: <CR> into string
      ... # I would like to insert which is 0x0D into a string at certain ... Originally libc functions did not call malloc internally, using arrays ... any library to do so would need a way to pass allocation ... There are many nonstandard libraries that are available on most ...
      (comp.lang.c)
    • Re: Is PSHUFW instruction MMX or SSE or SSE2? Is NASM manual correct?
      ... Then it would do a "first fit" search of the FAQE chain. ... allocation, and update the preceding FAQE to point to the ... MOVSTR with the target string address in SI, ...
      (alt.lang.asm)
    • Re: Strings in C are less optimal than in (say) Pascal - correct?
      ... >provided a string type with these 3 parts: ... I have a library of macros and functions that I use ... a vector of char can be used as a string. ... Here are the macros that access the size and allocation (I'm assuming ...
      (comp.lang.c)
    • Re: TStreams
      ... A string is an array of characters with the first character ... TMyRec = packed record ... FS.Seek(0, soFromBeginning); ... Note the protection of the resource allocation by a try / finally / end ...
      (comp.lang.pascal.delphi.misc)
    • Re: TStreams
      ... A string is an array of characters with the first character ... TMyRec = packed record ... FS.Seek(0, soFromBeginning); ... Note the protection of the resource allocation by a try / finally / end ...
      (comp.lang.pascal.delphi.misc)