The purchase
function in Swan.sol contains critical ordering vulnerabilities in asset transfers and state management that could lead to asset theft and payment manipulation.
Asset theft through reentrancy
Payment evasion
Multiple purchases of same asset
Protocol fee bypass
The two sequential transfers can temporarily cause the asset count for Swan (address(this)
) to increase by more than 1, violating the invariant that assets should only change by +/-1.
Swan.sol#purchase: https://github.com/Cyfrin/2024-10-swan-dria/blob/c3f6f027ed51dd31f60b224506de2bc847243eb7/contracts/swan/Swan.sol#L276-L302
State changes occur before asset transfers
Two-step asset transfer creates unnecessary complexity
Payment occurs after asset ownership changes
This ordering violates the Checks-Effects-Interactions pattern and could allow:
Reentrancy attacks during the two-step asset transfer
Failed payments after asset ownership has changed
Asset theft if payment fails but transfer succeeds
The vulnerability is particularly severe because it handles both high-value NFT assets and payment tokens in the same transaction without proper safeguards.
A malicious actor could potentially manipulate the transaction ordering to obtain assets without proper payment or execute multiple purchases of the same asset.
This has high ikelihood of manifesting because
Base chain's low gas costs make reentrancy attacks economically viable
Complex interaction between ERC721 and ERC20 transfers
Multiple external calls in single transaction
This directly impacts
Asset sellers who may lose NFTs
Buyers who could have assets stolen
Protocol fee collection
The purchase function violates CEI (Checks-Effects-Interactions) pattern
State changes occur before external calls
Vs
Implement nonReentrant modifier and ensure atomic operations by using a pull payment pattern for asset transfers. Add payment verification before any state changes or transfers occur.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.