Skip to content

Commit 24852c6

Browse files
authored
Merge pull request #20966 from geoffw0/lifetimetest
Rust: Fix FPs from rust/access-after-lifetime-ended
2 parents 10c0183 + 108db75 commit 24852c6

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module AccessAfterLifetime {
102102
// propagate through function calls
103103
exists(Call call |
104104
mayEncloseOnStack(a, call.getEnclosingBlock()) and
105-
call.getStaticTarget() = b.getEnclosingCallable()
105+
call.getARuntimeTarget() = b.getEnclosingCallable()
106106
)
107107
}
108108

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed false positives from the `rust/access-after-lifetime-ended` query, involving calls to trait methods.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ edges
194194
| lifetime.rs:798:9:798:12 | &val | lifetime.rs:798:2:798:12 | return ... | provenance | |
195195
| lifetime.rs:802:6:802:8 | ptr | lifetime.rs:808:23:808:25 | ptr | provenance | |
196196
| lifetime.rs:802:12:802:24 | get_pointer(...) | lifetime.rs:802:6:802:8 | ptr | provenance | |
197+
| lifetime.rs:841:13:841:27 | ...: ... | lifetime.rs:843:12:843:14 | ptr | provenance | |
198+
| lifetime.rs:851:6:851:8 | ptr | lifetime.rs:853:20:853:22 | ptr | provenance | |
199+
| lifetime.rs:851:12:851:23 | &local_value | lifetime.rs:851:6:851:8 | ptr | provenance | |
200+
| lifetime.rs:853:20:853:22 | ptr | lifetime.rs:841:13:841:27 | ...: ... | provenance | |
197201
| main.rs:18:9:18:10 | p1 [&ref] | main.rs:21:19:21:20 | p1 | provenance | |
198202
| main.rs:18:9:18:10 | p1 [&ref] | main.rs:29:19:29:20 | p1 | provenance | |
199203
| main.rs:18:14:18:29 | ...::as_ptr(...) [&ref] | main.rs:18:9:18:10 | p1 [&ref] | provenance | |
@@ -409,6 +413,11 @@ nodes
409413
| lifetime.rs:802:6:802:8 | ptr | semmle.label | ptr |
410414
| lifetime.rs:802:12:802:24 | get_pointer(...) | semmle.label | get_pointer(...) |
411415
| lifetime.rs:808:23:808:25 | ptr | semmle.label | ptr |
416+
| lifetime.rs:841:13:841:27 | ...: ... | semmle.label | ...: ... |
417+
| lifetime.rs:843:12:843:14 | ptr | semmle.label | ptr |
418+
| lifetime.rs:851:6:851:8 | ptr | semmle.label | ptr |
419+
| lifetime.rs:851:12:851:23 | &local_value | semmle.label | &local_value |
420+
| lifetime.rs:853:20:853:22 | ptr | semmle.label | ptr |
412421
| main.rs:18:9:18:10 | p1 [&ref] | semmle.label | p1 [&ref] |
413422
| main.rs:18:14:18:29 | ...::as_ptr(...) [&ref] | semmle.label | ...::as_ptr(...) [&ref] |
414423
| main.rs:18:26:18:28 | &b1 | semmle.label | &b1 |

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,33 @@ pub fn test_lifetimes_example_good() {
827827

828828
println!(" val = {dereferenced_ptr}");
829829
}
830+
831+
// --- generic calls ---
832+
833+
trait Processor {
834+
fn process(ptr: *const i64) -> i64;
835+
}
836+
837+
struct MyProcessor {
838+
}
839+
840+
impl Processor for MyProcessor {
841+
fn process(ptr: *const i64) -> i64 {
842+
unsafe {
843+
return *ptr; // good
844+
}
845+
}
846+
}
847+
848+
fn generic_caller<T: Processor>() -> i64
849+
{
850+
let local_value: i64 = 10;
851+
let ptr = &local_value as *const i64;
852+
853+
return T::process(ptr);
854+
}
855+
856+
pub fn test_generic() {
857+
let result = generic_caller::<MyProcessor>();
858+
println!(" result = {result}");
859+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,7 @@ fn main() {
209209

210210
println!("test_lifetimes_example_good:");
211211
test_lifetimes_example_good();
212+
213+
println!("test_generic:");
214+
test_generic();
212215
}

0 commit comments

Comments
 (0)