Skip to content

Commit 4d0c53d

Browse files
committed
Rust: Add support for transitive results via calls in the ctor query.
1 parent 14c0bbf commit 4d0c53d

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ class PathElement = AstNode;
4141

4242
query predicate edges(PathElement pred, PathElement succ) {
4343
// starting edge
44-
exists(CtorAttr ctor, Function f, StdCall call |
44+
exists(CtorAttr ctor, Function f, CallExprBase call |
4545
f.getAnAttr() = ctor and
4646
call.getEnclosingCallable() = f and
4747
pred = ctor and // source
48-
succ = call // sink
48+
succ = call // flow or sink node
4949
)
50-
// or
50+
or
5151
// transitive edge
52-
// TODO
52+
exists(Function f |
53+
pred.(CallExprBase).getStaticTarget() = f and
54+
succ.(CallExprBase).getEnclosingCallable() = f
55+
)
5356
}
5457

5558
from CtorAttr ctor, StdCall call

rust/ql/test/query-tests/security/CWE-696/BadCTorInitialization.expected

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,49 @@
88
| test.rs:69:9:69:24 | ...::stdin(...) | test.rs:66:1:66:7 | Attr | test.rs:69:9:69:24 | ...::stdin(...) | Call to ...::stdin(...) in a function with the ctor attribute. |
99
| test.rs:90:5:90:35 | ...::sleep(...) | test.rs:88:1:88:7 | Attr | test.rs:90:5:90:35 | ...::sleep(...) | Call to ...::sleep(...) in a function with the ctor attribute. |
1010
| test.rs:97:5:97:23 | ...::exit(...) | test.rs:95:1:95:7 | Attr | test.rs:97:5:97:23 | ...::exit(...) | Call to ...::exit(...) in a function with the ctor attribute. |
11+
| test.rs:126:9:126:16 | stderr(...) | test.rs:129:1:129:7 | Attr | test.rs:126:9:126:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. |
12+
| test.rs:126:9:126:16 | stderr(...) | test.rs:145:1:145:7 | Attr | test.rs:126:9:126:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. |
13+
| test.rs:126:9:126:44 | ... .write_all(...) | test.rs:129:1:129:7 | Attr | test.rs:126:9:126:44 | ... .write_all(...) | Call to ... .write_all(...) in a function with the ctor attribute. |
14+
| test.rs:126:9:126:44 | ... .write_all(...) | test.rs:145:1:145:7 | Attr | test.rs:126:9:126:44 | ... .write_all(...) | Call to ... .write_all(...) in a function with the ctor attribute. |
1115
| test.rs:171:5:171:15 | ...::stdout(...) | test.rs:169:1:169:7 | Attr | test.rs:171:5:171:15 | ...::stdout(...) | Call to ...::stdout(...) in a function with the ctor attribute. |
1216
edges
1317
| test.rs:29:1:29:13 | Attr | test.rs:31:9:31:25 | ...::stdout(...) |
18+
| test.rs:29:1:29:13 | Attr | test.rs:31:9:31:49 | ... .write(...) |
1419
| test.rs:34:1:34:13 | Attr | test.rs:36:9:36:25 | ...::stdout(...) |
20+
| test.rs:34:1:34:13 | Attr | test.rs:36:9:36:49 | ... .write(...) |
1521
| test.rs:40:1:40:13 | Attr | test.rs:43:9:43:25 | ...::stdout(...) |
22+
| test.rs:40:1:40:13 | Attr | test.rs:43:9:43:49 | ... .write(...) |
1623
| test.rs:51:1:51:7 | Attr | test.rs:53:9:53:16 | stdout(...) |
24+
| test.rs:51:1:51:7 | Attr | test.rs:53:9:53:40 | ... .write(...) |
1725
| test.rs:56:1:56:7 | Attr | test.rs:58:9:58:16 | stderr(...) |
26+
| test.rs:56:1:56:7 | Attr | test.rs:58:9:58:44 | ... .write_all(...) |
1827
| test.rs:61:1:61:7 | Attr | test.rs:63:14:63:28 | ...::_print(...) |
28+
| test.rs:66:1:66:7 | Attr | test.rs:68:20:68:32 | ...::new(...) |
1929
| test.rs:66:1:66:7 | Attr | test.rs:69:9:69:24 | ...::stdin(...) |
30+
| test.rs:66:1:66:7 | Attr | test.rs:69:9:69:45 | ... .read_line(...) |
31+
| test.rs:74:1:74:7 | Attr | test.rs:76:17:76:45 | ...::create(...) |
32+
| test.rs:74:1:74:7 | Attr | test.rs:76:17:76:54 | ... .unwrap(...) |
33+
| test.rs:79:1:79:7 | Attr | test.rs:81:14:81:38 | ...::now(...) |
2034
| test.rs:88:1:88:7 | Attr | test.rs:90:5:90:35 | ...::sleep(...) |
2135
| test.rs:95:1:95:7 | Attr | test.rs:97:5:97:23 | ...::exit(...) |
36+
| test.rs:100:1:100:13 | Attr | test.rs:102:5:102:46 | ... .write_nl(...) |
37+
| test.rs:100:1:100:13 | Attr | test.rs:102:5:102:46 | ...::new(...) |
38+
| test.rs:100:1:100:13 | Attr | test.rs:102:31:102:45 | ... .write_fmt(...) |
39+
| test.rs:105:1:105:13 | Attr | test.rs:107:5:107:23 | panic_cold_explicit(...) |
40+
| test.rs:113:1:113:13 | Attr | test.rs:115:18:115:37 | ...::new::<...>(...) |
41+
| test.rs:113:1:113:13 | Attr | test.rs:116:15:116:27 | alloc(...) |
42+
| test.rs:113:1:113:13 | Attr | test.rs:118:9:118:21 | ... .is_null(...) |
43+
| test.rs:113:1:113:13 | Attr | test.rs:119:9:119:28 | dealloc(...) |
44+
| test.rs:129:1:129:7 | Attr | test.rs:131:5:131:20 | call_target3_1(...) |
45+
| test.rs:131:5:131:20 | call_target3_1(...) | test.rs:126:9:126:16 | stderr(...) |
46+
| test.rs:131:5:131:20 | call_target3_1(...) | test.rs:126:9:126:44 | ... .write_all(...) |
47+
| test.rs:140:1:140:7 | Attr | test.rs:142:5:142:20 | call_target3_2(...) |
48+
| test.rs:145:1:145:7 | Attr | test.rs:147:5:147:20 | call_target3_1(...) |
49+
| test.rs:145:1:145:7 | Attr | test.rs:148:5:148:20 | call_target3_2(...) |
50+
| test.rs:147:5:147:20 | call_target3_1(...) | test.rs:126:9:126:16 | stderr(...) |
51+
| test.rs:147:5:147:20 | call_target3_1(...) | test.rs:126:9:126:44 | ... .write_all(...) |
52+
| test.rs:151:1:151:7 | Attr | test.rs:153:5:153:12 | bad3_3(...) |
53+
| test.rs:157:5:157:20 | call_target3_1(...) | test.rs:126:9:126:16 | stderr(...) |
54+
| test.rs:157:5:157:20 | call_target3_1(...) | test.rs:126:9:126:44 | ... .write_all(...) |
55+
| test.rs:169:1:169:7 | Attr | test.rs:171:5:171:15 | ... .write(...) |
2256
| test.rs:169:1:169:7 | Attr | test.rs:171:5:171:15 | ...::stdout(...) |

rust/ql/test/query-tests/security/CWE-696/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ unsafe fn harmless2_11() {
123123
// --- transitive cases ---
124124

125125
fn call_target3_1() {
126-
_ = stderr().write_all(b"Hello, world!"); // $ MISSING: Alert=source3_1 Alert=source3_3 Alert=source3_4
126+
_ = stderr().write_all(b"Hello, world!"); // $ Alert=source3_1 Alert=source3_3 MISSING: Alert=source3_4
127127
}
128128

129-
#[ctor] // $ MISSING: Source=source3_1
129+
#[ctor] // $ Source=source3_1
130130
fn bad3_1() {
131131
call_target3_1();
132132
}
@@ -142,7 +142,7 @@ fn harmless3_2() {
142142
call_target3_2();
143143
}
144144

145-
#[ctor] // $ MISSING: Source=source3_3
145+
#[ctor] // $ Source=source3_3
146146
fn bad3_3() {
147147
call_target3_1();
148148
call_target3_2();

0 commit comments

Comments
 (0)