@@ -10,6 +10,8 @@ private import codeql.rust.dataflow.FlowSink
1010private import codeql.rust.Concepts
1111private import codeql.rust.dataflow.internal.Node
1212private import codeql.rust.security.Barriers as Barriers
13+ private import codeql.rust.internal.TypeInference as TypeInference
14+ private import codeql.rust.internal.Type
1315
1416/**
1517 * Provides default sources, sinks and barriers for detecting accesses to
@@ -47,16 +49,22 @@ module AccessInvalidPointer {
4749 ModelsAsDataSource ( ) { sourceNode ( this , "pointer-invalidate" ) }
4850 }
4951
50- /**
51- * A pointer access using the unary `*` operator.
52- */
52+ /** A raw pointer access using the unary `*` operator. */
5353 private class DereferenceSink extends Sink {
54- DereferenceSink ( ) { any ( DerefExpr p ) .getExpr ( ) = this .asExpr ( ) }
54+ DereferenceSink ( ) {
55+ exists ( Expr p , DerefExpr d | p = d .getExpr ( ) and p = this .asExpr ( ) |
56+ // Dereferencing a raw pointer is an unsafe operation. Hence relevant
57+ // dereferences must occur inside code marked as unsafe.
58+ // See: https://doc.rust-lang.org/reference/types/pointer.html#r-type.pointer.raw.safety
59+ ( p .getEnclosingBlock * ( ) .isUnsafe ( ) or p .getEnclosingCallable ( ) .( Function ) .isUnsafe ( ) ) and
60+ // We are only interested in dereferences of raw pointers, as other uses
61+ // of `*` are safe.
62+ ( not exists ( TypeInference:: inferType ( p ) ) or TypeInference:: inferType ( p ) instanceof PtrType )
63+ )
64+ }
5565 }
5666
57- /**
58- * A pointer access from model data.
59- */
67+ /** A pointer access from model data. */
6068 private class ModelsAsDataSink extends Sink {
6169 ModelsAsDataSink ( ) { sinkNode ( this , "pointer-access" ) }
6270 }
0 commit comments