|
6 | 6 |
|
7 | 7 | <overview> |
8 | 8 | <p> |
9 | | -This rule finds <code>malloc</code> that use a <code>strlen</code> for the size but to not take the |
10 | | -zero terminator into consideration, and <code>strcat/strncat</code> calls that are done on buffers that do |
11 | | -not have the sufficient size to contain the new string. |
12 | | -</p> |
| 9 | +This query finds calls to:</p> |
| 10 | +<ul> |
| 11 | +<li><code>malloc</code> that use a <code>strlen</code> for the buffer size and do not take the |
| 12 | +zero terminator into consideration.</li> |
| 13 | +<li><code>strcat</code> or <code>strncat</code> that use buffers that are too small to contain the new string.</li> |
| 14 | +</ul> |
13 | 15 |
|
14 | 16 | <p> |
15 | | -The indicated expression will cause a buffer overflow due to a buffer that is of insufficient size to contain |
16 | | -the data being copied. Buffer overflows can result to anything from a segfault to a security vulnerability (particularly |
| 17 | +The highlighted expression will cause a buffer overflow because the buffer is too small to contain |
| 18 | +the data being copied. Buffer overflows can result to anything from a segmentation fault to a security vulnerability (particularly |
17 | 19 | if the array is on stack-allocated memory). |
18 | 20 | </p> |
19 | 21 |
|
20 | 22 | <include src="aliasAnalysisWarning.qhelp" /> |
21 | | - |
22 | 23 | </overview> |
| 24 | + |
23 | 25 | <recommendation> |
24 | 26 | <p> |
25 | 27 | Increase the size of the buffer being allocated. |
26 | 28 | </p> |
27 | | - |
28 | 29 | </recommendation> |
29 | | -<example><sample src="OverflowCalculated.cpp" /> |
30 | | - |
31 | 30 |
|
| 31 | +<example> |
| 32 | +<p>This example includes three annotated calls that copy a string into a buffer. |
| 33 | +The first call to <code>malloc</code> creates a buffer that's the |
| 34 | +same size as the string, leaving no space for the zero terminator |
| 35 | +and causing an overflow. The second call to <code>malloc</code> |
| 36 | +correctly calculates the required buffer size. The call to |
| 37 | +<code>strcat</code> appends an additional string to the same buffer |
| 38 | +causing a second overflow.</p> |
32 | 39 |
|
| 40 | +<sample src="OverflowCalculated.cpp" /> |
33 | 41 | </example> |
34 | | -<references> |
35 | 42 |
|
| 43 | +<references> |
36 | 44 | <li><a href="http://cwe.mitre.org/data/definitions/131.html">CWE-131: Incorrect Calculation of Buffer Size</a></li> |
37 | 45 | <li>I. Gerg. <em>An Overview and Example of the Buffer-Overflow Exploit</em>. IANewsletter vol 7 no 4. 2005.</li> |
38 | 46 | <li>M. Donaldson. <em>Inside the Buffer Overflow Attack: Mechanism, Method & Prevention</em>. SANS Institute InfoSec Reading Room. 2002.</li> |
39 | | - |
40 | 47 | </references> |
41 | 48 | </qhelp> |
0 commit comments