Skip to content

Commit 858eec3

Browse files
committed
Rust: Exclude results where the source is a reference.
1 parent 7bae451 commit 858eec3

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import rust
77
private import codeql.rust.dataflow.DataFlow
88
private import codeql.rust.security.AccessInvalidPointerExtensions
9+
private import codeql.rust.internal.Type
10+
private import codeql.rust.internal.TypeInference as TypeInference
911

1012
/**
1113
* Provides default sources, sinks and barriers for detecting accesses to a
@@ -55,9 +57,10 @@ module AccessAfterLifetime {
5557
* Holds if `value` accesses a variable `target` with scope `scope`.
5658
*/
5759
private predicate valueScope(Expr value, Variable target, BlockExpr scope) {
58-
// variable access
60+
// variable access (to a non-reference)
5961
target = value.(VariableAccess).getVariable() and
60-
scope = target.getEnclosingBlock()
62+
scope = target.getEnclosingBlock() and
63+
not TypeInference::inferType(value) instanceof RefType
6164
or
6265
// field access
6366
valueScope(value.(FieldExpr).getContainer(), target, scope)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
| 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 it's lifetime has ended. | lifetime.rs:653:8:653:11 | str1 | str1 |
2121
| 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 it's lifetime has ended. | lifetime.rs:653:8:653:11 | str1 | str1 |
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 it's lifetime has ended. | lifetime.rs:651:7:651:10 | str2 | str2 |
23-
| lifetime.rs:692:13:692:14 | r1 | lifetime.rs:682:4:682:12 | &... | lifetime.rs:692:13:692:14 | r1 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:681:8:681:9 | v1 | v1 |
24-
| lifetime.rs:693:13:693:14 | r2 | lifetime.rs:686:5:686:13 | &... | lifetime.rs:693:13:693:14 | r2 | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:685:8:685:9 | v2 | v2 |
2523
| lifetime.rs:743:10:743:12 | ptr | lifetime.rs:733:9:733:12 | &val | lifetime.rs:743:10:743:12 | ptr | Access of a pointer to $@ after it's lifetime has ended. | lifetime.rs:731:6:731:8 | val | val |
2624
edges
2725
| deallocation.rs:148:6:148:7 | p1 | deallocation.rs:151:14:151:15 | p1 | provenance | |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,18 +679,18 @@ impl MyType {
679679
fn test(&self) {
680680
let r1 = unsafe {
681681
let v1 = &self;
682-
&v1.value // $ SPURIOUS: Source[rust/access-after-lifetime-ended]=v1
682+
&v1.value
683683
};
684684
let (r2, r3) = unsafe {
685685
let v2 = &self;
686-
(&v2.value, // $ SPURIOUS: Source[rust/access-after-lifetime-ended]=v2
686+
(&v2.value,
687687
&self.value)
688688
};
689689

690690
use_the_stack();
691691

692-
let v1 = *r1; // $ SPURIOUS: Alert[rust/access-after-lifetime-ended]=v1
693-
let v2 = *r2; // $ SPURIOUS: Alert[rust/access-after-lifetime-ended]=v2
692+
let v1 = *r1;
693+
let v2 = *r2;
694694
let v3 = *r3;
695695
println!(" v1 = {v1}");
696696
println!(" v2 = {v2}");

0 commit comments

Comments
 (0)