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

Issue with totalSupplyAvg value calculation #134

Open
code423n4 opened this issue Sep 8, 2022 · 3 comments
Open

Issue with totalSupplyAvg value calculation #134

code423n4 opened this issue Sep 8, 2022 · 3 comments
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-09-canto/blob/65fbb8b9de22cf8f8f3d742b38b4be41ee35c468/src/Swap/BaseV1-core.sol#L260-L269

Vulnerability details

Impact

BaseV1-core.sol#L260
totalSupplyAvg may not provide the average value when granularity is lesser than or greater(too away from median value) than the total number of _totalSupplyAvg

Proof of Concept

https://github.com/code-423n4/2022-09-canto/blob/65fbb8b9de22cf8f8f3d742b38b4be41ee35c468/src/Swap/BaseV1-core.sol#L260-L269

function totalSupplyAvg(uint granularity) external view returns(uint) {
    uint[] memory _totalSupplyAvg = sampleSupply(granularity, 1);
    uint totalSupplyCumulativeAvg;


    for (uint i = 0; i < _totalSupplyAvg.length; ++i) {
        totalSupplyCumulativeAvg += _totalSupplyAvg[i]; //totalSupply denominated in terms of 1e18 
    }


    return (totalSupplyCumulativeAvg / granularity);
}

In above code the average is computed based on granularity but thie granularity can be a value which is too far away from the median value.
say, it could be too away from _totalSupplyAvg.length

Tools Used

VS code and Manual code review

Recommended Mitigation Steps

It is suggested to calculate the average value based on _totalSupplyAvg.length
totalSupplyCumulativeAvg / _totalSupplyAvg.length

@code423n4 code423n4 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 Sep 8, 2022
code423n4 added a commit that referenced this issue Sep 8, 2022
@nivasan1 nivasan1 added disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") and removed disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) labels Sep 10, 2022
@auditor0517
Copy link

I think _totalSupplyAvg.length is same as granularity after sampleSupply() function.

@nivasan1 nivasan1 added the disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) label Sep 12, 2022
@nivasan1
Copy link
Collaborator

Although this issue may indeed exist, it does not affect availability / function of the protocol, as there will be very few cases in which a pair will not be able to return a price. Furthermore, in these cases, it is desired behavior that the price is not available (first 8 observations have not been made).

@0xean
Copy link
Collaborator

0xean commented Oct 13, 2022

warden doesn't effectively demonstrate impact to rest of protocol, will downgrade to QA

@0xean 0xean added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

4 participants