The fee for the protocol in the function RamsesV3Pool::flash() is not calculated correctly
The flash loan fee split in RamsesV3Pool::flash() divided the paid fee by the feeProtocol value instead of multiplying by a percentage, so the protocol received far less than its intended share. With the initial value of 80, the protocol got one-eightieth of the fee rather than 80 percent, leaking value away from the protocol.
Submitted by BenRai, also found by 0x37 and wasm_it
Impact
Because the fee for the protocol is not calculated correctly, the split of fees is wrong resulting in less fees for the protocol.
Proof of Concept
When calling the function flash on a RamsesV3Vault the caller initiates a flash loan. For the amount he flashes he needs to pay a fee. This fee is then divided between the protocol and the liquidity provider of the protocol. The issue arises from the fact how the share of the fees the protocol should get is calculated:
uint256 pFees0 = feeProtocol == 0 ? 0 : paid0 / feeProtocol;
The feeProtocol is a number between 0 and 100 representing the % of fees the protocol should get, which is initially set to 80 meaning 80% of the fees should go to the protocol. With the current calculation and the initial value of 80, the protocol would only get 1/80 of the fees instead of 80% of the fees, contradicting the intended fee distribution and resulting in way less fees for the protocol than intended.
Recommended Mitigation Steps
To ensure the right amount of fees are distributed to the protocol when a user initiates a flashloan change the current calculations for token0 and token 1 to the following:
function flash(address recipient, uint256 amount0, uint256 amount1, bytes calldata data) external override lock {
…
if (paid0 > 0) {
- uint256 pFees0 = feeProtocol == 0 ? 0 : paid0 / feeProtocol;
+ uint256 pFees0 = feeProtocol == 0 ? 0 : paid0 * feeProtocol / 100;
if (uint128(pFees0) > 0) $.protocolFees.token0 += uint128(pFees0);
$.feeGrowthGlobal0X128 += FullMath.mulDiv(paid0 - pFees0, FixedPoint128.Q128, _liquidity);
}
if (paid1 > 0) {
- uint256 pFees1 = feeProtocol == 0 ? 0 : paid1 / feeProtocol;
+ uint256 pFees1 = feeProtocol == 0 ? 0 : paid1 * feeProtocol / 100;
if (uint128(pFees1) > 0) $.protocolFees.token1 += uint128(pFees1);
$.feeGrowthGlobal1X128 += FullMath.mulDiv(paid1 - pFees1, FixedPoint128.Q128, _liquidity);
}
…
}
While it passes the
UniswapV3Pool.specbefore the fix, this is an oversight due tofeeProtocolno longer using bitshifting (like in regular UniswapV3). Our live V2 contracts were upgraded to makefeeProtocoloperate like this, and handle it — but was not translated over. Seems to be a valid finding by reading, would need to run test(s) to ensure it isn’t handled elsewhere. 👍
keccakdog (Ramses) confirmed, but disagreed with severity and commented:
Valid finding; however, this should be downgraded to a Low due to no funds at risk.
“2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.”
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 protocol fee share was computed as paid divided by feeProtocol rather than paid multiplied by feeProtocol and divided by 100, so a percentage value was treated as a divisor.
Impact
The protocol receives substantially less fee revenue than intended on every flash loan; the maintainer argued no funds were at direct risk and the judge rated it medium.
What the attacker needed
Flash loan on a RamsesV3 pool; feeProtocol set to a non-zero value
The fix
The calculation should multiply the paid fee by feeProtocol and divide by 100 for both tokens; the report recommends this change but does not state it was applied.
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.
A flash loan is a loan that is borrowed and repaid within a single transaction. The borrower pays a fee, and that fee is meant to be shared between the protocol and the people who supplied the liquidity. Here the code used division where it should have used a percentage calculation, so the protocol's cut came out tiny instead of large.
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.