Skip to content
DoS Audit finding Advanced

Max magnitude lock check will lead to a DoS and possible monopolization of gov/Option on twTAP / TapiocaOptionBroker

A maximum magnitude check in the twTAP and TapiocaOptionBroker contracts can be exploited to reduce the cumulative magnitude below a threshold, causing a denial of service for new staking and allowing an attacker to monopolise governance voting.

ronnyx2017 Follow

Originally disclosed on Code4rena

Submitted by ronnyx2017

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

https://github.com/Tapioca-DAO/tap-token/blob/20a83b1d2d5577653610a6c3879dff9df4968345/contracts/options/TapiocaOptionBroker.sol#L261

Vulnerability details

There is a max magnitude lock check in the twTAP.participate function, it can’t be greater than 4x the cumulative:

    if (magnitude >= pool.cumulative * 4) revert NotValid();

Because the param _duration can’t be less than EPOCH_DURATION = 7 days = 604800, lets assume pool.cumulative is x, and solve for its minimum value without DoS:

(604800 * 604800 + x * x)**0.5 - x < 4 * x

Solution derived: the x, pool.cumulative, must be >= 123455.

The main issue is that, when a user participates the lock with divergenceForce = false, his participation will decrease pool.cumulative. Meanwhile, if there are positions previously entered with divergenceForce = true that are now exiting, then pool.cumulative will be reduced to below 123455.

Impact

TAP cannot be staked until pool.cumulative rises to 123455, and the system will remain under the DoS for even months until positions with divergenceForce = false can exit (arrive at the unlocking time). During this DoS period, the attacker has the potential to monopolize gov.

The same issue is also in the TapiocaOptionBroker. The following PoC uses the twTap as the exploit case.

Proof of Concept

Flow of attack:

  1. The user1 locked some (with hasVotingPower = true) for a duration = 1004800 (11.6 days).
  2. After 11.6 days and before the user1 unlocks, the user2 lock some TAP (with hasVotingPower = true) for a duration = 1172776 (13.5 days).
  3. The user1 unlocks his twTap position.
  4. After this unlocking, the pool.cumulative will be 77922, which is < 123455.

So the twTAP will be DoS until the user2 (attacker) unlocks, which has to wait about 13.5 days. During this DoS period, the attacker is the only voter of the gov, the attack could have complete control of gov.

def computeMagnitude(_duration, cumulative):
    return int((_duration**2 + cumulative**2)**0.5) - cumulative

def assetValid4(_magnitude, _cumulative, tag):
    # print(_magnitude, _cumulative * 4)
    assert _magnitude < _cumulative * 4, f"Not Valid here #{tag}"

def update(_duration, _averageMagnitude, _cumulative, _NO):
    print(f"======{_NO}======")
    magnitude0 = computeMagnitude(_duration, _cumulative)
    print("duration, cumulative_before", _duration, _cumulative)
    assetValid4(magnitude0, _cumulative, _NO)
    _averageMagnitude = int((_averageMagnitude + magnitude0) / _NO)
    if _duration >= _cumulative:
        print("cumulative_delta", _averageMagnitude)
        _cumulative += _averageMagnitude
    else:
        print("cumulative_delta", -_averageMagnitude)
        _cumulative -= _averageMagnitude
    print("averageMagnitude,cumulative_after", _averageMagnitude, _cumulative)
    return _averageMagnitude, _cumulative

def main():
    averageMagnitude = 0
    cumulative = 7 * 24 * 3600
    # 1
    NO = 1
    duration0 = 1004800
    averageMagnitude, cumulative = update(duration0, averageMagnitude, cumulative, NO)
    cache_c_delta0 = averageMagnitude

    # 2
    NO += 1
    duration1 = 1172776
    averageMagnitude, cumulative = update(duration1, averageMagnitude, cumulative, NO)

    # after #1 unlock
    # pool.cumulative -= #1.averageMagnitude
    print("=================")
    cumulative_after_unlock = cumulative - cache_c_delta0
    print(cumulative_after_unlock, cumulative_after_unlock <= 123454)

main()

The above py script simulates the math process of the attack, print log:

╰─ python3 cumulative.py
======1======
duration, cumulative_before 1004800 604800
cumulative_delta 567977
averageMagnitude,cumulative_after 567977 1172777
======2======
duration, cumulative_before 1172776 1172777
cumulative_delta -526878
averageMagnitude,cumulative_after 526878 645899
=================
77922 True

Recommendation

Set the minimum threshold for the check, such as 7 days; if the duration meets EPOCH_DURATION <= duration <= minimum threshold || magnitude < pool.cumulative * 4, then lock-in is permitted.

Assessed type

Context

0xWeiss (Tapioca) acknowledged

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 maximum magnitude check compares against a cumulative value that can be reduced by legitimate unlocks, so an attacker can manipulate the cumulative to make the check fail for new participants.

Impact

TAP cannot be staked until pool.cumulative rises to 123455, and during this DoS the attacker could monopolise governance voting.

What the attacker needed

Attacker can lock tokens with voting power; attacker can time locks and unlocks; victim locks tokens before attacker

The fix

Set a minimum threshold for the check, such as 7 days, so that locks with duration between EPOCH_DURATION and the threshold are permitted even if magnitude exceeds the limit.

How it was found

  1. User1 locks tokens with voting power for about 11.6 days.
  2. After 11.6 days and before user1 unlocks, user2 locks tokens with voting power for about 13.5 days.
  3. User1 unlocks their twTap position.
  4. After this unlocking, pool.cumulative becomes 77922, which is below 123455, causing a DoS.

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.

These smart contracts let people lock tokens to get voting power. A check meant to limit how much voting power one lock can have can be abused: by carefully timing locks and unlocks, an attacker can make a shared number drop so low that no one else can lock tokens, leaving the attacker as the only voter.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.