Bind pool reserves to PoolConfig and validate them in handlers - #180
Merged
Merged
Conversation
…r a swap deposit_liquidity, withdraw_liquidity, swap_tokens and claim_admin_fees took pool_a and pool_b with no check that they were the pool's reserves. A trader could name a mint-A token account of their own as pool_a: the swap priced itself from that account's one-unit balance, sent the input back into it, and paid out 9,999,989 of a 10,000,000 pool_b. PoolConfig now records both reserve addresses at initialize_pool, and the four handlers check them with has_one, failing with InvalidPoolVault. swap_rejects_substituted_pool_vault and deposit_rejects_substituted_pool_vaults run the attack. The Anchor v1 and v2 versions already bound the reserves as the pool's associated token accounts. swap_tokens also re-checked the constant-product invariant against reserves computed from the amounts it meant to move. It now reads both vaults after the transfers land, as the Anchor versions do. Claude-Session: https://claude.ai/code/session_01BvopS6WYzsT9Qf32NTu29V
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical vulnerability in the Quasar token swap program where handlers accepted arbitrary token accounts as pool reserves without validation. An attacker could supply their own token account as
pool_aorpool_b, causing the handler to price trades from the attacker's balance while draining the real reserve on the other side.Key Changes
Store reserve addresses in PoolConfig: Added
pool_aandpool_bfields toPoolConfigstruct, recorded during pool initialization. This growsPoolConfigby 64 bytes, making pools created before this change incompatible.Add validation constraints: Added
has_one(pool_a)andhas_one(pool_b)checks to all four handlers that touch reserves:swap_tokensdeposit_liquiditywithdraw_liquidityclaim_admin_feesThese checks fail with the new
InvalidPoolVaulterror if the provided accounts don't match the stored addresses.Improve invariant verification in swap_tokens: Changed the constant-product invariant check to read vault balances directly after transfers complete, rather than computing them from the amounts the handler intended to move. This catches cases where transfers moved different amounts than expected.
Add security tests: Included
swap_rejects_substituted_pool_vaultanddeposit_rejects_substituted_pool_vaultstests that demonstrate the attack and verify it is now prevented.Implementation Details
PoolConfigstruct now records both reserve addresses at initialization time, since a token account's mint and authority alone don't uniquely identify it.PoolConfignow preserve thepool_aandpool_bfields when updating other fields.https://claude.ai/code/session_01BvopS6WYzsT9Qf32NTu29V