@@ -49,8 +49,8 @@ class CallToFunctionWithRestrictParameters extends FunctionCall {
49
49
.getIndex ( ) )
50
50
}
51
51
52
- Expr getAPtrArg ( ) {
53
- result = this .getAnArgument ( ) and
52
+ Expr getAPtrArg ( int index ) {
53
+ result = this .getArgument ( index ) and
54
54
pointerValue ( result )
55
55
}
56
56
@@ -69,9 +69,13 @@ class CallToFunctionWithRestrictParameters extends FunctionCall {
69
69
* A `PointsToExpr` that is an argument of a pointer-type in a `CallToFunctionWithRestrictParameters`
70
70
*/
71
71
class CallToFunctionWithRestrictParametersArgExpr extends Expr {
72
+ int paramIndex ;
73
+
72
74
CallToFunctionWithRestrictParametersArgExpr ( ) {
73
- this = any ( CallToFunctionWithRestrictParameters call ) .getAPtrArg ( )
75
+ this = any ( CallToFunctionWithRestrictParameters call ) .getAPtrArg ( paramIndex )
74
76
}
77
+
78
+ int getParamIndex ( ) { result = paramIndex }
75
79
}
76
80
77
81
int getStatedValue ( Expr e ) {
@@ -101,28 +105,41 @@ class PointerValueToRestrictArgConfig extends DataFlow::Configuration {
101
105
102
106
override predicate isSink ( DataFlow:: Node sink ) {
103
107
exists ( CallToFunctionWithRestrictParameters call |
104
- sink .asExpr ( ) = call .getAPtrArg ( ) .getAChild * ( )
108
+ sink .asExpr ( ) = call .getAPtrArg ( _ ) .getAChild * ( )
105
109
)
106
110
}
111
+
112
+ override predicate isBarrierIn ( DataFlow:: Node node ) {
113
+ exists ( AddressOfExpr a | node .asExpr ( ) = a .getOperand ( ) .getAChild * ( ) )
114
+ }
107
115
}
108
116
109
117
from
110
118
CallToFunctionWithRestrictParameters call , CallToFunctionWithRestrictParametersArgExpr arg1 ,
111
- CallToFunctionWithRestrictParametersArgExpr arg2 , int argOffset1 , int argOffset2
119
+ CallToFunctionWithRestrictParametersArgExpr arg2 , int argOffset1 , int argOffset2 , Expr source1 ,
120
+ Expr source2 , string sourceMessage1 , string sourceMessage2
112
121
where
113
122
not isExcluded ( call , Pointers3Package:: doNotPassAliasedPointerToRestrictQualifiedParamQuery ( ) ) and
114
123
arg1 = call .getARestrictPtrArg ( ) and
115
- arg2 = call .getAPtrArg ( ) and
116
- arg1 != arg2 and
117
- exists ( PointerValueToRestrictArgConfig config , Expr source1 , Expr source2 |
124
+ arg2 = call .getAPtrArg ( _) and
125
+ // enforce ordering to remove permutations if multiple restrict-qualified args exist
126
+ ( not arg2 = call .getARestrictPtrArg ( ) or arg2 .getParamIndex ( ) > arg1 .getParamIndex ( ) ) and
127
+ // check if two pointers address the same object
128
+ exists ( PointerValueToRestrictArgConfig config |
118
129
config .hasFlow ( DataFlow:: exprNode ( source1 ) , DataFlow:: exprNode ( arg1 .getAChild * ( ) ) ) and
119
130
(
120
131
// one pointer value flows to both args
121
- config .hasFlow ( DataFlow:: exprNode ( source1 ) , DataFlow:: exprNode ( arg2 .getAChild * ( ) ) )
132
+ config .hasFlow ( DataFlow:: exprNode ( source1 ) , DataFlow:: exprNode ( arg2 .getAChild * ( ) ) ) and
133
+ sourceMessage1 = "$@" and
134
+ sourceMessage2 = "source" and
135
+ source1 = source2
122
136
or
123
137
// there are two separate values that flow from an AddressOfExpr of the same target
124
138
getAddressOfExprTargetBase ( source1 ) = getAddressOfExprTargetBase ( source2 ) and
125
- config .hasFlow ( DataFlow:: exprNode ( source2 ) , DataFlow:: exprNode ( arg2 .getAChild * ( ) ) )
139
+ config .hasFlow ( DataFlow:: exprNode ( source2 ) , DataFlow:: exprNode ( arg2 .getAChild * ( ) ) ) and
140
+ sourceMessage1 = "a pair of address-of expressions ($@, $@)" and
141
+ sourceMessage2 = "addressof1" and
142
+ not source1 = source2
126
143
)
127
144
) and
128
145
// get the offset of the pointer arithmetic operand (or '0' if there is none)
@@ -146,5 +163,6 @@ where
146
163
not exists ( call .getAPossibleSizeArg ( ) )
147
164
)
148
165
select call ,
149
- "Call to '" + call .getTarget ( ) .getName ( ) +
150
- "' passes an aliased pointer to a restrict-qualified parameter."
166
+ "Call to '" + call .getTarget ( ) .getName ( ) + "' passes an $@ to a $@ (pointer value derived from " +
167
+ sourceMessage1 + "." , arg2 , "aliased pointer" , arg1 , "restrict-qualified parameter" , source1 ,
168
+ sourceMessage2 , source2 , "addressof2"
0 commit comments