|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | +<overview> |
| 6 | +<p>This rule finds calls to functions that are dangerous to |
| 7 | +use. Currently, it checks for calls |
| 8 | +to <code>gets</code>, <code>gmtime</code>, <code>localtime</code>, |
| 9 | +<code>ctime</code> and <code>asctime</code>. See <strong>Related |
| 10 | +rules</strong> below for rules that identify other dangerous functions.</p> |
| 11 | + |
| 12 | +<p>The <code>gets</code> function is one of the vulnerabilities exploited by the Internet Worm of 1988, one of the first computer worms to spread through the Internet. The <code>gets</code> function provides no way to limit the amount of data that is read and stored, so without prior knowledge of the input it is impossible to use it safely with any size of buffer.</p> |
| 13 | + |
| 14 | +<p>The time related functions such as <code>gmtime</code> |
| 15 | +fill data into a <code>tm</code> struct or <code>char</code> array in |
| 16 | +shared memory and then returns a pointer to that memory. If |
| 17 | +the function is called from multiple places in the same program, and |
| 18 | +especially if it is called from multiple threads in the same program, |
| 19 | +then the calls will overwrite each other's data.</p> |
| 20 | + |
| 21 | +</overview> |
| 22 | +<recommendation> |
| 23 | + |
| 24 | +<p>Replace calls to <code>gets</code> with <code>fgets</code>, specifying the maximum length to copy. This will prevent the buffer overflow.</p> |
| 25 | + |
| 26 | +<p>Replace calls to <code>gmtime</code> with <code>gmtime_r</code>. |
| 27 | +With <code>gmtime_r</code>, the application code manages allocation of |
| 28 | +the <code>tm</code> struct. That way, separate calls to the function |
| 29 | +can use their own storage.</p> |
| 30 | + |
| 31 | +<p>Similarly replace calls to <code>localtime</code> with |
| 32 | +<code>localtime_r</code>, calls to <code>ctime</code> with |
| 33 | +<code>ctime_r</code> and calls to <code>asctime</code> with |
| 34 | +<code>asctime_r</code>.</p> |
| 35 | + |
| 36 | +</recommendation> |
| 37 | +<example> |
| 38 | +<p>The following example checks the local time in two ways:</p> |
| 39 | +<sample src="PotentiallyDangerousFunction.c" /> |
| 40 | + |
| 41 | +<p>The first version uses <code>gmtime</code>, so it is vulnerable to |
| 42 | +its data being overwritten by another thread. Even if this code is not |
| 43 | +used in a multi-threaded context right now, future changes may |
| 44 | +make the program multi-threaded. The second version of the code |
| 45 | +uses <code>gmtime_r</code>. Since it allocates a new <code>tm</code> |
| 46 | +struct on every call, it is immune to other calls to <code>gmtime</code> |
| 47 | +or <code>gmtime_r</code>.</p> |
| 48 | + |
| 49 | +</example> |
| 50 | +<section title="Related rules"> |
| 51 | +<p>Other dangerous functions identified by CWE-676 ("Use of |
| 52 | +Potentially Dangerous Function") include <code>strcpy</code> |
| 53 | +and <code>strcat</code>. Use of these functions is highlighted by |
| 54 | +rules for the following CWEs:</p> |
| 55 | +<ul> |
| 56 | +<li>CWE-120 Classic Buffer Overflow |
| 57 | +</li><li>CWE-131 Incorrect Calculation of Buffer Size |
| 58 | +</li></ul> |
| 59 | + |
| 60 | +</section> |
| 61 | +<references> |
| 62 | +<li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Morris_worm">Morris worm</a>.</li> |
| 63 | +<li>E. Spafford. <i>The Internet Worm Program: An Analysis</i>. Purdue Technical Report CSD-TR-823, <a href="http://www.textfiles.com/100/tr823.txt">(online)</a>, 1988.</li> |
| 64 | +<li>SEI CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/CON33-C.+Avoid+race+conditions+when+using+library+functions">CON33-C. Avoid race conditions when using library functions</a>.</li> |
| 65 | +</references> |
| 66 | +</qhelp> |
0 commit comments