Skip to content

Commit 30580e9

Browse files
committed
C++: Add a TaintFunction model to FormattingFunction.
1 parent 1d46971 commit 30580e9

5 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import semmle.code.cpp.models.interfaces.ArrayFunction
10+
import semmle.code.cpp.models.interfaces.Taint
1011

1112
private Type stripTopLevelSpecifiersOnly(Type t) {
1213
result = stripTopLevelSpecifiersOnly(t.(SpecifiedType).getBaseType())
@@ -39,7 +40,7 @@ private Type getAFormatterWideTypeOrDefault() {
3940
/**
4041
* A standard library function that uses a `printf`-like formatting string.
4142
*/
42-
abstract class FormattingFunction extends ArrayFunction {
43+
abstract class FormattingFunction extends ArrayFunction, TaintFunction {
4344
/** Gets the position at which the format parameter occurs. */
4445
abstract int getFormatParameterIndex();
4546

@@ -155,4 +156,9 @@ abstract class FormattingFunction extends ArrayFunction {
155156
predicate hasArrayOutput(int bufParam) {
156157
bufParam = getOutputParameterIndex()
157158
}
159+
160+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
161+
input.isParameterDeref(getFormatParameterIndex()) and
162+
output.isParameterDeref(getOutputParameterIndex())
163+
}
158164
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void test1()
5555
{
5656
char buffer[256] = {0};
5757
sink(snprintf(buffer, 256, string::source(), "Hello."));
58-
sink(buffer); // tainted [NOT DETECTED]
58+
sink(buffer); // tainted
5959
}
6060
{
6161
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
@@ -11,50 +11,62 @@
1111
| format.cpp:46:21:46:24 | {...} | format.cpp:48:8:48:13 | buffer | |
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 | |
14+
| format.cpp:47:30:47:33 | %s | format.cpp:47:17:47:22 | ref arg buffer | TAINT |
1415
| format.cpp:51:21:51:24 | {...} | format.cpp:52:17:52:22 | buffer | |
1516
| format.cpp:51:21:51:24 | {...} | format.cpp:53:8:53:13 | buffer | |
1617
| format.cpp:51:23:51:23 | 0 | format.cpp:51:21:51:24 | {...} | TAINT |
1718
| format.cpp:52:17:52:22 | ref arg buffer | format.cpp:53:8:53:13 | buffer | |
19+
| format.cpp:52:30:52:33 | %s | format.cpp:52:17:52:22 | ref arg buffer | TAINT |
1820
| format.cpp:56:21:56:24 | {...} | format.cpp:57:17:57:22 | buffer | |
1921
| format.cpp:56:21:56:24 | {...} | format.cpp:58:8:58:13 | buffer | |
2022
| format.cpp:56:23:56:23 | 0 | format.cpp:56:21:56:24 | {...} | TAINT |
2123
| format.cpp:57:17:57:22 | ref arg buffer | format.cpp:58:8:58:13 | buffer | |
24+
| format.cpp:57:30:57:43 | call to source | format.cpp:57:17:57:22 | ref arg buffer | TAINT |
2225
| format.cpp:61:21:61:24 | {...} | format.cpp:62:17:62:22 | buffer | |
2326
| format.cpp:61:21:61:24 | {...} | format.cpp:63:8:63:13 | buffer | |
2427
| format.cpp:61:23:61:23 | 0 | format.cpp:61:21:61:24 | {...} | TAINT |
2528
| format.cpp:62:17:62:22 | ref arg buffer | format.cpp:63:8:63:13 | buffer | |
29+
| format.cpp:62:30:62:39 | %s %s %s | format.cpp:62:17:62:22 | ref arg buffer | TAINT |
2630
| format.cpp:66:21:66:24 | {...} | format.cpp:67:17:67:22 | buffer | |
2731
| format.cpp:66:21:66:24 | {...} | format.cpp:68:8:68:13 | buffer | |
2832
| format.cpp:66:23:66:23 | 0 | format.cpp:66:21:66:24 | {...} | TAINT |
2933
| format.cpp:67:17:67:22 | ref arg buffer | format.cpp:68:8:68:13 | buffer | |
34+
| format.cpp:67:30:67:35 | %.*s | format.cpp:67:17:67:22 | ref arg buffer | TAINT |
3035
| format.cpp:72:21:72:24 | {...} | format.cpp:73:17:73:22 | buffer | |
3136
| format.cpp:72:21:72:24 | {...} | format.cpp:74:8:74:13 | buffer | |
3237
| format.cpp:72:23:72:23 | 0 | format.cpp:72:21:72:24 | {...} | TAINT |
3338
| format.cpp:73:17:73:22 | ref arg buffer | format.cpp:74:8:74:13 | buffer | |
39+
| format.cpp:73:30:73:33 | %i | format.cpp:73:17:73:22 | ref arg buffer | TAINT |
3440
| format.cpp:77:21:77:24 | {...} | format.cpp:78:17:78:22 | buffer | |
3541
| format.cpp:77:21:77:24 | {...} | format.cpp:79:8:79:13 | buffer | |
3642
| format.cpp:77:23:77:23 | 0 | format.cpp:77:21:77:24 | {...} | TAINT |
3743
| format.cpp:78:17:78:22 | ref arg buffer | format.cpp:79:8:79:13 | buffer | |
44+
| format.cpp:78:30:78:33 | %i | format.cpp:78:17:78:22 | ref arg buffer | TAINT |
3845
| format.cpp:82:21:82:24 | {...} | format.cpp:83:17:83:22 | buffer | |
3946
| format.cpp:82:21:82:24 | {...} | format.cpp:84:8:84:13 | buffer | |
4047
| format.cpp:82:23:82:23 | 0 | format.cpp:82:21:82:24 | {...} | TAINT |
4148
| format.cpp:83:17:83:22 | ref arg buffer | format.cpp:84:8:84:13 | buffer | |
49+
| format.cpp:83:30:83:35 | %.*s | format.cpp:83:17:83:22 | ref arg buffer | TAINT |
4250
| format.cpp:88:21:88:24 | {...} | format.cpp:89:17:89:22 | buffer | |
4351
| format.cpp:88:21:88:24 | {...} | format.cpp:90:8:90:13 | buffer | |
4452
| format.cpp:88:23:88:23 | 0 | format.cpp:88:21:88:24 | {...} | TAINT |
4553
| format.cpp:89:17:89:22 | ref arg buffer | format.cpp:90:8:90:13 | buffer | |
54+
| format.cpp:89:30:89:33 | %p | format.cpp:89:17:89:22 | ref arg buffer | TAINT |
4655
| format.cpp:94:21:94:24 | {...} | format.cpp:95:16:95:21 | buffer | |
4756
| format.cpp:94:21:94:24 | {...} | format.cpp:96:8:96:13 | buffer | |
4857
| format.cpp:94:23:94:23 | 0 | format.cpp:94:21:94:24 | {...} | TAINT |
4958
| format.cpp:95:16:95:21 | ref arg buffer | format.cpp:96:8:96:13 | buffer | |
59+
| format.cpp:95:24:95:27 | %s | format.cpp:95:16:95:21 | ref arg buffer | TAINT |
5060
| format.cpp:99:21:99:24 | {...} | format.cpp:100:16:100:21 | buffer | |
5161
| format.cpp:99:21:99:24 | {...} | format.cpp:101:8:101:13 | buffer | |
5262
| format.cpp:99:23:99:23 | 0 | format.cpp:99:21:99:24 | {...} | TAINT |
5363
| format.cpp:100:16:100:21 | ref arg buffer | format.cpp:101:8:101:13 | buffer | |
64+
| format.cpp:100:24:100:28 | %ls | format.cpp:100:16:100:21 | ref arg buffer | TAINT |
5465
| format.cpp:104:25:104:28 | {...} | format.cpp:105:17:105:23 | wbuffer | |
5566
| format.cpp:104:25:104:28 | {...} | format.cpp:106:8:106:14 | wbuffer | |
5667
| format.cpp:104:27:104:27 | 0 | format.cpp:104:25:104:28 | {...} | TAINT |
5768
| format.cpp:105:17:105:23 | ref arg wbuffer | format.cpp:106:8:106:14 | wbuffer | |
69+
| format.cpp:105:31:105:35 | %s | format.cpp:105:17:105:23 | ref arg wbuffer | TAINT |
5870
| format.cpp:109:21:109:24 | {...} | format.cpp:110:18:110:23 | buffer | |
5971
| format.cpp:109:21:109:24 | {...} | format.cpp:111:8:111:13 | buffer | |
6072
| 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| format.cpp:58:8:58:13 | buffer | format.cpp:57:30:57:43 | call to source |
12
| taint.cpp:8:8:8:13 | clean1 | taint.cpp:4:27:4:33 | source1 |
23
| taint.cpp:16:8:16:14 | source1 | taint.cpp:12:22:12:27 | call to source |
34
| 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| format.cpp:58:8:58:13 | format.cpp:57:30:57:43 | AST only |
12
| taint.cpp:41:7:41:13 | taint.cpp:35:12:35:17 | AST only |
23
| taint.cpp:42:7:42:13 | taint.cpp:35:12:35:17 | AST only |
34
| taint.cpp:43:7:43:13 | taint.cpp:37:22:37:27 | AST only |

0 commit comments

Comments
 (0)