Skip to content

Commit b54d8dc

Browse files
authored
Merge pull request #18176 from geoffw0/ctor2
Rust: Improve rust/ctor-initialization
2 parents 10592bb + 0865397 commit b54d8dc

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import rust
1515

1616
/**
17-
* A `#[ctor]` or `#[dtor]` attribute.
17+
* A `#[ctor]` or `#[dtor]` attribute, that is, a source for this query.
1818
*/
1919
class CtorAttr extends Attr {
2020
string whichAttr;
@@ -28,7 +28,7 @@ class CtorAttr extends Attr {
2828
}
2929

3030
/**
31-
* A call into the Rust standard library.
31+
* A call into the Rust standard library, that is, a sink for this query.
3232
*/
3333
class StdCall extends Expr {
3434
StdCall() {
@@ -39,20 +39,38 @@ class StdCall extends Expr {
3939

4040
class PathElement = AstNode;
4141

42+
/**
43+
* Holds if (`pred`, `succ`) represents a candidate edge for the query that is
44+
* reachable from a source.
45+
*/
46+
predicate edgesFwd(PathElement pred, PathElement succ) {
47+
// attribute (source) -> callable
48+
pred.(CtorAttr) = succ.(Callable).getAnAttr()
49+
or
50+
// [forwards reachable] callable -> enclosed call
51+
edgesFwd(_, pred) and
52+
pred = succ.(CallExprBase).getEnclosingCallable()
53+
or
54+
// [forwards reachable] call -> target callable
55+
edgesFwd(_, pred) and
56+
pred.(CallExprBase).getStaticTarget() = succ
57+
}
58+
59+
/**
60+
* Holds if (`pred`, `succ`) represents an edge for the query that is reachable
61+
* from a source and backwards reachable from a sink (adding the backwards
62+
* reachability constraint reduces the amount of output data produced).
63+
*/
4264
query predicate edges(PathElement pred, PathElement succ) {
43-
// starting edge
44-
exists(CtorAttr ctor, Function f, StdCall call |
45-
f.getAnAttr() = ctor and
46-
call.getEnclosingCallable() = f and
47-
pred = ctor and // source
48-
succ = call // sink
65+
edgesFwd(pred, succ) and
66+
(
67+
succ instanceof StdCall // sink
68+
or
69+
edges(succ, _) // backwards reachable from a sink
4970
)
50-
// or
51-
// transitive edge
52-
// TODO
5371
}
5472

55-
from CtorAttr ctor, StdCall call
56-
where edges*(ctor, call)
57-
select call, ctor, call,
58-
"Call to " + call.toString() + " in a function with the " + ctor.getWhichAttr() + " attribute."
73+
from CtorAttr source, StdCall sink
74+
where edges+(source, sink)
75+
select sink, source, sink,
76+
"Call to " + sink.toString() + " in a function with the " + source.getWhichAttr() + " attribute."

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,37 @@
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:166:5:166:15 | ...::stdout(...) | test.rs:164:1:164:7 | Attr | test.rs:166:5:166:15 | ...::stdout(...) | Call to ...::stdout(...) 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. |
15+
| 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
13-
| test.rs:29:1:29:13 | Attr | test.rs:31:9:31:25 | ...::stdout(...) |
14-
| test.rs:34:1:34:13 | Attr | test.rs:36:9:36:25 | ...::stdout(...) |
15-
| test.rs:40:1:40:13 | Attr | test.rs:43:9:43:25 | ...::stdout(...) |
16-
| test.rs:51:1:51:7 | Attr | test.rs:53:9:53:16 | stdout(...) |
17-
| test.rs:56:1:56:7 | Attr | test.rs:58:9:58:16 | stderr(...) |
18-
| test.rs:61:1:61:7 | Attr | test.rs:63:14:63:28 | ...::_print(...) |
19-
| test.rs:66:1:66:7 | Attr | test.rs:69:9:69:24 | ...::stdin(...) |
20-
| test.rs:88:1:88:7 | Attr | test.rs:90:5:90:35 | ...::sleep(...) |
21-
| test.rs:95:1:95:7 | Attr | test.rs:97:5:97:23 | ...::exit(...) |
22-
| test.rs:164:1:164:7 | Attr | test.rs:166:5:166:15 | ...::stdout(...) |
17+
| test.rs:29:1:29:13 | Attr | test.rs:29:1:32:1 | fn bad1_1 |
18+
| test.rs:29:1:32:1 | fn bad1_1 | test.rs:31:9:31:25 | ...::stdout(...) |
19+
| test.rs:34:1:34:13 | Attr | test.rs:34:1:37:1 | fn bad1_2 |
20+
| test.rs:34:1:37:1 | fn bad1_2 | test.rs:36:9:36:25 | ...::stdout(...) |
21+
| test.rs:39:1:44:1 | fn bad1_3 | test.rs:43:9:43:25 | ...::stdout(...) |
22+
| test.rs:40:1:40:13 | Attr | test.rs:39:1:44:1 | fn bad1_3 |
23+
| test.rs:51:1:51:7 | Attr | test.rs:51:1:54:1 | fn bad2_1 |
24+
| test.rs:51:1:54:1 | fn bad2_1 | test.rs:53:9:53:16 | stdout(...) |
25+
| test.rs:56:1:56:7 | Attr | test.rs:56:1:59:1 | fn bad2_2 |
26+
| test.rs:56:1:59:1 | fn bad2_2 | test.rs:58:9:58:16 | stderr(...) |
27+
| test.rs:61:1:61:7 | Attr | test.rs:61:1:64:1 | fn bad2_3 |
28+
| test.rs:61:1:64:1 | fn bad2_3 | test.rs:63:14:63:28 | ...::_print(...) |
29+
| test.rs:66:1:66:7 | Attr | test.rs:66:1:70:1 | fn bad2_4 |
30+
| test.rs:66:1:70:1 | fn bad2_4 | test.rs:69:9:69:24 | ...::stdin(...) |
31+
| test.rs:88:1:88:7 | Attr | test.rs:88:1:91:1 | fn bad2_7 |
32+
| test.rs:88:1:91:1 | fn bad2_7 | test.rs:90:5:90:35 | ...::sleep(...) |
33+
| test.rs:95:1:95:7 | Attr | test.rs:95:1:98:1 | fn bad2_8 |
34+
| test.rs:95:1:98:1 | fn bad2_8 | test.rs:97:5:97:23 | ...::exit(...) |
35+
| test.rs:125:1:127:1 | fn call_target3_1 | test.rs:126:9:126:16 | stderr(...) |
36+
| test.rs:125:1:127:1 | fn call_target3_1 | test.rs:126:9:126:44 | ... .write_all(...) |
37+
| test.rs:129:1:129:7 | Attr | test.rs:129:1:132:1 | fn bad3_1 |
38+
| test.rs:129:1:132:1 | fn bad3_1 | test.rs:131:5:131:20 | call_target3_1(...) |
39+
| test.rs:131:5:131:20 | call_target3_1(...) | test.rs:125:1:127:1 | fn call_target3_1 |
40+
| test.rs:145:1:145:7 | Attr | test.rs:145:1:149:1 | fn bad3_3 |
41+
| test.rs:145:1:149:1 | fn bad3_3 | test.rs:147:5:147:20 | call_target3_1(...) |
42+
| test.rs:147:5:147:20 | call_target3_1(...) | test.rs:125:1:127:1 | fn call_target3_1 |
43+
| test.rs:169:1:169:7 | Attr | test.rs:169:1:172:1 | fn bad4_1 |
44+
| test.rs:169:1:172:1 | fn bad4_1 | test.rs:171:5:171:15 | ...::stdout(...) |

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

Lines changed: 9 additions & 4 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
}
@@ -137,12 +137,12 @@ fn call_target3_2() {
137137
}
138138
}
139139

140-
#[ctor] // $ MISSING: Source=source3_2
140+
#[ctor]
141141
fn harmless3_2() {
142142
call_target3_2();
143143
}
144144

145-
#[ctor]
145+
#[ctor] // $ Source=source3_3
146146
fn bad3_3() {
147147
call_target3_1();
148148
call_target3_2();
@@ -153,6 +153,11 @@ fn bad3_4() {
153153
bad3_3();
154154
}
155155

156+
fn harmless3_5() {
157+
call_target3_1();
158+
call_target3_2();
159+
}
160+
156161
// --- macros ---
157162

158163
macro_rules! macro4_1 {

0 commit comments

Comments
 (0)