Skip to content

Commit 9577c35

Browse files
committed
Incremental update to NonConstantFormat.ql
1 parent 643817e commit 9577c35

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

cpp/ql/src/Likely Bugs/Format/NonConstantFormat.ql

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
import semmle.code.cpp.ir.dataflow.TaintTracking
1919
import semmle.code.cpp.commons.Printf
20+
import semmle.code.cpp.security.FlowSources
21+
22+
class UncalledFunction extends Function {
23+
UncalledFunction() { not exists(Call c | c.getTarget() = this) }
24+
}
2025

2126
// For the following `...gettext` functions, we assume that
2227
// all translations preserve the type and order of `%` specifiers
@@ -79,38 +84,23 @@ predicate isNonConst(DataFlow::Node node, boolean isIndirect) {
7984
)
8085
)
8186
or
82-
exists(Parameter p | p = e.(VariableAccess).getTarget() |
83-
p.getFunction().getName() = "main" and p.getType() instanceof PointerType
84-
)
85-
or
86-
e instanceof CrementOperation
87-
or
88-
e instanceof AddressOfExpr
89-
or
90-
e instanceof ReferenceToExpr
91-
or
92-
e instanceof AssignPointerAddExpr
93-
or
94-
e instanceof AssignPointerSubExpr
95-
or
96-
e instanceof PointerArithmeticOperation
97-
or
98-
e instanceof FieldAccess
99-
or
100-
e instanceof PointerDereferenceExpr
101-
or
102-
e instanceof AddressOfExpr
103-
or
104-
e instanceof ExprCall
105-
or
106-
e instanceof NewArrayExpr
107-
or
10887
exists(Variable v | v = e.(VariableAccess).getTarget() |
10988
v.getType().(ArrayType).getBaseType() instanceof CharType and
11089
exists(AssignExpr ae |
11190
ae.getLValue().(ArrayExpr).getArrayBase().(VariableAccess).getTarget() = v
11291
)
11392
)
93+
or
94+
exists(UncalledFunction f, Parameter p| f.getAParameter() = p |
95+
p = e.(VariableAccess).getTarget())
96+
or
97+
node instanceof FlowSource
98+
or
99+
(
100+
node instanceof DataFlow::DefinitionByReferenceNode and
101+
not exists(FormattingFunctionCall fc | node.asDefiningArgument() = fc.getOutputArgument(_)) and
102+
not exists(Call c | c.getAnArgument() = node.asDefiningArgument() and c.getTarget().hasDefinition())
103+
)
114104
)
115105
or
116106
node instanceof DataFlow::DefinitionByReferenceNode and
@@ -132,11 +122,13 @@ predicate isSinkImpl(DataFlow::Node sink, Expr formatString) {
132122

133123
module NonConstFlowConfig implements DataFlow::ConfigSig {
134124
predicate isSource(DataFlow::Node source) {
135-
exists(boolean isIndirect, Type t |
136-
isNonConst(source, isIndirect) and
137-
t = source.getType() and
138-
not cannotContainString(t, isIndirect)
139-
)
125+
// isNonConst(source)
126+
isNonConst(source,_)
127+
// exists(boolean isIndirect, Type t |
128+
// isNonConst(source, isIndirect) and
129+
// t = source.getType() and
130+
// not cannotContainString(t, isIndirect)
131+
// )
140132
}
141133

142134
predicate isSink(DataFlow::Node sink) { isSinkImpl(sink, _) }
@@ -146,13 +138,18 @@ module NonConstFlowConfig implements DataFlow::ConfigSig {
146138

147139
module NonConstFlow = TaintTracking::Global<NonConstFlowConfig>;
148140

149-
from FormattingFunctionCall call, Expr formatString
141+
// import NonConstFlow::PathGraph
142+
143+
from
144+
FormattingFunctionCall call, Expr formatString, DataFlow::Node sink
145+
// ,NonConstFlow::PathNode src,
146+
// NonConstFlow::PathNode sink
150147
where
151148
call.getArgument(call.getFormatParameterIndex()) = formatString and
152-
exists(DataFlow::Node sink |
153-
NonConstFlow::flowTo(sink) and
154-
isSinkImpl(sink, formatString)
155-
)
156-
select formatString,
149+
//NonConstFlow::flowPath(src, sink) and
150+
NonConstFlow::flowTo(sink) and
151+
//isSinkImpl(sink.getNode(), formatString)
152+
isSinkImpl(sink, formatString)
153+
select formatString, //sink.getNode(), src, sink,
157154
"The format string argument to " + call.getTarget().getName() +
158155
" should be constant to prevent security issues and other potential errors."

cpp/ql/test/query-tests/Likely Bugs/Format/NonConstantFormat/NonConstantFormat.expected

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,5 @@
1111
| test.cpp:63:12:63:18 | * ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
1212
| test.cpp:64:12:64:18 | & ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
1313
| test.cpp:65:12:65:39 | ... + ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
14-
| test.cpp:67:10:67:35 | ... + ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
15-
| test.cpp:70:12:70:20 | ... + ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
16-
| test.cpp:76:12:76:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
17-
| test.cpp:82:12:82:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
18-
| test.cpp:88:12:88:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
19-
| test.cpp:93:12:93:18 | ++ ... | The format string argument to printf should be constant to prevent security issues and other potential errors. |
20-
| test.cpp:100:12:100:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
21-
| test.cpp:110:12:110:24 | new[] | The format string argument to printf should be constant to prevent security issues and other potential errors. |
22-
| test.cpp:115:12:115:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
2314
| test.cpp:130:20:130:26 | access to array | The format string argument to sprintf should be constant to prevent security issues and other potential errors. |
2415
| test.cpp:157:12:157:15 | data | The format string argument to printf should be constant to prevent security issues and other potential errors. |

0 commit comments

Comments
 (0)