Skip to content

Commit d66f608

Browse files
committed
C++: Taint from FormattingFunction varargs.
1 parent 8b215c1 commit d66f608

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

cpp/ql/src/semmle/code/cpp/models/interfaces/FormattingFunction.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
154154
override predicate hasArrayOutput(int bufParam) { bufParam = getOutputParameterIndex() }
155155

156156
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
157-
input.isParameterDeref(getFormatParameterIndex()) and
158-
output.isParameterDeref(getOutputParameterIndex())
157+
exists(int arg |
158+
(
159+
arg = getFormatParameterIndex() or
160+
arg >= getFirstFormatArgumentIndex()
161+
) and
162+
input.isParameterDeref(arg) and
163+
output.isParameterDeref(getOutputParameterIndex())
164+
)
159165
}
160166
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void test1()
5050
{
5151
char buffer[256] = {0};
5252
sink(snprintf(buffer, 256, "%s", string::source()));
53-
sink(buffer); // tainted [NOT DETECTED]
53+
sink(buffer); // tainted
5454
}
5555
{
5656
char buffer[256] = {0};
@@ -76,34 +76,34 @@ void test1()
7676
{
7777
char buffer[256] = {0};
7878
sink(snprintf(buffer, 256, "%i", source()));
79-
sink(buffer); // tainted [NOT DETECTED]
79+
sink(buffer); // tainted
8080
}
8181
{
8282
char buffer[256] = {0};
8383
sink(snprintf(buffer, 256, "%.*s", source(), "Hello."));
84-
sink(buffer); // tainted [NOT DETECTED]
84+
sink(buffer); // tainted
8585
}
8686

8787
{
8888
char buffer[256] = {0};
8989
sink(snprintf(buffer, 256, "%p", string::source()));
90-
sink(buffer);
90+
sink(buffer); // tainted (debatable)
9191
}
9292

9393
{
9494
char buffer[256] = {0};
9595
sink(sprintf(buffer, "%s", string::source()));
96-
sink(buffer); // tainted [NOT DETECTED]
96+
sink(buffer); // tainted
9797
}
9898
{
9999
char buffer[256] = {0};
100100
sink(sprintf(buffer, "%ls", wstring::source()));
101-
sink(buffer); // tainted [NOT DETECTED]
101+
sink(buffer); // tainted
102102
}
103103
{
104104
wchar_t wbuffer[256] = {0};
105105
sink(swprintf(wbuffer, 256, L"%s", wstring::source()));
106-
sink(wbuffer); // tainted [NOT DETECTED]
106+
sink(wbuffer); // tainted
107107
}
108108
{
109109
char buffer[256] = {0};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,73 @@
1212
| format.cpp:46:23:46:23 | 0 | format.cpp:46:21:46:24 | {...} | TAINT |
1313
| format.cpp:47:17:47:22 | ref arg buffer | format.cpp:48:8:48:13 | buffer | |
1414
| format.cpp:47:30:47:33 | %s | format.cpp:47:17:47:22 | ref arg buffer | TAINT |
15+
| format.cpp:47:36:47:43 | Hello. | format.cpp:47:17:47:22 | ref arg buffer | TAINT |
1516
| format.cpp:51:21:51:24 | {...} | format.cpp:52:17:52:22 | buffer | |
1617
| format.cpp:51:21:51:24 | {...} | format.cpp:53:8:53:13 | buffer | |
1718
| format.cpp:51:23:51:23 | 0 | format.cpp:51:21:51:24 | {...} | TAINT |
1819
| format.cpp:52:17:52:22 | ref arg buffer | format.cpp:53:8:53:13 | buffer | |
1920
| format.cpp:52:30:52:33 | %s | format.cpp:52:17:52:22 | ref arg buffer | TAINT |
21+
| format.cpp:52:36:52:49 | call to source | format.cpp:52:17:52:22 | ref arg buffer | TAINT |
2022
| format.cpp:56:21:56:24 | {...} | format.cpp:57:17:57:22 | buffer | |
2123
| format.cpp:56:21:56:24 | {...} | format.cpp:58:8:58:13 | buffer | |
2224
| format.cpp:56:23:56:23 | 0 | format.cpp:56:21:56:24 | {...} | TAINT |
2325
| format.cpp:57:17:57:22 | ref arg buffer | format.cpp:58:8:58:13 | buffer | |
2426
| format.cpp:57:30:57:43 | call to source | format.cpp:57:17:57:22 | ref arg buffer | TAINT |
27+
| format.cpp:57:48:57:55 | Hello. | format.cpp:57:17:57:22 | ref arg buffer | TAINT |
2528
| format.cpp:61:21:61:24 | {...} | format.cpp:62:17:62:22 | buffer | |
2629
| format.cpp:61:21:61:24 | {...} | format.cpp:63:8:63:13 | buffer | |
2730
| format.cpp:61:23:61:23 | 0 | format.cpp:61:21:61:24 | {...} | TAINT |
2831
| format.cpp:62:17:62:22 | ref arg buffer | format.cpp:63:8:63:13 | buffer | |
2932
| format.cpp:62:30:62:39 | %s %s %s | format.cpp:62:17:62:22 | ref arg buffer | TAINT |
33+
| format.cpp:62:42:62:44 | a | format.cpp:62:17:62:22 | ref arg buffer | TAINT |
3034
| format.cpp:66:21:66:24 | {...} | format.cpp:67:17:67:22 | buffer | |
3135
| format.cpp:66:21:66:24 | {...} | format.cpp:68:8:68:13 | buffer | |
3236
| format.cpp:66:23:66:23 | 0 | format.cpp:66:21:66:24 | {...} | TAINT |
3337
| format.cpp:67:17:67:22 | ref arg buffer | format.cpp:68:8:68:13 | buffer | |
3438
| format.cpp:67:30:67:35 | %.*s | format.cpp:67:17:67:22 | ref arg buffer | TAINT |
39+
| format.cpp:67:38:67:39 | 10 | format.cpp:67:17:67:22 | ref arg buffer | TAINT |
3540
| format.cpp:72:21:72:24 | {...} | format.cpp:73:17:73:22 | buffer | |
3641
| format.cpp:72:21:72:24 | {...} | format.cpp:74:8:74:13 | buffer | |
3742
| format.cpp:72:23:72:23 | 0 | format.cpp:72:21:72:24 | {...} | TAINT |
3843
| format.cpp:73:17:73:22 | ref arg buffer | format.cpp:74:8:74:13 | buffer | |
3944
| format.cpp:73:30:73:33 | %i | format.cpp:73:17:73:22 | ref arg buffer | TAINT |
45+
| format.cpp:73:36:73:36 | 0 | format.cpp:73:17:73:22 | ref arg buffer | TAINT |
4046
| format.cpp:77:21:77:24 | {...} | format.cpp:78:17:78:22 | buffer | |
4147
| format.cpp:77:21:77:24 | {...} | format.cpp:79:8:79:13 | buffer | |
4248
| format.cpp:77:23:77:23 | 0 | format.cpp:77:21:77:24 | {...} | TAINT |
4349
| format.cpp:78:17:78:22 | ref arg buffer | format.cpp:79:8:79:13 | buffer | |
4450
| format.cpp:78:30:78:33 | %i | format.cpp:78:17:78:22 | ref arg buffer | TAINT |
51+
| format.cpp:78:36:78:41 | call to source | format.cpp:78:17:78:22 | ref arg buffer | TAINT |
4552
| format.cpp:82:21:82:24 | {...} | format.cpp:83:17:83:22 | buffer | |
4653
| format.cpp:82:21:82:24 | {...} | format.cpp:84:8:84:13 | buffer | |
4754
| format.cpp:82:23:82:23 | 0 | format.cpp:82:21:82:24 | {...} | TAINT |
4855
| format.cpp:83:17:83:22 | ref arg buffer | format.cpp:84:8:84:13 | buffer | |
4956
| format.cpp:83:30:83:35 | %.*s | format.cpp:83:17:83:22 | ref arg buffer | TAINT |
57+
| format.cpp:83:38:83:43 | call to source | format.cpp:83:17:83:22 | ref arg buffer | TAINT |
5058
| format.cpp:88:21:88:24 | {...} | format.cpp:89:17:89:22 | buffer | |
5159
| format.cpp:88:21:88:24 | {...} | format.cpp:90:8:90:13 | buffer | |
5260
| format.cpp:88:23:88:23 | 0 | format.cpp:88:21:88:24 | {...} | TAINT |
5361
| format.cpp:89:17:89:22 | ref arg buffer | format.cpp:90:8:90:13 | buffer | |
5462
| format.cpp:89:30:89:33 | %p | format.cpp:89:17:89:22 | ref arg buffer | TAINT |
63+
| format.cpp:89:36:89:49 | call to source | format.cpp:89:17:89:22 | ref arg buffer | TAINT |
5564
| format.cpp:94:21:94:24 | {...} | format.cpp:95:16:95:21 | buffer | |
5665
| format.cpp:94:21:94:24 | {...} | format.cpp:96:8:96:13 | buffer | |
5766
| format.cpp:94:23:94:23 | 0 | format.cpp:94:21:94:24 | {...} | TAINT |
5867
| format.cpp:95:16:95:21 | ref arg buffer | format.cpp:96:8:96:13 | buffer | |
5968
| format.cpp:95:24:95:27 | %s | format.cpp:95:16:95:21 | ref arg buffer | TAINT |
69+
| format.cpp:95:30:95:43 | call to source | format.cpp:95:16:95:21 | ref arg buffer | TAINT |
6070
| format.cpp:99:21:99:24 | {...} | format.cpp:100:16:100:21 | buffer | |
6171
| format.cpp:99:21:99:24 | {...} | format.cpp:101:8:101:13 | buffer | |
6272
| format.cpp:99:23:99:23 | 0 | format.cpp:99:21:99:24 | {...} | TAINT |
6373
| format.cpp:100:16:100:21 | ref arg buffer | format.cpp:101:8:101:13 | buffer | |
6474
| format.cpp:100:24:100:28 | %ls | format.cpp:100:16:100:21 | ref arg buffer | TAINT |
75+
| format.cpp:100:31:100:45 | call to source | format.cpp:100:16:100:21 | ref arg buffer | TAINT |
6576
| format.cpp:104:25:104:28 | {...} | format.cpp:105:17:105:23 | wbuffer | |
6677
| format.cpp:104:25:104:28 | {...} | format.cpp:106:8:106:14 | wbuffer | |
6778
| format.cpp:104:27:104:27 | 0 | format.cpp:104:25:104:28 | {...} | TAINT |
6879
| format.cpp:105:17:105:23 | ref arg wbuffer | format.cpp:106:8:106:14 | wbuffer | |
6980
| format.cpp:105:31:105:35 | %s | format.cpp:105:17:105:23 | ref arg wbuffer | TAINT |
81+
| format.cpp:105:38:105:52 | call to source | format.cpp:105:17:105:23 | ref arg wbuffer | TAINT |
7082
| format.cpp:109:21:109:24 | {...} | format.cpp:110:18:110:23 | buffer | |
7183
| format.cpp:109:21:109:24 | {...} | format.cpp:111:8:111:13 | buffer | |
7284
| format.cpp:109:23:109:23 | 0 | format.cpp:109:21:109:24 | {...} | TAINT |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
| format.cpp:53:8:53:13 | buffer | format.cpp:52:36:52:49 | call to source |
12
| format.cpp:58:8:58:13 | buffer | format.cpp:57:30:57:43 | call to source |
3+
| format.cpp:79:8:79:13 | buffer | format.cpp:78:36:78:41 | call to source |
4+
| format.cpp:84:8:84:13 | buffer | format.cpp:83:38:83:43 | call to source |
5+
| format.cpp:90:8:90:13 | buffer | format.cpp:89:36:89:49 | call to source |
6+
| format.cpp:96:8:96:13 | buffer | format.cpp:95:30:95:43 | call to source |
7+
| format.cpp:101:8:101:13 | buffer | format.cpp:100:31:100:45 | call to source |
8+
| format.cpp:106:8:106:14 | wbuffer | format.cpp:105:38:105:52 | call to source |
29
| taint.cpp:8:8:8:13 | clean1 | taint.cpp:4:27:4:33 | source1 |
310
| taint.cpp:16:8:16:14 | source1 | taint.cpp:12:22:12:27 | call to source |
411
| taint.cpp:17:8:17:16 | ++ ... | taint.cpp:12:22:12:27 | call to source |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
| format.cpp:53:8:53:13 | format.cpp:52:36:52:49 | AST only |
12
| format.cpp:58:8:58:13 | format.cpp:57:30:57:43 | AST only |
3+
| format.cpp:79:8:79:13 | format.cpp:78:36:78:41 | AST only |
4+
| format.cpp:84:8:84:13 | format.cpp:83:38:83:43 | AST only |
5+
| format.cpp:90:8:90:13 | format.cpp:89:36:89:49 | AST only |
6+
| format.cpp:96:8:96:13 | format.cpp:95:30:95:43 | AST only |
7+
| format.cpp:101:8:101:13 | format.cpp:100:31:100:45 | AST only |
8+
| format.cpp:106:8:106:14 | format.cpp:105:38:105:52 | AST only |
29
| taint.cpp:41:7:41:13 | taint.cpp:35:12:35:17 | AST only |
310
| taint.cpp:42:7:42:13 | taint.cpp:35:12:35:17 | AST only |
411
| taint.cpp:43:7:43:13 | taint.cpp:37:22:37:27 | AST only |

0 commit comments

Comments
 (0)