Submitted by zzzitron
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L263-L268 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L721-L732 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/a4793e75a79060f8332927f97c6451362ae30201/contracts/markets/singularity/SGLLendingCommon.sol#L83-L95 
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/a4793e75a79060f8332927f97c6451362ae30201/contracts/markets/singularity/SGLBorrow.sol#L45-L52
When an user allows certain amount to a spender, the spender can spend more than the allowance.
Note that this is a different issue from the misuse of allowedBorrow for the share amount
(i.e. issue BigBang::repay uses allowedBorrow with the asset amount, whereas other functions use it with share of collateral), as the fix in the other issue will not mitigate this issue.
This issue is the misuse of part and elastic, whereas the other issue is the misuse of the share and asset.
The spec in the MarketERC20::approve function specifies that the approved amount is the maximum amount that the spender can draw.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/364dead3a42b06a34c802eee951cea1a654d438e/contracts/markets/MarketERC20.sol#L189-L200
However, the spender can draw more than the allowance if the totalBorrow.base is more thant totalBorrow.elastic, which is likely condition.
The proof of concept below demonstrates that more asset was pulled than allowed.
It is only a part of proof of concept; to see the full proof of concept see https://gist.github.com/zzzitron/8dd809c0ea39dc0ea727534c3ba804f9
To use it, put it in the test/bigBang.test.ts in the tapiocabar-audit repo
The eoa1 allows deployer 1e18. After the timeTravel, the elastic of totalBorrow is more than the base. Under the condition, the deployer uses the allowance with the BigBang::repay function. As the result, more asset than allowance was pulled from eoa1.
The result of the poc is below, which shows that 1000136987569097987 is pulled from the eoa1, which is more than the allowance (i.e. 1e18).
The same issue is also in the Singularity. In the same manner shown above, the spender will pull more than allowed when the totalBorrow.elastic is bigger than the totalBorrow.base.
The function BigBang::repay uses part to check for the allowance.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L263-L268
However, the BigBang::_repay draws actually the corresponding elastic of the part from the from address.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L721-L732
The similar lines of code is also in the Singularity. The Singularity::repay will delegate call on the SGLBorrow::repay, which has the modifier of allowedBorrow(from, part):
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/a4793e75a79060f8332927f97c6451362ae30201/contracts/markets/singularity/SGLBorrow.sol#L45-L51
Then, amount is calculated from the part, and the amount is pulled from the from address in the below code snippet.
https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/a4793e75a79060f8332927f97c6451362ae30201/contracts/markets/singularity/SGLLendingCommon.sol#L83-L95
The amount is likely to be bigger than the part, since the calculation is based on the totalBorrows ratio between elastic and base.
Then the amount is used to withdraw from from address, meaning that more than the allowance is withdrawn.
The discrepancy between the allowance and actually spendable amount is going to grow in time, as the totalBorrows elastic will outgrow the base in time.
Hardhat
Instead of using the part to check the allowance, calculate the actual amount to be pulled and use the amount to check the allowance.
0xRektora (Tapioca) confirmed
