The BigBang contract take more fees than it should
The BigBang contract's repay function burns less USDO than it should, causing the protocol to take more fees than intended. An attacker could exploit this to make other borrowers pay excessive fees, though the report does not specify a direct attacker gain.
0xRobocop Follow with mojito_auditor, KIntern_NA and 2 others
Originally disclosed on Code4rena
Submitted by 0xRobocop, also found by mojito_auditor, KIntern_NA, xuwinnie, and rvierdiiev
The repay function in the BigBang contract is used for users to repay their loans. The mechanics of the function are simple:
-
- Update the exchange rate
-
- Accrue the fees generated
-
- Call internal function _repay
The internal function _repay handles the state changes regarding the user debt. Specifically, fees are taken by withdrawing all the user’s debt from yieldbox and burning the proportion that does not correspond to fees. The fees stay in the contract’s balance to later be taken by the penrose contract. The logic can be seen here:
function _repay(
address from,
address to,
uint256 part
) internal returns (uint256 amount) {
(totalBorrow, amount) = totalBorrow.sub(part, true);
userBorrowPart[to] -= part;
uint256 toWithdraw = (amount - part); //acrrued
// @audit-issue Takes more fees than it should
uint256 toBurn = amount - toWithdraw;
yieldBox.withdraw(assetId, from, address(this), amount, 0);
//burn USDO
if (toBurn > 0) {
IUSDOBase(address(asset)).burn(address(this), toBurn);
}
emit LogRepay(from, to, amount, part);
}
The problem is that the function burns less than it should, hence, taking more fees than it should.
Proof of Concept
I will provide a symbolic proof and coded proof to illustrate the issue. To show the issue clearly we will assume that there is no opening fee, and that the yearly fee is of 10%. Hence, for the coded PoC it is important to change the values of bigBangEthDebtRate and borrowOpeningFee:
// Penrose contract
bigBangEthDebtRate = 1e17;
// BigBang contract
borrowOpeningFee;
Symbolic
How much fees do the protocol should take?. The answer of this question can be represented in the following equation:
ProtocolFees = CurrentUserDebt - OriginalUserDebt
The fees accrued for the protocol is the difference of the current debt of the user and the original debt of the user. If we examine the implementation of the _repay function we found the next:
//uint256 amount;
(totalBorrow, amount) = totalBorrow.sub(part, true);
userBorrowPart[to] -= part;
uint256 toWithdraw = (amount - part); //acrrued
// @audit-issue Takes more fees than it should
uint256 toBurn = amount - toWithdraw;
yieldBox.withdraw(assetId, from, address(this), amount, 0);
//burn USDO
if (toBurn > 0) {
IUSDOBase(address(asset)).burn(address(this), toBurn);
}
The important variables are:
-
partrepresents the base part of the debt of the user
-
amountis the elastic part that was paid givingpart, elastic means this is the real debt.
At the following line the contract takes amount which is the real user debt from yield box:
yieldBox.withdraw(assetId, from, address(this), amount, 0);
Then it burns some tokens:
if (toBurn > 0) {
IUSDOBase(address(asset)).burn(address(this), toBurn);
}
But how toBurn is calculated?:
uint256 toWithdraw = (amount - part); //acrrued
uint256 toBurn = amount - toWithdraw;
toBurn is just part. Hence, the contract is computing the fees as:
ProtocolFees = amount - part. Rewriting this with the first equation terms will be:
ProtocolFees = CurrentDebt - part.
But it is part equal to OriginalDebt?. Remember that part is not the actual debt, is just the part of the real debt to be paid, this can be found in a comment in the code:
elastic = Total token amount to be repayed by borrowers, base = Total parts of the debt held by borrowers.
So they are equal only for the first borrower, but for the others this wont be the case since the relation of elastic and part wont be 1:1 due to accrued fees, making part < OriginalDebt, and hence the protocol taking more fees. Let’s use some number to showcase it better:
TIME = 0
First borrower A asks 1,000 units, state:
part[A] = 1000
total.part = 1000
total.elastic = 1000
TIME = 1 YEAR
part[A] = 1000 --> no change from borrower A
total.part = 1000 --> no change yet
total.elastic = 1100 --> fees accrued in one year 100 units
Second borrower B asks 1,000 units, state:
part[B] = 909.09
total.part = 1909.09
total.elastic = 2100
B part was computed as:
1000 * 1000 / 1100 = 909.09
TIME = 2 YEAR
Fees are accrued, hence:
total.elastic = 2100 * 1.1 = 2310.
Hence the total fees accrued by the protocol are:
2310 - 2000 = 310.
These 310 are collected from A and B in the following proportions:
A Fee = 210
B Fee = 100
Borrower B produced 100 units of fees, which makes sense, he asked for 1000 units at 10%/year.
When B repays its debt, he needs to repay 1,100 units. Then the contract burns the proportion that was real debt, the problem as stated above is that the function burns the part and not the original debt, hence the contract will burn 909.09 units. Hence it took:
1100 - 909.09 = 190.91 units
The contract took 190.91 in fees rather than 100 units.
Coded PoC
Follow the next steps to run the coded PoC:
- 1.- Make the contract changes described at the beginning.
- 2.- Add the following test under
test/bigBang.test.ts:
describe.only('borrow() & repay() check fees', () => {
it('should borrow and repay check fees', async () => {
const {
wethBigBangMarket,
weth,
wethAssetId,
yieldBox,
deployer,
bar,
usd0,
__wethUsdcPrice,
timeTravel,
eoa1,
} = await loadFixture(register);
await weth.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox.setApprovalForAll(wethBigBangMarket.address, true);
await weth
.connect(eoa1)
.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox
.connect(eoa1)
.setApprovalForAll(wethBigBangMarket.address, true);
const wethMintVal = ethers.BigNumber.from((1e18).toString()).mul(
10,
);
await weth.freeMint(wethMintVal);
await weth.connect(eoa1).freeMint(wethMintVal);
const valShare = await yieldBox.toShare(
wethAssetId,
wethMintVal,
false,
);
await yieldBox.depositAsset(
wethAssetId,
deployer.address,
deployer.address,
0,
valShare,
);
await wethBigBangMarket.addCollateral(
deployer.address,
deployer.address,
false,
0,
valShare,
);
await yieldBox
.connect(eoa1)
.depositAsset(
wethAssetId,
eoa1.address,
eoa1.address,
0,
valShare,
);
await wethBigBangMarket
.connect(eoa1)
.addCollateral(eoa1.address, eoa1.address, false, 0, valShare);
//borrow
const usdoBorrowVal = wethMintVal
.mul(10)
.div(100)
.mul(__wethUsdcPrice.div((1e18).toString()));
await wethBigBangMarket.borrow(
deployer.address,
deployer.address,
usdoBorrowVal,
);
const userBorrowPart = await wethBigBangMarket.userBorrowPart(
deployer.address,
);
console.log('User A Borrow Part: ' + userBorrowPart);
timeTravel(365 * 86400);
await wethBigBangMarket
.connect(eoa1)
.borrow(eoa1.address, eoa1.address, usdoBorrowVal);
timeTravel(365 * 86400);
const eoa1BorrowPart = await wethBigBangMarket.userBorrowPart(
eoa1.address,
);
console.log('User B Borrow Part: ' + eoa1BorrowPart);
const usd0Extra = ethers.BigNumber.from((1e18).toString()).mul(500);
await usd0.mint(eoa1.address, usd0Extra);
await usd0.connect(eoa1).approve(yieldBox.address, usd0Extra);
await yieldBox
.connect(eoa1)
.depositAsset(
await wethBigBangMarket.assetId(),
eoa1.address,
eoa1.address,
usd0Extra,
0,
);
const contractusdoB1 = await usd0.balanceOf(
wethBigBangMarket.address,
);
console.log('Fees before repayment: ' + contractusdoB1);
// Repayment happens
await wethBigBangMarket
.connect(eoa1)
.repay(eoa1.address, eoa1.address, false, eoa1BorrowPart);
const userBorrowPartAfter = await wethBigBangMarket.userBorrowPart(
eoa1.address,
);
// User paid all its debt.
expect(userBorrowPartAfter.eq(0)).to.be.true;
const contractusdoB2 = await usd0.balanceOf(
wethBigBangMarket.address,
);
console.log('Fees after repayment: ' + contractusdoB2);
});
});
Tools Used
Hardhat
Recommended Mitigation Steps
Not only store the user borrow part but also the original debt which is debtAsked + openingFee. So, during repayment the contract can compute the real fees generated.
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 contract calculates the fee as the difference between the current debt amount and the fixed part, rather than between the current debt and the original debt, so it overcharges when the part-to-amount ratio is not 1:1.
Impact
The protocol could take more fees than intended from borrowers, as shown in the example where the second borrower is charged 190.91 instead of 100 units.
What the attacker needed
A borrower repays after fees have accrued; multiple borrowers with different entry times
The fix
The contract should store the original debt (debt asked plus opening fee) and use it to compute the real fees during repayment.
How it was found
- Set the yearly fee to 10% and opening fee to zero.
- First borrower borrows 1,000 units.
- After one year, second borrower borrows 1,000 units.
- After another year, second borrower repays their debt.
- Observe that the contract takes more fees than the 100 units the second borrower should owe.
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 borrow, your debt is tracked in two ways: a fixed 'part' and a growing 'amount' that includes interest. The contract mistakenly uses the fixed part to decide how much of the borrowed token to destroy, so it keeps more as fees than it should. This means borrowers pay more interest than they owe.
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.