Skip to content

Commit be746b7

Browse files
authored
Merge pull request #21493 from MarkLee131/fix/format-string-fp-in-printf-impl
C++: exclude printf implementation internals from uncontrolled format string sinks
2 parents 06ea72c + 6452cc5 commit be746b7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cpp/ql/src/Security/CWE/CWE-134/UncontrolledFormatString.ql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,31 @@ import Flow::PathGraph
2323

2424
predicate isSource(FlowSource source, string sourceType) { sourceType = source.getSourceType() }
2525

26+
/**
27+
* Holds if `f` is a printf-like function or a (possibly nested) wrapper
28+
* that forwards a format-string parameter to one.
29+
*
30+
* Functions that *implement* printf-like behavior (e.g. a custom
31+
* `vsnprintf` variant) internally parse the caller-supplied format string
32+
* and build small, bounded, local format strings such as `"%d"` or `"%ld"`
33+
* for inner `sprintf` calls. Taint that reaches those inner calls via the
34+
* parsed format specifier is not exploitable, so sinks inside such
35+
* functions should be excluded.
36+
*/
37+
private predicate isPrintfImplementation(Function f) {
38+
f instanceof PrintfLikeFunction
39+
or
40+
exists(PrintfLikeFunction printf | printf.wrapperFunction(f, _, _))
41+
}
42+
2643
module Config implements DataFlow::ConfigSig {
2744
predicate isSource(DataFlow::Node node) { isSource(node, _) }
2845

2946
predicate isSink(DataFlow::Node node) {
3047
exists(PrintfLikeFunction printf |
3148
printf.outermostWrapperFunctionCall([node.asExpr(), node.asIndirectExpr()], _)
32-
)
49+
) and
50+
not isPrintfImplementation([node.asExpr(), node.asIndirectExpr()].getEnclosingFunction())
3351
}
3452

3553
private predicate isArithmeticNonCharType(ArithmeticType type) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed an issue with the "Uncontrolled format string" (`cpp/tainted-format-string`) query involving certain kinds of formatting function implementations.

0 commit comments

Comments
 (0)