Memorializing an NFT position on the same bucket of a previously memorialized NFT locks redemption
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/13
Found by
MalfurionWhitehat
Summary
Memorializing a position as an NFT on the same bucket of an existing memorialized position will not allow any of the owners to directly redeem it back later.
Vulnerability Detail
This issue happens because, after a position is memorialized on the PositionManager, this contract will centralize LP positions from different users, but these will be mapped to the same address from the point of view of Ajna pools (different users will be mapped as the same lender from the point of view of a Pool).
If more than one user has memorialized a position to the same bucket index, when attempting to PositionManager.redeemPositions, the call to pool.transferLPs will revert with NoAllowance, as LenderActions does not allow a transfer with value lower than the total lenderLpBalance.
Because of that, any of the users' that share a bucket redeemPositions calls will fail.
Impact
Although users that share a bucket with memorialized positions are not able to direct redeem their positions, they can eventually get their LPs back with a specific set of actions.
By first calling PositionManager.moveLiquidity to a bucket without any other LPs managed by PositionsManager (since LenderActions.moveQuoteToken accepts moving less liquidity than the total a LP balance for a specific bucket), and then calling PositionManager.redeemPositions, LP-ers will be able to redeem their positions back. Because of this possibility, this is a Medium-severity issue.
Code Snippet
diff --git a/contracts/tests/forge/PositionManager.t.sol b/contracts/tests/forge/PositionManager.t.sol
index 5e691fc..5d5e822 100644
--- a/contracts/tests/forge/PositionManager.t.sol
+++ b/contracts/tests/forge/PositionManager.t.sol
@@ -186,6 +186,91 @@ contract PositionManagerERC20PoolTest is PositionManagerERC20PoolHelperContract
assertTrue(_positionManager.isIndexInPosition(tokenId, 2552));
}
+ function testMemorializePositionsTwoAccountsSameBucket() external {
+ address alice = makeAddr("alice");
+ address bob = makeAddr("bob");
+ uint256 mintAmount = 10_000 * 1e18;
+
+ uint256 lpBalance;
+ uint256 depositTime;
+
+ _mintQuoteAndApproveManagerTokens(alice, mintAmount);
+ _mintQuoteAndApproveManagerTokens(bob, mintAmount);
+
+ // call pool contract directly to add quote tokens
+ uint256[] memory indexes = new uint256[](1);
+ indexes[0] = 2550;
+
+ // alice adds liquidity now
+ _addInitialLiquidity(
+ {
+ from: alice,
+ amount: 3_000 * 1e18,
+ index: indexes[0]
+ }
+ );
+ (lpBalance, depositTime) = _pool.lenderInfo(indexes[0], alice);
+ uint256 aliceDepositTime = block.timestamp;
+ assertEq(lpBalance, 3_000 * 1e27);
+ assertEq(depositTime, aliceDepositTime);
+
+ // bob adds liquidity later
+ skip(1 hours);
+ _addInitialLiquidity(
+ {
+ from: bob,
+ amount: 3_000 * 1e18,
+ index: indexes[0]
+ }
+ );
+ (lpBalance, depositTime) = _pool.lenderInfo(indexes[0], bob);
+ assertEq(lpBalance, 3_000 * 1e27);
+ assertEq(depositTime, aliceDepositTime + 1 hours);
+
+
+ // bob memorializes first, alice memorializes second
+ address[] memory addresses = new address[](2);
+ addresses[0] = bob;
+ addresses[1] = alice;
+ uint256[] memory tokenIds = new uint256[](2);
+
+ // bob and alice mint an NFT to later memorialize existing positions into
+ tokenIds[0] = _mintNFT(bob, bob, address(_pool));
+ assertFalse(_positionManager.isIndexInPosition(tokenIds[0], 2550));
+ tokenIds[1] = _mintNFT(alice, alice, address(_pool));
+ assertFalse(_positionManager.isIndexInPosition(tokenIds[1], 2550));
+
+ for(uint256 i = 0; i < addresses.length; ++i) {
+ // construct memorialize params struct
+ IPositionManagerOwnerActions.MemorializePositionsParams memory memorializeParams = IPositionManagerOwnerActions.MemorializePositionsParams(
+ tokenIds[i], indexes
+ );
+
+ // allow position manager to take ownership of the position
+ changePrank(addresses[i]);
+ _pool.approveLpOwnership(address(_positionManager), indexes[0], 3_000 * 1e27);
+
+ // memorialize quote tokens into minted NFT
+ vm.expectEmit(true, true, true, true);
+ emit MemorializePosition(addresses[i], tokenIds[i]);
+ vm.expectEmit(true, true, true, true);
+ emit TransferLPTokens(addresses[i], address(_positionManager), indexes, 3_000 * 1e27);
+
+ _positionManager.memorializePositions(memorializeParams);
+ }
+
+ // now both redeem
+ // will revert
+ for(uint256 i = 0; i < addresses.length; ++i) {
+ // construct memorialize params struct
+ IPositionManagerOwnerActions.RedeemPositionsParams memory params = IPositionManagerOwnerActions.RedeemPositionsParams(
+ tokenIds[i], address(_pool), indexes
+ );
+ changePrank(addresses[i]);
+ _positionManager.reedemPositions(params);
+ }
+ }
+
function testRememorializePositions() external {
address testAddress = makeAddr("testAddress");
uint256 mintAmount = 50_000 * 1e18;
Tool used
Manual Review
Recommendation
There are some possible alternatives to solving this issue.
- Allow
LenderActions.transferLPsto transfer an amount different than the total LP balance. Appropriate care must be taken withapprove/transferattack vectors, that do not exist now as approvals are always exact and cleared after every transfer, but they may become important if the this flow is refactored. - Reconsider the
PositionsManagerarchitecture and the way the LP balances are stored on the Ajna protocol. Since positions are not tokenized by default, but instead are stored on state variables on each pool, it is arguably more complex to reason about transferring positions between two users. Conversely, the ERC-20 and ERC-721 standards already provide straightforward methods for minting/burning/sending tokens for accounts. A suggestion is, for example, to remove the optionality of NFT minting through another independent contract (thePositionManager.sol), and instead to refactor the Ajna pools so that every position is an ERC-721 NFT minted directly by thePool.sol.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Ajna #2. Sherlock made it public on 14 February 2023. Found in the 32 audit contest (finding M-22).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.