    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Buffer Overflows and the Standard C Library</title>
        <link>https://members.accu.org/index.php/journals/979</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 12, #2 - Mar 2000 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c127/">122</a>
                    (18)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c127-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c127+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Buffer Overflows and the Standard C Library</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 March 2000 13:15:35 +00:00 or Fri, 03 March 2000 13:15:35 +00:00</p>
<p><strong>Summary:</strong>&nbsp;<p>In C Vu 12.1 (page 19), Edward Collier reasonably asked what was wrong with scanf() and fscanf(). The short answer is that they are two of several Standard C Library functions which fail to honour buffer bounds.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a></h2>
</div>
<p>In C Vu 12.1 (page 19), Edward Collier reasonably asked what was
wrong with scanf() and fscanf(). The short answer is that they are
two of several Standard C Library functions which fail to honour
buffer bounds. The following functions have this failing:
<tt class="function">gets()</tt>, <tt class=
"function">strcpy()</tt>, <tt class="function">strcat()</tt>,
<tt class="function">sprintf()</tt>, <tt class=
"function">fscanf()</tt>, <tt class="function">scanf()</tt>,
<tt class="function">vsprintf()</tt> and <tt class=
"function">strxfrm()</tt>.</p>
<p>Buffer overflows are caused by programs allocating blocks of
memory and then trying to put too much data into them.</p>
<p>At the very least, a buffer overflow may result in garbage being
written over other data structures and code, causing corruption and
or program failure. In the worst case, deliberately created
overflows can be used to acquire administrative privileges, if the
program itself runs with elevated privileges. Buffer overflows are
a major source of problems with software today and are responsible
for a significant percentage of the exploitable security problems.
A great deal of sysadmin time is spent applying fixes to these
problems (assuming that the author or vendor of the code will
support it).</p>
<p>This article was intended to be part of a series on writing
secure code. Anything that makes code more secure will in general
make the code better and more reliable. I certainly wouldn't put
myself forward as a C programming expert, but I am paid to know
about computer security issues and I know enough about the coding
to know what causes problems.</p>
<p>There are fundamentally two approaches to the problem.</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Allocate buffers that are larger than necessary and hope that
they never overflow. This is not always a defence. For example,
hackers found a way of exploiting a buffer overflow in IIS by using
an 8000 character URL, even though you would not expect anyone to
type in a URL of anything approaching that length. This approach is
wasteful of resources and only masks the problem -as we have seen,
it does not solve the problem.</p>
</li>
<li>
<p>Use alternative functions or use the functions in such a way as
to reduce the risk.</p>
</li>
</ol>
</div>
<p>Sometimes there are alternative ways of using the Standard C
Library to achieve the same functionality given by the more
dangerous functions. However, the programmer has to be fully aware
of the subtle differences between a function and its
replacement:</p>
<pre class="programlisting">
gets() vs fgets()
char *gets(char *s)
char *fgets(char *s, int maxsize, FILE *fp)
</pre>
<p><tt class="function">gets()</tt> will copy the input line into
the array and replace the terminating '<tt class="literal">\n</tt>'
with '<tt class="literal">\0</tt>'. The normally recommended
replacement is <tt class="function">fgets()</tt>, which reads at
most, <tt class="literal">maxsize-1</tt> characters from a file
into the array. Note however that the newline character '<tt class=
"literal">\n</tt>' is included in the array and then the array is
terminated with '<tt class="literal">\0</tt>'. While <tt class=
"function">fgets()</tt> will prevent a buffer overflow, the
programmer now has to decide what will happen if the input data has
been truncated. Whether it is important depends on the program. For
example a user may intended to delete the directory <tt class=
"filename">C:\winnt\system32\backup</tt>, but if this is truncated
to <tt class="filename">C:\winnt\system32</tt>, the result will be
very different. This is a somewhat extreme example - but something
equally catastrophic could happen. A simple example of a problem
with <tt class="function">gets()</tt> is shown by The Harpist in C
Vu 12.1 page 23 (leaving to one side the issue of whether the
storage pointed to by reply should be modifiable).</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e103" id="d0e103"></a>strcpy() vs
strncpy</h2>
</div>
<pre class="programlisting">
char *strcpy(char *s, char const *c)
char *strncpy(char *s, char const *c, size_t n)
</pre>
<p><tt class="function">strcpy()</tt> will copy string <i class=
"parameter"><tt>c</tt></i> into string <i class=
"parameter"><tt>s</tt></i> including the terminating '<tt class=
"literal">\0</tt>'. Note that no checks are made on the buffer size
pointed to by <i class="parameter"><tt>s</tt></i>. The normally
recommended replacement is <tt class="function">strncpy</tt> which
will copy at most <i class="parameter"><tt>n</tt></i> characters
from <i class="parameter"><tt>c</tt></i> into <i class=
"parameter"><tt>s</tt></i>. <i class="parameter"><tt>s</tt></i> is
padded with '<tt class="literal">\0</tt>' if <i class=
"parameter"><tt>c</tt></i> has fewer than <i class=
"parameter"><tt>n</tt></i> characters. There is a subtle feature
here waiting to trap the unwary. If the characters from <i class=
"parameter"><tt>c</tt></i> exactly fill <i class=
"parameter"><tt>s</tt></i>, then no terminating '<tt class=
"literal">\0</tt>' will be appended. If the contents of s are
passed to any function or routine expecting to see a standard
nul-terminated C string, the routine will look beyond the end of
<i class="parameter"><tt>s</tt></i> (and into an arbitrary location
in memory) until a <tt class="literal">nul</tt> character is found.
A commonly adopted solution is to force a <tt class=
"literal">nul</tt> character into the final location.</p>
<pre class="programlisting">
#define BUFSIZE 80
char s[BUFSIZE];
strncpy{s,c,BUFSIZE);
s[BUFSIZE-1] = '\0';
</pre>
<p>This is OK as far as it goes, but it does have the same problem
with truncation described above. People have pointed out that the
action of <tt class="function">strncpy()</tt> to fill an overlarge
buffer with '<tt class="literal">\0</tt>' may be time consuming.
Unless the buffer is huge, this is unlikely to be a problem on most
modern systems.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e176" id="d0e176"></a>strcat() vs
strncat()</h2>
</div>
<pre class="programlisting">
char *strcat(char *s, const char *c)
char *strncat(char*s, const char *c, size_t n)
</pre>
<p><tt class="function">strcat()</tt> will append the string
<i class="parameter"><tt>c</tt></i> to string <i class=
"parameter"><tt>s</tt></i>, without checking if there is space. The
problems seen with <tt class="function">strcpy()</tt> are seen with
<tt class="function">strcat()</tt>. <tt class=
"function">strncat()</tt> will copy at most <i class=
"parameter"><tt>n</tt></i> characters from <i class=
"parameter"><tt>c</tt></i> to the end of <i class=
"parameter"><tt>s</tt></i> and will terminate it with '<tt class=
"literal">\0</tt>'. Again, the problems with <tt class=
"function">strncat()</tt> are like those of <tt class=
"function">strncpy()</tt>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e218" id="d0e218"></a>scanf() and
fscanf()</h2>
</div>
<p><tt class="function">scanf()</tt> is really just <tt class=
"function">fscanf()</tt> using <tt class="filename">stdin</tt> as
the source file. Bjarne Stroustrup explains what the problem with
these functions are and how to code around them in C Vu 12.1 page
20, so there is little point in repeating that information here.
The point to note about the expert-level solution presented in that
article is the use of a fixed length buffer in the <tt class=
"function">sprintf()</tt> function. It has to be oversized to allow
for the fact that a very large number may be used here (e.g.
<tt class="literal">MAXINT</tt>). <tt class=
"function">sprintf()</tt> does the same formatting of arguments as
<tt class="function">printf()</tt>, but stores the output in a
string rather than to <tt class="filename">stdout</tt>. No checks
are done on the size of the output string buffer by <tt class=
"function">sprintf()</tt>, so once more there is an opportunity for
a buffer overflow for the unwary. <tt class=
"function">sprintf()</tt> is problematic because there is no
portable way of replicating its functionality with buffer bound
checking.</p>
<p>It is worth remembering that other &quot;standard&quot; code might have
problems. Consider for example the <tt class=
"function">getopts()</tt> function, which although not part of the
standard library is available in most installations as a standard
method of parsing command line arguments. This may have been
written to be safe - but have you checked it?</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e257" id=
"d0e257"></a>Conclusions</h2>
</div>
<p>Unless you have taken action to check the input into a fixed
size buffer, you will have problems. The problems may not emerge
for sometime.</p>
<p>Generous use of <tt class="function">strlen()</tt> to check the
length of strings before copying or concatenating them will reduce
the number of problems experienced. <tt class=
"function">realloc()</tt> is a much underused function, but may be
used where unknown amounts of input data may be experienced. An
example of its use can be found in <tt class=
"function">checkISNB2</tt> and <tt class="function">checkISBN3</tt>
on the code disk for C Vu 11.6 (or in the FTP archive).</p>
<p>The &quot;dangerous&quot; functions discussed above can be used. The
programmer needs to be aware of the risks and how to minimise them
and then make a considered judgement. Because they do few checks
they tend to be swift and efficient - but then so is a cutthroat
razor.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
