Skip to content

Commit f952f90

Browse files
committed
Crypto: Update CtxFlow to flow from any "source ctx" which is any ctx that is an argument or a return.
1 parent 33e239d commit f952f90

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/CtxFlow.qll

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ private class CtxPointerArgument extends CtxPointerExpr {
6363
Call getCall() { result.getAnArgument() = this }
6464
}
6565

66+
/**
67+
* A call returning a CtxPointerExpr.
68+
*/
69+
private class CtxPointerReturn extends CtxPointerExpr {
70+
CtxPointerReturn() { exists(Call c | c = this) }
71+
72+
Call getCall() { result = this.(Call) }
73+
}
74+
6675
/**
6776
* A call whose target contains 'free' or 'reset' and has an argument of type
6877
* CtxPointerArgument.
@@ -97,10 +106,26 @@ private class CtxCopyReturnCall extends Call, CtxPointerExpr {
97106
}
98107

99108
/**
100-
* Flow from any CtxPointerArgument to any other CtxPointerArgument
109+
* A source Ctx of interest is any argument or return of type CtxPointerExpr.
110+
*/
111+
private class CtxPointerSource extends CtxPointerExpr {
112+
CtxPointerSource() {
113+
this instanceof CtxPointerReturn or
114+
this instanceof CtxPointerArgument
115+
}
116+
117+
DataFlow::Node asNode() {
118+
result.asExpr() = this
119+
or
120+
result.asDefiningArgument() = this
121+
}
122+
}
123+
124+
/**
125+
* Flow from any CtxPointerSource to any CtxPointerArgument.
101126
*/
102-
module OpenSSLCtxArgumentFlowConfig implements DataFlow::ConfigSig {
103-
predicate isSource(DataFlow::Node source) { source.asExpr() instanceof CtxPointerArgument }
127+
module OpenSSLCtxSourceToArgumentFlowConfig implements DataFlow::ConfigSig {
128+
predicate isSource(DataFlow::Node source) { exists(CtxPointerSource s | s.asNode() = source) }
104129

105130
predicate isSink(DataFlow::Node sink) { sink.asExpr() instanceof CtxPointerArgument }
106131

@@ -125,15 +150,15 @@ module OpenSSLCtxArgumentFlowConfig implements DataFlow::ConfigSig {
125150
}
126151
}
127152

128-
module OpenSSLCtxArgumentFlow = DataFlow::Global<OpenSSLCtxArgumentFlowConfig>;
153+
module OpenSSLCtxSourceToArgumentFlow = DataFlow::Global<OpenSSLCtxSourceToArgumentFlowConfig>;
129154

130155
/**
131156
* Holds if there is a context flow from the source to the sink.
132157
*/
133-
predicate ctxArgFlowsToCtxArg(CtxPointerArgument source, CtxPointerArgument sink) {
158+
predicate ctxArgOrRetFlowsToCtxArg(CtxPointerSource source, CtxPointerArgument sink) {
134159
exists(DataFlow::Node a, DataFlow::Node b |
135-
OpenSSLCtxArgumentFlow::flow(a, b) and
136-
a.asExpr() = source and
160+
OpenSSLCtxSourceToArgumentFlow::flow(a, b) and
161+
a = source.asNode() and
137162
b.asExpr() = sink
138163
)
139164
}

cpp/ql/lib/experimental/quantum/OpenSSL/Operations/OpenSSLOperationBase.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ abstract class EVPOperation extends OpenSSLOperation {
122122
* Finds the initialization call, may be none.
123123
*/
124124
EVPInitialize getInitCall() {
125-
CTXFlow::ctxArgFlowsToCtxArg(result.getContextArg(), this.getContextArg())
125+
CTXFlow::ctxArgOrRetFlowsToCtxArg(result.getContextArg(), this.getContextArg())
126126
}
127127

128128
Crypto::ArtifactOutputDataFlowNode getOutputArtifact() {
@@ -138,14 +138,15 @@ abstract class EVPOperation extends OpenSSLOperation {
138138
}
139139

140140
/**
141-
* The final calls of the EVP API.
141+
* An EVP final call,
142+
* which is typicall usesed in an update/final pattern.
142143
*/
143144
abstract class EVPFinal extends EVPOperation {
144145
/**
145146
* All update calls that were executed before this final call.
146147
*/
147148
EVPUpdate getUpdateCalls() {
148-
CTXFlow::ctxArgFlowsToCtxArg(result.getContextArg(), this.getContextArg())
149+
CTXFlow::ctxArgOrRetFlowsToCtxArg(result.getContextArg(), this.getContextArg())
149150
}
150151

151152
/**

0 commit comments

Comments
 (0)