Skip to content

Commit 32e9fdf

Browse files
committed
Rust: Fix the false positives.
1 parent 8594c7a commit 32e9fdf

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ module AccessAfterLifetime {
9999
// `b` is a child of `a`
100100
a = b.getEnclosingBlock*()
101101
or
102-
// propagate through function calls
102+
// propagate through function calls (static target)
103103
exists(CallExprBase ce |
104104
mayEncloseOnStack(a, ce.getEnclosingBlock()) and
105105
ce.getStaticTarget() = b.getEnclosingCallable()
106106
)
107+
or
108+
// propagate through function calls (runtime target)
109+
exists(Call c |
110+
mayEncloseOnStack(a, c.getEnclosingBlock()) and
111+
c.getARuntimeTarget() = b.getEnclosingCallable()
112+
)
107113
}
108114

109115
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
| lifetime.rs:667:14:667:17 | ref1 | lifetime.rs:655:11:655:25 | &raw const str2 | lifetime.rs:667:14:667:17 | ref1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:651:7:651:10 | str2 | str2 |
2323
| lifetime.rs:789:12:789:13 | p1 | lifetime.rs:781:9:781:19 | &my_local10 | lifetime.rs:789:12:789:13 | p1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:779:6:779:15 | my_local10 | my_local10 |
2424
| lifetime.rs:808:23:808:25 | ptr | lifetime.rs:798:9:798:12 | &val | lifetime.rs:808:23:808:25 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:796:6:796:8 | val | val |
25-
| lifetime.rs:843:12:843:14 | ptr | lifetime.rs:851:12:851:23 | &local_value | lifetime.rs:843:12:843:14 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:850:6:850:16 | local_value | local_value |
2625
| main.rs:64:23:64:24 | p2 | main.rs:44:26:44:28 | &b2 | main.rs:64:23:64:24 | p2 | Access of a pointer to $@ after its lifetime has ended. | main.rs:43:13:43:14 | b2 | b2 |
2726
edges
2827
| deallocation.rs:242:6:242:7 | p1 | deallocation.rs:245:14:245:15 | p1 | provenance | |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,15 +840,15 @@ struct MyProcessor {
840840
impl Processor for MyProcessor {
841841
fn process(ptr: *const i64) -> i64 {
842842
unsafe {
843-
return *ptr; // $ SPURIOUS: Alert[rust/access-after-lifetime-ended]=local_value
843+
return *ptr; // good
844844
}
845845
}
846846
}
847847

848848
fn generic_caller<T: Processor>() -> i64
849849
{
850850
let local_value: i64 = 10;
851-
let ptr = &local_value as *const i64; // $ Source[rust/access-after-lifetime-ended]=local_value
851+
let ptr = &local_value as *const i64;
852852

853853
return T::process(ptr);
854854
}

0 commit comments

Comments
 (0)