Skip to content

Commit 241ef3c

Browse files
authored
Merge pull request #1315 from geoffw0/ctime
CPP: Split PotentiallyDangerousFunction.ql
2 parents 884ef4c + 6c267f4 commit 241ef3c

File tree

12 files changed

+86
-29
lines changed

12 files changed

+86
-29
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| `()`-declared function called with too few arguments (`cpp/too-few-arguments`) | Correctness | Find all cases where the number of arguments is less than the number of parameters of the function, provided the function is also properly declared/defined elsewhere. |
1010
| `()`-declared function called with mismatched arguments (`cpp/mismatched-function-arguments`) | Correctness | Find all cases where the types of arguments do not match the types of parameters of the function, provided the function is also properly declared/defined elsewhere. |
1111
| Call to alloca in a loop (`cpp/alloca-in-loop`) | reliability, correctness, external/cwe/cwe-770 | Finds calls to `alloca` in loops, which can lead to stack overflow if the number of iterations is large. Newly displayed on LGTM. |
12+
| Use of dangerous function (`dangerous-function-overflow`) | reliability, security, external/cwe/cwe-242 | Finds calls to `gets`, which does not guard against buffer overflow. These results were previously detected by the `cpp/potentially-dangerous-function` query. |
1213

1314
## Changes to existing queries
1415

@@ -28,6 +29,7 @@
2829
| Wrong type of arguments to formatting function (`cpp/wrong-type-format-argument`) | More correct results and fewer false positive results | This query now more accurately identifies wide and non-wide string/character format arguments on different platforms. Platform detection has also been made more accurate for the purposes of this query. |
2930
| Wrong type of arguments to formatting function (`cpp/wrong-type-format-argument`) | Fewer false positive results | Non-standard uses of %L are now understood. |
3031
| `()`-declared function called with too many arguments (`cpp/futile-params`) | Improved coverage | Query has been generalized to find all cases where the number of arguments exceedes the number of parameters of the function, provided the function is also properly declared/defined elsewhere. |
32+
| Use of potentially dangerous function (`cpp/potentially-dangerous-function`) | Fewer results | Results relating to the standard library `gets` function have been moved into a new query (`dangerous-function-overflow`). |
3133

3234
## Changes to QL libraries
3335
- The predicate `Declaration.hasGlobalName` now only holds for declarations that are not nested in a class. For example, it no longer holds for a member function `MyClass::myFunction` or a constructor `MyClass::MyClass`, whereas previously it would classify those two declarations as global names.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#define BUFFERSIZE (1024)
2+
3+
// BAD: using gets
4+
void echo_bad() {
5+
char buffer[BUFFERSIZE];
6+
gets(buffer);
7+
printf("Input was: '%s'\n", buffer);
8+
}
9+
10+
// GOOD: using fgets
11+
void echo_good() {
12+
char buffer[BUFFERSIZE];
13+
fgets(buffer, BUFFERSIZE, stdin);
14+
printf("Input was: '%s'\n", buffer);
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>This rule finds calls to the <code>gets</code> function, which is dangerous and
7+
should not be used. See <strong>Related
8+
rules</strong> below for rules that identify other dangerous functions.</p>
9+
10+
<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>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>Replace calls to <code>gets</code> with <code>fgets</code>, specifying the maximum length to copy. This will prevent the buffer overflow.</p>
16+
17+
</recommendation>
18+
<example>
19+
<p>The following example gets a string from standard input in two ways:</p>
20+
<sample src="DangerousUseOfGets" />
21+
22+
<p>The first version uses <code>gets</code> and will overflow if the input
23+
is longer than the buffer. The second version of the code
24+
uses <code>fgets</code> and will not overflow, because the amount of data
25+
written is limited by the length parameter.</p>
26+
</example>
27+
<section title="Related rules">
28+
<p>Other dangerous functions identified by CWE-676 ("Use of
29+
Potentially Dangerous Function") include <code>strcpy</code>
30+
and <code>strcat</code>. Use of these functions is highlighted by
31+
rules for the following CWEs:</p>
32+
<ul>
33+
<li><a href="https://cwe.mitre.org/data/definitions/120.html">CWE-120 Classic Buffer Overflow</a>.
34+
</li><li><a href="https://cwe.mitre.org/data/definitions/131.html">CWE-131 Incorrect Calculation of Buffer Size</a>.
35+
</li></ul>
36+
37+
</section>
38+
<references>
39+
<li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Morris_worm">Morris worm</a>.</li>
40+
<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>
41+
</references>
42+
</qhelp>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Use of dangerous function
3+
* @description Use of a standard library function that does not guard against buffer overflow.
4+
* @kind problem
5+
* @problem.severity error
6+
* @precision very-high
7+
* @id cpp/dangerous-function-overflow
8+
* @tags reliability
9+
* security
10+
* external/cwe/cwe-242
11+
*/
12+
import cpp
13+
14+
from FunctionCall call, Function target
15+
where
16+
call.getTarget() = target and
17+
target.hasGlobalName("gets")
18+
select call, "gets does not guard against buffer overflow"

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
<overview>
66
<p>This rule finds calls to functions that are dangerous to
77
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>
8+
to <code>gmtime</code>, <code>localtime</code>,
9+
<code>ctime</code> and <code>asctime</code>.
1310

1411
<p>The time related functions such as <code>gmtime</code>
1512
fill data into a <code>tm</code> struct or <code>char</code> array in
@@ -21,8 +18,6 @@ then the calls will overwrite each other's data.</p>
2118
</overview>
2219
<recommendation>
2320

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-
2621
<p>Replace calls to <code>gmtime</code> with <code>gmtime_r</code>.
2722
With <code>gmtime_r</code>, the application code manages allocation of
2823
the <code>tm</code> struct. That way, separate calls to the function
@@ -47,20 +42,7 @@ struct on every call, it is immune to other calls to <code>gmtime</code>
4742
or <code>gmtime_r</code>.</p>
4843

4944
</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>
6145
<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>
6446
<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>
6547
</references>
6648
</qhelp>

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* @name Use of potentially dangerous function
3-
* @description Certain standard library functions are dangerous to call.
3+
* @description Use of a standard library function that is not thread-safe.
44
* @kind problem
5-
* @problem.severity error
5+
* @problem.severity warning
66
* @precision high
77
* @id cpp/potentially-dangerous-function
88
* @tags reliability
99
* security
10-
* external/cwe/cwe-242
10+
* external/cwe/cwe-676
1111
*/
1212
import cpp
1313

@@ -20,9 +20,6 @@ predicate potentiallyDangerousFunction(Function f, string message) {
2020
name = "asctime"
2121
) and
2222
message = "Call to " + name + " is potentially dangerous"
23-
) or (
24-
f.hasGlobalName("gets") and
25-
message = "gets does not guard against buffer overflow"
2623
)
2724
}
2825

cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/PotentiallyDangerousFunction.expected renamed to cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/DangerousFunctionOverflow.expected

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-676/DangerousFunctionOverflow.ql

cpp/ql/test/query-tests/Security/CWE/CWE-242/semmle/tests/PotentiallyDangerousFunction.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:42:2:42:5 | call to gets | gets does not guard against buffer overflow |
2+
| test.c:43:6:43:9 | call to gets | gets does not guard against buffer overflow |

0 commit comments

Comments
 (0)