Soft Restricted Staker Role can withdraw stUSDe for USDe
The contract allowed addresses with the soft restricted staker role to withdraw their staked tokens for the underlying asset, contrary to the stated legal requirement. An attacker with that role could exchange staked tokens for the underlying asset and earn yield.
squeaky_cactus Follow with Arz, Kaysoft and 17 others
Originally disclosed on Code4rena
Submitted by squeaky_cactus, also found by Arz, Kaysoft, Udsen, deepkin, SovaSlava, Oxsadeeq, pep7siup, Shubham, peanuts, Limbooo, Yanchuan, btk, RamenPeople, Cosine, ast3ros, HChang26, 0xAlix2, dirk_y, and Krace
A requirement is stated that a user with the SOFT_RESTRICTED_STAKER_ROLE is not allowed to withdraw USDe for stUSDe.
The code does not satisfy that condition, when a holder has the SOFT_RESTRICTED_STAKER_ROLE, they can exchange their stUSDe for USDe using StakedUSDeV2.
Description
The Ethena readme has the following decription of legal requirements for the Soft Restricted Staker Role: https://github.com/code-423n4/2023-10-ethena/blob/ee67d9b542642c9757a6b826c82d0cae60256509/README.md?plain=1#L98
Due to legal requirements, there's a `SOFT_RESTRICTED_STAKER_ROLE` and `FULL_RESTRICTED_STAKER_ROLE`.
The former is for addresses based in countries we are not allowed to provide yield to, for example USA.
Addresses under this category will be soft restricted. They cannot deposit USDe to get stUSDe or withdraw stUSDe for USDe.
However they can participate in earning yield by buying and selling stUSDe on the open market.
In summary, legal requires are that a SOFT_RESTRICTED_STAKER_ROLE:
- MUST NOT deposit USDe to get stUSDe
- MUST NOT withdraw USDe for USDe
- MAY earn yield by trading stUSDe on the open market
As StakedUSDeV2 is a ERC4626, the stUSDe is a share on the underlying USDe asset. There are two distinct entrypoints for a user to exchange their share for their claim on the underlying the asset, withdraw and redeem. Each cater for a different input (withdraw being by asset, redeem being by share), however both invoked the same internal _withdraw function, hence both entrypoints are affected.
There are two cases where a user with SOFT_RESTRICTED_STAKER_ROLE may have acquired stUSDe:
- Brought
stUSDeon the open market - Deposited
USDeinStakedUSDeV2before being granted theSOFT_RESTRICTED_STAKER_ROLE
In both cases the user can call either withdraw their holding by calling withdraw or redeem (when cooldown is off), or unstake (if cooldown is on) and successfully exchange their stUSDe for USDe.
Proof of Concept
The following two tests demonstrate the use case of a user staking, then being granted the SOFT_RESTRICTED_STAKER_ROLE, then exchanging their stUSDe for USDe (first using redeem function, the second using withdrawm).
The use case for acquiring on the open market, only requiring a different setup, however the exchange behaviour is identical and the cooldown enabled cooldownAssets and cooldownShares function still use the same _withdraw as redeem and withdraw, which leads to the same outcome.
(Place code into StakedUSDe.t.sol and run with forge test)
https://github.com/code-423n4/2023-10-ethena/blob/ee67d9b542642c9757a6b826c82d0cae60256509/test/foundry/staking/StakedUSDe.t.sol
bytes32 public constant SOFT_RESTRICTED_STAKER_ROLE = keccak256("SOFT_RESTRICTED_STAKER_ROLE");
bytes32 private constant BLACKLIST_MANAGER_ROLE = keccak256("BLACKLIST_MANAGER_ROLE");
function test_redeem_while_soft_restricted() public {
// Set up Bob with 100 stUSDe
uint256 initialAmount = 100 ether;
_mintApproveDeposit(bob, initialAmount);
uint256 stakeOfBob = stakedUSDe.balanceOf(bob);
// Alice becomes a blacklist manager
vm.prank(owner);
stakedUSDe.grantRole(BLACKLIST_MANAGER_ROLE, alice);
// Blacklist Bob with the SOFT_RESTRICTED_STAKER_ROLE
vm.prank(alice);
stakedUSDe.addToBlacklist(bob, false);
// Assert that Bob has staked and is now has the soft restricted role
assertEq(usdeToken.balanceOf(bob), 0);
assertEq(stakedUSDe.totalSupply(), stakeOfBob);
assertEq(stakedUSDe.totalAssets(), initialAmount);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
// Rewards to StakeUSDe and vest
uint256 rewardAmount = 50 ether;
_transferRewards(rewardAmount, rewardAmount);
vm.warp(block.timestamp + 8 hours);
// Assert that only the total assets have increased after vesting
assertEq(usdeToken.balanceOf(bob), 0);
assertEq(stakedUSDe.totalSupply(), stakeOfBob);
assertEq(stakedUSDe.totalAssets(), initialAmount + rewardAmount);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
// Bob withdraws his stUSDe for USDe
vm.prank(bob);
stakedUSDe.redeem(stakeOfBob, bob, bob);
// End state being while being soft restricted Bob redeemed USDe with rewards
assertApproxEqAbs(usdeToken.balanceOf(bob), initialAmount + rewardAmount, 2);
assertApproxEqAbs(stakedUSDe.totalAssets(), 0, 2);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
}
function test_withdraw_while_soft_restricted() public {
// Set up Bob with 100 stUSDe
uint256 initialAmount = 100 ether;
_mintApproveDeposit(bob, initialAmount);
uint256 stakeOfBob = stakedUSDe.balanceOf(bob);
// Alice becomes a blacklist manager
vm.prank(owner);
stakedUSDe.grantRole(BLACKLIST_MANAGER_ROLE, alice);
// Blacklist Bob with the SOFT_RESTRICTED_STAKER_ROLE
vm.prank(alice);
stakedUSDe.addToBlacklist(bob, false);
// Assert that Bob has staked and is now has the soft restricted role
assertEq(usdeToken.balanceOf(bob), 0);
assertEq(stakedUSDe.totalSupply(), stakeOfBob);
assertEq(stakedUSDe.totalAssets(), initialAmount);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
// Rewards to StakeUSDe and vest
uint256 rewardAmount = 50 ether;
_transferRewards(rewardAmount, rewardAmount);
vm.warp(block.timestamp + 8 hours);
// Assert that only the total assets have increased after vesting
assertEq(usdeToken.balanceOf(bob), 0);
assertEq(stakedUSDe.totalSupply(), stakeOfBob);
assertEq(stakedUSDe.totalAssets(), initialAmount + rewardAmount);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
// Bob withdraws his stUSDe for USDe (-1 as dust is lost in asset to share rounding in ERC4626)
vm.prank(bob);
stakedUSDe.withdraw(initialAmount + rewardAmount - 1, bob, bob);
// End state being while being soft restricted Bob redeemed USDe with rewards
assertApproxEqAbs(usdeToken.balanceOf(bob), initialAmount + rewardAmount, 2);
assertApproxEqAbs(stakedUSDe.totalAssets(), 0, 2);
assertTrue(stakedUSDe.hasRole(SOFT_RESTRICTED_STAKER_ROLE, bob));
}
Tools Used
Manual review, Foundry test
Recommended Mitigation Steps
With the function overriding present, to prevent the SOFT_RESTRICTED_STAKER_ROLE from being able to exchange their stUSDs for USDe, make the following change in StakedUSDe
- if (hasRole(FULL_RESTRICTED_STAKER_ROLE, caller) || hasRole(FULL_RESTRICTED_STAKER_ROLE, receiver)) {
+ if (hasRole(FULL_RESTRICTED_STAKER_ROLE, caller) || hasRole(FULL_RESTRICTED_STAKER_ROLE, receiver) || hasRole(SOFT_RESTRICTED_STAKER_ROLE, caller)) {
revert OperationNotAllowed();
}
0xDjango (judge) decreased severity to Medium
kayinnnn (Ethena) disputed and commented:
For this issue, the docs were incorrect to say withdrawal by soft restricted role is not allowed. Only depositing is not allowed.
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 withdrawal check only blocked the fully restricted role and did not check the soft restricted role.
What the attacker needed
User has soft restricted role; user holds staked tokens
The fix
The withdrawal check should also block callers with the soft restricted staker role.
How it was found
- A user deposits the underlying asset and receives staked tokens.
- A blacklist manager grants the user the soft restricted staker role.
- The user calls the redeem function to exchange staked tokens for the underlying asset.
- The user successfully receives the underlying asset plus rewards.
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 staking contract had a special role meant to stop certain users from taking their staked tokens back out. The code only checked a different, stricter role, so users with the softer role could still withdraw. This let them get their tokens plus any earned rewards.
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.