Skip to content

Swift: Add taint models for the Data class #11345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 24, 2022
Merged
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
47 changes: 46 additions & 1 deletion swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Data.qll
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import swift
private import codeql.swift.dataflow.ExternalFlow

private class DataSources extends SourceModelCsv {
override predicate row(string row) {
row = ";Data;true;init(contentsOf:options:);;;ReturnValue;remote"
}
}

private class DataSummaries extends SummaryModelCsv {
override predicate row(string row) { row = ";Data;true;init(_:);;;Argument[0];ReturnValue;taint" }
override predicate row(string row) {
row =
[
";Data;true;init(_:);;;Argument[0];ReturnValue;taint",
";Data;true;init(base64Encoded:options:);;;Argument[0];ReturnValue;taint",
";Data;true;init(buffer:);;;Argument[0];ReturnValue;taint",
";Data;true;init(bytes:count:);;;Argument[0];ReturnValue;taint",
";Data;true;init(contentsOf:options:);;;Argument[0];ReturnValue;taint",
";Data;true;init(bytesNoCopy:count:deallocator:);;;Argument[0];ReturnValue;taint",
";Data;true;init(referencing:);;;Argument[0];ReturnValue;taint",
";Data;true;append(_:);;;Argument[0];Argument[-1];taint",
";Data;true;append(_:count:);;;Argument[0];Argument[-1];taint",
";Data;true;append(contentsOf:);;;Argument[0];Argument[-1];taint",
";Data;true;base64EncodedData(options:);;;Argument[-1];ReturnValue;taint",
";Data;true;base64EncodedString(options:);;;Argument[-1];ReturnValue;taint",
";Data;true;compactMap(_:);;;Argument[-1];ReturnValue;taint",
";Data;true;copyBytes(to:);;;Argument[-1];Argument[0];taint",
";Data;true;copyBytes(to:count:);;;Argument[-1];Argument[0];taint",
";Data;true;copyBytes(to:from:);;;Argument[-1];Argument[0];taint",
";Data;true;flatMap(_:);;;Argument[-1];ReturnValue;taint",
";Data;true;insert(_:at:);;;Argument[0];Argument[-1];taint",
";Data;true;insert(contentsOf:at:);;;Argument[0];Argument[-1];taint",
";Data;true;map(_:);;;Argument[-1];ReturnValue;taint",
";Data;true;reduce(into:_:);;;Argument[-1];ReturnValue;taint",
";Data;true;replace(_:with:maxReplacements:);;;Argument[1];Argument[-1];taint",
";Data;true;replaceSubrange(_:with:);;;Argument[1];Argument[-1];taint",
";Data;true;replaceSubrange(_:with:count:);;;Argument[1];Argument[-1];taint",
";Data;true;replacing(_:with:maxReplacements:);;;Argument[1];Argument[-1];taint",
";Data;true;replacing(_:with:subrange:maxReplacements:);;;Argument[1];Argument[-1];taint",
// TODO: this should be implemented by a model of BidirectionalCollection
// ";Data;true;reversed();;;Argument[-1];ReturnValue;taint",
";Data;true;sorted();;;Argument[-1];ReturnValue;taint",
";Data;true;sorted(by:);;;Argument[-1];ReturnValue;taint",
";Data;true;sorted(using:);;;Argument[-1];ReturnValue;taint",
";Data;true;shuffled();;;Argument[-1];ReturnValue;taint",
";Data;true;shuffled(using:);;;Argument[-1];ReturnValue;taint",
";Data;true;trimmingPrefix(_:);;;Argument[-1];ReturnValue;taint",
";Data;true;trimmingPrefix(while:);;;Argument[-1];ReturnValue;taint"
]
}
}
4 changes: 0 additions & 4 deletions swift/ql/src/queries/Security/CWE-094/UnsafeJsEval.ql
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ class UnsafeJsEvalConfig extends TaintTracking::Configuration {
])
).getArgument(0)
or
arg =
any(CallExpr ce | ce.getStaticTarget().(MethodDecl).hasQualifiedName("Data", "init(_:)"))
.getArgument(0)
or
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

arg =
any(CallExpr ce |
ce.getStaticTarget().(MethodDecl).hasQualifiedName("String", "init(decoding:as:)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
| customurlschemes.swift:38:52:38:62 | url | external |
| customurlschemes.swift:43:9:43:28 | ...[...] | Remote URL in UIApplicationDelegate.application.launchOptions |
| customurlschemes.swift:48:9:48:28 | ...[...] | Remote URL in UIApplicationDelegate.application.launchOptions |
| data.swift:18:20:18:20 | call to init(contentsOf:options:) | external |
| data.swift:18:20:18:54 | call to init(contentsOf:options:) | external |
| nsdata.swift:18:17:18:17 | call to init(contentsOf:) | external |
| nsdata.swift:18:17:18:40 | call to init(contentsOf:) | external |
| nsdata.swift:19:17:19:17 | call to init(contentsOf:options:) | external |
Expand Down
19 changes: 19 additions & 0 deletions swift/ql/test/library-tests/dataflow/flowsources/data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// --- stubs ---

struct URL
{
init?(string: String) {}
}


struct Data {
struct ReadingOptions : OptionSet { let rawValue: Int }
init(contentsOf: URL, options: ReadingOptions) {}
}

// --- tests ---

func testData() {
let url = URL(string: "http://example.com/")
let data = try Data(contentsOf: url!, options: []) // SOURCE
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| data.swift:195:58:195:58 | &... | data.swift:195:58:195:73 | ...[...] |
| nsdata.swift:139:15:139:15 | nsDataTainted24 | nsdata.swift:139:15:139:31 | .bytes |
| nsdata.swift:140:15:140:15 | nsDataTainted24 | nsdata.swift:140:15:140:31 | .description |
| nsmutabledata.swift:49:15:49:15 | nsMutableDataTainted6 | nsmutabledata.swift:49:15:49:37 | .mutableBytes |
Expand Down
Loading