Skip to content

Commit 0476b97

Browse files
authored
Merge pull request github#3789 from dilanbhalla/cpp
C++ Memory Unsafe Functions
2 parents 1f432dc + 7bd5464 commit 0476b97

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
///// Library routines /////
2+
3+
int scanf(const char *format, ...);
4+
int sscanf(const char *str, const char *format, ...);
5+
int fscanf(const char *str, const char *format, ...);
6+
7+
///// EXAMPLES /////
8+
9+
int main(int argc, char **argv)
10+
{
11+
12+
// BAD, do not use scanf without specifying a length first
13+
char buf1[10];
14+
scanf("%s", buf1);
15+
16+
// GOOD, length is specified. The length should be one less than the size of the buffer, since the last character is the NULL terminator.
17+
char buf2[10];
18+
sscanf(buf2, "%9s");
19+
20+
// BAD, do not use scanf without specifying a length first
21+
char file[10];
22+
fscanf(file, "%s", buf2);
23+
24+
return 0;
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>It is bad practice to use any of the <code>scanf</code> functions without including a specified length within the format parameter, as it will be vulnerable to buffer overflows.</p>
7+
8+
</overview>
9+
10+
<recommendation>
11+
12+
<p>Specify a length within the format string parameter, and make this length one less than the size of the buffer, since the last character should be reserved for the NULL terminator.</p>
13+
14+
</recommendation>
15+
16+
<example>
17+
<p>The following example demonstrates safe and unsafe uses of <code>scanf</code> type functions.</p>
18+
<sample src="MemoryUnsafeFunctionScan.cpp" />
19+
20+
</example>
21+
22+
<references>
23+
</references>
24+
25+
</qhelp>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Scanf function without a specified length
3+
* @description Use of one of the scanf functions without a specified length.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id cpp/memory-unsafe-function-scan
7+
* @tags reliability
8+
* security
9+
* external/cwe/cwe-120
10+
*/
11+
12+
import cpp
13+
import semmle.code.cpp.commons.Scanf
14+
15+
from FunctionCall call, ScanfFunction sff
16+
where
17+
call.getTarget() = sff and
18+
call.getArgument(sff.getFormatParameterIndex()).getValue().regexpMatch(".*%l?s.*")
19+
select call, "Dangerous use of one of the scanf functions"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
///// Library routines /////
2+
3+
int scanf(const char *format, ...);
4+
int sscanf(const char *str, const char *format, ...);
5+
int fscanf(const char *str, const char *format, ...);
6+
7+
///// Test code /////
8+
9+
int main(int argc, char **argv)
10+
{
11+
12+
// BAD, do not use scanf without specifying a length first
13+
char buf1[10];
14+
scanf("%s", buf1);
15+
16+
// GOOD, length is specified
17+
char buf2[10];
18+
sscanf(buf2, "%9s");
19+
20+
// BAD, do not use scanf without specifying a length first
21+
char file[10];
22+
fscanf(file, "%s", buf2);
23+
24+
return 0;
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| MemoryUnsafeFunctionScan.cpp:14:5:14:9 | call to scanf | Dangerous use of one of the scanf functions |
2+
| MemoryUnsafeFunctionScan.cpp:22:5:22:10 | call to fscanf | Dangerous use of one of the scanf functions |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-120/MemoryUnsafeFunctionScan.ql

0 commit comments

Comments
 (0)