AuraSpell `openPositionFarm` will revert when the tokens contains `lpToken`
The AuraSpell contract's openPositionFarm function can revert when the tokens array includes the lpToken, because the amountsIn array is shortened via assembly while maxAmountsIn remains full length, causing a length mismatch that Balancer's vault rejects. This prevents users from opening positions in such cases.
bitsurfer Follow with nobody2018 and twcctop
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-07-blueberry-judging/issues/125
Found by
bitsurfer, nobody2018, twcctop
AuraSpell openPositionFarm will revert when the tokens contains lpToken due to array length mismatch
Vulnerability Detail
In AuraSpell, the openPositionFarm will call joinPool in Balancer's vault. But when analyzing the JoinPoolRequest struct, we see issue on maxAmountsIn and amountsIn which can be in different length, thus this will be reverted since in Balancer's vault, this two array should be in the same length.
File: AuraSpell.sol
088: function openPositionFarm(
089: OpenPosParam calldata param,
090: uint256 minimumBPT
091: )
...
095: {
...
110: /// 3. Add liquidity to the Balancer pool and receive BPT in return.
111: {
...
128: if (poolAmountOut != 0) {
129: vault.joinPool(
130: wAuraPools.getBPTPoolId(lpToken),
131: address(this),
132: address(this),
133: IBalancerVault.JoinPoolRequest({
134: assets: tokens,
135: maxAmountsIn: maxAmountsIn,
136: userData: abi.encode(1, amountsIn, _minimumBPT),
137: fromInternalBalance: false
138: })
139: );
140: }
141: }
...
178: }
...
296: function _getJoinPoolParamsAndApprove(
297: address vault,
298: address[] memory tokens,
299: uint256[] memory balances,
300: address lpToken
301: ) internal returns (uint256[] memory, uint256[] memory, uint256) {
...
304: uint256 length = tokens.length;
305: uint256[] memory maxAmountsIn = new uint256[](length);
306: uint256[] memory amountsIn = new uint256[](length);
307: bool isLPIncluded;
308:
309: for (i; i != length; ) {
310: if (tokens[i] != lpToken) {
311: amountsIn[j] = IERC20(tokens[i]).balanceOf(address(this));
312: if (amountsIn[j] > 0) {
313: _ensureApprove(tokens[i], vault, amountsIn[j]);
314: }
315: ++j;
316: } else isLPIncluded = true;
317:
318: maxAmountsIn[i] = IERC20(tokens[i]).balanceOf(address(this));
319:
320: unchecked {
321: ++i;
322: }
323: }
324:
325: if (isLPIncluded) {
326: assembly {
327: mstore(amountsIn, sub(mload(amountsIn), 1))
328: }
329: }
...
345: return (maxAmountsIn, amountsIn, poolAmountOut);
346: }
these maxAmountsIn and amountsIn are coming from _getJoinPoolParamsAndApprove. And by seeing the function, we can see that there is possible issue when the tokens[i] == lpToken.
When tokens[i] == lpToken, the flag isLPIncluded will be true. And will enter this block,
325: if (isLPIncluded) {
326: assembly {
327: mstore(amountsIn, sub(mload(amountsIn), 1))
328: }
329: }
this will decrease the amountsIn length. Thus, amountsIn and maxAmountsIn will be in different length.
In Balancer's JoinPoolRequest struct, the maxAmountsIn, and userData second decoded bytes (amountsIn) should be the same array length, because it will be checked in Balancer.
133: IBalancerVault.JoinPoolRequest({
134: assets: tokens,
135: maxAmountsIn: maxAmountsIn,
136: userData: abi.encode(1, amountsIn, _minimumBPT),
137: fromInternalBalance: false
138: })
Therefore, in this situation, it will be reverted.
Impact
User can't open position on AuraSpell when tokens contains lpToken
Code Snippet
Tool used
Manual Review
Recommendation
Remove the assembly code where it will decrease the amountsIn length when isLPIncluded is true to make sure the array length are same.
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 code removes an element from amountsIn using assembly when lpToken is present, but does not adjust maxAmountsIn, leading to mismatched array lengths that Balancer's joinPool requires to be equal.
Impact
Users cannot open positions on AuraSpell when the tokens array contains the lpToken, as the transaction will revert.
What the attacker needed
Tokens array includes lpToken
The fix
Remove the assembly code that decreases the length of amountsIn when isLPIncluded is true, so that both arrays have the same length.
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 add money to a Balancer pool, you provide two lists: one for the maximum amounts you're willing to put in, and one for the actual amounts. Balancer requires these lists to be the same length. In this code, if one of the tokens is the pool's own token (lpToken), the actual amounts list is shortened but the maximum list is not, so Balancer rejects the transaction and it fails.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.