Skip to content

Commit 048e5d8

Browse files
committed
C++: IR data flow through global variables
1 parent c216176 commit 048e5d8

File tree

4 files changed

+156
-8
lines changed

4 files changed

+156
-8
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,25 @@ OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) {
244244
* calling context. For example, this would happen with flow through a
245245
* global or static variable.
246246
*/
247-
predicate jumpStep(Node n1, Node n2) { none() }
247+
predicate jumpStep(Node n1, Node n2) {
248+
exists(GlobalOrNamespaceVariable v |
249+
v =
250+
n1.asInstruction()
251+
.(StoreInstruction)
252+
.getResultAddress()
253+
.(VariableAddressInstruction)
254+
.getAstVariable() and
255+
v = n2.asVariable()
256+
or
257+
v =
258+
n2.asInstruction()
259+
.(LoadInstruction)
260+
.getSourceAddress()
261+
.(VariableAddressInstruction)
262+
.getAstVariable() and
263+
v = n1.asVariable()
264+
)
265+
}
248266

249267
/**
250268
* Holds if data can flow from `node1` to `node2` via an assignment to `f`.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,19 @@ namespace FlowThroughGlobals {
334334
}
335335

336336
int f() {
337-
sink(globalVar); // tainted or clean? Not sure.
337+
sink(globalVar); // $ ir=333:17 ir=347:17 // tainted or clean? Not sure.
338338
taintGlobal();
339-
sink(globalVar); // $ MISSING: ast,ir
339+
sink(globalVar); // $ ir=333:17 ir=347:17 MISSING: ast
340340
}
341341

342342
int calledAfterTaint() {
343-
sink(globalVar); // $ MISSING: ast,ir
343+
sink(globalVar); // $ ir=333:17 ir=347:17 MISSING: ast
344344
}
345345

346346
int taintAndCall() {
347347
globalVar = source();
348348
calledAfterTaint();
349-
sink(globalVar); // $ ast,ir
349+
sink(globalVar); // $ ast ir=333:17 ir=347:17
350350
}
351351
}
352352

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void do_sink()
5252
sink(global4); // $ MISSING: ast,ir
5353
sink(global5);
5454
sink(global6);
55-
sink(global7); // $ MISSING: ast,ir
56-
sink(global8); // $ MISSING: ast,ir
57-
sink(global9); // $ MISSING: ast,ir
55+
sink(global7); // $ ir MISSING: ast
56+
sink(global8); // $ ir MISSING: ast
57+
sink(global9); // $ ir MISSING: ast
5858
sink(global10);
5959
}
6060

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,134 @@
11
edges
2+
| globalVars.c:8:7:8:10 | copy | globalVars.c:27:9:27:12 | copy |
3+
| globalVars.c:8:7:8:10 | copy | globalVars.c:27:9:27:12 | copy |
4+
| globalVars.c:8:7:8:10 | copy | globalVars.c:27:9:27:12 | copy |
5+
| globalVars.c:8:7:8:10 | copy | globalVars.c:30:15:30:18 | copy |
6+
| globalVars.c:8:7:8:10 | copy | globalVars.c:30:15:30:18 | copy |
7+
| globalVars.c:8:7:8:10 | copy | globalVars.c:30:15:30:18 | copy |
8+
| globalVars.c:8:7:8:10 | copy | globalVars.c:33:15:33:18 | copy |
9+
| globalVars.c:8:7:8:10 | copy | globalVars.c:35:11:35:14 | copy |
10+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:38:9:38:13 | copy2 |
11+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:38:9:38:13 | copy2 |
12+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:38:9:38:13 | copy2 |
13+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:41:15:41:19 | copy2 |
14+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:41:15:41:19 | copy2 |
15+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:41:15:41:19 | copy2 |
16+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:44:15:44:19 | copy2 |
17+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:50:9:50:13 | copy2 |
18+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:50:9:50:13 | copy2 |
19+
| globalVars.c:9:7:9:11 | copy2 | globalVars.c:50:9:50:13 | copy2 |
20+
| globalVars.c:11:22:11:25 | *argv | globalVars.c:12:2:12:15 | Store |
21+
| globalVars.c:11:22:11:25 | argv | globalVars.c:12:2:12:15 | Store |
22+
| globalVars.c:12:2:12:15 | Store | globalVars.c:8:7:8:10 | copy |
23+
| globalVars.c:15:21:15:23 | val | globalVars.c:16:2:16:12 | Store |
24+
| globalVars.c:16:2:16:12 | Store | globalVars.c:9:7:9:11 | copy2 |
25+
| globalVars.c:19:25:19:27 | *str | globalVars.c:19:25:19:27 | ReturnIndirection |
26+
| globalVars.c:24:11:24:14 | argv | globalVars.c:11:22:11:25 | argv |
27+
| globalVars.c:24:11:24:14 | argv | globalVars.c:24:11:24:14 | argv |
28+
| globalVars.c:24:11:24:14 | argv | globalVars.c:24:11:24:14 | argv |
29+
| globalVars.c:24:11:24:14 | argv | globalVars.c:24:11:24:14 | argv indirection |
30+
| globalVars.c:24:11:24:14 | argv | globalVars.c:24:11:24:14 | argv indirection |
31+
| globalVars.c:24:11:24:14 | argv indirection | globalVars.c:11:22:11:25 | *argv |
32+
| globalVars.c:27:9:27:12 | copy | globalVars.c:27:9:27:12 | (const char *)... |
33+
| globalVars.c:27:9:27:12 | copy | globalVars.c:27:9:27:12 | copy |
34+
| globalVars.c:27:9:27:12 | copy | globalVars.c:27:9:27:12 | copy indirection |
35+
| globalVars.c:30:15:30:18 | copy | globalVars.c:30:15:30:18 | copy |
36+
| globalVars.c:30:15:30:18 | copy | globalVars.c:30:15:30:18 | copy |
37+
| globalVars.c:30:15:30:18 | copy | globalVars.c:30:15:30:18 | copy |
38+
| globalVars.c:30:15:30:18 | copy | globalVars.c:30:15:30:18 | copy indirection |
39+
| globalVars.c:30:15:30:18 | copy | globalVars.c:30:15:30:18 | copy indirection |
40+
| globalVars.c:30:15:30:18 | copy | globalVars.c:35:11:35:14 | copy |
41+
| globalVars.c:30:15:30:18 | copy indirection | globalVars.c:19:25:19:27 | *str |
42+
| globalVars.c:30:15:30:18 | copy indirection | globalVars.c:30:15:30:18 | printWrapper output argument |
43+
| globalVars.c:30:15:30:18 | printWrapper output argument | globalVars.c:35:11:35:14 | copy |
44+
| globalVars.c:33:15:33:18 | copy | globalVars.c:35:11:35:14 | copy |
45+
| globalVars.c:35:11:35:14 | copy | globalVars.c:15:21:15:23 | val |
46+
| globalVars.c:35:11:35:14 | copy | globalVars.c:35:11:35:14 | copy |
47+
| globalVars.c:38:9:38:13 | copy2 | globalVars.c:38:9:38:13 | (const char *)... |
48+
| globalVars.c:38:9:38:13 | copy2 | globalVars.c:38:9:38:13 | copy2 |
49+
| globalVars.c:38:9:38:13 | copy2 | globalVars.c:38:9:38:13 | copy2 indirection |
50+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:41:15:41:19 | copy2 |
51+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:41:15:41:19 | copy2 |
52+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:41:15:41:19 | copy2 |
53+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:41:15:41:19 | copy2 indirection |
54+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:41:15:41:19 | copy2 indirection |
55+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:50:9:50:13 | (const char *)... |
56+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
57+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
58+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
59+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:50:9:50:13 | copy2 indirection |
60+
| globalVars.c:41:15:41:19 | copy2 indirection | globalVars.c:19:25:19:27 | *str |
61+
| globalVars.c:41:15:41:19 | copy2 indirection | globalVars.c:41:15:41:19 | printWrapper output argument |
62+
| globalVars.c:41:15:41:19 | printWrapper output argument | globalVars.c:50:9:50:13 | (const char *)... |
63+
| globalVars.c:41:15:41:19 | printWrapper output argument | globalVars.c:50:9:50:13 | copy2 |
64+
| globalVars.c:41:15:41:19 | printWrapper output argument | globalVars.c:50:9:50:13 | copy2 |
65+
| globalVars.c:41:15:41:19 | printWrapper output argument | globalVars.c:50:9:50:13 | copy2 |
66+
| globalVars.c:41:15:41:19 | printWrapper output argument | globalVars.c:50:9:50:13 | copy2 indirection |
67+
| globalVars.c:44:15:44:19 | copy2 | globalVars.c:50:9:50:13 | (const char *)... |
68+
| globalVars.c:44:15:44:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
69+
| globalVars.c:44:15:44:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
70+
| globalVars.c:44:15:44:19 | copy2 | globalVars.c:50:9:50:13 | copy2 |
71+
| globalVars.c:44:15:44:19 | copy2 | globalVars.c:50:9:50:13 | copy2 indirection |
72+
| globalVars.c:50:9:50:13 | copy2 | globalVars.c:50:9:50:13 | (const char *)... |
73+
| globalVars.c:50:9:50:13 | copy2 | globalVars.c:50:9:50:13 | copy2 |
74+
| globalVars.c:50:9:50:13 | copy2 | globalVars.c:50:9:50:13 | copy2 indirection |
275
subpaths
76+
| globalVars.c:30:15:30:18 | copy indirection | globalVars.c:19:25:19:27 | *str | globalVars.c:19:25:19:27 | ReturnIndirection | globalVars.c:30:15:30:18 | printWrapper output argument |
77+
| globalVars.c:41:15:41:19 | copy2 indirection | globalVars.c:19:25:19:27 | *str | globalVars.c:19:25:19:27 | ReturnIndirection | globalVars.c:41:15:41:19 | printWrapper output argument |
378
nodes
79+
| globalVars.c:8:7:8:10 | copy | semmle.label | copy |
80+
| globalVars.c:9:7:9:11 | copy2 | semmle.label | copy2 |
81+
| globalVars.c:11:22:11:25 | *argv | semmle.label | *argv |
82+
| globalVars.c:11:22:11:25 | argv | semmle.label | argv |
83+
| globalVars.c:12:2:12:15 | Store | semmle.label | Store |
84+
| globalVars.c:15:21:15:23 | val | semmle.label | val |
85+
| globalVars.c:16:2:16:12 | Store | semmle.label | Store |
86+
| globalVars.c:19:25:19:27 | *str | semmle.label | *str |
87+
| globalVars.c:19:25:19:27 | ReturnIndirection | semmle.label | ReturnIndirection |
88+
| globalVars.c:24:11:24:14 | argv | semmle.label | argv |
89+
| globalVars.c:24:11:24:14 | argv | semmle.label | argv |
90+
| globalVars.c:24:11:24:14 | argv | semmle.label | argv |
91+
| globalVars.c:24:11:24:14 | argv indirection | semmle.label | argv indirection |
92+
| globalVars.c:27:9:27:12 | (const char *)... | semmle.label | (const char *)... |
93+
| globalVars.c:27:9:27:12 | (const char *)... | semmle.label | (const char *)... |
94+
| globalVars.c:27:9:27:12 | copy | semmle.label | copy |
95+
| globalVars.c:27:9:27:12 | copy | semmle.label | copy |
96+
| globalVars.c:27:9:27:12 | copy | semmle.label | copy |
97+
| globalVars.c:27:9:27:12 | copy indirection | semmle.label | copy indirection |
98+
| globalVars.c:27:9:27:12 | copy indirection | semmle.label | copy indirection |
99+
| globalVars.c:30:15:30:18 | copy | semmle.label | copy |
100+
| globalVars.c:30:15:30:18 | copy | semmle.label | copy |
101+
| globalVars.c:30:15:30:18 | copy | semmle.label | copy |
102+
| globalVars.c:30:15:30:18 | copy indirection | semmle.label | copy indirection |
103+
| globalVars.c:30:15:30:18 | copy indirection | semmle.label | copy indirection |
104+
| globalVars.c:30:15:30:18 | printWrapper output argument | semmle.label | printWrapper output argument |
105+
| globalVars.c:33:15:33:18 | copy | semmle.label | copy |
106+
| globalVars.c:35:11:35:14 | copy | semmle.label | copy |
107+
| globalVars.c:35:11:35:14 | copy | semmle.label | copy |
108+
| globalVars.c:38:9:38:13 | (const char *)... | semmle.label | (const char *)... |
109+
| globalVars.c:38:9:38:13 | (const char *)... | semmle.label | (const char *)... |
110+
| globalVars.c:38:9:38:13 | copy2 | semmle.label | copy2 |
111+
| globalVars.c:38:9:38:13 | copy2 | semmle.label | copy2 |
112+
| globalVars.c:38:9:38:13 | copy2 | semmle.label | copy2 |
113+
| globalVars.c:38:9:38:13 | copy2 indirection | semmle.label | copy2 indirection |
114+
| globalVars.c:38:9:38:13 | copy2 indirection | semmle.label | copy2 indirection |
115+
| globalVars.c:41:15:41:19 | copy2 | semmle.label | copy2 |
116+
| globalVars.c:41:15:41:19 | copy2 | semmle.label | copy2 |
117+
| globalVars.c:41:15:41:19 | copy2 | semmle.label | copy2 |
118+
| globalVars.c:41:15:41:19 | copy2 indirection | semmle.label | copy2 indirection |
119+
| globalVars.c:41:15:41:19 | copy2 indirection | semmle.label | copy2 indirection |
120+
| globalVars.c:41:15:41:19 | printWrapper output argument | semmle.label | printWrapper output argument |
121+
| globalVars.c:44:15:44:19 | copy2 | semmle.label | copy2 |
122+
| globalVars.c:50:9:50:13 | (const char *)... | semmle.label | (const char *)... |
123+
| globalVars.c:50:9:50:13 | (const char *)... | semmle.label | (const char *)... |
124+
| globalVars.c:50:9:50:13 | copy2 | semmle.label | copy2 |
125+
| globalVars.c:50:9:50:13 | copy2 | semmle.label | copy2 |
126+
| globalVars.c:50:9:50:13 | copy2 | semmle.label | copy2 |
127+
| globalVars.c:50:9:50:13 | copy2 indirection | semmle.label | copy2 indirection |
128+
| globalVars.c:50:9:50:13 | copy2 indirection | semmle.label | copy2 indirection |
4129
#select
130+
| globalVars.c:27:9:27:12 | copy | globalVars.c:24:11:24:14 | argv | globalVars.c:27:9:27:12 | copy | The value of this argument may come from $@ and is being used as a formatting argument to printf(format) | globalVars.c:24:11:24:14 | argv | argv |
131+
| globalVars.c:30:15:30:18 | copy | globalVars.c:24:11:24:14 | argv | globalVars.c:30:15:30:18 | copy | The value of this argument may come from $@ and is being used as a formatting argument to printWrapper(str), which calls printf(format) | globalVars.c:24:11:24:14 | argv | argv |
132+
| globalVars.c:38:9:38:13 | copy2 | globalVars.c:24:11:24:14 | argv | globalVars.c:38:9:38:13 | copy2 | The value of this argument may come from $@ and is being used as a formatting argument to printf(format) | globalVars.c:24:11:24:14 | argv | argv |
133+
| globalVars.c:41:15:41:19 | copy2 | globalVars.c:24:11:24:14 | argv | globalVars.c:41:15:41:19 | copy2 | The value of this argument may come from $@ and is being used as a formatting argument to printWrapper(str), which calls printf(format) | globalVars.c:24:11:24:14 | argv | argv |
134+
| globalVars.c:50:9:50:13 | copy2 | globalVars.c:24:11:24:14 | argv | globalVars.c:50:9:50:13 | copy2 | The value of this argument may come from $@ and is being used as a formatting argument to printf(format) | globalVars.c:24:11:24:14 | argv | argv |

0 commit comments

Comments
 (0)