|
| 1 | +/** |
| 2 | + * Definitions for reasoning about lists and arrays that are to be used as arguments to an external process. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +import semmle.code.java.dataflow.SSA |
| 7 | +import semmle.code.java.Collections |
| 8 | + |
| 9 | +/** |
| 10 | + * Holds if `ex` is used safely as an argument to a command; |
| 11 | + * i.e. it's not in the first position and it's not a shell command. |
| 12 | + */ |
| 13 | +predicate isSafeCommandArgument(Expr ex) { |
| 14 | + exists(ArrayInit ai, int i | |
| 15 | + ex = ai.getInit(i) and |
| 16 | + i > 0 and |
| 17 | + not isShell(ai.getInit(0)) |
| 18 | + ) |
| 19 | + or |
| 20 | + exists(CommandArgumentList cal | |
| 21 | + not cal.isShell() and |
| 22 | + ex = cal.getASubsequentAdd().getArgument(0) |
| 23 | + ) |
| 24 | + or |
| 25 | + exists(CommandArgArrayImmutableFirst caa | |
| 26 | + not caa.isShell() and |
| 27 | + ex = caa.getAWrite(any(int i | i > 0)) |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Holds if the given expression is the name of a shell command such as bash or python |
| 33 | + */ |
| 34 | +private predicate isShell(Expr ex) { |
| 35 | + exists(string cmd | cmd = ex.(StringLiteral).getValue() | |
| 36 | + cmd.regexpMatch(".*(sh|javac?|python[23]?|osascript|cmd)(\\.exe)?$") |
| 37 | + ) |
| 38 | + or |
| 39 | + exists(SsaVariable ssa | |
| 40 | + ex = ssa.getAUse() and |
| 41 | + isShell(ssa.getAnUltimateDefinition().(SsaExplicitUpdate).getDefiningExpr()) |
| 42 | + ) |
| 43 | + or |
| 44 | + isShell(ex.(Assignment).getRhs()) |
| 45 | + or |
| 46 | + isShell(ex.(LocalVariableDeclExpr).getInit()) |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * A type that could be a list of strings. Includes raw `List` types. |
| 51 | + */ |
| 52 | +private class ListOfStringType extends CollectionType { |
| 53 | + ListOfStringType() { |
| 54 | + this.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "List") and |
| 55 | + this.getElementType().getASubtype*() instanceof TypeString |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A variable that could be used as a list of arguments to a command. |
| 61 | + */ |
| 62 | +private class CommandArgumentList extends SsaExplicitUpdate { |
| 63 | + CommandArgumentList() { |
| 64 | + this.getSourceVariable().getType() instanceof ListOfStringType and |
| 65 | + forex(CollectionMutation ma | ma.getQualifier() = this.getAUse() | |
| 66 | + ma.getMethod().getName().matches("add%") |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + /** Gets a use of the variable for which the list could be empty. */ |
| 71 | + private RValue getAUseBeforeFirstAdd() { |
| 72 | + result = getAFirstUse() |
| 73 | + or |
| 74 | + exists(RValue mid | |
| 75 | + mid = getAUseBeforeFirstAdd() and |
| 76 | + adjacentUseUse(mid, result) and |
| 77 | + not exists(MethodAccess ma | |
| 78 | + mid = ma.getQualifier() and |
| 79 | + ma.getMethod().hasName("add") |
| 80 | + ) |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets an addition to this list, i.e. a call to an `add` or `addAll` method. |
| 86 | + */ |
| 87 | + MethodAccess getAnAdd() { |
| 88 | + result.getQualifier() = getAUse() and |
| 89 | + result.getMethod().getName().matches("add%") |
| 90 | + } |
| 91 | + |
| 92 | + /** Gets an addition to this list which could be its first element. */ |
| 93 | + MethodAccess getAFirstAdd() { |
| 94 | + result = getAnAdd() and |
| 95 | + result.getQualifier() = getAUseBeforeFirstAdd() |
| 96 | + } |
| 97 | + |
| 98 | + /** Gets an addition to this list which is not the first element. */ |
| 99 | + MethodAccess getASubsequentAdd() { |
| 100 | + result = getAnAdd() and |
| 101 | + not result = getAFirstAdd() |
| 102 | + } |
| 103 | + |
| 104 | + /** Holds if the first element of this list is a shell command. */ |
| 105 | + predicate isShell() { |
| 106 | + exists(MethodAccess ma | ma = getAFirstAdd() and isShell(ma.getArgument(0))) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * The type `String[]`. |
| 112 | + */ |
| 113 | +private class ArrayOfStringType extends Array { |
| 114 | + ArrayOfStringType() { this.getElementType() instanceof TypeString } |
| 115 | +} |
| 116 | + |
| 117 | +private predicate arrayLValue(ArrayAccess acc) { exists(Assignment a | a.getDest() = acc) } |
| 118 | + |
| 119 | +/** |
| 120 | + * A variable that could be an array of arguments to a command. |
| 121 | + */ |
| 122 | +private class CommandArgumentArray extends SsaExplicitUpdate { |
| 123 | + CommandArgumentArray() { |
| 124 | + this.getSourceVariable().getType() instanceof ArrayOfStringType and |
| 125 | + forall(ArrayAccess a | a.getArray() = getAUse() and arrayLValue(a) | |
| 126 | + a.getIndexExpr() instanceof CompileTimeConstantExpr |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + /** Gets an expression that is written to the given index of this array at the given use. */ |
| 131 | + Expr getAWrite(int index, RValue use) { |
| 132 | + exists(Assignment a, ArrayAccess acc | |
| 133 | + acc.getArray() = use and |
| 134 | + use = this.getAUse() and |
| 135 | + index = acc.getIndexExpr().(CompileTimeConstantExpr).getIntValue() and |
| 136 | + acc = a.getDest() and |
| 137 | + result = a.getRhs() |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + /** Gets an expression that is written to the given index of this array. */ |
| 142 | + Expr getAWrite(int index) { result = getAWrite(index, _) } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * A `CommandArgArray` whose element at index 0 is never written to, except possibly once to initialise it. |
| 147 | + */ |
| 148 | +private class CommandArgArrayImmutableFirst extends CommandArgumentArray { |
| 149 | + CommandArgArrayImmutableFirst() { |
| 150 | + (exists(getAWrite(0)) or exists(firstElementOf(this.getDefiningExpr()))) and |
| 151 | + forall(RValue use | exists(this.getAWrite(0, use)) | use = this.getAFirstUse()) |
| 152 | + } |
| 153 | + |
| 154 | + /** Gets the first element of this array. */ |
| 155 | + Expr getFirstElement() { |
| 156 | + result = getAWrite(0) |
| 157 | + or |
| 158 | + not exists(getAWrite(0)) and |
| 159 | + result = firstElementOf(getDefiningExpr()) |
| 160 | + } |
| 161 | + |
| 162 | + /** Holds if the first element of this array is a shell command. */ |
| 163 | + predicate isShell() { isShell(getFirstElement()) } |
| 164 | +} |
| 165 | + |
| 166 | +/** Gets the first element of an imutable array of strings */ |
| 167 | +private Expr firstElementOf(Expr arr) { |
| 168 | + arr.getType() instanceof ArrayOfStringType and |
| 169 | + ( |
| 170 | + result = firstElementOf(arr.(Assignment).getRhs()) |
| 171 | + or |
| 172 | + result = firstElementOf(arr.(LocalVariableDeclExpr).getInit()) |
| 173 | + or |
| 174 | + exists(CommandArgArrayImmutableFirst caa | arr = caa.getAUse() | result = caa.getFirstElement()) |
| 175 | + or |
| 176 | + exists(MethodAccess ma, Method m | |
| 177 | + arr = ma and |
| 178 | + ma.getMethod() = m and |
| 179 | + m.getDeclaringType().hasQualifiedName("java.util", "Arrays") and |
| 180 | + m.hasName("copyOf") and |
| 181 | + result = firstElementOf(ma.getArgument(0)) |
| 182 | + ) |
| 183 | + or |
| 184 | + exists(Field f | |
| 185 | + f.isStatic() and |
| 186 | + arr.(FieldRead).getField() = f and |
| 187 | + result = firstElementOf(f.getInitializer()) |
| 188 | + ) |
| 189 | + or |
| 190 | + result = arr.(ArrayInit).getInit(0) |
| 191 | + or |
| 192 | + result = arr.(ArrayCreationExpr).getInit().getInit(0) |
| 193 | + ) |
| 194 | +} |
0 commit comments