|
| 1 | +/** |
| 2 | + * @name Arbitrary file write during archive extraction ("Zip Slip") |
| 3 | + * @description Extracting files from a malicious archive without validating that the |
| 4 | + * destination file path is within the destination directory can cause files outside |
| 5 | + * the destination directory to be overwritten. |
| 6 | + * @kind problem |
| 7 | + * @id java/zipslip |
| 8 | + * @problem.severity error |
| 9 | + * @precision high |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-022 |
| 12 | + */ |
| 13 | + |
| 14 | +import java |
| 15 | +import semmle.code.java.controlflow.Guards |
| 16 | +import semmle.code.java.dataflow.SSA |
| 17 | +import semmle.code.java.dataflow.TaintTracking |
| 18 | +import DataFlow |
| 19 | + |
| 20 | +/** |
| 21 | + * A method that returns the name of an archive entry. |
| 22 | + */ |
| 23 | +class ArchiveEntryNameMethod extends Method { |
| 24 | + ArchiveEntryNameMethod() { |
| 25 | + exists(RefType archiveEntry | |
| 26 | + archiveEntry.hasQualifiedName("java.util.zip", "ZipEntry") or |
| 27 | + archiveEntry.hasQualifiedName("org.apache.commons.compress.archivers", "ArchiveEntry") |
| 28 | + | |
| 29 | + this.getDeclaringType().getASupertype*() = archiveEntry and |
| 30 | + this.hasName("getName") |
| 31 | + ) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * An expression that will be treated as the destination of a write. |
| 37 | + */ |
| 38 | +class WrittenFileName extends Expr { |
| 39 | + WrittenFileName() { |
| 40 | + // Constructors that write to their first argument. |
| 41 | + exists(ConstructorCall ctr | this = ctr.getArgument(0) | |
| 42 | + exists(Class c | ctr.getConstructor() = c.getAConstructor() | |
| 43 | + c.hasQualifiedName("java.io", "FileOutputStream") or |
| 44 | + c.hasQualifiedName("java.io", "RandomAccessFile") or |
| 45 | + c.hasQualifiedName("java.io", "FileWriter") |
| 46 | + ) |
| 47 | + ) |
| 48 | + or |
| 49 | + // Methods that write to their n'th argument |
| 50 | + exists(MethodAccess call, int n | this = call.getArgument(n) | |
| 51 | + call.getMethod().getDeclaringType().hasQualifiedName("java.nio.file", "Files") and |
| 52 | + ( |
| 53 | + call.getMethod().getName().regexpMatch("new.*Reader|newOutputStream|create.*") and n = 0 |
| 54 | + or |
| 55 | + call.getMethod().hasName("copy") and n = 1 |
| 56 | + or |
| 57 | + call.getMethod().hasName("move") and n = 1 |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Holds if `n1` to `n2` is a dataflow step that converts between `String`, |
| 65 | + * `File`, and `Path`. |
| 66 | + */ |
| 67 | +predicate filePathStep(ExprNode n1, ExprNode n2) { |
| 68 | + exists(ConstructorCall cc | cc.getConstructedType() instanceof TypeFile | |
| 69 | + n1.asExpr() = cc.getAnArgument() and |
| 70 | + n2.asExpr() = cc |
| 71 | + ) |
| 72 | + or |
| 73 | + exists(MethodAccess ma, Method m | |
| 74 | + ma.getMethod() = m and |
| 75 | + n1.asExpr() = ma.getQualifier() and |
| 76 | + n2.asExpr() = ma |
| 77 | + | |
| 78 | + m.getDeclaringType() instanceof TypeFile and m.hasName("toPath") |
| 79 | + or |
| 80 | + m.getDeclaringType() instanceof TypePath and m.hasName("toAbsolutePath") |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +predicate fileTaintStep(ExprNode n1, ExprNode n2) { |
| 85 | + exists(MethodAccess ma, Method m | |
| 86 | + n1.asExpr() = ma.getQualifier() or |
| 87 | + n1.asExpr() = ma.getAnArgument() |
| 88 | + | |
| 89 | + n2.asExpr() = ma and |
| 90 | + ma.getMethod() = m and |
| 91 | + m.getDeclaringType() instanceof TypePath and |
| 92 | + m.hasName("resolve") |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +predicate localFileValueStep(Node n1, Node n2) { |
| 97 | + localFlowStep(n1, n2) or |
| 98 | + filePathStep(n1, n2) |
| 99 | +} |
| 100 | + |
| 101 | +predicate localFileValueStepPlus(Node n1, Node n2) = fastTC(localFileValueStep/2)(n1, n2) |
| 102 | + |
| 103 | +/** |
| 104 | + * Holds if `check` is a guard that checks whether `var` is a file path with a |
| 105 | + * specific prefix when put in canonical form, thus guarding against ZipSlip. |
| 106 | + */ |
| 107 | +predicate validateFilePath(SsaVariable var, Guard check) { |
| 108 | + // `var.getCanonicalFile().toPath().startsWith(...)`, |
| 109 | + // `var.getCanonicalPath().startsWith(...)`, or |
| 110 | + // `var.toPath().normalize().startsWith(...)` |
| 111 | + exists(MethodAccess normalize, MethodAccess startsWith, Node n1, Node n2, Node n3, Node n4 | |
| 112 | + n1.asExpr() = var.getAUse() and |
| 113 | + n2.asExpr() = normalize.getQualifier() and |
| 114 | + (n1 = n2 or localFileValueStepPlus(n1, n2)) and |
| 115 | + n3.asExpr() = normalize and |
| 116 | + n4.asExpr() = startsWith.getQualifier() and |
| 117 | + (n3 = n4 or localFileValueStepPlus(n3, n4)) and |
| 118 | + check = startsWith and |
| 119 | + startsWith.getMethod().hasName("startsWith") and |
| 120 | + ( |
| 121 | + normalize.getMethod().hasName("getCanonicalFile") or |
| 122 | + normalize.getMethod().hasName("getCanonicalPath") or |
| 123 | + normalize.getMethod().hasName("normalize") |
| 124 | + ) |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Holds if `m` validates its `arg`th parameter. |
| 130 | + */ |
| 131 | +predicate validationMethod(Method m, int arg) { |
| 132 | + exists(Guard check, SsaImplicitInit var, ControlFlowNode exit, ControlFlowNode normexit | |
| 133 | + validateFilePath(var, check) and |
| 134 | + var.isParameterDefinition(m.getParameter(arg)) and |
| 135 | + exit = m and |
| 136 | + normexit.getANormalSuccessor() = exit and |
| 137 | + 1 = strictcount(ControlFlowNode n | n.getANormalSuccessor() = exit) |
| 138 | + | |
| 139 | + check.(ConditionNode).getATrueSuccessor() = exit or |
| 140 | + check.controls(normexit.getBasicBlock(), true) |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +class ZipSlipConfiguration extends TaintTracking::Configuration { |
| 145 | + ZipSlipConfiguration() { this = "ZipSlip" } |
| 146 | + |
| 147 | + override predicate isSource(Node source) { |
| 148 | + source.asExpr().(MethodAccess).getMethod() instanceof ArchiveEntryNameMethod |
| 149 | + } |
| 150 | + |
| 151 | + override predicate isSink(Node sink) { sink.asExpr() instanceof WrittenFileName } |
| 152 | + |
| 153 | + override predicate isAdditionalTaintStep(Node n1, Node n2) { |
| 154 | + filePathStep(n1, n2) or fileTaintStep(n1, n2) |
| 155 | + } |
| 156 | + |
| 157 | + override predicate isSanitizer(Node node) { |
| 158 | + exists(Guard g, SsaVariable var, RValue varuse | validateFilePath(var, g) | |
| 159 | + varuse = node.asExpr() and |
| 160 | + varuse = var.getAUse() and |
| 161 | + g.controls(varuse.getBasicBlock(), true) |
| 162 | + ) |
| 163 | + or |
| 164 | + exists(MethodAccess ma, int pos, RValue rv | |
| 165 | + validationMethod(ma.getMethod(), pos) and |
| 166 | + ma.getArgument(pos) = rv and |
| 167 | + adjacentUseUseSameVar(rv, node.asExpr()) and |
| 168 | + ma.getBasicBlock().bbDominates(node.asExpr().getBasicBlock()) |
| 169 | + ) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +from Node source, Node sink |
| 174 | +where any(ZipSlipConfiguration c).hasFlow(source, sink) |
| 175 | +select source, "Unsanitized archive entry, which may contain '..', is used in a $@.", sink, |
| 176 | + "file system operation" |
0 commit comments