|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * unsafe unpack vulnerabilities, as well as extension points for |
| 4 | + * adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +import swift |
| 8 | +import codeql.swift.dataflow.DataFlow |
| 9 | +import codeql.swift.dataflow.ExternalFlow |
| 10 | + |
| 11 | +/** |
| 12 | + * A dataflow source for unsafe unpack vulnerabilities. |
| 13 | + */ |
| 14 | +abstract class UnsafeUnpackSource extends DataFlow::Node { } |
| 15 | + |
| 16 | +/** |
| 17 | + * A dataflow sink for unsafe unpack vulnerabilities. |
| 18 | + */ |
| 19 | +abstract class UnsafeUnpackSink extends DataFlow::Node { } |
| 20 | + |
| 21 | +/** |
| 22 | + * A barrier for unsafe unpack vulnerabilities. |
| 23 | + */ |
| 24 | +abstract class UnsafeUnpackBarrier extends DataFlow::Node { } |
| 25 | + |
| 26 | +/** |
| 27 | + * A unit class for adding additional flow steps. |
| 28 | + */ |
| 29 | +class UnsafeUnpackAdditionalFlowStep extends Unit { |
| 30 | + /** |
| 31 | + * Holds if the step from `node1` to `node2` should be considered a flow |
| 32 | + * step for paths related to unsafe unpack vulnerabilities. |
| 33 | + */ |
| 34 | + abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * A sink defined in a CSV model. |
| 39 | + */ |
| 40 | +private class DefaultUnsafeUnpackSink extends UnsafeUnpackSink { |
| 41 | + DefaultUnsafeUnpackSink() { sinkNode(this, "unsafe-unpack") } |
| 42 | +} |
| 43 | + |
| 44 | +private class UnsafeUnpackSinks extends SinkModelCsv { |
| 45 | + override predicate row(string row) { |
| 46 | + row = |
| 47 | + [ |
| 48 | + ";Zip;true;unzipFile(_:destination:overwrite:password:progress:fileOutputHandler:);;;Argument[0];unsafe-unpack", |
| 49 | + ";FileManager;true;unzipItem(at:to:skipCRC32:progress:pathEncoding:);;;Argument[0];unsafe-unpack", |
| 50 | + ] |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * An additional taint step for unsafe unpack vulnerabilities. |
| 56 | + */ |
| 57 | +private class UnsafeUnpackAdditionalDataFlowStep extends UnsafeUnpackAdditionalFlowStep { |
| 58 | + override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 59 | + exists(CallExpr initCall, CallExpr call | |
| 60 | + // If a zip file is remotely downloaded the destination path is tainted |
| 61 | + call.getStaticTarget().(Method).hasQualifiedName("Data", "write(to:options:)") and |
| 62 | + call.getQualifier() = initCall and |
| 63 | + initCall.getStaticTarget().(Initializer).getEnclosingDecl().(TypeDecl).getName() = "Data" and |
| 64 | + nodeFrom.asExpr() = initCall and |
| 65 | + nodeTo.asExpr() = call.getAnArgument().getExpr() |
| 66 | + ) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * A barrier for unsafe unpack vulnerabilities. |
| 72 | + */ |
| 73 | +private class UnsafeUnpackDefaultBarrier extends UnsafeUnpackBarrier { |
| 74 | + UnsafeUnpackDefaultBarrier() { |
| 75 | + // any numeric type |
| 76 | + this.asExpr().getType().getUnderlyingType().getABaseType*().getName() = "Numeric" |
| 77 | + } |
| 78 | +} |
0 commit comments