@@ -7,6 +7,7 @@ import semmle.code.java.controlflow.Guards
77private import semmle.code.java.environment.SystemProperty
88private import semmle.code.java.frameworks.apache.Lang
99private 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 */
4243bindingset [ osString]
4344private 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
6654private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAccess {
@@ -81,22 +69,26 @@ private Guard isOsFromSystemPropertyEqualityCheck(string propertyName, string co
8169}
8270
8371private class IsWindowsFromCharPathSeperator extends IsWindowsGuard {
84- IsWindowsFromCharPathSeperator ( ) { this = isOsFromSystemPropertyEqualityCheck ( "path.separator" , "\\" ) }
72+ IsWindowsFromCharPathSeperator ( ) {
73+ this = isOsFromSystemPropertyEqualityCheck ( "path.separator" , "\\" )
74+ }
8575}
8676
8777private class IsWindowsFromCharSeperator extends IsWindowsGuard {
8878 IsWindowsFromCharSeperator ( ) { this = isOsFromSystemPropertyEqualityCheck ( "file.separator" , ";" ) }
8979}
9080
9181private class IsUnixFromCharPathSeperator extends IsUnixGuard {
92- IsUnixFromCharPathSeperator ( ) { this = isOsFromSystemPropertyEqualityCheck ( "path.separator" , "/" ) }
82+ IsUnixFromCharPathSeperator ( ) {
83+ this = isOsFromSystemPropertyEqualityCheck ( "path.separator" , "/" )
84+ }
9385}
9486
9587private 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
119111private 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" ,
0 commit comments