feat: add balance-reliance detector for unsafe address.balance usage#2941
feat: add balance-reliance detector for unsafe address.balance usage#2941ep0chzer0 wants to merge 3 commits intocrytic:masterfrom
Conversation
|
Hi, we already have the |
1562b34 to
2423f4a
Compare
|
Hi @smonicas, thanks for the feedback! You raise a good point about Key differences from
Example patterns this catches that // Pattern 1: Stale balance storage (no equality, just assignment)
savedBalance = address(this).balance;
// Pattern 2: Indirect comparison (depends on data flow analysis)
uint256 bal = address(this).balance;
require(bal == expected); // incorrect-equality might miss the connectionThat said, I understand if you feel this overlaps too much. Would you prefer I:
Happy to go with whatever approach works best for the project! |
0aeb52b to
d3bb211
Compare
Adds a new detector that identifies potentially unsafe uses of address.balance in smart contracts: 1. Strict equality comparisons (== or !=) - vulnerable to ETH forcing via selfdestruct or pre-deployment balance manipulation 2. Assignment to state variables - leads to stale data and incorrect assumptions about current balances The detector uses data dependency analysis to catch indirect usage patterns where balance values flow through local variables. Fixes crytic#2778
d3bb211 to
58fc464
Compare
|
Hi @smonicas, just checking in — did you get a chance to look at my response above? Happy to adjust the scope or close this if you feel it overlaps too much with |
Summary
Adds a new detector that identifies potentially unsafe uses of
address.balancein smart contracts.Fixes #2778
Features
The detector flags two dangerous patterns:
1. Strict Equality Comparisons (
==or!=)2. State Variable Assignment
Why These Are Dangerous
selfdestruct, breaking equality assumptionsImplementation
SolidityCallwithbalance(address)to identify balance accessesBinaryType.EQUALandBinaryType.NOT_EQUALcomparisonsStateVariable(both direct and via SSA)Test Coverage
Test file includes:
==)!=)Example Output
Recommended Patterns