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

Set tokens will be updated incorrectly #214

Closed
code423n4 opened this issue Jun 14, 2022 · 3 comments
Closed

Set tokens will be updated incorrectly #214

code423n4 opened this issue Jun 14, 2022 · 3 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists Index QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L664
https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L670

Vulnerability details

Medium

Set tokens will be updated incorrectly

Although the functions doesn't utilise the other returned arguments, it will still impact values used elsewhere.

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L664

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L670

For the above mentioned functions, their calculations is as a result of an erroneous calculation of the pre and post notional values,

The comment below stipulates:

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/lib/Position.sol#L255

but it uses the prePosition unit :

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/lib/Position.sol#L216

instead of the _postTotalNotional.

Also, the returned value of position unit can be zero if it's the first time the component is being added, this is so because the default position is the last thing that gets edited in calculateAndEditDefaultPosition() but the value is used prior to its modification :

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/lib/Position.sol#L109

Recommendation

As per the comment on line 255 of calculateDefaultEditPositionUnit(), use the postTotalNotional as the subtrahend.

@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 Jun 14, 2022
code423n4 added a commit that referenced this issue Jun 14, 2022
@ckoopmann ckoopmann added sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue Index labels Jun 15, 2022
@ckoopmann
Copy link
Collaborator

As far as I can see the function works as intended

The point of this function is to calculate the position unit after (post) the trade.
Normally this would just be _postTotalNotional.preciseDiv(_setTokenSupply) and we wouldn't need the preTrade balance / units.

However a part of the notional (the total balance of the component token) might have been airdropped tokens (that have been airdropped long before the trade), which are not to be included in the set tokens components.
To avoid these airdropped tokens being added to the component position we subtract the airdropped amount (which is the difference between the preTradeNotional and the total amount of the component the set should contain based on its component position).

I agree the comment is somewhat misleading, however this was part of the pre-existing and audited SetProtocol setup.

@gzeoneth
Copy link
Member

gzeoneth commented Jun 26, 2022

Downgrading to QA due to confusing comment

@gzeoneth gzeoneth 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 Jun 26, 2022
@gzeoneth
Copy link
Member

Consider with #210

@gzeoneth gzeoneth added the duplicate This issue or pull request already exists label Jun 26, 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 duplicate This issue or pull request already exists Index QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

3 participants