Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Dec 1, 2025

This PR reduces the number of sinks in DereferenceSink by not including dereference expressions that are not the source of unsafety.

The DereferenceSink is used in rust/access-invalid-pointer and rust/access-after-lifetime-ended.

The number of sinks is reduced in two ways:

  1. In order for a dereference expression to dereference a pointer it must occur within an unsafe block. On rust this restriction reduces the number of sinks from 29,857 to 1,618. This filter was already applied in rust/access-after-lifetime-ended so it only makes a difference for the rust/access-invalid-pointer query.
  2. If the type of the thing being dereferenced is not a raw pointer, then the dereference should be safe. So use type inference to only consider those dereferences. On rust this further reduces the number of sinks from 1,618 to 1,301.

I've looked at some of the sink that are removed on rust due to the type check, and they look like good exclusions to me. Here's a typical example:

let byte = unsafe { *src_bytes.get_unchecked(i) };

This deref is excluded because get_unchecked returns a &. Excluding this seem fine because 1/ the query can't handle this anyway and 2/ if the above unsafe block goes wrong the root cause for blame is within get_unchecked and not *.

MRVA

MRVA top 100 for "Access of invalid pointer"

Overall there's only a small change in the number of results, which seems good. The removed results that I've looked at all seemed like FPs.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Dec 1, 2025
@paldepind paldepind force-pushed the rust/invalid-pointer-barriers branch from b569633 to 40f629e Compare December 1, 2025 14:33
@paldepind paldepind marked this pull request as ready for review December 1, 2025 14:50
@paldepind paldepind requested a review from a team as a code owner December 1, 2025 14:50
Copilot AI review requested due to automatic review settings December 1, 2025 14:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds barriers and refines sink definitions to reduce false positives in the rust/access-invalid-pointer query. The changes separate concerns between the AccessInvalidPointer and AccessAfterLifetime queries by giving each its own sink definitions with appropriate restrictions.

  • Adds type-based barriers (numeric, boolean, fieldless enum) to filter out non-pointer types
  • Restricts DereferenceSink in AccessInvalidPointer to only match raw pointer dereferences in unsafe contexts
  • Introduces a DefaultBarrier to handle imprecise Default::default calls that can resolve to null pointer implementations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
rust/ql/src/queries/summary/Stats.qll Adds import for AccessAfterLifetimeExtensions to ensure all query sink extensions are loaded
rust/ql/lib/codeql/rust/security/AccessInvalidPointerExtensions.qll Adds multiple barriers (type-based, NonNull::new, Default::default) and restricts DereferenceSink to unsafe contexts with raw pointer type checks
rust/ql/lib/codeql/rust/security/AccessAfterLifetimeExtensions.qll Refactors Sink from type alias to abstract class and defines its own sink implementations separate from AccessInvalidPointer

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@paldepind paldepind marked this pull request as draft December 2, 2025 07:53
@paldepind paldepind force-pushed the rust/invalid-pointer-barriers branch from 35cf657 to 0b3a694 Compare December 5, 2025 10:21
// TODO: Remove this condition if it can be done without negatively
// impacting performance. This condition only include nodes with
// corresponding to an expression. This excludes sinks from models-as-data.
exists(node.asExpr())
Copy link
Contributor Author

@paldepind paldepind Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is weird, but note that the check before the changes had the same effect of excluding all nodes without an expression. So this is only preserving what was already there.

Since my present goal is to improve performance I don't really have appetite for introducing additional sinks, hence I left this condition in place.

As the todo says I think we should try to remove this in the future and see what it does for performance and results.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could re-include the models-as-data sinks explicitly with something like:

or
node instanceof AccessAfterLifetime::ModelsAsDataSink

There ought not be too many of these.

@paldepind paldepind changed the title Rust: Add barriers for rust/access-invalid-pointer Rust: Reduce the number of sinks in DereferenceSink Dec 5, 2025
// exclude cases with sources in macros, since these results are difficult to interpret
not node.asExpr().isFromMacroExpansion()
not node.asExpr().isFromMacroExpansion() and
AccessAfterLifetime::sourceValueScope(node, _, _)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sources needs to satisfy this in the end anyway, so we might check for that here to prune some sources earlier in the process.

@paldepind
Copy link
Contributor Author

Thanks for the review @geoffw0. I've now updated the PR to (almost) only include the change to DereferenceSink. I'll save the other changes for future PRs.

@paldepind paldepind marked this pull request as ready for review December 5, 2025 13:09
@paldepind paldepind requested a review from geoffw0 December 5, 2025 13:09
@paldepind paldepind force-pushed the rust/invalid-pointer-barriers branch from 465641d to 49b7338 Compare December 9, 2025 10:44
@paldepind paldepind force-pushed the rust/invalid-pointer-barriers branch from 49b7338 to ef8d2c0 Compare December 9, 2025 13:41
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the QL and I'm happy from that point of view. I think I've figured out where I stand regarding the sink restrictions now - see the PR onto this PR - basically, losing a few mostly theoretical results is an acceptable cost to get the noise down. I've still to review your MRVA results and think about a few other details.

Rust: Add test case for rust/access-after-lifetime-ended
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants