-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Fix FP in "Time-of-check time-of-use race condition" (java/toctou-race-condition)
#19015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aed5164
Convert to inline expectations test
owen-mc dc2cbf7
Add tests for always-locked fields
owen-mc a8e993c
Fix FP for always-locked fields
owen-mc 6ca9a1f
Add change note
owen-mc 5c75888
Fix test output
owen-mc 7702e9d
Address review comments
owen-mc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
java/ql/src/change-notes/2025-03-13-fix-toctou-false-positive.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Fixed a false positive in "Time-of-check time-of-use race condition" (`java/toctou-race-condition`) where a field of a non-static class was not considered always-locked if it was accessed in a constructor. |
24 changes: 24 additions & 0 deletions
24
java/ql/test/query-tests/security/CWE-367/semmle/tests/FieldAlwaysLocked.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package test.cwe367.semmle.tests; | ||
|
|
||
| import java.util.Enumeration; | ||
| import java.util.Hashtable; | ||
|
|
||
| class FieldAlwaysLocked { | ||
|
|
||
| Hashtable field; | ||
|
|
||
| public FieldAlwaysLocked() { | ||
| field = new Hashtable(); | ||
| } | ||
|
|
||
| protected synchronized void checkOut() { | ||
| Object o; | ||
| if (field.size() > 0) { | ||
| Enumeration e = field.keys(); | ||
| while (e.hasMoreElements()) { | ||
| o = e.nextElement(); | ||
| field.remove(o); | ||
| } | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
java/ql/test/query-tests/security/CWE-367/semmle/tests/FieldNotAlwaysLocked.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package test.cwe367.semmle.tests; | ||
|
|
||
| import java.util.Enumeration; | ||
| import java.util.Hashtable; | ||
|
|
||
| class FieldNotAlwaysLocked { | ||
|
|
||
| Hashtable field; | ||
|
|
||
| public FieldNotAlwaysLocked() { | ||
| field = new Hashtable(); | ||
| } | ||
|
|
||
| protected synchronized void checkOut() { | ||
| Object o; | ||
| if (field.size() > 0) { | ||
| Enumeration e = field.keys(); // $ Alert | ||
| while (e.hasMoreElements()) { | ||
| o = e.nextElement(); | ||
| field.remove(o); // $ Alert | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected void modifyUnlocked() { | ||
| field = new Hashtable(); | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Security/CWE/CWE-367/TOCTOURace.ql | ||
| query: Security/CWE/CWE-367/TOCTOURace.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe we need to add a restriction that
Field.getDeclaringType() = thisType. It's probably always true because it would be rare for a field to not be accessed at all in its declaring class.We are trying to encode the logic that "
fis only accessed in synchronized methods on its declaring class (or the equivalent with synchronized statements)". We have already excluded initializers.My reasoning is:
thisreference from the constructor, but I think we can assume that doesn't happen.)funtil the constructor has finished.So, my conclusion is: the code relating to
thisprobably assumed thatfis a field ofthisType, and that's probably almost always implied by the code; as it stands, I think the code is okay even iffis not a field ofthisType; but if we make the change above to exclude constructors then we probably do want to add the restriction thatfis a field ofthisTypeto keep correctness.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that should not be necessary - it's not completely inconceivable to synchronize on a
thisfrom some other class. As long as that's done consistently, then it's fine.What we might need instead, though, is to check that the
Constructorbelongs to the same class as the field. And in a similar vein, we probably ought to check the same for theInitializerMethod.Also, this extension from
InitializerMethods toConstructors also applies to the two other disjuncts.So at this point we should probably factor out a helper predicate identifying field accesses that aren't in constructors or initializers of the declaring type of the field, and use that as the common limitation in all three
forexs.