-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Add Guard Classes for checking OS & unify System Property Access #8032
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 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
cd073a2
Java: Add Guard Classes for checking OS
JLLeitschuh 39828fd
Apply OS guard checks to TempDirLocalInformationDisclosure
JLLeitschuh 3cdfc00
Cleanup from review feedback
JLLeitschuh 4951344
Update java/ql/lib/semmle/code/java/os/OSCheck.qll
JLLeitschuh 9f5022e
Review fixup and add test for apache SystemUtils
JLLeitschuh fd63107
Update OS Check from Review Feedback
JLLeitschuh 5913c9a
Refactor OS Guard Checks
JLLeitschuh dad9a02
Update TempDirInfoDisclosure with new OS Guards
JLLeitschuh 82d3cd8
Improve system property lookup
JLLeitschuh 3c53a05
Add OS Checks based upon separator or path separator
JLLeitschuh a7adbb7
Refactor more system property access logic
JLLeitschuh 85de9f3
Fix naming of OSCheck method
JLLeitschuh fea5006
Fix duplicated comment
JLLeitschuh 103c770
Apply suggestions from code review
JLLeitschuh 31527a6
Refactor OS Checks & SystemProperty logic from review feedback
JLLeitschuh 7ab193d
Add System.getProperties().getProperty support
JLLeitschuh 5243fe3
Apply suggestions from code review
JLLeitschuh 523ddb7
Cleanup after code review feedback
JLLeitschuh b282c7f
Apply suggestions from code review
JLLeitschuh 5b651f2
Fix insufficient tests and add documentation
JLLeitschuh a21992a
Minor refactoring to improve tests and documentation
JLLeitschuh 2a6c4e9
Add localFlowPlusInitializers
JLLeitschuh ecb8911
Apply suggestions from code review
JLLeitschuh 1c98642
Remove SystemProperty from FlowSources
JLLeitschuh 50ff2c2
Code cleanup from code review
JLLeitschuh 451661d
Improve guard class names
smowton 09cc8ee
Add tests for StandardSystemProperty
JLLeitschuh b11340c
Change note tense and detail level
smowton 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
Next
Next commit
Java: Add Guard Classes for checking OS
- Loading branch information
commit cd073a21736b8a9aeab480c438b09d3656bead1b
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * Provides classes and predicates for reasoning about checking characterizations about strings. | ||
| */ | ||
|
|
||
| import java | ||
|
|
||
| /** | ||
| * Holds if `ma` is a call to a method that checks a partial string match. | ||
| */ | ||
| predicate isStringPartialMatch(MethodAccess ma) { | ||
| ma.getMethod().getDeclaringType() instanceof TypeString and | ||
| ma.getMethod().getName() = | ||
| ["contains", "startsWith", "matches", "regionMatches", "indexOf", "lastIndexOf"] | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
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
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,93 @@ | ||
| /** | ||
| * Provides classes and predicates for guards that check for the current OS. | ||
| */ | ||
|
|
||
| import java | ||
| import semmle.code.java.controlflow.Guards | ||
| private import semmle.code.java.frameworks.apache.Lang | ||
| private import semmle.code.java.dataflow.DataFlow | ||
| private import semmle.code.java.StringCheck | ||
|
|
||
| /** | ||
| * A complimentatry guard that checks if the current platform is Windows. | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| */ | ||
| abstract class IsWindowsGuard extends Guard { } | ||
|
|
||
| /** | ||
| * A complimentatry guard that checks if the current platform is unix or unix-like. | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| */ | ||
| abstract class IsUnixGuard extends Guard { } | ||
|
|
||
| /** | ||
| * Holds when the MethodAccess is a call to check the current OS using either the upper case `osUpperCase` or lower case `osLowerCase` string constants. | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| */ | ||
| bindingset[osUpperCase, osLowerCase] | ||
| private predicate isOsFromSystemProp(MethodAccess ma, string osUpperCase, string osLowerCase) { | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| exists(MethodAccessSystemGetProperty sgpMa | | ||
| sgpMa.hasCompileTimeConstantGetPropertyName("os.name") | ||
| | | ||
| DataFlow::localExprFlow(sgpMa, ma.getQualifier()) and // Call from System.getProperty to some partial match method | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| ma.getAnArgument().(CompileTimeConstantExpr).getStringValue().matches(osUpperCase) | ||
| or | ||
| exists(MethodAccess toLowerCaseMa | | ||
| toLowerCaseMa.getMethod() = | ||
| any(Method m | m.getDeclaringType() instanceof TypeString and m.hasName("toLowerCase")) | ||
| | | ||
| DataFlow::localExprFlow(sgpMa, toLowerCaseMa.getQualifier()) and // Call from System.getProperty to toLowerCase | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| DataFlow::localExprFlow(toLowerCaseMa, ma.getQualifier()) and // Call from toLowerCase to some partial match method | ||
| ma.getAnArgument().(CompileTimeConstantExpr).getStringValue().matches(osLowerCase) | ||
| ) | ||
| ) and | ||
| isStringPartialMatch(ma) | ||
| } | ||
|
|
||
| private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAccess { | ||
| IsWindowsFromSystemProp() { isOsFromSystemProp(this, "Window%", "window%") } | ||
| } | ||
|
|
||
| private class IsUnixFromSystemProp extends IsUnixGuard instanceof MethodAccess { | ||
| IsUnixFromSystemProp() { | ||
| isOsFromSystemProp(this, ["Mac%", "Linux%", "LINUX%"], ["mac%", "linux%"]) | ||
| } | ||
| } | ||
|
|
||
| private predicate isOsFromApacheCommons(FieldAccess fa, string fieldName) { | ||
| exists(Field f | f = fa.getField() | | ||
| f.getDeclaringType() instanceof ApacheSystemUtilis and | ||
| f.hasName(fieldName) | ||
| ) | ||
| } | ||
|
|
||
| private class IsWindowsFromApacheCommons extends IsWindowsGuard instanceof FieldAccess { | ||
| IsWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS%") } | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private class IsUnixFromApacheCommons extends IsUnixGuard instanceof FieldAccess { | ||
| IsUnixFromApacheCommons() { isOsFromApacheCommons(this, ["IS_OS_UNIX"]) } | ||
| } | ||
|
|
||
| /** | ||
| * A guard that checks if the `java.nio.file.FileSystem` supports posix file permissions. | ||
| * This is often used to infer if the OS is unix-based. | ||
| * Looks for calls to `contains("poxix")` on the `supportedFileAttributeViews` method returned by `FileSystem`. | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| */ | ||
| private class IsUnixFromPosixFromFileSystem extends IsUnixGuard instanceof MethodAccess { | ||
| IsUnixFromPosixFromFileSystem() { | ||
| exists(Method m | m = this.getMethod() | | ||
| m.getDeclaringType() | ||
| .getASupertype*() | ||
| .getSourceDeclaration() | ||
| .hasQualifiedName("java.util", "Set") and | ||
| m.hasName("contains") | ||
| ) and | ||
| this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "posix" and | ||
| exists(Method supportedFileAttribtueViewsMethod | | ||
| supportedFileAttribtueViewsMethod.hasName("supportedFileAttributeViews") and | ||
| supportedFileAttribtueViewsMethod.getDeclaringType() instanceof TypeFileSystem | ||
| | | ||
| DataFlow::localExprFlow(any(MethodAccess ma | | ||
| ma.getMethod() = supportedFileAttribtueViewsMethod | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| ), super.getQualifier()) | ||
| ) | ||
| } | ||
| } | ||
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,34 @@ | ||
| import java.nio.file.FileSystems; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class Test { | ||
| void test() { | ||
| if (System.getProperty("os.name").contains("Windows")) { | ||
|
|
||
| } | ||
|
|
||
| if (System.getProperty("os.name").toLowerCase().contains("windows")) { | ||
|
|
||
| } | ||
|
|
||
| if (System.getProperty("os.name").contains("Linux")) { | ||
|
|
||
| } | ||
|
|
||
| if (System.getProperty("os.name").contains("Mac OS X")) { | ||
|
|
||
| } | ||
|
|
||
| if (System.getProperty("os.name").toLowerCase().contains("mac")) { | ||
|
|
||
| } | ||
|
|
||
| if (Path.of("whatever").getFileSystem().supportedFileAttributeViews().contains("posix")) { | ||
|
|
||
| } | ||
|
|
||
| if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { | ||
|
|
||
| } | ||
| } | ||
| } |
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,5 @@ | ||
| | Test.java:14:13:14:59 | contains(...) | | ||
| | Test.java:18:13:18:62 | contains(...) | | ||
| | Test.java:22:13:22:71 | contains(...) | | ||
| | Test.java:26:13:26:95 | contains(...) | | ||
| | Test.java:30:13:30:84 | contains(...) | |
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,5 @@ | ||
| import default | ||
| import semmle.code.java.os.OSCheck | ||
|
|
||
| from IsUnixGuard isUnix | ||
| select isUnix |
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,2 @@ | ||
| | Test.java:6:13:6:61 | contains(...) | | ||
| | Test.java:10:13:10:75 | contains(...) | |
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,5 @@ | ||
| import default | ||
| import semmle.code.java.os.OSCheck | ||
|
|
||
| from IsWindowsGuard isWindows | ||
| select isWindows |
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.