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

Wrong implementation of twAML in function TapiocaOptionBroker.exitPosition() #1103

Closed
code423n4 opened this issue Aug 4, 2023 · 5 comments
Closed
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-1623 satisfactory satisfies C4 submission criteria; eligible for awards 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/Tapioca-DAO/tap-token-audit/blob/59749be5bc2286f0bdbf59d7ddc258ddafd49a9f/contracts/options/TapiocaOptionBroker.sol#L314-L322

Vulnerability details

Impact

Incorrect accounting for pool.cumulative can lead to incorrect voting power calculation in the broker contract.

Proof of Concept

The function TapiocaOptionBroker.exitPosition() is utilized to exit a twAML (twTAP) participation.

/// link = https://github.com/Tapioca-DAO/tap-token-audit/blob/59749be5bc2286f0bdbf59d7ddc258ddafd49a9f/contracts/options/TapiocaOptionBroker.sol#L314-L322

function exitPosition(uint256 _oTAPTokenID) external {
    ... 

    if (participation.divergenceForce) {
        if (pool.cumulative > pool.averageMagnitude) {
            pool.cumulative -= pool.averageMagnitude;
        } else {
            pool.cumulative = 0;
        }
    } else {
        pool.cumulative += pool.averageMagnitude;
    }

    ...
}

Note that the participation entry tracks the average magnitude as it was at the time the participant entered. So, to exit the twAML, we need to reverse the participation process. In other words, if the position has voting power and is divergenceForce, the correct exit process should be:

if (position.divergenceForce) {
    if (pool.cumulative > participation.averageMagnitude) {
        pool.cumulative -= participation.averageMagnitude;
    } else {
        pool.cumulative = 0;
    }
} else {
    pool.cumulative += participation.averageMagnitude;
}

However, in the function TapiocaOptionBroker.exitPosition(), the pool.cumulative is updated with pool.averageMagnitude instead of participation.averageMagnitude, leading to an incorrect implementation.

To consolidate the issue, we can examine the update of exit process in twTAP.sol in this link:

Tools Used

Manual review

Recommended Mitigation Steps

Modifying the update of pool.cumulative as follows:

if (position.divergenceForce) {
    if (pool.cumulative > participation.averageMagnitude) {
        pool.cumulative -= participation.averageMagnitude;
    } else {
        pool.cumulative = 0;
    }
} else {
    pool.cumulative += participation.averageMagnitude;
}

Assessed type

Other

@code423n4 code423n4 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Aug 4, 2023
code423n4 added a commit that referenced this issue Aug 4, 2023
@c4-pre-sort
Copy link

minhquanym marked the issue as primary issue

@c4-pre-sort c4-pre-sort added the primary issue Highest quality submission among a set of duplicates label Aug 8, 2023
@c4-sponsor
Copy link

0xRektora (sponsor) confirmed

@c4-sponsor c4-sponsor added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Sep 1, 2023
@0xRektora
Copy link

Dup #1623

@c4-judge c4-judge added duplicate-1623 and removed primary issue Highest quality submission among a set of duplicates labels Sep 29, 2023
@c4-judge
Copy link

dmvt marked the issue as duplicate of #1623

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Sep 29, 2023
@c4-judge
Copy link

dmvt marked the issue as satisfactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working duplicate-1623 satisfactory satisfies C4 submission criteria; eligible for awards 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

5 participants