@@ -49,8 +49,8 @@ class CallToFunctionWithRestrictParameters extends FunctionCall {
4949 .getIndex ( ) )
5050 }
5151
52- Expr getAPtrArg ( ) {
53- result = this .getAnArgument ( ) and
52+ Expr getAPtrArg ( int index ) {
53+ result = this .getArgument ( index ) and
5454 pointerValue ( result )
5555 }
5656
@@ -69,9 +69,13 @@ class CallToFunctionWithRestrictParameters extends FunctionCall {
6969 * A `PointsToExpr` that is an argument of a pointer-type in a `CallToFunctionWithRestrictParameters`
7070 */
7171class CallToFunctionWithRestrictParametersArgExpr extends Expr {
72+ int paramIndex ;
73+
7274 CallToFunctionWithRestrictParametersArgExpr ( ) {
73- this = any ( CallToFunctionWithRestrictParameters call ) .getAPtrArg ( )
75+ this = any ( CallToFunctionWithRestrictParameters call ) .getAPtrArg ( paramIndex )
7476 }
77+
78+ int getParamIndex ( ) { result = paramIndex }
7579}
7680
7781int getStatedValue ( Expr e ) {
@@ -101,28 +105,41 @@ class PointerValueToRestrictArgConfig extends DataFlow::Configuration {
101105
102106 override predicate isSink ( DataFlow:: Node sink ) {
103107 exists ( CallToFunctionWithRestrictParameters call |
104- sink .asExpr ( ) = call .getAPtrArg ( ) .getAChild * ( )
108+ sink .asExpr ( ) = call .getAPtrArg ( _ ) .getAChild * ( )
105109 )
106110 }
111+
112+ override predicate isBarrierIn ( DataFlow:: Node node ) {
113+ exists ( AddressOfExpr a | node .asExpr ( ) = a .getOperand ( ) .getAChild * ( ) )
114+ }
107115}
108116
109117from
110118 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
112121where
113122 not isExcluded ( call , Pointers3Package:: doNotPassAliasedPointerToRestrictQualifiedParamQuery ( ) ) and
114123 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 |
118129 config .hasFlow ( DataFlow:: exprNode ( source1 ) , DataFlow:: exprNode ( arg1 .getAChild * ( ) ) ) and
119130 (
120131 // 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
122136 or
123137 // there are two separate values that flow from an AddressOfExpr of the same target
124138 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
126143 )
127144 ) and
128145 // get the offset of the pointer arithmetic operand (or '0' if there is none)
@@ -146,5 +163,6 @@ where
146163 not exists ( call .getAPossibleSizeArg ( ) )
147164 )
148165select 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