|
| 1 | +/** Provides classes and predicates to reason about path injection vulnerabilities. */ |
| 2 | + |
| 3 | +import swift |
| 4 | +private import codeql.swift.controlflow.BasicBlocks |
| 5 | +private import codeql.swift.controlflow.ControlFlowGraph |
| 6 | +private import codeql.swift.dataflow.DataFlow |
| 7 | +private import codeql.swift.dataflow.ExternalFlow |
| 8 | +private import codeql.swift.dataflow.TaintTracking |
| 9 | +private import codeql.swift.generated.ParentChild |
| 10 | +private import codeql.swift.frameworks.StandardLibrary.FilePath |
| 11 | + |
| 12 | +/** A data flow sink for path injection vulnerabilities. */ |
| 13 | +abstract class PathInjectionSink extends DataFlow::Node { } |
| 14 | + |
| 15 | +/** A sanitizer for path injection vulnerabilities. */ |
| 16 | +abstract class PathInjectionSanitizer extends DataFlow::Node { } |
| 17 | + |
| 18 | +/** |
| 19 | + * A unit class for adding additional taint steps. |
| 20 | + * |
| 21 | + * Extend this class to add additional taint steps that should apply to paths related to |
| 22 | + * path injection vulnerabilities. |
| 23 | + */ |
| 24 | +class PathInjectionAdditionalTaintStep extends Unit { |
| 25 | + /** |
| 26 | + * Holds if the step from `node1` to `node2` should be considered a taint |
| 27 | + * step for paths related to path injection vulnerabilities. |
| 28 | + */ |
| 29 | + abstract predicate step(DataFlow::Node node1, DataFlow::Node node2); |
| 30 | +} |
| 31 | + |
| 32 | +private class DefaultPathInjectionSink extends PathInjectionSink { |
| 33 | + DefaultPathInjectionSink() { sinkNode(this, "path-injection") } |
| 34 | +} |
| 35 | + |
| 36 | +private class DefaultPathInjectionSanitizer extends PathInjectionSanitizer { |
| 37 | + DefaultPathInjectionSanitizer() { |
| 38 | + // This is a simplified implementation. |
| 39 | + // TODO: Implement a complete path sanitizer when Guards are available. |
| 40 | + exists(CallExpr starts, CallExpr normalize, DataFlow::Node validated | |
| 41 | + starts.getStaticTarget().getName() = "starts(with:)" and |
| 42 | + starts.getStaticTarget().getEnclosingDecl() instanceof FilePath and |
| 43 | + normalize.getStaticTarget().getName() = "lexicallyNormalized()" and |
| 44 | + normalize.getStaticTarget().getEnclosingDecl() instanceof FilePath |
| 45 | + | |
| 46 | + TaintTracking::localTaint(validated, DataFlow::exprNode(normalize.getQualifier())) and |
| 47 | + DataFlow::localExprFlow(normalize, starts.getQualifier()) and |
| 48 | + DataFlow::localFlow(validated, this) and |
| 49 | + exists(ConditionBlock bb, SuccessorTypes::BooleanSuccessor b | |
| 50 | + bb.getANode().getNode().asAstNode().(IfStmt).getACondition() = getImmediateParent*(starts) and |
| 51 | + b.getValue() = true |
| 52 | + | |
| 53 | + bb.controls(this.getCfgNode().getBasicBlock(), b) |
| 54 | + ) |
| 55 | + ) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +private class PathInjectionSinks extends SinkModelCsv { |
| 60 | + override predicate row(string row) { |
| 61 | + row = |
| 62 | + [ |
| 63 | + ";Data;true;write(to:options:);;;Argument[0];path-injection", |
| 64 | + ";NSData;true;write(to:atomically:);;;Argument[0];path-injection", |
| 65 | + ";NSData;true;write(to:options:);;;Argument[0];path-injection", |
| 66 | + ";NSData;true;write(toFile:atomically:);;;Argument[0];path-injection", |
| 67 | + ";NSData;true;write(toFile:options:);;;Argument[0];path-injection", |
| 68 | + ";FileManager;true;contentsOfDirectory(at:includingPropertiesForKeys:options:);;;Argument[0];path-injection", |
| 69 | + ";FileManager;true;contentsOfDirectory(atPath:);;;Argument[0];path-injection", |
| 70 | + ";FileManager;true;enumerator(at:includingPropertiesForKeys:options:errorHandler:);;;Argument[0];path-injection", |
| 71 | + ";FileManager;true;enumerator(atPath:);;;Argument[0];path-injection", |
| 72 | + ";FileManager;true;subpathsOfDirectory(atPath:);;;Argument[0];path-injection", |
| 73 | + ";FileManager;true;subpaths(atPath:);;;Argument[0];path-injection", |
| 74 | + ";FileManager;true;createDirectory(at:withIntermediateDirectories:attributes:);;;Argument[0];path-injection", |
| 75 | + ";FileManager;true;createDirectory(atPath:withIntermediateDirectories:attributes:);;;Argument[0];path-injection", |
| 76 | + ";FileManager;true;createFile(atPath:contents:attributes:);;;Argument[0];path-injection", |
| 77 | + ";FileManager;true;removeItem(at:);;;Argument[0];path-injection", |
| 78 | + ";FileManager;true;removeItem(atPath:);;;Argument[0];path-injection", |
| 79 | + ";FileManager;true;trashItem(at:resultingItemURL:);;;Argument[0];path-injection", |
| 80 | + ";FileManager;true;replaceItem(at:withItemAt:backupItemName:options:resultingItemURL:);;;Argument[0..1];path-injection", |
| 81 | + ";FileManager;true;replaceItemAt(_:withItemAt:backupItemName:options:);;;Argument[0..1];path-injection", |
| 82 | + ";FileManager;true;copyItem(at:to:);;;Argument[0..1];path-injection", |
| 83 | + ";FileManager;true;copyItem(atPath:toPath:);;;Argument[0..1];path-injection", |
| 84 | + ";FileManager;true;moveItem(at:to:);;;Argument[0..1];path-injection", |
| 85 | + ";FileManager;true;moveItem(atPath:toPath:);;;Argument[0..1];path-injection", |
| 86 | + ";FileManager;true;createSymbolicLink(at:withDestinationURL:);;;Argument[0..1];path-injection", |
| 87 | + ";FileManager;true;createSymbolicLink(atPath:withDestinationPath:);;;Argument[0..1];path-injection", |
| 88 | + ";FileManager;true;linkItem(at:to:);;;Argument[0..1];path-injection", |
| 89 | + ";FileManager;true;linkItem(atPath:toPath:);;;Argument[0..1];path-injection", |
| 90 | + ";FileManager;true;destinationOfSymbolicLink(atPath:);;;Argument[0];path-injection", |
| 91 | + ";FileManager;true;fileExists(atPath:);;;Argument[0];path-injection", |
| 92 | + ";FileManager;true;fileExists(atPath:isDirectory:);;;Argument[0];path-injection", |
| 93 | + ";FileManager;true;setAttributes(_:ofItemAtPath:);;;Argument[1];path-injection", |
| 94 | + ";FileManager;true;contents(atPath:);;;Argument[0];path-injection", |
| 95 | + ";FileManager;true;contentsEqual(atPath:andPath:);;;Argument[0..1];path-injection", |
| 96 | + ";FileManager;true;changeCurrentDirectoryPath(_:);;;Argument[0];path-injection", |
| 97 | + ";FileManager;true;unmountVolume(at:options:completionHandler:);;;Argument[0];path-injection", |
| 98 | + // Deprecated FileManager methods: |
| 99 | + ";FileManager;true;changeFileAttributes(_:atPath:);;;Argument[1];path-injection", |
| 100 | + ";FileManager;true;directoryContents(atPath:);;;Argument[0];path-injection", |
| 101 | + ";FileManager;true;createDirectory(atPath:attributes:);;;Argument[0];path-injection", |
| 102 | + ";FileManager;true;createSymbolicLink(atPath:pathContent:);;;Argument[0..1];path-injection", |
| 103 | + ";FileManager;true;pathContentOfSymbolicLink(atPath:);;;Argument[0];path-injection", |
| 104 | + ";FileManager;true;replaceItemAtURL(originalItemURL:withItemAtURL:backupItemName:options:);;;Argument[0..1];path-injection", |
| 105 | + ";NIOFileHandle;true;init(descriptor:);;;Argument[0];path-injection", |
| 106 | + ";NIOFileHandle;true;init(path:mode:flags:);;;Argument[0];path-injection", |
| 107 | + ";NIOFileHandle;true;init(path:);;;Argument[0];path-injection" |
| 108 | + ] |
| 109 | + } |
| 110 | +} |
0 commit comments