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

Skip to content

Commit c747914

Browse files
committed
C++: Add sscanf and fscanf model implementations.
1 parent 064d897 commit c747914

4 files changed

Lines changed: 95 additions & 2 deletions

File tree

cpp/ql/src/semmle/code/cpp/models/Models.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ private import implementations.StdString
2525
private import implementations.Swap
2626
private import implementations.GetDelim
2727
private import implementations.SmartPointer
28+
private import implementations.Sscanf
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Provides implementation classes modeling `sscanf`, `fscanf` and various similar
3+
* functions. See `semmle.code.cpp.models.Models` for usage information.
4+
*/
5+
6+
import semmle.code.cpp.Function
7+
import semmle.code.cpp.models.interfaces.ArrayFunction
8+
import semmle.code.cpp.models.interfaces.Taint
9+
import semmle.code.cpp.models.interfaces.Alias
10+
import semmle.code.cpp.models.interfaces.SideEffect
11+
12+
/**
13+
* The standard function `sscanf`, `fscanf` and its assorted variants
14+
*/
15+
private class Sscanf extends ArrayFunction, TaintFunction, AliasFunction, SideEffectFunction {
16+
Sscanf() {
17+
this.hasGlobalOrStdName([
18+
"sscanf", // sscanf(src_stream, format, args...)
19+
"swscanf", // swscanf(src, format, args...)
20+
"fscanf", // fscanf(src_stream, format, args...)
21+
"fwscanf" // fwscanf(src_stream, format, args...)
22+
]) or
23+
this.hasGlobalName([
24+
"_sscanf_l", // _sscanf_l(src, format, locale, args...)
25+
"_swscanf_l", // _swscanf_l(src, format, locale, args...)
26+
"_snscanf", // _snscanf(src, length, format, args...)
27+
"_snscanf_l", // _snscanf_l(src, length, format, locale, args...)
28+
"_snwscanf", // _snwscanf(src, length, format, args...)
29+
"_snwscanf_l", // _snwscanf_l(src, length, format, locale, args...)
30+
"_fscanf_l", // _fscanf_l(src_stream, format, locale, args...)
31+
"_fwscanf_l" // _fwscanf_l(src_stream, format, locale, args...)
32+
])
33+
}
34+
35+
override predicate hasArrayWithNullTerminator(int bufParam) {
36+
bufParam = [0, getFormatPosition()]
37+
}
38+
39+
override predicate hasArrayInput(int bufParam) { bufParam = [0, getFormatPosition()] }
40+
41+
private int getLengthPosition() {
42+
this.getName().matches("\\_sn%") and
43+
result = 1
44+
}
45+
46+
private int getLocalePosition() {
47+
this.getName().matches("%\\_l") and
48+
(if exists(getLengthPosition()) then result = getLengthPosition() + 2 else result = 2)
49+
}
50+
51+
private int getFormatPosition() { if exists(getLengthPosition()) then result = 2 else result = 1 }
52+
53+
private int getArgsStartPosition() {
54+
exists(int nLength, int nLocale |
55+
(if exists(getLocalePosition()) then nLocale = 1 else nLocale = 0) and
56+
(if exists(getLengthPosition()) then nLength = 1 else nLength = 0) and
57+
result = 2 + nLocale + nLength
58+
)
59+
}
60+
61+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
62+
input.isParameterDeref(0) and
63+
output.isParameterDeref(any(int i | i >= getArgsStartPosition()))
64+
}
65+
66+
override predicate parameterNeverEscapes(int index) {
67+
index = [0 .. max(getACallToThisFunction().getNumberOfArguments())]
68+
}
69+
70+
override predicate parameterEscapesOnlyViaReturn(int index) { none() }
71+
72+
override predicate parameterIsAlwaysReturned(int index) { none() }
73+
74+
override predicate hasOnlySpecificReadSideEffects() { any() }
75+
76+
override predicate hasOnlySpecificWriteSideEffects() { any() }
77+
78+
override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) {
79+
i >= getArgsStartPosition() and
80+
buffer = true and
81+
mustWrite = true
82+
}
83+
84+
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) {
85+
buffer = true and
86+
i = [0, getFormatPosition(), getLocalePosition()]
87+
}
88+
}

cpp/ql/test/library-tests/dataflow/taint-tests/format.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void test1()
123123
{
124124
int i = 0;
125125
sink(sscanf(string::source(), "%i", &i));
126-
sink(i); // $ MISSING: ast,ir
126+
sink(i); // $ ast,ir
127127
}
128128
{
129129
char buffer[256] = {0};
@@ -133,7 +133,7 @@ void test1()
133133
{
134134
char buffer[256] = {0};
135135
sink(sscanf(string::source(), "%s", &buffer));
136-
sink(buffer); // $ MISSING: ast,ir
136+
sink(buffer); // $ ast,ir
137137
}
138138
}
139139

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,23 +378,27 @@
378378
| format.cpp:114:37:114:50 | call to source | format.cpp:114:18:114:23 | ref arg buffer | TAINT |
379379
| format.cpp:119:10:119:11 | 0 | format.cpp:120:29:120:29 | i | |
380380
| format.cpp:119:10:119:11 | 0 | format.cpp:121:8:121:8 | i | |
381+
| format.cpp:120:15:120:19 | 123 | format.cpp:120:28:120:29 | ref arg & ... | TAINT |
381382
| format.cpp:120:28:120:29 | ref arg & ... | format.cpp:120:29:120:29 | i [inner post update] | |
382383
| format.cpp:120:28:120:29 | ref arg & ... | format.cpp:121:8:121:8 | i | |
383384
| format.cpp:120:29:120:29 | i | format.cpp:120:28:120:29 | & ... | |
384385
| format.cpp:124:10:124:11 | 0 | format.cpp:125:40:125:40 | i | |
385386
| format.cpp:124:10:124:11 | 0 | format.cpp:126:8:126:8 | i | |
387+
| format.cpp:125:15:125:28 | call to source | format.cpp:125:39:125:40 | ref arg & ... | TAINT |
386388
| format.cpp:125:39:125:40 | ref arg & ... | format.cpp:125:40:125:40 | i [inner post update] | |
387389
| format.cpp:125:39:125:40 | ref arg & ... | format.cpp:126:8:126:8 | i | |
388390
| format.cpp:125:40:125:40 | i | format.cpp:125:39:125:40 | & ... | |
389391
| format.cpp:129:21:129:24 | {...} | format.cpp:130:32:130:37 | buffer | |
390392
| format.cpp:129:21:129:24 | {...} | format.cpp:131:8:131:13 | buffer | |
391393
| format.cpp:129:23:129:23 | 0 | format.cpp:129:21:129:24 | {...} | TAINT |
394+
| format.cpp:130:15:130:22 | Hello. | format.cpp:130:31:130:37 | ref arg & ... | TAINT |
392395
| format.cpp:130:31:130:37 | ref arg & ... | format.cpp:130:32:130:37 | buffer [inner post update] | |
393396
| format.cpp:130:31:130:37 | ref arg & ... | format.cpp:131:8:131:13 | buffer | |
394397
| format.cpp:130:32:130:37 | buffer | format.cpp:130:31:130:37 | & ... | |
395398
| format.cpp:134:21:134:24 | {...} | format.cpp:135:40:135:45 | buffer | |
396399
| format.cpp:134:21:134:24 | {...} | format.cpp:136:8:136:13 | buffer | |
397400
| format.cpp:134:23:134:23 | 0 | format.cpp:134:21:134:24 | {...} | TAINT |
401+
| format.cpp:135:15:135:28 | call to source | format.cpp:135:39:135:45 | ref arg & ... | TAINT |
398402
| format.cpp:135:39:135:45 | ref arg & ... | format.cpp:135:40:135:45 | buffer [inner post update] | |
399403
| format.cpp:135:39:135:45 | ref arg & ... | format.cpp:136:8:136:13 | buffer | |
400404
| format.cpp:135:40:135:45 | buffer | format.cpp:135:39:135:45 | & ... | |

0 commit comments

Comments
 (0)