Skip to content

Commit 31527a6

Browse files
committed
Refactor OS Checks & SystemProperty logic from review feedback
1 parent 103c770 commit 31527a6

File tree

8 files changed

+27
-34
lines changed

8 files changed

+27
-34
lines changed

java/ql/lib/semmle/code/java/JDK.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class StringPartialMatchMethod extends Method {
4949
}
5050

5151
/**
52-
* The index of the parameter that is being matched against.
52+
* Gets the index of the parameter that is being matched against.
5353
*/
5454
int getMatchParameterIndex() {
5555
if this.hasName("regionMatches")
@@ -266,7 +266,8 @@ class MethodAccessSystemGetProperty extends MethodAccess {
266266
* Holds if this call has a compile-time constant first argument with the value `propertyName`.
267267
* For example: `System.getProperty("user.dir")`.
268268
*
269-
* Note: Better to use `semmle.code.java.environment.SystemProperty#getSystemProperty` instead.
269+
* Note: Better to use `semmle.code.java.environment.SystemProperty#getSystemProperty` instead
270+
* as that predicate covers more libraries' and JDK API's ways of accessing the same information
270271
*/
271272
predicate hasCompileTimeConstantGetPropertyName(string propertyName) {
272273
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = propertyName

java/ql/lib/semmle/code/java/environment/SystemProperty.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private FieldAccess getSystemPropertyFromApacheSystemUtils(string propertyName)
8888
f.hasName("JAVA_SPECIFICATION_VENDOR") and propertyName = "java.specification.vendor"
8989
or
9090
f.hasName("JAVA_UTIL_PREFS_PREFERENCES_FACTORY") and
91-
propertyName = "java.util.prefs.PreferencesFactory"
91+
propertyName = "java.util.prefs.PreferencesFactory" // This really does break the lowercase convention obeyed everywhere else
9292
or
9393
f.hasName("JAVA_VENDOR") and propertyName = "java.vendor"
9494
or

java/ql/lib/semmle/code/java/os/OSCheck.qll

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import semmle.code.java.controlflow.Guards
77
private import semmle.code.java.environment.SystemProperty
88
private import semmle.code.java.frameworks.apache.Lang
99
private import semmle.code.java.dataflow.DataFlow
10+
private import semmle.code.java.dataflow.TaintTracking
1011

1112
/**
1213
* A guard that checks if the current os is Windows.
@@ -20,7 +21,7 @@ abstract class IsWindowsGuard extends Guard { }
2021
* When True, the OS is Windows.
2122
* When False, the OS *may* still be Windows.
2223
*/
23-
abstract class IsAnyWindowsGuard extends Guard { }
24+
abstract class IsSpecificWindowsVariant extends Guard { }
2425

2526
/**
2627
* A guard that checks if the current OS is unix or unix-like.
@@ -34,33 +35,20 @@ abstract class IsUnixGuard extends Guard { }
3435
* When True, the OS is unix or unix-like.
3536
* When False, the OS *may* still be unix or unix-like.
3637
*/
37-
abstract class IsAnyUnixGuard extends Guard { }
38+
abstract class IsSpecificUnixVariant extends Guard { }
3839

3940
/**
4041
* Holds when `ma` compares the current OS against the string constant `osString`.
4142
*/
4243
bindingset[osString]
4344
private predicate isOsFromSystemProp(MethodAccess ma, string osString) {
44-
exists(Expr systemGetPropertyExpr, Expr systemGetPropertyFlowsToExpr |
45-
systemGetPropertyExpr = getSystemProperty("os.name")
45+
TaintTracking::localExprTaint(getSystemProperty("os.name"), ma.getQualifier()) and // Call from System.getProperty (or equvalent) to some partial match method
46+
exists(StringPartialMatchMethod m, CompileTimeConstantExpr matchedStringConstant |
47+
m = ma.getMethod() and
48+
matchedStringConstant.getStringValue().toLowerCase().matches(osString)
4649
|
47-
DataFlow::localExprFlow(systemGetPropertyExpr, systemGetPropertyFlowsToExpr) and
48-
ma.getAnArgument().(CompileTimeConstantExpr).getStringValue().toLowerCase().matches(osString) and // Call from System.getProperty to some partial match method
49-
(
50-
systemGetPropertyFlowsToExpr = ma.getQualifier()
51-
or
52-
exists(MethodAccess caseChangeMa |
53-
caseChangeMa.getMethod() =
54-
any(Method m |
55-
m.getDeclaringType() instanceof TypeString and m.hasName(["toLowerCase", "toUpperCase"])
56-
)
57-
|
58-
systemGetPropertyFlowsToExpr = caseChangeMa.getQualifier() and // Call from System.getProperty to case-switching method
59-
DataFlow::localExprFlow(caseChangeMa, ma.getQualifier()) // Call from case-switching method to some partial match method
60-
)
61-
)
62-
) and
63-
ma.getMethod() instanceof StringPartialMatchMethod
50+
DataFlow::localExprFlow(matchedStringConstant, ma.getArgument(m.getMatchParameterIndex()))
51+
)
6452
}
6553

6654
private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAccess {
@@ -81,22 +69,26 @@ private Guard isOsFromSystemPropertyEqualityCheck(string propertyName, string co
8169
}
8270

8371
private class IsWindowsFromCharPathSeperator extends IsWindowsGuard {
84-
IsWindowsFromCharPathSeperator() { this = isOsFromSystemPropertyEqualityCheck("path.separator", "\\") }
72+
IsWindowsFromCharPathSeperator() {
73+
this = isOsFromSystemPropertyEqualityCheck("path.separator", "\\")
74+
}
8575
}
8676

8777
private class IsWindowsFromCharSeperator extends IsWindowsGuard {
8878
IsWindowsFromCharSeperator() { this = isOsFromSystemPropertyEqualityCheck("file.separator", ";") }
8979
}
9080

9181
private class IsUnixFromCharPathSeperator extends IsUnixGuard {
92-
IsUnixFromCharPathSeperator() { this = isOsFromSystemPropertyEqualityCheck("path.separator", "/") }
82+
IsUnixFromCharPathSeperator() {
83+
this = isOsFromSystemPropertyEqualityCheck("path.separator", "/")
84+
}
9385
}
9486

9587
private class IsUnixFromCharSeperator extends IsUnixGuard {
9688
IsUnixFromCharSeperator() { this = isOsFromSystemPropertyEqualityCheck("file.separator", ":") }
9789
}
9890

99-
private class IsUnixFromSystemProp extends IsAnyUnixGuard instanceof MethodAccess {
91+
private class IsUnixFromSystemProp extends IsSpecificUnixVariant instanceof MethodAccess {
10092
IsUnixFromSystemProp() { isOsFromSystemProp(this, ["mac%", "linux%"]) }
10193
}
10294

@@ -112,16 +104,16 @@ private class IsWindowsFromApacheCommons extends IsWindowsGuard instanceof Field
112104
IsWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS") }
113105
}
114106

115-
private class IsAnyWindowsFromApacheCommons extends IsAnyWindowsGuard instanceof FieldAccess {
116-
IsAnyWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS_%") }
107+
private class IsSpecificWindowsVariantFromApacheCommons extends IsSpecificWindowsVariant instanceof FieldAccess {
108+
IsSpecificWindowsVariantFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS_%") }
117109
}
118110

119111
private class IsUnixFromApacheCommons extends IsUnixGuard instanceof FieldAccess {
120112
IsUnixFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_UNIX") }
121113
}
122114

123-
private class IsAnyUnixFromApacheCommons extends IsAnyUnixGuard instanceof FieldAccess {
124-
IsAnyUnixFromApacheCommons() {
115+
private class IsSpecificUnixVariantFromApacheCommons extends IsSpecificUnixVariant instanceof FieldAccess {
116+
IsSpecificUnixVariantFromApacheCommons() {
125117
isOsFromApacheCommons(this,
126118
[
127119
"IS_OS_AIX", "IS_OS_HP_UX", "IS_OS_IRIX", "IS_OS_LINUX", "IS_OS_MAC%", "IS_OS_FREE_BSD",

java/ql/src/Security/CWE/CWE-200/TempDirLocalInformationDisclosure.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private class IsWindowsBarrierGuard extends WindowsOsBarrierGuard instanceof IsW
118118
override predicate checks(Expr e, boolean branch) { this.controls(e.getBasicBlock(), branch) }
119119
}
120120

121-
private class IsAnyWindowsBarrierGuard extends WindowsOsBarrierGuard instanceof IsAnyWindowsGuard {
121+
private class IsAnyWindowsBarrierGuard extends WindowsOsBarrierGuard instanceof IsSpecificWindowsVariant {
122122
override predicate checks(Expr e, boolean branch) {
123123
branch = true and this.controls(e.getBasicBlock(), branch)
124124
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import default
22
import semmle.code.java.os.OSCheck
33

4-
from IsAnyUnixGuard isAnyUnix
4+
from IsSpecificUnixVariant isAnyUnix
55
select isAnyUnix
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import default
22
import semmle.code.java.os.OSCheck
33

4-
from IsAnyWindowsGuard isAnyWindows
4+
from IsSpecificWindowsVariant isAnyWindows
55
select isAnyWindows

0 commit comments

Comments
 (0)