griefing / blocking / delaying users to withdraw
The WithdrawHook contract resets the per-user withdrawal limit only for the first user to interact in a new epoch, leaving other users' limits unchanged. A user who has reached their limit in a previous epoch must be the first to withdraw in the next epoch or wait another full epoch, causing a delay in accessing funds.
zaskoh Follow with unforgiven, deliriusz and 2 others
Originally disclosed on Code4rena
Submitted by zaskoh, also found by unforgiven, deliriusz, rvierdiiev, and Tricko
To withdraw, a user needs to convert his collateral for the base token. This is done in the withdraw function in Collateral.
The WithdrawHook has some security mechanics that can be activated like a global max withdraw in a specific timeframe, also for users to have a withdraw limit for them in a specific timeframe. It also collects the fees.
The check for the user withdraw is wrongly implemented and can lead to an unepexted delay for a user with a position > userWithdrawLimitPerPeriod. To withdraw all his funds he needs to be the first in every first new epoch (lastUserPeriodReset + userPeriodLength) to get his amount out. If he is not the first transaction in the new epoch, he needs to wait for a complete new epoch and depending on the timeframe from lastUserPeriodReset + userPeriodLength this can get a long delay to get his funds out.
The documentation says, that after every epoch all the user withdraws will be reset and they can withdraw the next set.
File: apps/smart-contracts/core/contracts/interfaces/IWithdrawHook.sol
63: /**
64: * @notice Sets the length in seconds for which user withdraw limits will
65: * be evaluated against. Every time `userPeriodLength` seconds passes, the
66: * amount withdrawn for all users will be reset to 0. This amount is only
But the implementation only resets the amount for the first user that interacts with the contract in the new epoch and leaves all other users with their old limit. This can lead to a delay for every user that is on his limit from a previous epoch until they manage to be the first to interact with the contract in the new epoch.
Proof of Concept
The following test shows how a user is locked out to withdraw if he’s at his limit from a previous epoch and another withdraw is done before him.
apps/smart-contracts/core/test/WithdrawHook.test.ts
describe('user withdraw is delayd', () => {
beforeEach(async () => {
await withdrawHook.setCollateral(collateral.address)
await withdrawHook.connect(deployer).setWithdrawalsAllowed(true)
await withdrawHook.connect(deployer).setGlobalPeriodLength(0)
await withdrawHook.connect(deployer).setUserPeriodLength(TEST_USER_PERIOD_LENGTH)
await withdrawHook.connect(deployer).setGlobalWithdrawLimitPerPeriod(0)
await withdrawHook.connect(deployer).setUserWithdrawLimitPerPeriod(TEST_USER_WITHDRAW_LIMIT)
await withdrawHook.connect(deployer).setDepositRecord(depositRecord.address)
await withdrawHook.connect(deployer).setTreasury(treasury.address)
await withdrawHook.connect(deployer).setTokenSender(tokenSender.address)
await testToken.connect(deployer).mint(collateral.address, TEST_GLOBAL_DEPOSIT_CAP)
await testToken.connect(deployer).mint(user.address, TEST_GLOBAL_DEPOSIT_CAP)
await testToken.connect(deployer).mint(user2.address, TEST_GLOBAL_DEPOSIT_CAP)
await testToken
.connect(collateralSigner)
.approve(withdrawHook.address, ethers.constants.MaxUint256)
tokenSender.send.returns()
})
it('reverts if user withdraw limit exceeded for period', async () => {
// first withdraw with the limit amount for a user
await withdrawHook.connect(collateralSigner).hook(user.address, TEST_USER_WITHDRAW_LIMIT, TEST_USER_WITHDRAW_LIMIT)
expect(await withdrawHook.getAmountWithdrawnThisPeriod(user.address)).to.eq(TEST_USER_WITHDRAW_LIMIT)
// we move to a new epoch in the future
const previousResetTimestamp = await getLastTimestamp(ethers.provider)
await setNextTimestamp(
ethers.provider,
previousResetTimestamp + TEST_USER_PERIOD_LENGTH + 1
)
// now another user is the first one to withdraw in this new epoch
await withdrawHook.connect(collateralSigner).hook(user2.address, TEST_USER_WITHDRAW_LIMIT, TEST_USER_WITHDRAW_LIMIT)
expect(await withdrawHook.getAmountWithdrawnThisPeriod(user2.address)).to.eq(TEST_USER_WITHDRAW_LIMIT)
// this will revert, because userToAmountWithdrawnThisPeriod[_sender] is not reset
// but it should not revert as it's a new epoch and the user didn't withdraw yet
await expect(
withdrawHook.connect(collateralSigner).hook(user.address, 1, 1)
).to.revertedWith('user withdraw limit exceeded')
})
})
To get the test running you need to add let user2: SignerWithAddress and the user2 in await ethers.getSigners()
Recommended Mitigation Steps
The check how the user periods are handled need to be changed. One possible way is to change the lastUserPeriodReset to a mapping like mapping(address => uint256) private lastUserPeriodReset to track the time for every user separately.
With a mapping you can change the condition to:
File: apps/smart-contracts/core/contracts/WithdrawHook.sol
18: mapping(address => uint256) lastUserPeriodReset;
File: apps/smart-contracts/core/contracts/WithdrawHook.sol
66: if (lastUserPeriodReset[_sender] + userPeriodLength < block.timestamp) {
67: lastUserPeriodReset[_sender] = block.timestamp;
68: userToAmountWithdrawnThisPeriod[_sender] = _amountBeforeFee;
69: } else {
70: require(userToAmountWithdrawnThisPeriod[_sender] + _amountBeforeFee <= userWithdrawLimitPerPeriod, "user withdraw limit exceeded");
71: userToAmountWithdrawnThisPeriod[_sender] += _amountBeforeFee;
72: }
With this change, we can change the test to how we would normaly expect the contract to work and see that it is correct.
it('withdraw limit is checked for every use seperatly', async () => {
// first withdraw with the limit amount for a user
await withdrawHook.connect(collateralSigner).hook(user.address, TEST_USER_WITHDRAW_LIMIT, TEST_USER_WITHDRAW_LIMIT)
// we move to a new epoch in the future
const previousResetTimestamp = await getLastTimestamp(ethers.provider)
await setNextTimestamp(
ethers.provider,
previousResetTimestamp + TEST_USER_PERIOD_LENGTH + 1
)
// now another user is the first one to withdraw in this new epoch
await withdrawHook.connect(collateralSigner).hook(user2.address, TEST_USER_WITHDRAW_LIMIT, TEST_USER_WITHDRAW_LIMIT)
// the first user also can withdraw his limit in this epoch
await withdrawHook.connect(collateralSigner).hook(user.address, TEST_USER_WITHDRAW_LIMIT, TEST_USER_WITHDRAW_LIMIT)
// we move the time, but stay in the same epoch
const previousResetTimestamp2 = await getLastTimestamp(ethers.provider)
await setNextTimestamp(
ethers.provider,
previousResetTimestamp2 + TEST_USER_PERIOD_LENGTH - 1
)
// this now will fail as we're in the same epoch
await expect(
withdrawHook.connect(collateralSigner).hook(user.address, 1, 1)
).to.revertedWith('user withdraw limit exceeded')
})
ramenforbreakfast (prePO) confirmed
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
Likely the contract uses a single global timestamp for the last reset instead of tracking each user's last reset time separately, so the reset condition only applies to the first caller in a new period.
Impact
A user who has reached their withdrawal limit in a previous epoch may be unable to withdraw funds until they are the first to interact in a new epoch, potentially causing a long delay.
What the attacker needed
User has reached their withdrawal limit; another user withdraws first in the new epoch
The fix
The contract should track the last reset time per user, for example with a mapping from address to timestamp, so that each user's limit resets independently.
How it was found
- Set up the WithdrawHook with a user period length and a user withdraw limit.
- Have a user withdraw exactly the limit amount in the current period.
- Advance time to the next period.
- Have a different user make the first withdrawal in the new period.
- Attempt to withdraw a small amount as the first user; the transaction reverts with 'user withdraw limit exceeded'.
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 limits how much each user can withdraw in a time period. The code is supposed to reset everyone's limit at the start of each new period, but it only resets the limit for the first person who withdraws. So if you used your full limit last period, you might have to wait a long time to withdraw again unless you happen to be the first to act.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.