Skip to content

Commit 47ad280

Browse files
authored
Merge pull request #842 from geoffw0/gets
CPP: Clean up PotentialBufferOverflow.ql, PotentiallyDangerousFunction.ql
2 parents b4b37b3 + 2321ae9 commit 47ad280

File tree

11 files changed

+51
-49
lines changed

11 files changed

+51
-49
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
| Lossy function result cast (`cpp/lossy-function-result-cast`) | Fewer false positive results | The whitelist of rounding functions built into this query has been expanded. |
2424
| Unused static variable (`cpp/unused-static-variable`) | Fewer false positive results | Variables with the attribute `unused` are now excluded from the query. |
2525
| Resource not released in destructor (`cpp/resource-not-released-in-destructor`) | Fewer false positive results | Fix false positives where a resource is released via a virtual method call, function pointer, or lambda. |
26+
| Use of inherently dangerous function (`cpp/potential-buffer-overflow`) | Cleaned up | This query no longer catches uses of `gets`, and has been renamed 'Potential buffer overflow'. |
27+
| Use of potentially dangerous function (`cpp/potentially-dangerous-function`) | More correct results | This query now catches uses of `gets`. |
2628

2729
## Changes to QL libraries
2830

cpp/ql/src/Likely Bugs/Memory Management/PotentialBufferOverflow.qhelp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>This rule highlights potentially overflowing calls to the functions <code>sprintf</code>, <code>vsprintf</code>, and <code>gets</code> with a warning.
7-
These functions allow unbounded writes to buffers, which may cause an overflow when used on untrusted data or without adequate checks on the size of the data. Function calls of this type constitute a security risk through buffer overflows. The <code>gets</code> function, in particular,
8-
is one of the vulnerabilities exploited by the Internet Worm of 1988, one of the first computer worms to spread through the Internet.</p>
6+
<p>This rule highlights potentially overflowing calls to the functions <code>sprintf</code> and <code>vsprintf</code> with a warning.
7+
These functions allow unbounded writes to buffers, which may cause an overflow when used on untrusted data or without adequate checks on the size of the data. Function calls of this type constitute a security risk through buffer overflows.</p>
98

109
</overview>
1110
<recommendation>
12-
<p>Always control the length of buffer copy and buffer write operations. Use the safer variants <code>snprintf</code>, <code>vsnprintf</code>, and <code>fgets</code>, which include an extra buffer length argument.</p>
11+
<p>Always control the length of buffer copy and buffer write operations. Use the safer variants <code>snprintf</code> and <code>vsnprintf</code>, which include an extra buffer length argument.</p>
1312

1413
</recommendation>
1514
<example>
@@ -18,7 +17,6 @@ is one of the vulnerabilities exploited by the Internet Worm of 1988, one of the
1817
<p>To improve the security of this example code, three changes should be made:</p>
1918
<ol>
2019
<li>Introduce a preprocessor define for the size of the buffer.</li>
21-
<li>Replace the call to <code>gets</code> with <code>fgets</code>, specifying the define as the maximum length to copy. This will prevent the buffer overflow.</li>
2220
<li>Replace both calls to <code>sprintf</code> with <code>snprintf</code>, specifying the define as the maximum length to copy. This will prevent the buffer overflow.</li>
2321
<li>Consider using the %g format specifier instead of %f.</li>
2422
</ol>
@@ -33,8 +31,6 @@ Standard: <a href="https://www.securecoding.cert.org/confluence/display/c/STR31-
3331
that storage for strings has sufficient space for character data and
3432
the null terminator</a>.</li>
3533
<li>M. Howard, D. Leblanc, J. Viega, <i>19 Deadly Sins of Software Security: Programming Flaws and How to Fix Them</i>, McGraw-Hill Osborne, 2005.</li>
36-
<li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Morris_worm">Morris worm</a>.</li>
37-
<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>
3834

3935

4036
</references>
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Use of inherently dangerous function
2+
* @name Potential buffer overflow
33
* @description Using a library function that does not check buffer bounds
44
* requires the surrounding program to be very carefully written
55
* to avoid buffer overflows.
@@ -8,31 +8,12 @@
88
* @problem.severity warning
99
* @tags reliability
1010
* security
11-
* external/cwe/cwe-242
11+
* external/cwe/cwe-676
1212
*/
1313
import cpp
1414
import semmle.code.cpp.commons.Buffer
1515

16-
abstract class PotentiallyDangerousFunctionCall extends FunctionCall {
17-
abstract predicate isDangerous();
18-
abstract string getDescription();
19-
}
20-
21-
class GetsCall extends PotentiallyDangerousFunctionCall {
22-
GetsCall() {
23-
this.getTarget().hasName("gets")
24-
}
25-
26-
override predicate isDangerous() {
27-
any()
28-
}
29-
30-
override string getDescription() {
31-
result = "gets does not guard against buffer overflow"
32-
}
33-
}
34-
35-
class SprintfCall extends PotentiallyDangerousFunctionCall {
16+
class SprintfCall extends FunctionCall {
3617
SprintfCall() {
3718
this.getTarget().hasName("sprintf") or this.getTarget().hasName("vsprintf")
3819
}
@@ -45,16 +26,16 @@ class SprintfCall extends PotentiallyDangerousFunctionCall {
4526
result = this.getArgument(1).(FormatLiteral).getMaxConvertedLength()
4627
}
4728

48-
override predicate isDangerous() {
29+
predicate isDangerous() {
4930
this.getMaxConvertedLength() > this.getBufferSize()
5031
}
5132

52-
override string getDescription() {
33+
string getDescription() {
5334
result = "This conversion may yield a string of length "+this.getMaxConvertedLength().toString()+
5435
", which exceeds the allocated buffer size of "+this.getBufferSize().toString()
5536
}
5637
}
5738

58-
from PotentiallyDangerousFunctionCall c
39+
from SprintfCall c
5940
where c.isDangerous()
6041
select c, c.getDescription()

cpp/ql/src/Security/CWE/CWE-676/DangerousUseOfCin.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @tags reliability
1010
* security
1111
* external/cwe/cwe-676
12-
* external/cwe/cwe-242
1312
*/
1413
import cpp
1514

cpp/ql/src/Security/CWE/CWE-676/PotentiallyDangerousFunction.qhelp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
<overview>
66
<p>This rule finds calls to functions that are dangerous to
77
use. Currently, it checks for calls
8-
to <code>gmtime</code>. See <strong>Related rules</strong>
8+
to <code>gets</code> and <code>gmtime</code>. See <strong>Related rules</strong>
99
below for rules that identify other dangerous functions.</p>
1010

11+
<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>
12+
1113
<p>The <code>gmtime</code> function fills data into a <code>tm</code>
1214
struct in shared memory and then returns a pointer to that struct. If
1315
the function is called from multiple places in the same program, and
@@ -17,7 +19,9 @@ then the calls will overwrite each other's data.</p>
1719
</overview>
1820
<recommendation>
1921

20-
<p>It is safer to use <code>gmtime_r</code>.
22+
<p>Replace calls to <code>gets</code> with <code>fgets</code>, specifying the maximum length to copy. This will prevent the buffer overflow.</p>
23+
24+
<p>Replace calls to <code>gmtime</code> with <code>gmtime_r</code>.
2125
With <code>gmtime_r</code>, the application code manages allocation of
2226
the <code>tm</code> struct. That way, separate calls to the function
2327
can use their own storage.</p>
@@ -48,7 +52,8 @@ rules for the following CWEs:</p>
4852

4953
</section>
5054
<references>
51-
52-
55+
<li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Morris_worm">Morris worm</a>.</li>
56+
<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>
57+
<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>
5358
</references>
5459
</qhelp>

cpp/ql/src/Security/CWE/CWE-676/PotentiallyDangerousFunction.ql

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
* @id cpp/potentially-dangerous-function
88
* @tags reliability
99
* security
10-
* external/cwe/cwe-676
10+
* external/cwe/cwe-242
1111
*/
1212
import cpp
1313

14-
15-
predicate dangerousFunction(Function function) {
16-
exists (string name | name = function.getQualifiedName() |
17-
name = "gmtime")
14+
predicate potentiallyDangerousFunction(Function f, string message) {
15+
(
16+
f.getQualifiedName() = "gmtime" and
17+
message = "Call to gmtime is potentially dangerous"
18+
) or (
19+
f.hasName("gets") and
20+
message = "gets does not guard against buffer overflow"
21+
)
1822
}
1923

2024

21-
from FunctionCall call, Function target
22-
where call.getTarget() = target
23-
and dangerousFunction(target)
24-
select call, "Call to " + target.getQualifiedName() + " is potentially dangerous"
25+
from FunctionCall call, Function target, string message
26+
where
27+
call.getTarget() = target and
28+
potentiallyDangerousFunction(target, message)
29+
select call, message

cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/PotentialBufferOverflow.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
| tests.cpp:112:9:112:12 | call to gets | gets does not guard against buffer overflow |
2-
| tests.cpp:249:2:249:5 | call to gets | gets does not guard against buffer overflow |
3-
| tests.cpp:250:2:250:5 | call to gets | gets does not guard against buffer overflow |
41
| tests.cpp:258:2:258:8 | call to sprintf | This conversion may yield a string of length 17, which exceeds the allocated buffer size of 10 |
52
| tests.cpp:259:2:259:8 | call to sprintf | This conversion may yield a string of length 17, which exceeds the allocated buffer size of 10 |
63
| tests.cpp:272:2:272:8 | call to sprintf | This conversion may yield a string of length 9, which exceeds the allocated buffer size of 8 |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| tests.cpp:112:9:112:12 | call to gets | gets does not guard against buffer overflow |
2+
| tests.cpp:249:2:249:5 | call to gets | gets does not guard against buffer overflow |
3+
| tests.cpp:250:2:250:5 | call to gets | gets does not guard against buffer overflow |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-676/PotentiallyDangerousFunction.ql
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
| test.c:28:22:28:27 | call to gmtime | Call to gmtime is potentially dangerous |
2+
| test.c:39:2:39:5 | call to gets | gets does not guard against buffer overflow |
3+
| test.c:40:6:40:9 | call to gets | gets does not guard against buffer overflow |

0 commit comments

Comments
 (0)