AutoRoller#eject can be used to steal all the yield from vault's YTs
The AutoRoller contract's eject function transfers the entire target asset balance of the contract to the caller, which includes yield collected from all YTs in the pool, not just the caller's share. An attacker could call eject to steal the yield belonging to other users.
Source: https://github.com/sherlock-audit/2022-11-sense-judging/issues/22
Found by
0x52
Summary
AutoRoller#eject collects all the current yield of the YTs, combines the users share of the PTs and YTs then sends the user the entire target balance of the contract. The problem is that combine claims the yield for ALL YTs, which sends the AutoRoller target assets. Since it sends the user the entire target balance of the contract it accidentally sends the user the yield from all the pool's YTs.
Vulnerability Detail
function eject(
uint256 shares,
address receiver,
address owner
) public returns (uint256 assets, uint256 excessBal, bool isExcessPTs) {
...
//@audit call of interest
(excessBal, isExcessPTs) = _exitAndCombine(shares);
_burn(owner, shares); // Burn after percent ownership is determined in _exitAndCombine.
if (isExcessPTs) {
pt.transfer(receiver, excessBal);
} else {
yt.transfer(receiver, excessBal);
}
//@audit entire asset (adapter.target) balance transferred to caller, which includes collected YT yield and combined
asset.transfer(receiver, assets = asset.balanceOf(address(this)));
emit Ejected(msg.sender, receiver, owner, assets, shares,
isExcessPTs ? excessBal : 0,
isExcessPTs ? 0 : excessBal
);
}
function _exitAndCombine(uint256 shares) internal returns (uint256, bool) {
uint256 supply = totalSupply; // Save extra SLOAD.
uint256 lpBal = shares.mulDivDown(space.balanceOf(address(this)), supply);
uint256 totalPTBal = pt.balanceOf(address(this));
uint256 ptShare = shares.mulDivDown(totalPTBal, supply);
...
uint256 ytBal = shares.mulDivDown(yt.balanceOf(address(this)), supply);
ptShare += pt.balanceOf(address(this)) - totalPTBal;
unchecked {
// Safety: an inequality check is done before subtraction.
if (ptShare > ytBal) {
//@audit call of interest
divider.combine(address(adapter), maturity, ytBal);
return (ptShare - ytBal, true);
} else { // Set excess PTs to false if the balances are exactly equal.
divider.combine(address(adapter), maturity, ptShare);
return (ytBal - ptShare, false);
}
}
}
Eject allows the user to leave the liquidity pool by withdrawing their liquidity from the Balancer pool and combining the PTs and YTs via divider.combine.
https://etherscan.io/address/0x86bA3E96Be68563E41c2f5769F1AF9fAf758e6E0#code#F1#L259
function combine(
address adapter,
uint256 maturity,
uint256 uBal
) external nonReentrant whenNotPaused returns (uint256 tBal) {
if (!adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
uint256 level = adapterMeta[adapter].level;
if (level.combineRestricted() && msg.sender != adapter) revert Errors.CombineRestricted();
// Burn the PT
Token(series[adapter][maturity].pt).burn(msg.sender, uBal);
//@audit call of interest
uint256 collected = _collect(msg.sender, adapter, maturity, uBal, uBal, address(0));
...
// Convert from units of Underlying to units of Target
tBal = uBal.fdiv(cscale);
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, msg.sender, tBal);
// Notify only when Series is not settled as when it is, the _collect() call above would trigger a _redeemYT which will call notify
if (!settled) Adapter(adapter).notify(msg.sender, tBal, false);
unchecked {
// Safety: bounded by the Target's total token supply
tBal += collected;
}
emit Combined(adapter, maturity, tBal, msg.sender);
}
https://etherscan.io/address/0x86bA3E96Be68563E41c2f5769F1AF9fAf758e6E0#code#F1#L445
function _collect(
address usr,
address adapter,
uint256 maturity,
uint256 uBal,
uint256 uBalTransfer,
address to
) internal returns (uint256 collected) {
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
if (!adapterMeta[adapter].enabled && !_settled(adapter, maturity)) revert Errors.InvalidAdapter();
Series memory _series = series[adapter][maturity];
uint256 lscale = lscales[adapter][maturity][usr];
...
uint256 tBalNow = uBal.fdivUp(_series.maxscale); // preventive round-up towards the protocol
uint256 tBalPrev = uBal.fdiv(lscale);
unchecked {
collected = tBalPrev > tBalNow ? tBalPrev - tBalNow : 0;
}
//@audit adapter.target is transferred to AutoRoller
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, usr, collected);
Adapter(adapter).notify(usr, collected, false); // Distribute reward tokens
...
}
Inside divider#combine the collected yield from the YTs are transferred to the AutoRoller. The AutoRoller balance will now contain both the collected yield of the YTs and the target yielded by combining. The end of eject transfers this entire balance to the caller, effectively stealing the yield of the entire AutoRoller.
Impact
User funds given to the wrong person
Code Snippet
https://github.com/sherlock-audit/2022-11-sense/blob/main/contracts/src/AutoRoller.sol#L636-L664
Tool used
Manual Review
Recommendation
Combine returns the amount of target yielded by combining the PT and YT. This balance is the amount of assets that should be transferred to the user.
Discussion
jparklev
This one seems to be valid and we're working on a fix. Appreciate the find 🕵️
jparklev
Fix: https://github.com/sense-finance/auto-roller/pull/21
aktech297
The fix that is done inside the eject function looks good. Also, there are three new functions are added, I am looking for clarification from @sherlock-admin to confirm whether they are also part of audit and need to be reviewed.
jacksanford1
Bringing in a Discord comment from @aktech297:
After looking at the code changes, my observation is: initially the issue is identified in eject function and the changes in eject are fixed in that function alone...after looking at other places, the dev team identified similar issues in other places and added the new functions with an appropriate collect function that ensures fairness in the system. Overall the changes look good.
jacksanford1
Categorized as "fixed."
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 eject function transfers the contract's entire target asset balance instead of only the amount returned by the combine operation.
Impact
An attacker could steal the yield from all YTs in the pool, causing user funds to be given to the wrong person.
What the attacker needed
A user with shares in the AutoRoller
The fix
The fix was to transfer only the amount of target yielded by combining the PT and YT, not the entire contract balance.
How it was found
- Call the eject function with some shares.
- The function calls _exitAndCombine, which calls divider.combine and collects yield from all YTs into the AutoRoller contract.
- The function then transfers the entire target asset balance of the contract to the caller.
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 use a shared savings pool, you should only get your own share of the interest. Here, a function that lets you leave the pool accidentally gives you all the interest earned by everyone else in the pool. An attacker could call this function to take other people's earnings.
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.