|
| 1 | +/** |
| 2 | + * Provides classes and predicates for guards that check for the current OS. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +import semmle.code.java.controlflow.Guards |
| 7 | +private import semmle.code.java.frameworks.apache.Lang |
| 8 | +private import semmle.code.java.dataflow.DataFlow |
| 9 | +private import semmle.code.java.StringCheck |
| 10 | + |
| 11 | +/** |
| 12 | + * A complimentatry guard that checks if the current platform is Windows. |
| 13 | + */ |
| 14 | +abstract class IsWindowsGuard extends Guard { } |
| 15 | + |
| 16 | +/** |
| 17 | + * A complimentatry guard that checks if the current platform is unix or unix-like. |
| 18 | + */ |
| 19 | +abstract class IsUnixGuard extends Guard { } |
| 20 | + |
| 21 | +/** |
| 22 | + * Holds when the MethodAccess is a call to check the current OS using either the upper case `osUpperCase` or lower case `osLowerCase` string constants. |
| 23 | + */ |
| 24 | +bindingset[osUpperCase, osLowerCase] |
| 25 | +private predicate isOsFromSystemProp(MethodAccess ma, string osUpperCase, string osLowerCase) { |
| 26 | + exists(MethodAccessSystemGetProperty sgpMa | |
| 27 | + sgpMa.hasCompileTimeConstantGetPropertyName("os.name") |
| 28 | + | |
| 29 | + DataFlow::localExprFlow(sgpMa, ma.getQualifier()) and // Call from System.getProperty to some partial match method |
| 30 | + ma.getAnArgument().(CompileTimeConstantExpr).getStringValue().matches(osUpperCase) |
| 31 | + or |
| 32 | + exists(MethodAccess toLowerCaseMa | |
| 33 | + toLowerCaseMa.getMethod() = |
| 34 | + any(Method m | m.getDeclaringType() instanceof TypeString and m.hasName("toLowerCase")) |
| 35 | + | |
| 36 | + DataFlow::localExprFlow(sgpMa, toLowerCaseMa.getQualifier()) and // Call from System.getProperty to toLowerCase |
| 37 | + DataFlow::localExprFlow(toLowerCaseMa, ma.getQualifier()) and // Call from toLowerCase to some partial match method |
| 38 | + ma.getAnArgument().(CompileTimeConstantExpr).getStringValue().matches(osLowerCase) |
| 39 | + ) |
| 40 | + ) and |
| 41 | + isStringPartialMatch(ma) |
| 42 | +} |
| 43 | + |
| 44 | +private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAccess { |
| 45 | + IsWindowsFromSystemProp() { isOsFromSystemProp(this, "Window%", "window%") } |
| 46 | +} |
| 47 | + |
| 48 | +private class IsUnixFromSystemProp extends IsUnixGuard instanceof MethodAccess { |
| 49 | + IsUnixFromSystemProp() { |
| 50 | + isOsFromSystemProp(this, ["Mac%", "Linux%", "LINUX%"], ["mac%", "linux%"]) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +private predicate isOsFromApacheCommons(FieldAccess fa, string fieldName) { |
| 55 | + exists(Field f | f = fa.getField() | |
| 56 | + f.getDeclaringType() instanceof ApacheSystemUtilis and |
| 57 | + f.hasName(fieldName) |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +private class IsWindowsFromApacheCommons extends IsWindowsGuard instanceof FieldAccess { |
| 62 | + IsWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS%") } |
| 63 | +} |
| 64 | + |
| 65 | +private class IsUnixFromApacheCommons extends IsUnixGuard instanceof FieldAccess { |
| 66 | + IsUnixFromApacheCommons() { isOsFromApacheCommons(this, ["IS_OS_UNIX"]) } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A guard that checks if the `java.nio.file.FileSystem` supports posix file permissions. |
| 71 | + * This is often used to infer if the OS is unix-based. |
| 72 | + * Looks for calls to `contains("poxix")` on the `supportedFileAttributeViews` method returned by `FileSystem`. |
| 73 | + */ |
| 74 | +private class IsUnixFromPosixFromFileSystem extends IsUnixGuard instanceof MethodAccess { |
| 75 | + IsUnixFromPosixFromFileSystem() { |
| 76 | + exists(Method m | m = this.getMethod() | |
| 77 | + m.getDeclaringType() |
| 78 | + .getASupertype*() |
| 79 | + .getSourceDeclaration() |
| 80 | + .hasQualifiedName("java.util", "Set") and |
| 81 | + m.hasName("contains") |
| 82 | + ) and |
| 83 | + this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "posix" and |
| 84 | + exists(Method supportedFileAttribtueViewsMethod | |
| 85 | + supportedFileAttribtueViewsMethod.hasName("supportedFileAttributeViews") and |
| 86 | + supportedFileAttribtueViewsMethod.getDeclaringType() instanceof TypeFileSystem |
| 87 | + | |
| 88 | + DataFlow::localExprFlow(any(MethodAccess ma | |
| 89 | + ma.getMethod() = supportedFileAttribtueViewsMethod |
| 90 | + ), super.getQualifier()) |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
0 commit comments