Read-only reentrancy in BondFixedTermTeller
The BondFixedTermTeller contract updates the total supply of an ERC1155 bond after minting the tokens, allowing a recipient to reenter and observe an outdated supply. An attacker could exploit this to cause a miscalculation in protocols that use the supply to determine bond price.
Source: https://github.com/sherlock-audit/2022-11-bond-judging/issues/23
Found by
Zarf
Summary
When minting new ERC1155 bonds in the BondFixedTermTeller contract, the total supply of this specific bond is updated after the new bonds are sent to the recipient, which introduces a reentrancy attack.
Vulnerability Detail
Whenever a new ERC1155 bond is minted in the BondFixedTermTeller contract, either through _handlePayout() or create(), the total supply is updated after the bond has been minted.
ERC1155 tokens will perform a callback to the recipient in case the recipient implements the ERC1155TokenReceiver interface. Therefore, the recipient (msg.sender in create() or recipient_ in _handlePayout() ) is able to perform a call to an arbitrary contract before the total supply of the bonds is updated.
While the recipient could enter the current BondFixedTermTeller contract to call any function, there is no interesting function which might result in financial loss in case it gets called in the callback. Alternatively, the recipient could enter a smart contract which uses the the public mapping tokenMetadata in BondFixedTermTeller to calculate the current bond price based on the supply. As the supply is not yet updated, but the tokens are minted, this might result in a miscalculation of the price.
Impact
While the BondFixedTermTeller contract itself is not at risk, any protocols integrating with BondFixedTermTeller and using the total supply of the ERC1155 bond token to calculate the price, might come at risk.
Code Snippet
https://github.com/sherlock-audit/2022-11-bond/blob/main/src/BondFixedTermTeller.sol#L218-L225
Tool used
Manual Review
Recommendation
Update the total supply and mint the tokens afterwards:
function _mintToken(
address to_,
uint256 tokenId_,
uint256 amount_
) internal {
tokenMetadata[tokenId_].supply += amount_;
_mint(to_, tokenId_, amount_, bytes(""));
}
Discussion
Evert0x
Message from sponsor
Agree with this issue. We updated the _mintToken() and _burnToken() functions to update supply prior to minting/burning tokens to avoid the reentrancy issue.
xiaoming9090
Fixed in https://github.com/Bond-Protocol/bonds/commit/fafd81d04d685d15612cc56af635513e11ddc626
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
Impact
Protocols that integrate with BondFixedTermTeller and use the total supply to calculate price could be at risk of miscalculation, though the BondFixedTermTeller contract itself is not directly at risk.
What the attacker needed
Recipient is a contract; integrating protocol uses supply for price
The fix
The issue was fixed by updating the total supply before minting the tokens.
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 a contract mints tokens, it should update its records before sending them out. Here, it sends the tokens first, and the recipient can call back into the system before the supply count is corrected. This can trick other contracts that rely on the supply number into making wrong calculations.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.