Skip to content

Commit b3330b5

Browse files
committed
Rust: Allow parameter accesses as sources.
1 parent d3d0a53 commit b3330b5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

rust/ql/lib/codeql/rust/security/AccessAfterLifetimeExtensions.qll

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,27 @@ module AccessAfterLifetime {
5353
)
5454
}
5555

56+
/**
57+
* Holds if `var` has scope `scope`.
58+
*/
59+
private predicate variableScope(Variable var, BlockExpr scope) {
60+
// local variable
61+
scope = var.getEnclosingBlock()
62+
or
63+
// parameter
64+
exists(Callable c |
65+
var.getParameter().getEnclosingCallable() = c and
66+
scope.getParentNode() = c
67+
)
68+
}
69+
5670
/**
5771
* Holds if `value` accesses a variable `target` with scope `scope`.
5872
*/
5973
private predicate valueScope(Expr value, Variable target, BlockExpr scope) {
6074
// variable access (to a non-reference)
6175
target = value.(VariableAccess).getVariable() and
62-
scope = target.getEnclosingBlock() and
76+
variableScope(target, scope) and
6377
not TypeInference::inferType(value) instanceof RefType
6478
or
6579
// field access

rust/ql/test/query-tests/security/CWE-825/AccessAfterLifetime.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| lifetime.rs:70:13:70:14 | p2 | lifetime.rs:27:9:27:22 | &mut my_local2 | lifetime.rs:70:13:70:14 | p2 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:25:10:25:18 | my_local2 | my_local2 |
44
| lifetime.rs:71:13:71:14 | p3 | lifetime.rs:33:9:33:28 | &raw const my_local3 | lifetime.rs:71:13:71:14 | p3 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:31:6:31:14 | my_local3 | my_local3 |
55
| lifetime.rs:72:13:72:14 | p4 | lifetime.rs:39:9:39:26 | &raw mut my_local4 | lifetime.rs:72:13:72:14 | p4 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:37:10:37:18 | my_local4 | my_local4 |
6+
| lifetime.rs:73:13:73:14 | p5 | lifetime.rs:43:9:43:15 | &param5 | lifetime.rs:73:13:73:14 | p5 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:42:23:42:28 | param5 | param5 |
67
| lifetime.rs:74:13:74:14 | p6 | lifetime.rs:50:9:50:18 | &... | lifetime.rs:74:13:74:14 | p6 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:47:6:47:8 | val | val |
78
| lifetime.rs:75:13:75:14 | p7 | lifetime.rs:63:8:63:27 | &raw const my_local7 | lifetime.rs:75:13:75:14 | p7 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:62:7:62:15 | my_local7 | my_local7 |
89
| lifetime.rs:76:4:76:5 | p2 | lifetime.rs:27:9:27:22 | &mut my_local2 | lifetime.rs:76:4:76:5 | p2 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:25:10:25:18 | my_local2 | my_local2 |

rust/ql/test/query-tests/security/CWE-825/lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn get_local_dangling_raw_mut() -> *mut i64 {
4040
} // (return value immediately becomes dangling)
4141

4242
fn get_param_dangling(param5: i64) -> *const i64 {
43-
return &param5; // $ MISSING: Source[rust/access-after-lifetime-ended]=param5
43+
return &param5; // $ Source[rust/access-after-lifetime-ended]=param5
4444
} // (return value immediately becomes dangling)
4545

4646
fn get_local_field_dangling() -> *const i64 {
@@ -70,7 +70,7 @@ pub fn test_local_dangling() {
7070
let v2 = *p2; // $ Alert[rust/access-after-lifetime-ended]=local2
7171
let v3 = *p3; // $ Alert[rust/access-after-lifetime-ended]=local3
7272
let v4 = *p4; // $ Alert[rust/access-after-lifetime-ended]=local4
73-
let v5 = *p5; // $ MISSING: Alert[rust/access-after-lifetime-ended]=param5
73+
let v5 = *p5; // $ Alert[rust/access-after-lifetime-ended]=param5
7474
let v6 = *p6; // $ Alert[rust/access-after-lifetime-ended]=localfield
7575
let v7 = *p7; // $ Alert[rust/access-after-lifetime-ended]=local7
7676
*p2 = 8; // $ Alert[rust/access-after-lifetime-ended]=local2

0 commit comments

Comments
 (0)