Skip to content

Commit 5edd6e8

Browse files
committed
Rust: Restrict results to 'unsafe' blocks.
1 parent dbde841 commit 5edd6e8

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

rust/ql/src/queries/security/CWE-825/AccessAfterLifetime.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ where
4040
AccessAfterLifetimeFlow::flowPath(sourceNode, sinkNode) and
4141
// check that the dereference is outside the lifetime of the target
4242
AccessAfterLifetime::dereferenceAfterLifetime(sourceNode.getNode(), sinkNode.getNode(), target) and
43+
// include only results inside `unsafe` blocks, as other results tend to be false positives
44+
(
45+
sinkNode.getNode().asExpr().getExpr().getEnclosingBlock*().isUnsafe() or
46+
sinkNode.getNode().asExpr().getExpr().getEnclosingCallable().(Function).isUnsafe()
47+
) and
4348
// exclude cases with sources / sinks in macros, since these results are difficult to interpret
4449
not sourceNode.getNode().asExpr().getExpr().isFromMacroExpansion() and
4550
not sinkNode.getNode().asExpr().getExpr().isFromMacroExpansion()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
| lifetime.rs:659:15:659:18 | ref1 | lifetime.rs:654:31:654:35 | &str1 | lifetime.rs:659:15:659:18 | ref1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:653:8:653:11 | str1 | str1 |
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 |
24-
| 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 |
2524
| 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 |
2625
| 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 |
2726
edges

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ enum MyEnum3 {
716716
impl MyEnum3 {
717717
pub fn test_match(&self) -> &i64 {
718718
let r1 = match self {
719-
MyEnum3::Value(v2) => &v2.value, // $ SPURIOUS: Source[rust/access-after-lifetime-ended]=v2_value
719+
MyEnum3::Value(v2) => &v2.value,
720720
};
721721

722722
r1
@@ -731,7 +731,7 @@ pub fn test_enum_members() {
731731

732732
use_the_stack();
733733

734-
let v3 = *r1; // $ SPURIOUS: Alert[rust/access-after-lifetime-ended]=v2_value
734+
let v3 = *r1;
735735
println!(" v3 = {v3}");
736736
}
737737

0 commit comments

Comments
 (0)