Malicious Users Can Exploit Residual Allowance To Steal Assets
The vault's batch deposit functions let any caller specify an arbitrary source address, so anyone can pull tokens from a user who has granted the vault an allowance. An attacker could steal the user's remaining approved tokens and NFTs.
Originally disclosed on Code4rena
Submitted by xiaoming90, also found by 0x29A, 0xalpharush, 0xDjango, ayeslick, Critical, infosec_us_team, and Treasure-Seeker
Vulnerability Details
A depositor cannot have any residual allowance after depositing to the vault because the tokens can be stolen by anyone.
Proof of Concept
Assume that Alice has finished deploying the vault, and she would like to deposit her ERC20, ERC721, and ERC1155 tokens to the vault. She currently holds the following assets in her wallet
1000XYZ ERC20 tokens- APE #1 ERC721 NFT, APE #2 ERC721 NFT, APE #3 ERC721 NFT,
1000ABC ERC1155 tokens
Thus, she sets up the necessary approval to grant baseVault contract the permission to transfer her tokens to the vault.
erc20.approve(address(baseVault), type(uint256).max);
erc721.setApprovalForAll(address(baseVault), true);
erc1155.setApprovalForAll(address(baseVault), true);
Alice decided to deposit 50 XYZ ERC20 tokens, APE #1 ERC721 NFT, and 50 ABC tokens to the vault by calling baseVault.batchDepositERC20, baseVault.batchDepositERC721, and baseVault.batchDepositERC1155 as shown below:
baseVault.batchDepositERC20(alice.addr, vault, [XYZ.addr], [50])
baseVault.batchDepositERC721(alice.addr, vault, [APE.addr], [#1])
baseVault.batchDepositERC1155(alice.addr, vault, [ABC.addr], [#1], [50], "")
An attacker notices that there is residual allowance left on the baseVault, thus the attacker executes the following transactions to steal Alice’s assets and send them to the attacker’s wallet address.
baseVault.batchDepositERC20(alice.addr, attacker.addr, [XYZ.addr], [950])
baseVault.batchDepositERC721(alice.addr, attacker.addr, [APE.addr, APE.addr], [#2, #3])
baseVault.batchDepositERC1155(alice.addr, attacker.addr, [ABC.addr], [#1], [950], "")
function batchDepositERC20(
address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _amounts
) external {
for (uint256 i = 0; i < _tokens.length; ) {
IERC20(_tokens[i]).transferFrom(_from, _to, _amounts[i]);
unchecked {
++i;
}
}
}
function batchDepositERC721(
address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _ids
) external {
for (uint256 i = 0; i < _tokens.length; ) {
IERC721(_tokens[i]).safeTransferFrom(_from, _to, _ids[i]);
unchecked {
++i;
}
}
}
function batchDepositERC1155(
address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _ids,
uint256[] calldata _amounts,
bytes[] calldata _datas
) external {
unchecked {
for (uint256 i = 0; i < _tokens.length; ++i) {
IERC1155(_tokens[i]).safeTransferFrom(
_from,
_to,
_ids[i],
_amounts[i],
_datas[i]
);
}
}
}
Impact
Loss of assets for users as a malicious user could utilise the baseVault contract to exploit the user’s residual allowance to steal their assets.
Recommended Mitigation Steps
It is recommended to only allow the baseVault.batchDepositERC20, baseVault.batchDepositERC721, and baseVault.batchDepositERC1155 functions to pull tokens from the caller (msg.sender).
Considering updating the affected functions to remove the from parameter, and use msg.sender instead.
function batchDepositERC20(
- address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _amounts
) external {
for (uint256 i = 0; i < _tokens.length; ) {
- IERC20(_tokens[i]).transferFrom(_from, _to, _amounts[i]);
+ IERC20(_tokens[i]).transferFrom(msg.sender, _to, _amounts[i]);
unchecked {
++i;
}
}
}
function batchDepositERC721(
- address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _ids
) external {
for (uint256 i = 0; i < _tokens.length; ) {
- IERC721(_tokens[i]).safeTransferFrom(_from, _to, _ids[i]);
+ IERC721(_tokens[i]).safeTransferFrom(msg.sender, _to, _ids[i]);
unchecked {
++i;
}
}
}
function batchDepositERC1155(
- address _from,
address _to,
address[] calldata _tokens,
uint256[] calldata _ids,
uint256[] calldata _amounts,
bytes[] calldata _datas
) external {
unchecked {
for (uint256 i = 0; i < _tokens.length; ++i) {
IERC1155(_tokens[i]).safeTransferFrom(
- _from,
+ msg.sender,
_to,
_ids[i],
_amounts[i],
_datas[i]
);
}
}
}
stevennevins (Fractional) confirmed and commented:
Confirmed, we will be addressing this issue!
HardlyDifficult (judge) commented:
Anyone who approved the BaseVault can have their tokens stolen. Agree this is high risk.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The batch deposit functions accept a `_from` address parameter instead of always using `msg.sender`, allowing any caller to transfer tokens from any address that has approved the vault.
Impact
A malicious user could steal any tokens or NFTs from users who have approved the vault, resulting in loss of assets.
What the attacker needed
Victim has approved the vault contract; attacker can call the batch deposit functions
The fix
The functions should be changed to pull tokens only from `msg.sender` by removing the `_from` parameter.
How it was found
- Alice approves the baseVault contract to spend her ERC20, ERC721, and ERC1155 tokens.
- Alice deposits a small portion of her tokens via the batch deposit functions, leaving a residual allowance.
- An attacker calls the same batch deposit functions with Alice's address as the source and the attacker's address as the destination.
- The vault transfers Alice's remaining tokens to the attacker.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you approve a smart contract to move your tokens, you are giving it permission to take them. Here, the contract let anyone tell it whose tokens to take, so a stranger could use your leftover permission to send your tokens to themselves.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.