Skip to content

Commit 0552f9b

Browse files
author
dilanbhalla
committed
memory unsafe scan functions
1 parent a2677f8 commit 0552f9b

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>It is generally considered bad practice to use the scanf, sscanf, and fscanf functions as they are vulnerable to buffer overflows. This may even be the case if a specfied length
7+
is provided with "%s", if the the buffer is dynamic in size.
8+
It is recommended to use the scanf_s, sscanf_s, and fscanf_s functions instead.</p>
9+
10+
</overview>
11+
12+
<recommendation>
13+
<p>Use the scanf_s, sscanf_s, or fscanf_s functions instead.</p>
14+
</recommendation>
15+
16+
<references>
17+
<li>https://cwe.mitre.org/data/definitions/120</li>
18+
<!-- LocalWords: CWE
19+
-->
20+
</references>
21+
22+
</qhelp>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @name Standard library function that is not memory-safe without a specified length
3+
* @description Use of a standard library function that is not memory-safe 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+
15+
predicate memoryUnsafeFunctionParameter(Call c, string message) {
16+
exists(string name | c.getTarget().hasGlobalName(name) |
17+
(
18+
(
19+
name = "scanf" or
20+
name = "sscanf" or
21+
name = "fscanf"
22+
)
23+
) and
24+
message = "Call to " + name + " is potentially dangerous. Please use " + name + "_s to avoid buffer overflows."
25+
)
26+
}
27+
28+
from FunctionCall call, string message
29+
where
30+
memoryUnsafeFunctionParameter(call, message)
31+
select call, message
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// BAD, do not use scanf, use scanf_s instead
12+
char buf1[10];
13+
scanf("%s", buf1);
14+
15+
// BAD, do not use sscanf, use sscanf_s instead
16+
char buf2[10];
17+
sscanf(buf2, "%s");
18+
19+
// BAD, do not use fscanf, use fscanf_s instead
20+
char file[10];
21+
fscanf(file, "%s", buf2);
22+
23+
return 0;
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| MemoryUnsafeFunctionScan.cpp:13:5:13:9 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
2+
| MemoryUnsafeFunctionScan.cpp:17:5:17:10 | call to sscanf | Call to sscanf is potentially dangerous. Please use sscanf_s to avoid buffer overflows. |
3+
| MemoryUnsafeFunctionScan.cpp:21:5:21:10 | call to fscanf | Call to fscanf is potentially dangerous. Please use fscanf_s to avoid buffer overflows. |
4+
| tests.c:31:3:31:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
5+
| tests.c:32:3:32:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
6+
| tests.c:33:3:33:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
7+
| tests.c:34:3:34:8 | call to sscanf | Call to sscanf is potentially dangerous. Please use sscanf_s to avoid buffer overflows. |
8+
| tests.c:60:3:60:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
9+
| tests.c:61:3:61:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
10+
| tests.c:62:3:62:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
11+
| tests.c:63:3:63:7 | call to scanf | Call to scanf is potentially dangerous. Please use scanf_s to avoid buffer overflows. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-120/MemoryUnsafeFunctionScan.ql

0 commit comments

Comments
 (0)