Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision Loss in getAmountOut Function Leads to Incorrect Price Calculations #174

Open
c4-bot-6 opened this issue Oct 30, 2024 · 0 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working edited-by-warden 🤖_primary AI based primary recommendation sufficient quality report This report is of sufficient quality

Comments

@c4-bot-6
Copy link
Contributor

c4-bot-6 commented Oct 30, 2024

Lines of code

https://github.com/ronin-chain/katana-v3-contracts/blob/03c80179e04f40d96f06c451ea494bb18f2a58fc/src/periphery/libraries/KatanaV2Library.sol#L46

Vulnerability details

Summary

The getAmountOut function in KatanaV2Library.sol can return zero due to precision loss when there's a large disparity between reserveIn and reserveOut. While this doesn't directly cause loss of funds, it can lead to incorrect price calculations that affect other protocol functions.

PoC

function testPrecisionLoss() public {
    uint256 amountIn = 100;
    uint256 reserveIn = 20000;
    uint256 reserveOut = 100;
    
    
    uint256 amountInWithFee = 100 * 997;                    // = 99,700
    uint256 numerator = 99,700 * 100;                       // = 9,970,000
    uint256 denominator = (20000 * 1000) + 99,700;         // = 20,099,700
    uint256 amountOut = 9,970,000 / 20,099,700;            // = 0 (0.496 rounded down)
    
    // User loses all expected output
    assert(amountOut == 0);  // True, demonstrating the issue
}

Assessed type

Other

@c4-bot-6 c4-bot-6 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Oct 30, 2024
c4-bot-8 added a commit that referenced this issue Oct 30, 2024
@c4-bot-12 c4-bot-12 added the 🤖_primary AI based primary recommendation label Oct 30, 2024
howlbot-integration bot added a commit that referenced this issue Nov 4, 2024
@howlbot-integration howlbot-integration bot added the sufficient quality report This report is of sufficient quality label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working edited-by-warden 🤖_primary AI based primary recommendation sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

3 participants