Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f80dea

Browse files
committed
CPP: Clone PotentiallyDangerousFunction query as DangerousUseOfGets.
1 parent 574a1d8 commit 1f80dea

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// BAD: using gmtime
2+
int is_morning_bad() {
3+
const time_t now_seconds = time(NULL);
4+
struct tm *now = gmtime(&now_seconds);
5+
return (now->tm_hour < 12);
6+
}
7+
8+
// GOOD: using gmtime_r
9+
int is_morning_good() {
10+
const time_t now_seconds = time(NULL);
11+
struct tm now;
12+
gmtime_r(&now_seconds, &now);
13+
return (now.tm_hour < 12);
14+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @name Use of potentially dangerous function
3+
* @description Certain standard library functions are dangerous to call.
4+
* @kind problem
5+
* @problem.severity error
6+
* @precision high
7+
* @id cpp/potentially-dangerous-function
8+
* @tags reliability
9+
* security
10+
* external/cwe/cwe-242
11+
*/
12+
import cpp
13+
14+
predicate potentiallyDangerousFunction(Function f, string message) {
15+
exists(string name | name = f.getQualifiedName() |
16+
(
17+
name = "gmtime" or
18+
name = "localtime" or
19+
name = "ctime" or
20+
name = "asctime"
21+
) and
22+
message = "Call to " + name + " is potentially dangerous"
23+
) or (
24+
f.getQualifiedName() = "gets" and
25+
message = "gets does not guard against buffer overflow"
26+
)
27+
}
28+
29+
30+
from FunctionCall call, Function target, string message
31+
where
32+
call.getTarget() = target and
33+
potentiallyDangerousFunction(target, message)
34+
select call, message

0 commit comments

Comments
 (0)