1515
1616import cpp
1717import semmle.code.cpp.security.Security
18- import semmle.code.cpp.security.FunctionWithWrappers
18+ import semmle.code.cpp.security.PrintfLike
1919import semmle.code.cpp.security.FlowSources
2020import semmle.code.cpp.ir.dataflow.TaintTracking
2121import semmle.code.cpp.ir.IR
2222import Flow:: PathGraph
2323
2424predicate 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 , _, _) )
26+ predicate isSink ( DataFlow:: Node node , Function f ) {
27+ exists ( Call c , int i |
28+ c .getTarget ( ) = f and
29+ printfLikeFunction ( f , i ) and
30+ c .getArgument ( i ) = node .asIndirectExpr ( )
31+ )
4132}
4233
4334module Config implements DataFlow:: ConfigSig {
4435 predicate isSource ( DataFlow:: Node node ) { isSource ( node , _) }
4536
46- predicate isSink ( DataFlow:: Node node ) {
47- exists ( PrintfLikeFunction printf |
48- printf .outermostWrapperFunctionCall ( [ node .asExpr ( ) , node .asIndirectExpr ( ) ] , _)
49- ) and
50- not isPrintfImplementation ( [ node .asExpr ( ) , node .asIndirectExpr ( ) ] .getEnclosingFunction ( ) )
51- }
37+ predicate isSink ( DataFlow:: Node node ) { isSink ( node , _) }
5238
5339 private predicate isArithmeticNonCharType ( ArithmeticType type ) {
5440 not type instanceof CharType and
@@ -69,14 +55,14 @@ module Config implements DataFlow::ConfigSig {
6955module Flow = TaintTracking:: Global< Config > ;
7056
7157from
72- PrintfLikeFunction printf , string printfFunction , string sourceType , DataFlow:: Node source ,
73- DataFlow :: Node sink , Flow:: PathNode sourceNode , Flow:: PathNode sinkNode
58+ Function printf , string sourceType , DataFlow :: Node source , DataFlow:: Node sink ,
59+ Flow:: PathNode sourceNode , Flow:: PathNode sinkNode
7460where
7561 source = sourceNode .getNode ( ) and
7662 sink = sinkNode .getNode ( ) and
7763 isSource ( source , sourceType ) and
78- printf . outermostWrapperFunctionCall ( [ sink . asExpr ( ) , sink . asIndirectExpr ( ) ] , printfFunction ) and
64+ isSink ( sink , printf ) and
7965 Flow:: flowPath ( sourceNode , sinkNode )
8066select sink , sourceNode , sinkNode ,
8167 "The value of this argument may come from $@ and is being used as a formatting argument to " +
82- printfFunction + "." , source , sourceType
68+ printf + "." , source , sourceType
0 commit comments