Skip to content

Commit 795f16b

Browse files
committed
Swift: Model 'printf' variants as cleartext logging sinks.
1 parent 06c2c42 commit 795f16b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

swift/ql/lib/codeql/swift/security/CleartextLoggingExtensions.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ private class CleartextLoggingFieldAdditionalFlowStep extends CleartextLoggingAd
9393
}
9494
}
9595

96+
/**
97+
* A sink that appears to be an imported C `printf` variant.
98+
* TODO: merge code with similar cases from the cleartext logging PR.
99+
*/
100+
private class PrintfCleartextLoggingSink extends CleartextLoggingSink {
101+
PrintfCleartextLoggingSink() {
102+
exists(CallExpr ce, FreeFunction f, int formatParamIndex |
103+
f.getShortName().matches("%printf%") and
104+
f.getParam(formatParamIndex).getName() = "format" and
105+
ce.getStaticTarget() = f and
106+
(
107+
this.asExpr() = ce.getArgument(formatParamIndex).getExpr() or
108+
this.asExpr() = ce.getArgument(f.getNumberOfParams() - 1).getExpr()
109+
)
110+
)
111+
}
112+
}
113+
96114
private class LoggingSinks extends SinkModelCsv {
97115
override predicate row(string row) {
98116
row =

swift/ql/test/query-tests/Security/CWE-312/cleartextLoggingTest.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,15 @@ func test6(passwordString: String) {
343343
NSException.raise(NSExceptionName("exception"), format: "\(passwordString) is incorrect!", arguments: getVaList([])) // $ MISSING: hasCleartextLogging=
344344
NSException.raise(NSExceptionName("exception"), format: "%s is incorrect!", arguments: getVaList([passwordString])) // $ MISSING: hasCleartextLogging=
345345

346-
_ = dprintf(0, "\(passwordString) is incorrect!") // $ MISSING: hasCleartextLogging=
347-
_ = dprintf(0, "%s is incorrect!", passwordString) // $ MISSING: hasCleartextLogging=
348-
_ = dprintf(0, "%s: %s is incorrect!", "foo", passwordString) // $ MISSING: hasCleartextLogging=
349-
_ = vprintf("\(passwordString) is incorrect!", getVaList([])) // $ MISSING: hasCleartextLogging=
350-
_ = vprintf("%s is incorrect!", getVaList([passwordString])) // $ MISSING: hasCleartextLogging=
346+
_ = dprintf(0, "\(passwordString) is incorrect!") // $ hasCleartextLogging=346
347+
_ = dprintf(0, "%s is incorrect!", passwordString) // $ hasCleartextLogging=347
348+
_ = dprintf(0, "%s: %s is incorrect!", "foo", passwordString) // $ hasCleartextLogging=348
349+
_ = vprintf("\(passwordString) is incorrect!", getVaList([])) // $ hasCleartextLogging=349
350+
_ = vprintf("%s is incorrect!", getVaList([passwordString])) // $ hasCleartextLogging=350
351351
_ = vfprintf(nil, "\(passwordString) is incorrect!", getVaList([])) // $ hasCleartextLogging=351
352352
_ = vfprintf(nil, "%s is incorrect!", getVaList([passwordString])) // $ hasCleartextLogging=352
353-
_ = vasprintf_l(nil, nil, "\(passwordString) is incorrect!", getVaList([])) // good (`sprintf` is not logging)
354-
_ = vasprintf_l(nil, nil, "%s is incorrect!", getVaList([passwordString])) // good (`sprintf` is not logging)
353+
_ = vasprintf_l(nil, nil, "\(passwordString) is incorrect!", getVaList([])) // $ SPURIOUS hasCleartextLogging=353 good (`sprintf` is not logging)
354+
_ = vasprintf_l(nil, nil, "%s is incorrect!", getVaList([passwordString])) // $ SPURIOUS hasCleartextLogging=354 good (`sprintf` is not logging)
355355
}
356356

357357
func test7(authKey: String, authKey2: Int, authKey3: Float) {

0 commit comments

Comments
 (0)