Skip to content
Smart contracts Audit finding Advanced

Gov ( twTAP ) and Tapioca Option can be monopolized by an attacker

The twTAP contract fails to update averageMagnitude when a position exits, so averageMagnitude only increases. An attacker can repeatedly enter and exit positions to inflate averageMagnitude and then reset cumulative to zero, preventing others from participating and monopolising governance.

ronnyx2017 Follow with immeas and GalloDaSballo

Originally disclosed on Code4rena

Submitted by ronnyx2017, also found by immeas and GalloDaSballo

https://github.com/Tapioca-DAO/tap-token/blob/20a83b1d2d5577653610a6c3879dff9df4968345/contracts/governance/twTAP.sol#L592-L594

Vulnerability details

The function twTAP._releaseTap updates only --twAML.totalParticipants and neglected to update twAML.averageMagnitude. This will result in twAML.averageMagnitude accumulating every time a new position participates, without ever decreasing.

            pool.averageMagnitude = (pool.averageMagnitude + magnitude) / pool.totalParticipants; // compute new average magnitude

Impact

An attacker can monopolize the gov by precisely controlling the number of tokens entering and exiting to ensure that only their own position remains in the current twTap.

On the other hand, there is another exploit way here. If pool.cumulative < pool.averageMagnitude, then pool.cumulative will be set to 0. But the greater position.averageMagnitude will be added to pool.cumulative when the position exits. This will cause pool.cumulative to continuously increase, which will result in the efficiency of twTAP/TapiocaOptionBroker becoming increasingly lower. Because the multiplier of the twTAP or target of the TapiocaOptionBroker will always be dMIN.

The same issue is also in the TapiocaOptionBroker. The only difference is that the pool.cumulative can’t be decreased to zero, because it will be reset to EPOCH_DURATION if it’s zero here. The following PoC uses the twTap as the exploit case.

Proof of Concept

We describe this attack using the simplest scenario, assuming that initially, the pool contains only the attacker’s own position, but the pool.averageMagnitude is not zero (which means there were some participators before).

  1. The attacker first needs to create a positions with divergenceForce = true, and magnitude = M1 = current pool.averageMagnitude. After the participation, the pool.averageMagnitude will be pool.averageMagnitude + M1.
  2. Waiting for the unlock time and exits the above position. After the exit, pool.averageMagnitude remains unchanged (pool.averageMagnitude +M1), but the pool.totalParticipants is back to 0.
  3. The attacker repeats the above process, which results in pool.averageMagnitude increasing by M1 each time.
  4. Before the last unlock, the pool.cumulative and pool.averageMagnitude now are very large values, the attacker participates with divergenceForce = false for a couple of months.
  5. Then the attacker exits his last position with divergenceForce = true. It will make pool.cumulative = 0;, which breaks the developers’ assumptions about this branch:

https://github.com/Tapioca-DAO/tap-token/blob/20a83b1d2d5577653610a6c3879dff9df4968345/contracts/governance/twTAP.sol#L331-L336

                // TODO: Strongly suspect this is never less. Prove it.
                if (pool.cumulative > pool.averageMagnitude) {
                    pool.cumulative -= pool.averageMagnitude;
                } else {
                    pool.cumulative = 0;
                }

Now others will no longer be able to participate in twTAP, and the gov will be monopolized by the attacker.

Please note that “the pool contains only the attacker’s own position” is not a necessary condition; the attack can still be carried out even with the participation of others. As the exit process accumulates, this attack will become increasingly easier to occur, and may even be triggered unintentionally by users.

Recommendation

Update pool.averageMagnitude in the _releaseTap.

Assessed type

Context

LSDan (judge) decreased severity to Medium

0xRektora (Tapioca) confirmed, but disagreed with severity and commented via duplicate Issue #165:

Low. While true, the probabilities of this happening are very thin.

Note: For full discussion, see here.


The original report as published on Code4rena, by ronnyx2017. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Likely the exit function fails to update averageMagnitude, so it only increases and can be manipulated to reset cumulative.

Impact

Others may no longer be able to participate in twTAP, and governance could be monopolised by the attacker; the project team considers the probability very low.

What the attacker needed

Attacker has tokens to lock; some prior participants existed; attacker can wait for unlock periods

The fix

Update pool.averageMagnitude in the _releaseTap function.

How it was found

  1. Create a position with divergenceForce true and magnitude equal to current averageMagnitude.
  2. Wait for unlock time and exit the position; averageMagnitude remains unchanged but totalParticipants becomes zero.
  3. Repeat the process to increase averageMagnitude each time.
  4. Before the last unlock, participate with divergenceForce false for a couple of months.
  5. Exit the last position with divergenceForce true, which sets cumulative to zero.

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.

In this system, people lock tokens to get voting power. The contract tracks an average 'magnitude' to decide how much voting power each participant gets. Because the average is never lowered when someone leaves, an attacker can make it grow huge and then reset a counter to zero, blocking everyone else from joining.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by ronnyx2017

View all

24 more disclosed reports, best first.

More from Tapioca Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.