1
1
/**
2
- * Provides implementation classes modeling `sscanf`, `fscanf` and various similar
3
- * functions. See `semmle.code.cpp.models.Models` for usage information.
2
+ * Provides implementation classes modeling the `scanf` family of functions.
3
+ * See `semmle.code.cpp.models.Models` for usage information.
4
4
*/
5
5
6
6
import semmle.code.cpp.Function
@@ -9,18 +9,15 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
9
9
import semmle.code.cpp.models.interfaces.Taint
10
10
import semmle.code.cpp.models.interfaces.Alias
11
11
import semmle.code.cpp.models.interfaces.SideEffect
12
+ import semmle.code.cpp.models.interfaces.FlowSource
12
13
13
14
/**
14
- * The standard function `sscanf`, `fscanf` and its assorted variants
15
+ * The `scanf` family of functions.
15
16
*/
16
- private class SscanfModel extends ArrayFunction , TaintFunction , AliasFunction , SideEffectFunction {
17
- SscanfModel ( ) { this instanceof Sscanf or this instanceof Fscanf or this instanceof Snscanf }
18
-
17
+ abstract private class ScanfFunctionModel extends ArrayFunction , TaintFunction , AliasFunction ,
18
+ SideEffectFunction {
19
19
override predicate hasArrayWithNullTerminator ( int bufParam ) {
20
20
bufParam = this .( ScanfFunction ) .getFormatParameterIndex ( )
21
- or
22
- not this instanceof Fscanf and
23
- bufParam = this .( ScanfFunction ) .getInputParameterIndex ( )
24
21
}
25
22
26
23
override predicate hasArrayInput ( int bufParam ) { this .hasArrayWithNullTerminator ( bufParam ) }
@@ -36,7 +33,7 @@ private class SscanfModel extends ArrayFunction, TaintFunction, AliasFunction, S
36
33
)
37
34
}
38
35
39
- private int getArgsStartPosition ( ) { result = this .getNumberOfParameters ( ) }
36
+ int getArgsStartPosition ( ) { result = this .getNumberOfParameters ( ) }
40
37
41
38
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
42
39
input .isParameterDeref ( this .( ScanfFunction ) .getInputParameterIndex ( ) ) and
@@ -70,3 +67,40 @@ private class SscanfModel extends ArrayFunction, TaintFunction, AliasFunction, S
70
67
]
71
68
}
72
69
}
70
+
71
+ /**
72
+ * The standard function `scanf` and its assorted variants
73
+ */
74
+ private class ScanfModel extends ScanfFunctionModel , LocalFlowSourceFunction {
75
+ ScanfModel ( ) { this instanceof Scanf }
76
+
77
+ override predicate hasLocalFlowSource ( FunctionOutput output , string description ) {
78
+ output .isParameterDeref ( any ( int i | i >= this .getArgsStartPosition ( ) ) ) and
79
+ description = "Value read by " + this .getName ( )
80
+ }
81
+ }
82
+
83
+ /**
84
+ * The standard function `fscanf` and its assorted variants
85
+ */
86
+ private class FscanfModel extends ScanfFunctionModel , RemoteFlowSourceFunction {
87
+ FscanfModel ( ) { this instanceof Fscanf }
88
+
89
+ override predicate hasRemoteFlowSource ( FunctionOutput output , string description ) {
90
+ output .isParameterDeref ( any ( int i | i >= this .getArgsStartPosition ( ) ) ) and
91
+ description = "Value read by " + this .getName ( )
92
+ }
93
+ }
94
+
95
+ /**
96
+ * The standard function `sscanf` and its assorted variants
97
+ */
98
+ private class SscanfModel extends ScanfFunctionModel {
99
+ SscanfModel ( ) { this instanceof Sscanf or this instanceof Snscanf }
100
+
101
+ override predicate hasArrayWithNullTerminator ( int bufParam ) {
102
+ super .hasArrayWithNullTerminator ( bufParam )
103
+ or
104
+ bufParam = this .( ScanfFunction ) .getInputParameterIndex ( )
105
+ }
106
+ }
0 commit comments