22 * Provides models for C++ containers `std::array`, `std::vector`, `std::deque`, `std::list` and `std::forward_list`.
33 */
44
5- import semmle.code.cpp.models.interfaces.Taint
5+ import semmle.code.cpp.models.interfaces.FlowSource
66import semmle.code.cpp.models.interfaces.Iterator
77
88/**
@@ -63,7 +63,7 @@ private class Vector extends StdSequenceContainer {
6363 * std::vector<std::string> v(100, potentially_tainted_string);
6464 * ```
6565 */
66- private class StdSequenceContainerConstructor extends Constructor , TaintFunction {
66+ private class StdSequenceContainerConstructor extends Constructor {
6767 StdSequenceContainerConstructor ( ) {
6868 this .getDeclaringType ( ) instanceof Vector or
6969 this .getDeclaringType ( ) instanceof Deque or
@@ -84,42 +84,6 @@ private class StdSequenceContainerConstructor extends Constructor, TaintFunction
8484 * Gets the index of a parameter to this function that is an iterator.
8585 */
8686 int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
87-
88- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
89- // taint flow from any parameter of the value type to the returned object
90- (
91- input .isParameterDeref ( this .getAValueTypeParameterIndex ( ) ) or
92- input .isParameter ( this .getAnIteratorParameterIndex ( ) )
93- ) and
94- (
95- output .isReturnValue ( ) // TODO: this is only needed for AST data flow, which treats constructors as returning the new object
96- or
97- output .isQualifierObject ( )
98- )
99- }
100- }
101-
102- /**
103- * The standard container function `data`.
104- */
105- private class StdSequenceContainerData extends TaintFunction {
106- StdSequenceContainerData ( ) {
107- this .getClassAndName ( "data" ) instanceof Array or
108- this .getClassAndName ( "data" ) instanceof Vector
109- }
110-
111- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
112- // flow from container itself (qualifier) to return value
113- input .isQualifierObject ( ) and
114- output .isReturnValueDeref ( )
115- or
116- // reverse flow from returned reference to the qualifier (for writes to
117- // `data`)
118- input .isReturnValueDeref ( ) and
119- output .isQualifierObject ( )
120- }
121-
122- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
12387}
12488
12589/**
@@ -143,35 +107,6 @@ class StdSequenceContainerPush extends MemberFunction {
143107 }
144108}
145109
146- private class StdSequenceContainerPushModel extends StdSequenceContainerPush , TaintFunction {
147- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
148- // flow from parameter to qualifier
149- input .isParameterDeref ( 0 ) and
150- output .isQualifierObject ( )
151- }
152-
153- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
154- }
155-
156- /**
157- * The standard container functions `front` and `back`.
158- */
159- private class StdSequenceContainerFrontBack extends TaintFunction {
160- StdSequenceContainerFrontBack ( ) {
161- this .getClassAndName ( [ "front" , "back" ] ) instanceof Array or
162- this .getClassAndName ( [ "front" , "back" ] ) instanceof Deque or
163- this .getClassAndName ( "front" ) instanceof ForwardList or
164- this .getClassAndName ( [ "front" , "back" ] ) instanceof List or
165- this .getClassAndName ( [ "front" , "back" ] ) instanceof Vector
166- }
167-
168- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
169- // flow from object to returned reference
170- input .isQualifierObject ( ) and
171- output .isReturnValueDeref ( )
172- }
173- }
174-
175110/**
176111 * The standard container functions `insert` and `insert_after`.
177112 */
@@ -198,58 +133,6 @@ class StdSequenceContainerInsert extends MemberFunction {
198133 int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
199134}
200135
201- private class StdSequenceContainerInsertModel extends StdSequenceContainerInsert , TaintFunction {
202- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
203- // flow from parameter to container itself (qualifier) and return value
204- (
205- input .isQualifierObject ( ) or
206- input .isParameterDeref ( this .getAValueTypeParameterIndex ( ) ) or
207- input .isParameter ( this .getAnIteratorParameterIndex ( ) )
208- ) and
209- (
210- output .isQualifierObject ( ) or
211- output .isReturnValue ( )
212- )
213- }
214-
215- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
216- }
217-
218- /**
219- * The standard container function `assign`.
220- */
221- private class StdSequenceContainerAssign extends TaintFunction {
222- StdSequenceContainerAssign ( ) {
223- this .getClassAndName ( "assign" ) instanceof Deque or
224- this .getClassAndName ( "assign" ) instanceof ForwardList or
225- this .getClassAndName ( "assign" ) instanceof List or
226- this .getClassAndName ( "assign" ) instanceof Vector
227- }
228-
229- /**
230- * Gets the index of a parameter to this function that is a reference to the
231- * value type of the container.
232- */
233- int getAValueTypeParameterIndex ( ) {
234- this .getParameter ( result ) .getUnspecifiedType ( ) .( ReferenceType ) .getBaseType ( ) =
235- this .getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. the `T` of this `std::vector<T>`
236- }
237-
238- /**
239- * Gets the index of a parameter to this function that is an iterator.
240- */
241- int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
242-
243- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
244- // flow from parameter to container itself (qualifier)
245- (
246- input .isParameterDeref ( this .getAValueTypeParameterIndex ( ) ) or
247- input .isParameter ( this .getAnIteratorParameterIndex ( ) )
248- ) and
249- output .isQualifierObject ( )
250- }
251- }
252-
253136/**
254137 * The standard container functions `at` and `operator[]`.
255138 */
@@ -261,20 +144,6 @@ class StdSequenceContainerAt extends MemberFunction {
261144 }
262145}
263146
264- private class StdSequenceContainerAtModel extends StdSequenceContainerAt , TaintFunction {
265- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
266- // flow from qualifier to referenced return value
267- input .isQualifierObject ( ) and
268- output .isReturnValueDeref ( )
269- or
270- // reverse flow from returned reference to the qualifier
271- input .isReturnValueDeref ( ) and
272- output .isQualifierObject ( )
273- }
274-
275- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
276- }
277-
278147/**
279148 * The standard `emplace` function.
280149 */
@@ -297,20 +166,6 @@ class StdSequenceEmplace extends MemberFunction {
297166 }
298167}
299168
300- private class StdSequenceEmplaceModel extends StdSequenceEmplace , TaintFunction {
301- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
302- // flow from any parameter except the position iterator to qualifier and return value
303- // (here we assume taint flow from any constructor parameter to the constructed object)
304- input .isParameterDeref ( [ 1 .. this .getNumberOfParameters ( ) - 1 ] ) and
305- (
306- output .isQualifierObject ( ) or
307- output .isReturnValue ( )
308- )
309- }
310-
311- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
312- }
313-
314169/**
315170 * The standard vector `emplace` function.
316171 */
@@ -340,17 +195,6 @@ class StdSequenceEmplaceBack extends MemberFunction {
340195 }
341196}
342197
343- private class StdSequenceEmplaceBackModel extends StdSequenceEmplaceBack , TaintFunction {
344- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
345- // flow from any parameter to qualifier
346- // (here we assume taint flow from any constructor parameter to the constructed object)
347- input .isParameterDeref ( [ 0 .. this .getNumberOfParameters ( ) - 1 ] ) and
348- output .isQualifierObject ( )
349- }
350-
351- override predicate isPartialWrite ( FunctionOutput output ) { output .isQualifierObject ( ) }
352- }
353-
354198/**
355199 * The standard vector `emplace_back` function.
356200 */
0 commit comments