Skip to content

Commit dbde841

Browse files
committed
Rust: Another test case (unsafe function).
1 parent 79cedc2 commit dbde841

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
| lifetime.rs:667:14:667:17 | ref1 | lifetime.rs:654:31:654:35 | &str1 | lifetime.rs:667:14:667:17 | ref1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:653:8:653:11 | str1 | str1 |
2323
| 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 |
2424
| lifetime.rs:734:12:734:13 | r1 | lifetime.rs:719:26:719:34 | &... | lifetime.rs:734:12:734:13 | r1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:719:19:719:20 | v2 | v2 |
25-
| lifetime.rs:791:10:791:12 | ptr | lifetime.rs:781:9:781:12 | &val | lifetime.rs:791:10:791:12 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:779:6:779:8 | val | val |
25+
| 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 |
26+
| lifetime.rs:808:10:808:12 | ptr | lifetime.rs:798:9:798:12 | &val | lifetime.rs:808:10:808:12 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:796:6:796:8 | val | val |
2627
edges
2728
| deallocation.rs:148:6:148:7 | p1 | deallocation.rs:151:14:151:15 | p1 | provenance | |
2829
| deallocation.rs:148:6:148:7 | p1 | deallocation.rs:158:14:158:15 | p1 | provenance | |
@@ -195,10 +196,14 @@ edges
195196
| lifetime.rs:769:6:769:8 | ptr | lifetime.rs:771:12:771:14 | ptr | provenance | |
196197
| lifetime.rs:769:12:769:23 | &val | lifetime.rs:769:12:769:23 | ptr | provenance | |
197198
| lifetime.rs:769:12:769:23 | ptr | lifetime.rs:769:6:769:8 | ptr | provenance | |
198-
| lifetime.rs:781:2:781:12 | return ... | lifetime.rs:785:12:785:24 | get_pointer(...) | provenance | |
199-
| lifetime.rs:781:9:781:12 | &val | lifetime.rs:781:2:781:12 | return ... | provenance | |
200-
| lifetime.rs:785:6:785:8 | ptr | lifetime.rs:791:10:791:12 | ptr | provenance | |
201-
| lifetime.rs:785:12:785:24 | get_pointer(...) | lifetime.rs:785:6:785:8 | ptr | provenance | |
199+
| lifetime.rs:781:2:781:19 | return ... | lifetime.rs:785:11:785:41 | get_local_for_unsafe_function(...) | provenance | |
200+
| lifetime.rs:781:9:781:19 | &my_local10 | lifetime.rs:781:2:781:19 | return ... | provenance | |
201+
| lifetime.rs:785:6:785:7 | p1 | lifetime.rs:789:12:789:13 | p1 | provenance | |
202+
| lifetime.rs:785:11:785:41 | get_local_for_unsafe_function(...) | lifetime.rs:785:6:785:7 | p1 | provenance | |
203+
| lifetime.rs:798:2:798:12 | return ... | lifetime.rs:802:12:802:24 | get_pointer(...) | provenance | |
204+
| lifetime.rs:798:9:798:12 | &val | lifetime.rs:798:2:798:12 | return ... | provenance | |
205+
| lifetime.rs:802:6:802:8 | ptr | lifetime.rs:808:10:808:12 | ptr | provenance | |
206+
| lifetime.rs:802:12:802:24 | get_pointer(...) | lifetime.rs:802:6:802:8 | ptr | provenance | |
202207
models
203208
| 1 | Summary: lang:core; crate::ptr::from_ref; Argument[0]; ReturnValue; value |
204209
nodes
@@ -405,9 +410,14 @@ nodes
405410
| lifetime.rs:769:12:769:23 | &val | semmle.label | &val |
406411
| lifetime.rs:769:12:769:23 | ptr | semmle.label | ptr |
407412
| lifetime.rs:771:12:771:14 | ptr | semmle.label | ptr |
408-
| lifetime.rs:781:2:781:12 | return ... | semmle.label | return ... |
409-
| lifetime.rs:781:9:781:12 | &val | semmle.label | &val |
410-
| lifetime.rs:785:6:785:8 | ptr | semmle.label | ptr |
411-
| lifetime.rs:785:12:785:24 | get_pointer(...) | semmle.label | get_pointer(...) |
412-
| lifetime.rs:791:10:791:12 | ptr | semmle.label | ptr |
413+
| lifetime.rs:781:2:781:19 | return ... | semmle.label | return ... |
414+
| lifetime.rs:781:9:781:19 | &my_local10 | semmle.label | &my_local10 |
415+
| lifetime.rs:785:6:785:7 | p1 | semmle.label | p1 |
416+
| lifetime.rs:785:11:785:41 | get_local_for_unsafe_function(...) | semmle.label | get_local_for_unsafe_function(...) |
417+
| lifetime.rs:789:12:789:13 | p1 | semmle.label | p1 |
418+
| lifetime.rs:798:2:798:12 | return ... | semmle.label | return ... |
419+
| lifetime.rs:798:9:798:12 | &val | semmle.label | &val |
420+
| lifetime.rs:802:6:802:8 | ptr | semmle.label | ptr |
421+
| lifetime.rs:802:12:802:24 | get_pointer(...) | semmle.label | get_pointer(...) |
422+
| lifetime.rs:808:10:808:12 | ptr | semmle.label | ptr |
413423
subpaths

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,23 @@ pub fn test_macros() {
773773
}
774774
}
775775

776+
// --- unsafe function ---
777+
778+
fn get_local_for_unsafe_function() -> *const f64 {
779+
let my_local10: f64 = 1.23;
780+
781+
return &my_local10; // $ Source[rust/access-after-lifetime-ended]=local10
782+
} // (return value immediately becomes dangling)
783+
784+
pub unsafe fn test_unsafe_function() {
785+
let p1 = get_local_for_unsafe_function();
786+
787+
use_the_stack();
788+
789+
let v1 = *p1; // $ Alert[rust/access-after-lifetime-ended]=local10
790+
println!(" v1 = {v1} (!)"); // corrupt in practice
791+
}
792+
776793
// --- examples from qhelp ---
777794

778795
fn get_pointer() -> *const i64 {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ fn main() {
190190
println!("test_macros:");
191191
test_macros();
192192

193+
println!("test_unsafe_function:");
194+
unsafe {
195+
test_unsafe_function();
196+
}
197+
193198
println!("test_lifetimes_example_bad:");
194199
test_lifetimes_example_bad();
195200

0 commit comments

Comments
 (0)