New gALCX token denomination can be depressed by the first depositor
The first depositor of a new gALCX contract can make a tiny stake and then directly transfer a large amount of ALCX to inflate the exchange rate, causing later depositors' shares to be rounded down and their funds captured by the attacker. This allows the attacker to steal a portion of subsequent deposits.
Submitted by hyh, also found by 0xsomeone
An attacker can become the first depositor for a recently created gALCX contract, providing a tiny amount of ALCX tokens by calling stake(1) (raw values here, 1 is 1 wei, 1e18 is 1 ALCX). Then the attacker can directly transfer, for example, 10^6 * 1e18 - 1 of ALCX to the gALCX contract and run bumpExchangeRate(), effectively setting the cost of 1 gALCX to be 10^6 * 1e18 of ALCX. The attacker will still own 100% of the gALCX’s ALCX pool being the only depositor.
All subsequent depositors will have their ALCX token investments rounded to 10^6 * 1e18, due to the lack of precision which initial tiny deposit caused, with the remainder divided between all current depositors, i.e. the subsequent depositors lose value to the attacker.
For example, if the second depositor brings in 1.9*10^6 * 1e18 of ALCX, only 1 of new vault to be issued as 1.9*10^6 * 1e18 divided by 10^6 * 1e18 will yield just 1, which means that 2.9*10^6 * 1e18 total ALCX pool will be divided 50/50 between the second depositor and the attacker, as each will have 1 wei of the total 2 wei of vault tokens, i.e. the depositor lost and the attacker gained 0.45 * 10^6 * 1e18 of ALCX tokens.
As there are no penalties to exit with gALCX.unstake(), the attacker can remain staked for an arbitrary time, gathering the share of all new deposits’ remainder amounts.
Placing severity to be medium as this is principal funds loss scenario for many users (most of depositors), easily executable, but only new gALCX contract instances are vulnerable.
Proof of Concept
gAmount of gALCX to be minted is determined as a quotient of amount provided and exchangeRate:
uint gAmount = amount * exchangeRatePrecision / exchangeRate;
_mint(msg.sender, gAmount);
uint public constant exchangeRatePrecision = 1e18;
exchangeRate accumulates balance increments relative to total gALCX supply:
function bumpExchangeRate() public {
// Claim from pool
pools.claim(poolId);
// Bump exchange rate
uint balance = alcx.balanceOf(address(this));
if (balance > 0) {
exchangeRate += (balance * exchangeRatePrecision) / totalSupply;
When gALCX contract is new, the very first stake -> bumpExchangeRate() yields nothing as the balance is empty, i.e. exchangeRate is still 1e18 and gAmount == amount:
function stake(uint amount) external {
// Get current exchange rate between ALCX and gALCX
bumpExchangeRate();
// Then receive new deposits
bool success = alcx.transferFrom(msg.sender, address(this), amount);
require(success, "Transfer failed");
pools.deposit(poolId, amount);
// gAmount always <= amount
uint gAmount = amount * exchangeRatePrecision / exchangeRate;
This way, as there is no minimum amount or special treatment for the first deposit, the very first gAmount can be made 1 wei with stake(1) call.
Then, a combination of direct ALCX transfer and bumpExchangeRate() will make exchangeRate equal to the total amount provided by the attacker, say 10^6 * 1e18 * 1e18, as totalSupply is 1 wei.
When a second depositor enters, the amount of gALCX to be minted is calculated as amount * exchangeRatePrecision / exchangeRate, which is amount / (10^6 * 1e18), which will trunk the amount to the nearest divisor of 10^6 * 1e18, effectively dividing the remainder between the depositor and the attacker.
For example, if the second depositor brings in 1.9*10^6 ALCX, only 1 (1 wei) of gALCX to be issued as 1.9*10^6 * 1e18 * 1e18 / (10^6 * 1e18 * 1e18) = 1.
As the attacker and depositor both have 1 of gALCX, each owns (2.9 / 2)*10^6 * 1e18 = 1.45*10^6 * 1e18, so the attacker effectively stole 0.45*10^6 * 1e18 from the depositor.
Any deposit lower than total attacker’s stake, 10^6 * 1e18, will be fully stolen from the depositor as 0 gALCX tokens will be issued in this case.
References
The issue is similar to the TOB-YEARN-003 one of the Trail of Bits audit of Yearn Finance:
https://github.com/yearn/yearn-security/tree/master/audits/20210719_ToB_yearn_vaultsv2
Recommended Mitigation Steps
A minimum for deposit value can drastically reduce the economic viability of the attack. I.e. stake() can require each amount to surpass the threshold, and then an attacker would have to provide too big direct investment to capture any meaningful share of the subsequent deposits.
An alternative is to require only the first depositor to freeze big enough initial amount of liquidity. This approach has been used long enough by various projects, for example in Uniswap V2:
Uniswap/UniswapV2Pair.sol#L119-L121
0xfoobar (Alchemix) acknowledged, disagreed with severity and commented:
Not a risk with current >400 tokenholders, but good to incorporated into future designs.
0xleastwood (judge) commented:
I think from the perspective of the contest, it is fair to assume that contracts are somewhat fresh. I’d be inclined to keep this as medium because it outlines a viable attack path that should be made public.
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
Likely the lack of a minimum deposit or special handling for the first deposit allows the exchange rate to be manipulated, causing rounding errors that favour the first depositor.
Impact
Subsequent depositors could lose a significant portion of their deposits to the attacker, but only new gALCX contract instances are vulnerable.
What the attacker needed
New gALCX contract; attacker is first depositor; attacker has ALCX tokens
The fix
A minimum deposit value should be enforced, or the first depositor should be required to lock a sufficiently large initial amount.
How it was found
- Call stake(1) to become the first depositor with 1 wei of gALCX.
- Directly transfer a large amount of ALCX to the gALCX contract.
- Call bumpExchangeRate() to set a high exchange rate.
- Subsequent depositors receive rounded-down gALCX shares, losing value to the attacker.
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 put money into a shared pool, you get shares representing your portion. The first person to join can manipulate the share price by adding extra money directly to the pool, so later joiners get fewer shares than they should and lose money to the first person.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.