Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions unified/ql/lib/codeql/unified/internal/dataflow/Content.qll
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Content extends TContent {
}
}

/**
* A content that inherits taint from its containing object.
* Conservatively includes all content except captured variables.
*/
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
class TaintInheritingContent extends Content {
TaintInheritingContent() { not exists(this.asCapturedVariable()) }
}

private class TContentApprox = TArrayElement or TNamedMember or TCapturedVariable;

class ContentApprox extends Content, TContentApprox { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ module TaintTrackingInput implements InputSig<Location, DataFlowInput> {
predicate defaultAdditionalTaintStep(Node src, Node sink, string model) {
step(src, any(Step s | s.taint()), sink) and model = ""
or
// Read steps give rise to taint steps.
// TODO: Restrict this to contents that may carry taint.
DataFlowInput::readStep(src, _, sink) and model = ""
exists(ContentSet c |
DataFlowInput::readStep(src, c, sink) and
c.getAReadContent() instanceof TaintInheritingContent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a regression where a concatenated, already-tainted value is captured and read inside a closure. The focused unified data-flow test passes.

) and
model = ""
or
FlowSummaryOutput::summaryLocalStep(src, sink, false, model)
}
Expand Down
35 changes: 35 additions & 0 deletions unified/ql/test/library-tests/dataflow/capture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,38 @@ class C {
closure()
}
}

func taintedClosureCapture() {
let clean = "safe"
func source(_ label: String) -> () -> Void {
return {
sink(clean) // no flow: taint on the closure must not taint its captures
}
}
let closure = source("capture.closure")
closure()
sink(clean) // no flow
}

func taintedValueCapturedByClosure() {
let value = "prefix" + source("capture.tainted", "value")
let closure = {
sink(value) // $ hasTaintFlow=capture.tainted
}
closure()
}

func taintedContents() {
func source<T>(_ label: String, _ value: T) -> T { return value }

let box = source("capture.field", FieldBox())
sink(box.field) // $ hasTaintFlow=capture.field

let tuple = source("capture.tuple", ("safe", "safe"))
sink(tuple.0) // $ hasTaintFlow=capture.tuple

let array = source("capture.array", ["safe"])
for element in array {
sink(element) // $ hasTaintFlow=capture.array
}
}
32 changes: 32 additions & 0 deletions unified/ql/test/library-tests/dataflow/test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ edges
| capture.swift:214:34:214:37 | self [x] | capture.swift:215:18:215:21 | self [x] | provenance | |
| capture.swift:214:34:214:37 | self [x] | capture.swift:216:18:216:18 | x | provenance | |
| capture.swift:215:18:215:21 | self [x] | capture.swift:215:18:215:23 | ... .x | provenance | |
| capture.swift:235:9:235:13 | value | capture.swift:237:14:237:18 | value | provenance | |
| capture.swift:235:28:235:61 | source(...) | capture.swift:235:9:235:13 | value | provenance | |
| capture.swift:245:9:245:11 | box | capture.swift:246:10:246:12 | box | provenance | |
| capture.swift:245:15:245:49 | source(...) | capture.swift:245:9:245:11 | box | provenance | |
| capture.swift:246:10:246:12 | box | capture.swift:246:10:246:18 | ... .field | provenance | |
| capture.swift:248:9:248:13 | tuple | capture.swift:249:10:249:14 | tuple | provenance | |
| capture.swift:248:17:248:57 | source(...) | capture.swift:248:9:248:13 | tuple | provenance | |
| capture.swift:249:10:249:14 | tuple | capture.swift:249:10:249:16 | ... .0 | provenance | |
| capture.swift:251:9:251:13 | array | capture.swift:252:20:252:24 | array | provenance | |
| capture.swift:251:17:251:49 | source(...) | capture.swift:251:9:251:13 | array | provenance | |
| capture.swift:252:9:252:15 | element | capture.swift:253:14:253:20 | element | provenance | |
| capture.swift:252:20:252:24 | array | capture.swift:252:9:252:15 | element | provenance | |
| enums.swift:12:9:12:9 | e [case1.0] | enums.swift:14:12:14:12 | e [case1.0] | provenance | |
| enums.swift:12:13:12:35 | ... .case1(...) [case1.0] | enums.swift:12:9:12:9 | e [case1.0] | provenance | |
| enums.swift:12:21:12:34 | source(...) | enums.swift:12:13:12:35 | ... .case1(...) [case1.0] | provenance | |
Expand Down Expand Up @@ -1046,6 +1058,22 @@ nodes
| capture.swift:215:18:215:21 | self [x] | semmle.label | self [x] |
| capture.swift:215:18:215:23 | ... .x | semmle.label | ... .x |
| capture.swift:216:18:216:18 | x | semmle.label | x |
| capture.swift:235:9:235:13 | value | semmle.label | value |
| capture.swift:235:28:235:61 | source(...) | semmle.label | source(...) |
| capture.swift:237:14:237:18 | value | semmle.label | value |
| capture.swift:245:9:245:11 | box | semmle.label | box |
| capture.swift:245:15:245:49 | source(...) | semmle.label | source(...) |
| capture.swift:246:10:246:12 | box | semmle.label | box |
| capture.swift:246:10:246:18 | ... .field | semmle.label | ... .field |
| capture.swift:248:9:248:13 | tuple | semmle.label | tuple |
| capture.swift:248:17:248:57 | source(...) | semmle.label | source(...) |
| capture.swift:249:10:249:14 | tuple | semmle.label | tuple |
| capture.swift:249:10:249:16 | ... .0 | semmle.label | ... .0 |
| capture.swift:251:9:251:13 | array | semmle.label | array |
| capture.swift:251:17:251:49 | source(...) | semmle.label | source(...) |
| capture.swift:252:9:252:15 | element | semmle.label | element |
| capture.swift:252:20:252:24 | array | semmle.label | array |
| capture.swift:253:14:253:20 | element | semmle.label | element |
| enums.swift:12:9:12:9 | e [case1.0] | semmle.label | e [case1.0] |
| enums.swift:12:13:12:35 | ... .case1(...) [case1.0] | semmle.label | ... .case1(...) [case1.0] |
| enums.swift:12:21:12:34 | source(...) | semmle.label | source(...) |
Expand Down Expand Up @@ -1829,6 +1857,10 @@ testFailures
| capture.swift:207:18:207:18 | x | capture.swift:203:13:203:25 | source(...) | capture.swift:207:18:207:18 | x | $@ | capture.swift:203:13:203:25 | source(...) | source(...) |
| capture.swift:215:18:215:23 | ... .x | capture.swift:213:13:213:25 | source(...) | capture.swift:215:18:215:23 | ... .x | $@ | capture.swift:213:13:213:25 | source(...) | source(...) |
| capture.swift:216:18:216:18 | x | capture.swift:213:13:213:25 | source(...) | capture.swift:216:18:216:18 | x | $@ | capture.swift:213:13:213:25 | source(...) | source(...) |
| capture.swift:237:14:237:18 | value | capture.swift:235:28:235:61 | source(...) | capture.swift:237:14:237:18 | value | $@ | capture.swift:235:28:235:61 | source(...) | source(...) |
| capture.swift:246:10:246:18 | ... .field | capture.swift:245:15:245:49 | source(...) | capture.swift:246:10:246:18 | ... .field | $@ | capture.swift:245:15:245:49 | source(...) | source(...) |
| capture.swift:249:10:249:16 | ... .0 | capture.swift:248:17:248:57 | source(...) | capture.swift:249:10:249:16 | ... .0 | $@ | capture.swift:248:17:248:57 | source(...) | source(...) |
| capture.swift:253:14:253:20 | element | capture.swift:251:17:251:49 | source(...) | capture.swift:253:14:253:20 | element | $@ | capture.swift:251:17:251:49 | source(...) | source(...) |
| enums.swift:16:14:16:14 | x | enums.swift:12:21:12:34 | source(...) | enums.swift:16:14:16:14 | x | $@ | enums.swift:12:21:12:34 | source(...) | source(...) |
| enums.swift:27:14:27:14 | x | enums.swift:23:21:23:34 | source(...) | enums.swift:27:14:27:14 | x | $@ | enums.swift:23:21:23:34 | source(...) | source(...) |
| enums.swift:36:10:36:10 | x | enums.swift:34:21:34:34 | source(...) | enums.swift:36:10:36:10 | x | $@ | enums.swift:34:21:34:34 | source(...) | source(...) |
Expand Down
Loading