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

Smart Contract Issues #193

Closed
1nc0gn170 opened this issue Feb 8, 2024 · 5 comments · Fixed by #195
Closed

Smart Contract Issues #193

1nc0gn170 opened this issue Feb 8, 2024 · 5 comments · Fixed by #195
Assignees
Labels
bug Something isn't working

Comments

@1nc0gn170
Copy link

1nc0gn170 commented Feb 8, 2024

Bug Report

Hey Team!

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

Issues List:

  • Incorrect loop counter implementation using assembly
    In the function Swaplace.acceptSwap(), while iterating over assets, counter is updated incorrectly;
Screenshot 2024-02-09 at 4 00 41 AM
    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.

  • Using transferFrom instead of safeTransferFrom
    • ERC721
      There are certain smart contracts that do not support ERC721, using transferFrom() may result in the NFT being sent to such contracts and get stuck. Also note that using safeTransferFrom can introduce reentrancy issues use reentrancy guard.
    • ERC20
      As there is no validation for asset addresses, a malicious user might forge a token contract which will not abort the execution on token failure, instead it just fails silently. This will help the attacker gain other persons tokens without even swaping. Use safeTransferFrom also maintain a token whitelist.

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

@1nc0gn170 1nc0gn170 added the bug Something isn't working label Feb 8, 2024
@1nc0gn170 1nc0gn170 changed the title fix: Smart Contract Issues! Smart Contract Issues Feb 8, 2024
@1nc0gn170
Copy link
Author

@0xneves @alextnetto Please have a look!

@0xneves
Copy link
Contributor

0xneves commented Feb 9, 2024

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.

@0xneves 0xneves added this to Swaplace Feb 9, 2024
@0xneves 0xneves moved this to 🔖 TODO in Swaplace Feb 9, 2024
blackbeard002 added a commit to blackbeard002/swaplace-contracts that referenced this issue Feb 9, 2024
@blackbeard002
Copy link
Contributor

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

@1nc0gn170
Copy link
Author

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

  • Currently an user can create expired swap or swap with 0 assets, by directly passing incorrect values to the function createSwap. Even though the checks were enforced in makeSwap function, user can bypass them by calling createSwap function.
  • Avoid initialisation with 0 to save gas.
    for (uint256 i = 0; i < assets.length; ) { => for (uint256 i; i < assets.length; ) {

Thanks!

@alextnetto
Copy link
Member

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

0xneves added a commit that referenced this issue Feb 9, 2024
@heronlancellot heronlancellot moved this from 🔖 TODO to ✅ Done in Swaplace Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

4 participants