backdoor in withdrawRedundant
The withdrawRedundant function in the Vault contract has flawed logic that allows the owner to transfer all of the vault's underlying token balance to any address. An attacker with owner privileges could steal all user deposits.
Submitted by cmichel, also found by camden, WatchPug, and Ruhum
The Vault.withdrawRedundant has wrong logic that allows the admins to steal the underlying vault token.
function withdrawRedundant(address _token, address _to)
external
override
onlyOwner
{
if (
_token == address(token) &&
balance < IERC20(token).balanceOf(address(this))
) {
uint256 _redundant = IERC20(token).balanceOf(address(this)) -
balance;
IERC20(token).safeTransfer(_to, _redundant);
} else if (IERC20(_token).balanceOf(address(this)) > 0) {
// @audit they can rug users. let's say balance == IERC20(token).balanceOf(address(this)) => first if false => transfers out everything
IERC20(_token).safeTransfer(
_to,
IERC20(_token).balanceOf(address(this))
);
}
}
POC
- Vault deposits increase as
Vault.addValueis called and thebalanceincreases by_amountas well as the actualIERC20(token).balanceOf(this). Note thatbalance == IERC20(token).balanceOf(this) - Admins call
vault.withdrawRedundant(vault.token(), attacker)which goes into theelse ifbranch due to the balance inequality condition beingfalse. It will transfer out allvault.token()amounts to the attacker.
Impact
There’s a backdoor in the withdrawRedundant that allows admins to steal all user deposits.
Recommended Mitigation Steps
I think the devs wanted this logic from the code instead:
function withdrawRedundant(address _token, address _to)
external
override
onlyOwner
{
if (
_token == address(token)
) {
if (balance < IERC20(token).balanceOf(address(this))) {
uint256 _redundant = IERC20(token).balanceOf(address(this)) -
balance;
IERC20(token).safeTransfer(_to, _redundant);
}
} else if (IERC20(_token).balanceOf(address(this)) > 0) {
IERC20(_token).safeTransfer(
_to,
IERC20(_token).balanceOf(address(this))
);
}
}
oishun1112 (Insure) confirmed:
similar to PVE03 (Peckshield audit) We will create a PR and merge after we merge both audit/code4rena and audit/peckshield branches in the InsureDAO repository.
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 condition in the first if branch is too restrictive, causing the function to fall into the else-if branch and transfer the entire token balance when the token is the vault's main token and the balance equals the contract's token balance.
What the attacker needed
Owner privileges
The fix
The function should be fixed by restructuring the logic so that the else-if branch only handles tokens other than the vault's main token, and the main token branch only transfers the redundant amount.
How it was found
- Vault deposits increase as Vault.addValue is called and the balance increases by _amount as well as the actual IERC20(token).balanceOf(this). Note that balance == IERC20(token).balanceOf(this)
- Admins call vault.withdrawRedundant(vault.token(), attacker) which goes into the else if branch due to the balance inequality condition being false. It will transfer out all vault.token() amounts 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.
A smart contract has a function meant to let the owner withdraw only extra tokens, but due to a logic mistake, the owner can withdraw all tokens, including user deposits. This means the owner could take everyone's money.
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.