Skip to content

Commit 97f1a5b

Browse files
authored
C++: add VeryLikelyOverrunWrite.qhelp
1 parent 10b6215 commit 97f1a5b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int sayHello(uint32_t userId)
2+
{
3+
char buffer[17];
4+
5+
if (userId > 9999) return USER_ID_OUT_OF_BOUNDS;
6+
7+
// BAD: this message overflows the buffer if userId >= 1000,
8+
// as no space for the null terminator was accounted for
9+
sprintf(buffer, "Hello, user %d!", userId);
10+
11+
MessageBox(hWnd, buffer, "New Message", MB_OK);
12+
13+
return SUCCESS;
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>The program performs a buffer copy or write operation with no upper limit on the size of the copy, and by analysing the bounds of the expressions involved it appears that certain inputs will cause a buffer overflow to occur in this case. In addition to causing program instability, techniques exist which may allow an attacker to use this vulnerability to execute arbitrary code.</p>
7+
8+
</overview>
9+
<recommendation>
10+
<p>Always control the length of buffer copy and buffer write operations. <code>strncpy</code> should be used over <code>strcpy</code>, <code>snprintf</code> over <code>sprintf</code>, and in other cases 'n-variant' functions should be preferred.</p>
11+
12+
</recommendation>
13+
<example>
14+
<sample src="OverrunWrite.c" />
15+
16+
<p>In this example, the call to <code>sprintf</code> writes a message of 14 characters (including the terminating null) plus the length of the string conversion of `userId` into a buffer with space for just 17 characters. While `userId` is checked to occupy no more than 4 characters when converted, there is no space in the buffer for the terminating null character if `userId >= 1000`. In this case, the null character overflows the buffer resulting in undefined behavior.</p>
17+
18+
<p>To fix this issue these changes should be made:</p>
19+
<ul>
20+
<li>Preferably, replace the call to <code>sprintf</code> with <code>snprintf</code>, specifying a define or `sizeof(buffer)` as maximum length to copy. This will prevent the buffer overflow.</li>
21+
<li>Increasing the buffer size to account for the full range of `userId` and the terminating null character.</li>
22+
</ul>
23+
24+
</example>
25+
<references>
26+
27+
<li>CERT C Coding Standard: <a href="https://www.securecoding.cert.org/confluence/display/c/STR31-C.+Guarantee+that+storage+for+strings+has+sufficient+space+for+character+data+and+the+null+terminator">STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator</a>.</li>
28+
29+
30+
<!-- LocalWords: preprocessor CWE STR
31+
-->
32+
33+
</references>
34+
</qhelp>

0 commit comments

Comments
 (0)