Skip to content

Exclude bounds-check arithmetic from tainted-arithmetic sinks#21608

Open
MarkLee131 wants to merge 5 commits intogithub:mainfrom
MarkLee131:fix/tainted-arithmetic-bounds-check-barrier
Open

Exclude bounds-check arithmetic from tainted-arithmetic sinks#21608
MarkLee131 wants to merge 5 commits intogithub:mainfrom
MarkLee131:fix/tainted-arithmetic-bounds-check-barrier

Conversation

@MarkLee131
Copy link
Copy Markdown
Contributor

fix #21607 :

The java/tainted-arithmetic query now recognizes when an arithmetic expression appears directly as an operand of a comparison (e.g., if (off + len > array.length)). Such expressions are bounds checks, not vulnerable computations, and are excluded via the existing overflowIrrelevant predicate.

Add test cases for bounds-checking patterns that should not be flagged.

The java/tainted-arithmetic query now recognizes when an arithmetic
expression appears directly as an operand of a comparison (e.g.,
`if (off + len > array.length)`). Such expressions are bounds checks,
not vulnerable computations, and are excluded via the existing
overflowIrrelevant predicate.

Add test cases for bounds-checking patterns that should not be flagged.
@MarkLee131 MarkLee131 requested a review from a team as a code owner March 28, 2026 09:46
Copilot AI review requested due to automatic review settings March 28, 2026 09:46
Copy link
Copy Markdown
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

Adjusts the Java java/tainted-arithmetic query to avoid reporting arithmetic that is used purely as part of bounds-check comparisons (addressing #21607), and adds regression coverage + a changelog entry.

Changes:

  • Extend the “overflow/underflow irrelevant” logic to treat arithmetic used directly in comparison-based bounds checks as non-sinks.
  • Add new “GOOD” bounds-checking patterns to the CWE-190 query tests.
  • Add a minor analysis change note describing the reduced false positives.

Reviewed changes

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

File Description
java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll Updates the exclusion logic for overflow/underflow-relevant arithmetic to cover bounds-check comparison operands.
java/ql/test/query-tests/security/CWE-190/semmle/tests/ArithmeticTainted.java Adds new “GOOD” bounds-check examples intended to ensure the query doesn’t flag these patterns.
java/ql/lib/change-notes/2026-03-28-tainted-arithmetic-bounds-check.md Documents the behavioral change for the java/tainted-arithmetic query.

Copy link
Copy Markdown
Contributor

@owen-mc owen-mc left a comment

Choose a reason for hiding this comment

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

Thank you for this contribution. Special thanks for giving real world examples where this false positive occurs - this really helps us to see that it is worth fixing. This PR just needs a few small changes and it can be merged.

MarkLee131 and others added 4 commits March 29, 2026 10:25
…ticTainted.java

Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
…check.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Clarify that arithmeticUsedInBoundsCheck applies to if-condition
  comparisons, not all comparisons
- Update expected test line numbers to reflect added test calls
@MarkLee131
Copy link
Copy Markdown
Contributor Author

Thanks for review. I have finished the changes and tested it locally. My test cmd is:

codeql test run java/ql/test/query-tests/security/CWE-190/semmle/tests/ --threads=4 --search-path .

Output:

  Executing 6 tests in 1 directories.
  [1/6] PASSED InformationLoss.qlref
  [2/6] PASSED IntMultToLong.qlref
  [3/6] PASSED ComparisonWithWiderType.qlref
  [4/6] PASSED ArithmeticWithExtremeValues.qlref
  [5/6] PASSED ArithmeticUncontrolled.qlref
  [6/6] PASSED ArithmeticTainted.qlref
  Completed in 8.3s. All 6 tests passed.

@MarkLee131 MarkLee131 requested a review from owen-mc March 29, 2026 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: java/tainted-arithmetic flags bounds-checking arithmetic expressions

3 participants