Fake balances can be add for not-yet-existing ERC20 tokens, which allows attackers to set traps to steal funds from future users #191
Labels
bug
Something isn't working
downgraded by judge
Judge downgraded the risk level of this issue
duplicate-98
QA (Quality Assurance)
Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L63-L99
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L18
Vulnerability details
Impact
When
add
a new vault,solmate's SafeTransferLib
is used for pulling vault.token from the caller's account, this issue won't exist ifOpenZeppelin's SafeERC20
is used instead.That's because there is a subtle difference between the implementation of solmate's SafeTransferLib and OZ's SafeERC20:
OZ's SafeERC20 checks if the token is a contract or not, solmate's SafeTransferLib does not.
See: https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol#L9
As a result, when the token's address has no code, the transaction will just succeed with no error.
This attack vector was made well-known by the qBridge hack back in Jan 2022.
For our project, this alone still won't be a problem, a vault created and wrongfully accounted for a certain amount of balance for a non-existing token won't be much of a problem, there will be no fund loss as long as the token stays that way (being non-existing).
However, it's becoming popular for protocols to deploy their token across multiple networks and when they do so, a common practice is to deploy the token contract from the same deployer address and with the same nonce so that the token address can be the same for all the networks.
For example: $1INCH is using the same token address for both Ethereum and BSC; Gelato's $GEL token is using the same token address for Ethereum, Fantom and Polygon.
A sophisticated attacker can exploit it by taking advantage of that and setting traps on multiple potential tokens to steal from the future users that deposits with such tokens.
Proof of Concept
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L63-L99
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L18
PoC
Given:
-
ProjectA has TokenA on another network;-
ProjectB has TokenB on another network;-
ProjectC has TokenC on another network;In summary, one of the traps set by the attacker was activated by the deployment of TokenB and Alice was the victim. As a result, 10000e18 TokenB was stolen by the attacker.
Recommended Mitigation Steps
Consider using OZ's SafeERC20 instead.
The text was updated successfully, but these errors were encountered: