blockful-io / swaplace-contracts

Swaplace is an open-source, ownerless and feeless token swap protocol
https://app.swaplace.xyz/
MIT License
33 stars 33 forks source link

Smart Contract Issues #193

Closed 1nc0gn170 closed 6 months ago

1nc0gn170 commented 6 months ago

Bug Report

Hey Team!

Just had a brief look at swaplace contracts and would like to report a couple of issues!

Issues List:

    Asset[] memory assets = swap.asking;

    for (uint256 i = 0; i < assets.length; ) {
     ...
      unchecked {
        assembly {
          i := mload(0x40)
          i := add(i, 1)
          mstore(0x40, i)
        }
      }
    }

    assets = swap.biding;

    for (uint256 i = 0; i < assets.length; ) {
      ...
      unchecked {
        assembly {
          i := mload(0x40)
          i := add(i, 1)
          mstore(0x40, i)
        }
      }
    }

This is actually incorrect, it should be just this

        assembly {
          i := add(i, 1)
        }

Note: Also there is no need to use unchecked for assembly, no validation for overflows happens inside assembly block.

For any further help regarding audits please reach out to me here: https://cantina.xyz/u/1nc0gn170 Thanks

1nc0gn170 commented 6 months ago

@0xneves @alextnetto Please have a look!

0xneves commented 6 months ago

Hey @1nc0gn170 , thank you so much for your submission!!

As you enlightened us, the mload is wrong - should be mload(i) instead of mload(0x40) but not needed for the current increment since already in memory. This issue you pointed out will need to be considered as well for refactoring the entire test scope as more validation is needed to catch such errors in the future.

The unchecked should be removed from every inline assembly as proposed!

Regarding best practices for token safety, I believe it can be reinforced in the front end instead, especially because safeTransferFrom is not implemented in all types of tokens. For this first version where don't threat every case separately, we will delegate token safety to the user himself. In next versions we indeed will create function such as safeAccetSwap where we call safeTokenTransfer instead.

blackbeard002 commented 6 months ago

Hey @0xneves made a PR that closes it #195

1nc0gn170 commented 6 months ago

Thanks for the quick response! Also few minor changes can be enforced!

Thanks!

alextnetto commented 6 months ago

Hey @1nc0gn170, thanks a lot for your efforts in reviewing the code!