Open S3v3ru5 opened 2 years ago
The above dataflow equations do not work correctly when there's a backedge(loops). Because:
Ex:
information from instructions txn OnCompletion == int CloseOut
in block 4 would not reach block 9 and 10. Similarly, information from instructions txn OnCompletion != int DeleteApplication
in block 9 (inside the loop) would not reach block 4 and other blocks outside the loop.
An easy solution for this would be to:
However, we are doing path-sensitive analysis. so, if the information is part of the back edge itself and is not part of the source or target block of the edge, we would lose that information. But, this should be fine for the use cases. Because, in practice, transaction constraints are not part of the loop conditions.
Another solution is to change the equations to follow GEN and KILL type analysis. And update the implementation accordingly.
Dataflow Analysis to find constraints on transaction fields
What are constraints on transaction fields
For each basic block in the CFG
For each Transaction Field, find all possible values for the given transaction field which leads to successful execution of the basic block.
e.g:
block_transaction_context[B][F]
stores the possible values form fieldF
in basic blockB
. ifblock_transaction_context[b]["GroupSize"] = [1, 3]
. Then, if and only if the group size of the transaction invoking this contracts has groupsize1
or3
,b
will be executed ANDb
without reverting ANDi.e All execution paths that contain basic block
b
constraint the group size to1
or3
.NOTE: only assert instructions are considered to check if execution reverts or not. e.g if sequence of instructions are
Then
block_transaction_context[b]["GroupSize"] = [3]
Implementation
Source of constraints for a given basic block
consider
B3 has predecessors B1, B2 and successors B4, B5. Given following transaction contexts,
Then new context will be,
Branch Based Constraints
Teal contracts generally use conditional branch instructions directly on the constraints(conditional expression). e.g, consider B1 from above is
This implies
To solve this, we can use path based transaction context while considering information from predecessors
Algorithm
Phase 1
Phase 2
Transaction Fields to consider for constraints
Global Fields:
Transaction Fields:
Note
==
,!=
are both considered for fields with small domain.If block B is,
Then,
block_transaction_context[B]["GroupSize"] = [2]
And if block B isThen,
block_transaction_context[B]["GroupSize"] = list(set(range(1, 16)) - set([2]))
However, We cannot do the same for Addr type fields. we will only consider constraints which are small enough to store and enumerate. if the number of possible values are small, then we store them otherwise the possible values are considered to be ALL possible values(no constraints).
Using the gtxn instruction, constraints can be computed for other transactions in the group as well. This helps in detecting the possible group structures. Constraints of other transactions in the group can also be used for contracts which distribute the security checks to multiple contracts.
Uses
Currently canUpdate, canDelete detectors check for following sequence of instructions and report execution paths that do not have that sequence
These detectors can be divided into isUpdatable, isDeletable and unprotectedUpdateApplication, unprotectedDeleteApplication detectors.
appl{UpdateApplication}
in it's context and doesn't containreturn 0
then there's a execution path that will be executed when transaction is UpdateApplication call.appl{UpdateApplication}
, if there is no constraint on SenderAddress, then report that execution path. Based on Tealer capabilities, it can be checked if SenderAddress is checked against state variable.Printers for UpdateApplication, DeleteApplication paths. Highlight basic blocks which has
appl{UpdateApplication}
in their paths for UpdateApplication printer. Highlight basic blocks which hasappl{DeleteApplication}
.Get Heuristics on contract type: stateless or stateful. We could check if there are any blocks which does not have any
appl{UpdateApplication, DeleteApplication, ...}
. This implies that either contract is stateless or contract is stateful and block is dead block. Similar deduction can be made if any of the blocks has onlyappl{...}
.Get information on the group size and possible combinations of group this contract might be part of.
Improve analysis of current detectors which check if contracts are validating certain transaction fields.
Blockers
&&
operations, which are much easier to extract from, than the generalised expressions.ToDo
Related/Solves issues #75, #82, #83, #87, #90, #95