Skip to content

Commit 35cf657

Browse files
committed
WIP
1 parent 40f629e commit 35cf657

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ module AccessAfterLifetime {
3131

3232
/**
3333
* A data flow sink for accesses to a pointer after its lifetime has ended,
34-
* that is, a dereference.
34+
* that is, a dereference. We re-use the same sinks as for the accesses to
35+
* invalid pointers query.
3536
*/
36-
abstract class Sink extends QuerySink::Range {
37-
override string getSinkType() { result = "AccessAfterLifetime" }
38-
}
37+
class Sink = AccessInvalidPointer::Sink;
3938

39+
// /**
40+
// * A data flow sink for accesses to a pointer after its lifetime has ended,
41+
// * that is, a dereference.
42+
// */
43+
// abstract class Sink extends QuerySink::Range {
44+
// override string getSinkType() { result = "AccessAfterLifetime" }
45+
// }
4046
/**
4147
* A barrier for accesses to a pointer after its lifetime has ended.
4248
*/
@@ -115,23 +121,14 @@ module AccessAfterLifetime {
115121
private class RefExprSource extends Source {
116122
Expr targetValue;
117123

118-
RefExprSource() { this.asExpr().(RefExpr).getExpr() = targetValue }
124+
RefExprSource() {
125+
this.asExpr().(RefExpr).getExpr() = targetValue and
126+
this.asExpr().(RefExpr).isRaw()
127+
}
119128

120129
override Expr getTarget() { result = targetValue }
121130
}
122131

123-
/**
124-
* A pointer access using the unary `*` operator.
125-
*/
126-
private class DereferenceSink extends Sink {
127-
DereferenceSink() { any(DerefExpr p).getExpr() = this.asExpr() }
128-
}
129-
130-
/** A pointer access from model data. */
131-
private class ModelsAsDataSink extends Sink {
132-
ModelsAsDataSink() { sinkNode(this, "pointer-access") }
133-
}
134-
135132
/**
136133
* A barrier for nodes inside closures, as we don't model lifetimes of
137134
* variables through closures properly.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ module AccessAfterLifetimeConfig implements DataFlow::ConfigSig {
2626
predicate isSource(DataFlow::Node node) {
2727
node instanceof AccessAfterLifetime::Source and
2828
// exclude cases with sources in macros, since these results are difficult to interpret
29-
not node.asExpr().isFromMacroExpansion()
29+
not node.asExpr().isFromMacroExpansion() and
30+
AccessAfterLifetime::sourceValueScope(node, _, _)
3031
}
3132

3233
predicate isSink(DataFlow::Node node) {
@@ -36,7 +37,8 @@ module AccessAfterLifetimeConfig implements DataFlow::ConfigSig {
3637
// include only results inside `unsafe` blocks, as other results tend to be false positives
3738
(
3839
node.asExpr().getEnclosingBlock*().isUnsafe() or
39-
node.asExpr().getEnclosingCallable().(Function).isUnsafe()
40+
node.asExpr().getEnclosingCallable().(Function).isUnsafe() or
41+
not exists(node.asExpr())
4042
)
4143
}
4244

0 commit comments

Comments
 (0)