Skip to content

Commit 3fdd11a

Browse files
author
dilanbhalla
committed
scanf fixes, still need to update qhelp file
1 parent 0552f9b commit 3fdd11a

File tree

9 files changed

+49
-69
lines changed

9 files changed

+49
-69
lines changed

cpp/ql/src/Security/CWE/CWE-120/MemoryUnsafeFunctionScan.qhelp

Lines changed: 0 additions & 22 deletions
This file was deleted.

cpp/ql/src/Security/CWE/CWE-120/MemoryUnsafeFunctionScan.ql

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 scanf functions without including a specified length within the format parameter, as it will be vulnerable to buffer overflows.</p>
7+
8+
</overview>
9+
10+
<references>
11+
<li>https://cwe.mitre.org/data/definitions/120</li>
12+
<!-- LocalWords: CWE
13+
-->
14+
</references>
15+
16+
</qhelp>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
* @precision medium
7+
* @id cpp/memory-unsafe-function-scan
8+
* @tags reliability
9+
* security
10+
* external/cwe/cwe-120
11+
*/
12+
13+
import cpp
14+
import semmle.code.cpp.commons.Scanf
15+
16+
17+
from FunctionCall call, ScanfFunction sff
18+
where
19+
call.getTarget() = sff
20+
and
21+
(
22+
call.getArgument(sff.getFormatParameterIndex()).toString().regexpMatch(".*%s.*")
23+
or
24+
call.getArgument(sff.getFormatParameterIndex()).toString() = (".*%ls.*")
25+
)
26+
select call, "Dangerous use of one of the scanf functions"

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/MemoryUnsafeFunctionScan.cpp renamed to cpp/ql/test/experimental/query-tests/Security/CWE/semmle/tests/MemoryUnsafeFunctionScan.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ int fscanf(const char* str, const char* format, ...);
88

99
int main(int argc, char** argv) {
1010

11-
// BAD, do not use scanf, use scanf_s instead
11+
// BAD, do not use scanf without specifying a length first
1212
char buf1[10];
1313
scanf("%s", buf1);
1414

15-
// BAD, do not use sscanf, use sscanf_s instead
15+
// GOOD, length is specified
1616
char buf2[10];
17-
sscanf(buf2, "%s");
17+
sscanf(buf2, "%9s");
1818

19-
// BAD, do not use fscanf, use fscanf_s instead
19+
// BAD, do not use scanf without specifying a length first
2020
char file[10];
2121
fscanf(file, "%s", buf2);
2222

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| MemoryUnsafeFunctionScan.cpp:13:5:13:9 | call to scanf | Dangerous use of one of the scanf functions |
2+
| MemoryUnsafeFunctionScan.cpp:21:5:21: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

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/MemoryUnsafeFunctionScan.expected

Lines changed: 0 additions & 11 deletions
This file was deleted.

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/MemoryUnsafeFunctionScan.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)