Stuck ether when use function stake with empty derivatives ( derivativeCount = 0)
The stake function in the SafEth contract allowed staking before any derivatives were added, so the deposited ether became stuck in the contract and the user received no tokens. An attacker could lose funds by calling stake when derivativeCount is zero.
rotcivegaf Follow with ArbitraryExecution, alexzoid and 19 others
Originally disclosed on Code4rena
Submitted by rotcivegaf, also found by ArbitraryExecution, alexzoid, d3e4, brgltd, nemveer, Evo, carlitox477, Emmanuel, idkwhatimdoing, ToonVH, codetilda, pfapostol, vagrant, ayden, wait, hihen, Cryptor, 0xc0ffEE, UdarTeam, 7siech, and Co0nan
After initialize the contract SafEth, if someone call stake before addDerivative, the function stake skip the two for cycles because the derivativeCount is equal to 0 and don’t deposit in the derivative contract also mint 0 tokens to the sender. Finally the amount of msg.value will stuck in the contract
Proof of Concept
/* eslint-disable new-cap */
import { network, upgrades, ethers } from "hardhat";
import { expect } from "chai";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { SafEth } from "../typechain-types";
describe("stake tests", function () {
let adminAccount: SignerWithAddress;
let safEthProxy: SafEth;
const depositAmount = ethers.utils.parseEther("200");
before(async () => {
const latestBlock = await ethers.provider.getBlock("latest");
await network.provider.request({
method: "hardhat_reset",
params: [{forking: {
jsonRpcUrl: process.env.MAINNET_URL,
blockNumber: latestBlock.number,
}}],
});
const accounts = await ethers.getSigners();
adminAccount = accounts[0];
safEthProxy = await upgrades.deployProxy(
await ethers.getContractFactory("SafEth"),
[
"Asymmetry Finance ETH",
"safETH",
]
) as SafEth;
await safEthProxy.deployed();
});
it("PoC: don't have derivatives", async function () {
// Check: don't have derivatives
expect(await safEthProxy.derivativeCount()).eq(0);
// This transaction should revert
await safEthProxy.stake({ value: depositAmount });
const ethBal = await ethers.provider.getBalance(safEthProxy.address);
const stakerBal = await safEthProxy.balanceOf(adminAccount.address);
// This log 200 ether, but should be 0
console.log("safEthProxy Balance:", ethBal.toString());
// The staker has 0 tokens
console.log("staker Balance:", stakerBal.toString());
});
});
Recommended Mitigation Steps
When stake the derivativeCount should be greater than 0:
@@ -64,6 +64,7 @@ contract SafEth is
require(pauseStaking == false, "staking is paused");
require(msg.value >= minAmount, "amount too low");
require(msg.value <= maxAmount, "amount too high");
+ require(derivativeCount > 0, "derivativeCount is zero");
uint256 underlyingValue = 0;
toshiSat (Asymmetry) confirmed, but disagreed with severity and commented:
Seems like low severity to me.
Picodes (judge) decreased severity to Medium
Check derivativeCount on stake.
Status: Incorrectly mitigated. Full details in reports from adriro and d3e4 - and also shared below in the Mitigation Review section.
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 stake function did not check that derivativeCount is greater than zero before processing the deposit.
Impact
A user could lose their staked ether, which becomes stuck in the contract, and receive no tokens in return.
What the attacker needed
DerivativeCount is zero; user calls stake with ether
The fix
The stake function should require that derivativeCount is greater than zero before accepting a stake.
How it was found
- Deploy the SafEth contract via proxy with initialisation.
- Confirm that derivativeCount is zero.
- Call stake with a value of 200 ether.
- Observe that the contract balance is 200 ether and the staker's token balance is zero.
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.
This is a smart contract bug where a user can send ether to stake it, but if no investment options (derivatives) have been set up yet, the contract keeps the ether and gives nothing back. The user loses their money because the contract does not check that there is at least one derivative before accepting the stake.
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.