<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://0xvolodya.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 23:37:22 GMT</lastBuildDate><atom:link href="https://0xvolodya.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Lowest-paying findings on Code4rena,  Sherlock]]></title><description><![CDATA[I have listed vulnerabilities that beginners should strive to find, while advanced auditors might not want to spend a lot of time reporting them. To…]]></description><link>https://0xvolodya.hashnode.dev/lowest-paying-findings-on-code4rena-sherlock</link><guid isPermaLink="true">https://0xvolodya.hashnode.dev/lowest-paying-findings-on-code4rena-sherlock</guid><category><![CDATA[Web3]]></category><category><![CDATA[Web3 Security]]></category><dc:creator><![CDATA[Volodya]]></dc:creator><pubDate>Mon, 26 Jun 2023 02:29:44 GMT</pubDate><content:encoded><![CDATA[<p>I have listed vulnerabilities that beginners should strive to find, while advanced auditors might not want to spend a lot of time reporting them. To incentivize users, <a target="_blank" href="https://code4rena.com/">Code4rena</a> and <a target="_blank" href="https://sherlock.xyz/">Sherlock</a> use a unique scoring system with the primary goal to reward contestants for finding unique bugs. The award will be shared among those who submitted. The fewer people who find a bug, the more money contestants will be paid. I've used <a target="_blank" href="https://solodit.xyz/auth">solodit</a> to organize finding with a lot of finders</p>
<h3 id="heading-use-safetransfer-safetransferfrom-instead-of-transfer-transferfrom-when-transferring">Use <code>safeTransfer, safeTransferFrom</code> instead of <code>transfer, transferFrom</code> when transferring</h3>
<p>Unsafe transfer does guarantee the transfer was fully complete only when it is implemented to fail whenever transfers can't be processed fully. Not all ERC20 implementations behave that way, it isn't required.</p>
<p>Due to that while having unsafe transfer for protocol controlled tokens produces no issues, using it for any external controlled token poses a vulnerability as long as this token implementation do not always fail for unsuccessful transfer, returning false instead, or a token can be upgraded to start behaving this way.</p>
<p>For example, as USDC is an upgradable contract, there is no guarantees that the next implementation will be failing instead of only returning bool success value.</p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/m-1-erc20-return-values-not-checked-sherlock-derby-derby-git">contest with this issue</a>, number of finders - 12</p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">xTransferToController</span>(<span class="hljs-params">
    <span class="hljs-keyword">uint256</span> _vaultNumber,
    <span class="hljs-keyword">uint256</span> _amount,
    <span class="hljs-keyword">address</span> _asset,
    <span class="hljs-keyword">uint256</span> _slippage,
    <span class="hljs-keyword">uint256</span> _relayerFee
  </span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> <span class="hljs-title">onlyVaults</span> </span>{
    <span class="hljs-keyword">if</span> (homeChain <span class="hljs-operator">=</span><span class="hljs-operator">=</span> xControllerChain) {
      IERC20(_asset).transferFrom(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, xController, _amount);
      IXChainController(xController).upFundsReceived(_vaultNumber);
    } <span class="hljs-keyword">else</span> {
      xTransfer(_asset, _amount, xController, xControllerChain, _slippage, _relayerFee);
      pushFeedbackToXController(_vaultNumber, _relayerFee);
    }
  }
</code></pre>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/m-5-call-should-be-used-instead-of-transfer-on-an-address-payable-sherlock-dodo-dodo-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-05-use-of-send-may-revert-if-the-recipients-fallback-function-consumes-more-than-2300-gas-code4rena-forgotten-runes-forgotten-runes-warrior-guild-contest-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/m-2-unsafe-erc20-methods-sherlock-telcoin-telcoin-git">3</a> <a target="_blank" href="https://solodit.xyz/auth#">4</a> <a target="_blank" href="https://solodit.xyz/issues/m-1-use-safetransferfrom-instead-of-transferfrom-for-outgoing-erc721-transfers-sherlock-harpie-harpie-git">5</a> <a target="_blank" href="https://solodit.xyz/issues/m-02-use-safetransferfrom-instead-of-transferfrom-for-erc721-code4rena-golom-golom-contest-git">6</a></p>
<h3 id="heading-use-call-instead-of-transfer-when-transferring-eth">Use <code>call()</code> instead of <code>transfer()</code> when transferring ETH</h3>
<p>The <code>transfer()</code> and <code>send()</code> functions forward a fixed amount of 2300 gas. Historically, it has often been recommended to use these functions for value transfers to guard against reentrancy attacks. However, the gas cost of EVM instructions may change significantly during hard forks which may break already deployed contract systems that make fixed assumptions about gas costs. Also, whenever these is a reentrancy should be checked as well</p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/m-4-fillquote-uses-transfer-instead-of-call-which-can-break-with-future-updates-to-gas-costs-sherlock-sense-sense-update-1-git">contest with this issue</a>, number of finders - 8</p>
<pre><code class="lang-solidity">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_fillQuote</span>(<span class="hljs-params">SwapQuote <span class="hljs-keyword">calldata</span> quote</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span> boughtAmount</span>) </span>{
        <span class="hljs-keyword">if</span> (quote.sellToken <span class="hljs-operator">=</span><span class="hljs-operator">=</span> quote.buyToken) <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-comment">// No swap if the tokens are the same.</span>
        <span class="hljs-keyword">if</span> (quote.swapTarget <span class="hljs-operator">!</span><span class="hljs-operator">=</span> exchangeProxy) <span class="hljs-keyword">revert</span> Errors.InvalidExchangeProxy();

        <span class="hljs-comment">// Give `spender` an infinite allowance to spend this contract's `sellToken`.</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">address</span>(quote.sellToken) <span class="hljs-operator">!</span><span class="hljs-operator">=</span> ETH)
            ERC20(<span class="hljs-keyword">address</span>(quote.sellToken)).safeApprove(quote.spender, <span class="hljs-keyword">type</span>(<span class="hljs-keyword">uint256</span>).<span class="hljs-built_in">max</span>);

        <span class="hljs-keyword">uint256</span> sellAmount <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(quote.sellToken) <span class="hljs-operator">=</span><span class="hljs-operator">=</span> ETH
            ? <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>
            : quote.sellToken.balanceOf(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>));

        <span class="hljs-comment">// Call the encoded swap function call on the contract at `swapTarget`,</span>
        <span class="hljs-comment">// passing along any ETH attached to this function call to cover protocol fees.</span>
        (<span class="hljs-keyword">bool</span> success, <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> res) <span class="hljs-operator">=</span> quote.swapTarget.<span class="hljs-built_in">call</span>{ <span class="hljs-built_in">value</span>: <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">value</span> }(quote.swapCallData);
        <span class="hljs-comment">// if (!success) revert(_getRevertMsg(res));</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-operator">!</span>success) <span class="hljs-keyword">revert</span> Errors.ZeroExSwapFailed(res);

        <span class="hljs-comment">// We assume the Periphery does not hold tokens so boughtAmount is always it's balance</span>
        boughtAmount <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(quote.buyToken) <span class="hljs-operator">=</span><span class="hljs-operator">=</span> ETH ? <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span> : quote.buyToken.balanceOf(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>));
        sellAmount <span class="hljs-operator">=</span>
            sellAmount <span class="hljs-operator">-</span>
            (<span class="hljs-keyword">address</span>(quote.sellToken) <span class="hljs-operator">=</span><span class="hljs-operator">=</span> ETH ? <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span> : quote.sellToken.balanceOf(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>)));
        <span class="hljs-keyword">if</span> (boughtAmount <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span> <span class="hljs-operator">|</span><span class="hljs-operator">|</span> sellAmount <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) <span class="hljs-keyword">revert</span> Errors.ZeroSwapAmt();

        <span class="hljs-comment">// Refund any unspent protocol fees (paid in ether) to the sender.</span>
        <span class="hljs-keyword">uint256</span> refundAmt <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>;
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">address</span>(quote.buyToken) <span class="hljs-operator">=</span><span class="hljs-operator">=</span> ETH) refundAmt <span class="hljs-operator">=</span> refundAmt <span class="hljs-operator">-</span> boughtAmount;
        <span class="hljs-keyword">payable</span>(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>).<span class="hljs-built_in">transfer</span>(refundAmt);
        <span class="hljs-keyword">emit</span> BoughtTokens(<span class="hljs-keyword">address</span>(quote.sellToken), <span class="hljs-keyword">address</span>(quote.buyToken), sellAmount, boughtAmount);
    }
</code></pre>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/m-5-call-should-be-used-instead-of-transfer-on-an-address-payable-sherlock-dodo-dodo-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-05-use-of-send-may-revert-if-the-recipients-fallback-function-consumes-more-than-2300-gas-code4rena-forgotten-runes-forgotten-runes-warrior-guild-contest-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/m-02-use-of-payabletransfer-might-render-eth-impossible-to-withdraw-code4rena-escher-escher-contest-git">3</a> <a target="_blank" href="https://solodit.xyz/issues/m-10-addresscallvaluex-should-be-used-instead-of-payabletransfer-code4rena-debt-dao-debt-dao-contest-git">4</a> <a target="_blank" href="https://solodit.xyz/issues/m-4-usage-of-deprecated-transfer-can-result-in-revert-sherlock-harpie-harpie-git">5</a></p>
<h3 id="heading-first-depositor-issue">First depositor issue</h3>
<p>A classic issue with vaults. First depositor can deposit a single wei and then donate to the vault to greatly inflate the share ratio. Due to truncation when converting to shares this can be used to steal funds from later depositors.</p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/h-03-first-depositor-can-break-minting-of-shares-code4rena-caviar-caviar-contest-git">contest with this issue</a>, number of finders - 41</p>
<pre><code class="lang-solidity"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addQuote</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> baseTokenAmount, <span class="hljs-keyword">uint256</span> fractionalTokenAmount</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
    <span class="hljs-keyword">uint256</span> lpTokenSupply <span class="hljs-operator">=</span> lpToken.totalSupply();
    <span class="hljs-keyword">if</span> (lpTokenSupply <span class="hljs-operator">&gt;</span> <span class="hljs-number">0</span>) {
        <span class="hljs-comment">// calculate amount of lp tokens as a fraction of existing reserves</span>
        <span class="hljs-keyword">uint256</span> baseTokenShare <span class="hljs-operator">=</span> (baseTokenAmount <span class="hljs-operator">*</span> lpTokenSupply) <span class="hljs-operator">/</span> baseTokenReserves();
        <span class="hljs-keyword">uint256</span> fractionalTokenShare <span class="hljs-operator">=</span> (fractionalTokenAmount <span class="hljs-operator">*</span> lpTokenSupply) <span class="hljs-operator">/</span> fractionalTokenReserves();
        <span class="hljs-keyword">return</span> Math.<span class="hljs-built_in">min</span>(baseTokenShare, fractionalTokenShare);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// if there is no liquidity then init</span>
        <span class="hljs-keyword">return</span> Math.sqrt(baseTokenAmount <span class="hljs-operator">*</span> fractionalTokenAmount);
    }
}
</code></pre>
<p>An attacker can exploit using these steps</p>
<ol>
<li><p>Create and add <code>1 wei baseToken - 1 wei quoteToken</code> to the pair. At this moment, attacker is minted <code>1 wei LP token</code> because <code>sqrt(1 * 1) = 1</code></p>
</li>
<li><p>Transfer large amount of <code>baseToken</code> and <code>quoteToken</code> directly to the pair, such as <code>1e9 baseToken - 1e9 quoteToken</code>. Since no new LP token is minted, <code>1 wei LP token</code> worths <code>1e9 baseToken - 1e9 quoteToken</code>.</p>
</li>
<li><p>Normal users add liquidity to pool will receive <code>0</code> LP token if they add less than <code>1e9</code> token because of rounding division.</p>
</li>
</ol>
<p>Recommended Mitigation Steps</p>
<ol>
<li><p><a target="_blank" href="https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol#L119-L124"><strong>Uniswap V2 solved this problem by sending the first 1000 LP tokens to the zero address</strong></a>. The same can be done in this case i.e. when <code>lpTokenSupply == 0</code>, send the first min liquidity LP tokens to the zero address to enable share dilution.</p>
</li>
<li><p>In <code>add()</code>, ensure the number of LP tokens to be minted is non-zero:</p>
</li>
</ol>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/h-05-underlying-assets-stealing-in-autopxgmx-and-autopxglp-via-share-price-manipulation-code4rena-redacted-cartel-redacted-cartel-contest-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/h-1-attacker-can-manipulate-the-pricepershare-to-profit-from-future-users-deposits-sherlock-mycelium-mycelium-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/first-vault-deposit-can-cause-excessive-rounding-spearbit-astaria-pdf">3</a> <a target="_blank" href="https://solodit.xyz/issues/first-pool-depositor-can-be-front-run-and-have-part-of-their-deposit-stolen-spearbit-maple-finance-pdf">4</a> <a target="_blank" href="https://solodit.xyz/issues/h-02-first-depositor-can-break-minting-of-shares-code4rena-prepo-prepo-contest-git">5</a></p>
<h3 id="heading-silent-overflow"><strong>Silent overflow</strong></h3>
<p>Whenever there is a downcast in the codebase there might be some problems. Solidity will just return result like this <code>uint8(1000000) = 64</code> Protocols usually use <a target="_blank" href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol">openzeppelin SafeCast library</a> where the same function looks like this</p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toUint8</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> value</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint8</span></span>) </span>{
        <span class="hljs-keyword">if</span> (value <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">type</span>(<span class="hljs-keyword">uint8</span>).<span class="hljs-built_in">max</span>) {
            <span class="hljs-keyword">revert</span> SafeCastOverflowedUintDowncast(<span class="hljs-number">8</span>, value);
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">uint8</span>(value);
    }
</code></pre>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/16245">contest with this issue</a>, number of finders - 24</p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">buy</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span>[] <span class="hljs-keyword">calldata</span> tokenIds, <span class="hljs-keyword">uint256</span>[] <span class="hljs-keyword">calldata</span> tokenWeights, MerkleMultiProof <span class="hljs-keyword">calldata</span> proof</span>) 
        <span class="hljs-title"><span class="hljs-keyword">public</span></span>
        <span class="hljs-title"><span class="hljs-keyword">payable</span></span>
        <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span> netInputAmount, <span class="hljs-keyword">uint256</span> feeAmount, <span class="hljs-keyword">uint256</span> protocolFeeAmount</span>)
    </span>{
        <span class="hljs-comment">// ~~~ Checks ~~~ //</span>

        <span class="hljs-comment">// calculate the sum of weights of the NFTs to buy</span>
        <span class="hljs-keyword">uint256</span> weightSum <span class="hljs-operator">=</span> sumWeightsAndValidateProof(tokenIds, tokenWeights, proof);

        <span class="hljs-comment">// calculate the required net input amount and fee amount</span>
        (netInputAmount, feeAmount, protocolFeeAmount) <span class="hljs-operator">=</span> buyQuote(weightSum);
        ...
        <span class="hljs-comment">// update the virtual reserves</span>
        virtualBaseTokenReserves <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-keyword">uint128</span>(netInputAmount <span class="hljs-operator">-</span> feeAmount <span class="hljs-operator">-</span> protocolFeeAmount); 
        virtualNftReserves <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-keyword">uint128</span>(weightSum);
        ...
</code></pre>
<p>the underflow would cause the invariants of the protocol to be broken, causing it to behave in undefined ways, most likely allowing discount tokens.</p>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/2649">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-08-unsafe-downcasting-operation-truncate-users-input-code4rena-escher-escher-contest-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/m-21-truncation-in-casting-can-lead-to-a-founder-receiving-all-the-base-tokens-code4rena-nouns-builder-nouns-builder-contest-git">3</a></p>
<h3 id="heading-did-not-approve-to-zero-first">Did not Approve to zero first</h3>
<p>Some ERC20 tokens like USDT require resetting the approval to 0 first before being able to reset it to another value.</p>
<p><a target="_blank" href="https://github.com/d-xo/weird-erc20#approval-race-protections">https://github.com/d-xo/weird-erc20#approval-race-protections</a></p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/m-3-did-not-approve-to-zero-first-sherlock-derby-derby-git">contest with this issue</a>, number of finders - 10</p>
<pre><code class="lang-diff">  function xTransfer(
    address _token,
    uint256 _amount,
    address _recipient,
    uint32 _destinationDomain,
    uint256 _slippage,
    uint256 _relayerFee
  ) internal {
    require(
      IERC20(_token).allowance(msg.sender, address(this)) &gt;= _amount,
      "User must approve amount"
    );

    // User sends funds to this contract
    IERC20(_token).transferFrom(msg.sender, address(this), _amount);

    // This contract approves transfer to Connext
<span class="hljs-addition">+   IERC20(_token).approve(address(connext), 0);</span>
    IERC20(_token).approve(address(connext), _amount);

    IERC20(_token).approve(address(connext), _amount);

    IConnext(connext).xcall{value: (msg.value - _relayerFee)}(
      _destinationDomain, // _destination: Domain ID of the destination chain
      _recipient, // _to: address receiving the funds on the destination
      _token, // _asset: address of the token contract
      msg.sender, // _delegate: address that can revert or forceLocal on destination
      _amount, // _amount: amount of tokens to transfer
      _slippage, // _slippage: the maximum amount of slippage the user will accept in BPS (e.g. 30 = 0.3%)
      bytes("") // _callData: empty bytes because we're only sending funds
    );
  }
</code></pre>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/m-17-did-not-approve-to-zero-first-sherlock-notional-notional-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-03-lack-of-safeapprove0-prevents-some-registrations-and-the-changing-of-stakers-and-lp-tokens-code4rena-backd-backd-contest-git">2</a></p>
<h3 id="heading-chainlinks-stale-or-incorrect-price"><strong>Chainlink's stale or incorrect price</strong></h3>
<p>Almost in all contests, this issue is being reported by many wardens. Whenever there is a request to Chainlink Oracle via <code>latestRoundData</code> there are supposed to be checks in a place like <a target="_blank" href="https://docs.chain.link/data-feeds/historical-data">the docs say</a></p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/m-12-chainlinks-latestrounddata-return-stale-or-incorrect-result-sherlock-blueberry-blueberry-git#">contest with this issue</a>, number of finders - 16</p>
<pre><code class="lang-solidity">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPrice</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _token</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">override</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
        <span class="hljs-comment">// remap token if possible</span>
        <span class="hljs-keyword">address</span> token <span class="hljs-operator">=</span> remappedTokens[_token];
        <span class="hljs-keyword">if</span> (token <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0</span>)) token <span class="hljs-operator">=</span> _token;

        <span class="hljs-keyword">uint256</span> maxDelayTime <span class="hljs-operator">=</span> maxDelayTimes[token];
        <span class="hljs-keyword">if</span> (maxDelayTime <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) <span class="hljs-keyword">revert</span> NO_MAX_DELAY(_token);

        <span class="hljs-comment">// try to get token-USD price</span>
        <span class="hljs-keyword">uint256</span> decimals <span class="hljs-operator">=</span> registry.decimals(token, USD);
        (, <span class="hljs-keyword">int256</span> answer, , <span class="hljs-keyword">uint256</span> updatedAt, ) <span class="hljs-operator">=</span> registry.latestRoundData(
            token,
            USD
        );
        <span class="hljs-keyword">if</span> (updatedAt <span class="hljs-operator">&lt;</span> <span class="hljs-built_in">block</span>.<span class="hljs-built_in">timestamp</span> <span class="hljs-operator">-</span> maxDelayTime)
            <span class="hljs-keyword">revert</span> PRICE_OUTDATED(_token);

        <span class="hljs-keyword">return</span> (answer.toUint256() <span class="hljs-operator">*</span> <span class="hljs-number">1e18</span>) <span class="hljs-operator">/</span> <span class="hljs-number">10</span><span class="hljs-operator">*</span><span class="hljs-operator">*</span>decimals;
    }
</code></pre>
<p>Here is how it suppose to be implemented</p>
<pre><code class="lang-solidity"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPrice</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _token</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">override</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
        <span class="hljs-comment">// remap token if possible</span>
        <span class="hljs-keyword">address</span> token <span class="hljs-operator">=</span> remappedTokens[_token];
        <span class="hljs-keyword">if</span> (token <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0</span>)) token <span class="hljs-operator">=</span> _token;

        <span class="hljs-keyword">uint256</span> maxDelayTime <span class="hljs-operator">=</span> maxDelayTimes[token];
        <span class="hljs-keyword">if</span> (maxDelayTime <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) <span class="hljs-keyword">revert</span> NO_MAX_DELAY(_token);

        <span class="hljs-comment">// try to get token-USD price</span>
        <span class="hljs-keyword">uint256</span> decimals <span class="hljs-operator">=</span> registry.decimals(token, USD);
        (<span class="hljs-keyword">uint80</span> roundID, <span class="hljs-keyword">int256</span> answer, <span class="hljs-keyword">uint256</span> timestamp, <span class="hljs-keyword">uint256</span> updatedAt, ) <span class="hljs-operator">=</span> registry.latestRoundData(
            token,
            USD
        );
        <span class="hljs-comment">//Solution</span>
        <span class="hljs-built_in">require</span>(updatedAt <span class="hljs-operator">&gt;</span><span class="hljs-operator">=</span> roundID, <span class="hljs-string">"Stale price"</span>);
        <span class="hljs-built_in">require</span>(timestamp <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>,<span class="hljs-string">"Round not complete"</span>);
        <span class="hljs-built_in">require</span>(answer <span class="hljs-operator">&gt;</span> <span class="hljs-number">0</span>,<span class="hljs-string">"Chainlink answer reporting 0"</span>);

        <span class="hljs-keyword">if</span> (updatedAt <span class="hljs-operator">&lt;</span> <span class="hljs-built_in">block</span>.<span class="hljs-built_in">timestamp</span> <span class="hljs-operator">-</span> maxDelayTime)
            <span class="hljs-keyword">revert</span> PRICE_OUTDATED(_token);

        <span class="hljs-keyword">return</span> (answer.toUint256() <span class="hljs-operator">*</span> <span class="hljs-number">1e18</span>) <span class="hljs-operator">/</span> <span class="hljs-number">10</span><span class="hljs-operator">*</span><span class="hljs-operator">*</span>decimals;
    }
</code></pre>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/m-5-call-should-be-used-instead-of-transfer-on-an-address-payable-sherlock-dodo-dodo-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-05-use-of-send-may-revert-if-the-recipients-fallback-function-consumes-more-than-2300-gas-code4rena-forgotten-runes-forgotten-runes-warrior-guild-contest-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/m-24-chainlink-price-feed-is-not-sufficiently-validated-and-can-return-stale-price-code4rena-tigris-trade-tigris-trade-contest-git">3</a> <a target="_blank" href="https://solodit.xyz/issues/m-12-during-oracle-outages-or-feeder-outagesdisagreement-the-paraspacefallbackoracle-is-not-used-code4rena-paraspace-paraspace-contest-git">4</a> <a target="_blank" href="https://solodit.xyz/issues/m-17-chainlink-oracle-data-feed-is-not-sufficiently-validated-and-can-return-stale-price-code4rena-inverse-finance-inverse-finance-contest-git">5</a> <a target="_blank" href="https://solodit.xyz/issues/m-2-chainlinks-latestrounddata-might-return-stale-or-incorrect-results-sherlock-knox-knox-finance-git">6</a> <a target="_blank" href="https://solodit.xyz/issues/m-1-lack-of-price-freshness-check-in-chainlinkoraclesolgetprice-allows-a-stale-price-to-be-used-sherlock-sentiment-sentiment-git">7</a> <a target="_blank" href="https://solodit.xyz/issues/m-13-different-oracle-issues-can-return-outdated-prices-code4rena-y2k-finance-y2k-finance-contest-git">8</a> <a target="_blank" href="https://solodit.xyz/issues/m-24-naz-m1-chainlinks-latestrounddata-might-return-stale-results-code4rena-olympus-dao-olympus-dao-contest-git">9</a></p>
<h3 id="heading-the-protocol-doesnt-work-correctly-with-decimals-other-than-the-default">The protocol doesn't work correctly with decimals other than the default</h3>
<p>In the code, there can be different assumptions about the token's decimals in the code and the documentation. E.x. protocol states that it supports some tokens but using those tokens in the code will result in incorrect behavior.</p>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/m-04-changefeequote-will-fail-for-low-decimal-erc20-tokens-code4rena-caviar-caviar-private-pools-git">contest with this issue</a>, number of finders - 24</p>
<pre><code class="lang-solidity">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">changeFeeQuote</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> inputAmount</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span> feeAmount, <span class="hljs-keyword">uint256</span> protocolFeeAmount</span>) </span>{
        <span class="hljs-comment">// multiply the changeFee to get the fee per NFT (4 decimals of accuracy)</span>
        <span class="hljs-keyword">uint256</span> exponent <span class="hljs-operator">=</span> baseToken <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0</span>) ? <span class="hljs-number">18</span> <span class="hljs-operator">-</span> <span class="hljs-number">4</span> : ERC20(baseToken).decimals() <span class="hljs-operator">-</span> <span class="hljs-number">4</span>; <span class="hljs-comment">// underflow - revert</span>
        <span class="hljs-keyword">uint256</span> feePerNft <span class="hljs-operator">=</span> changeFee <span class="hljs-operator">*</span> <span class="hljs-number">10</span> <span class="hljs-operator">*</span><span class="hljs-operator">*</span> exponent;

        feeAmount <span class="hljs-operator">=</span> inputAmount <span class="hljs-operator">*</span> feePerNft <span class="hljs-operator">/</span> <span class="hljs-number">1e18</span>;
        protocolFeeAmount <span class="hljs-operator">=</span> feeAmount <span class="hljs-operator">*</span> Factory(factory).protocolFeeRate() <span class="hljs-operator">/</span> <span class="hljs-number">10_000</span>;
    }
</code></pre>
<p>in case the <code>baseToken</code> is an ERC20, then the exponent is calculated as <code>ERC20(baseToken).decimals() - 4</code>. The main issue here is that if the token decimals are less than 4, then the subtraction will cause an underflow due to Solidity's default checked math, causing the whole transaction to be reverted.</p>
<p>Such tokens with low decimals exist, one major example is <a target="_blank" href="https://etherscan.io/token/0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd"><strong>GUSD</strong></a>, the Gemini dollar, which has only two decimals. If any of these tokens is used as the base token of a pool, then any call to the <code>change</code> will be reverted, as the scaling of the charge fee will result in an underflow.</p>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/h-1-protocol-assumes-18-decimals-collateral-sherlock-taurus-taurus-git">1</a> <a target="_blank" href="https://solodit.xyz/issues/m-02-price-will-not-always-be-18-decimals-as-expected-and-outlined-in-the-comments-code4rena-caviar-caviar-contest-git">2</a> <a target="_blank" href="https://solodit.xyz/issues/m-19-_handledeposit-and-_handlewithdraw-do-not-account-for-tokens-with-decimals-higher-than-18-code4rena-tigris-trade-tigris-trade-contest-git">3</a> <a target="_blank" href="https://solodit.xyz/issues/m-15-oracle-assumes-token-and-feed-decimals-will-be-limited-to-18-decimals-code4rena-inverse-finance-inverse-finance-contest-git">4</a> <a target="_blank" href="https://solodit.xyz/issues/h-01-incorrect-handling-of-pricefeeddecimals-code4rena-y2k-finance-y2k-finance-contest-git">5</a></p>
<h3 id="heading-about-0xvolodya">About <strong>0xVolodya</strong></h3>
<p>0xVolodya is an independent smart contract security researcher. Ranked #2 on the 60-day leaderboard and ranked #2 on the 90-day leaderboard at <a target="_blank" href="https://code4rena.com/">code4rena</a>. Currently available for projects. dm me on Twitter <a target="_blank" href="https://twitter.com/0xVolodya">0xVolodya</a>.</p>
]]></content:encoded></item><item><title><![CDATA[NFT attacks]]></title><description><![CDATA[In this article, I have organized the common NFT vulnerabilities that I have identified during previous audits on Solodit. It may not cover all possible…]]></description><link>https://0xvolodya.hashnode.dev/nft-attacks</link><guid isPermaLink="true">https://0xvolodya.hashnode.dev/nft-attacks</guid><category><![CDATA[Web3]]></category><category><![CDATA[Web3 Security]]></category><dc:creator><![CDATA[Volodya]]></dc:creator><pubDate>Fri, 16 Jun 2023 10:12:00 GMT</pubDate><content:encoded><![CDATA[<p>In this article, I have organized the common NFT vulnerabilities that I have identified during previous audits on Solodit. It may not cover all possible vulnerabilities, but I have made my best effort to include as many as possible.</p>
<h3 id="heading-use-safe-functions-instead-of-mint-transferfrom-for-erc721">Use safe functions instead of mint, transferFrom for ERC721</h3>
<p>There are certain smart contracts that do not support ERC721, using <code>transferFrom()</code> , <code>mint()</code> may result in the NFT being sent to such contracts. <a target="_blank" href="https://eips.ethereum.org/EIPS/eip-721">As per the documentation of EIP-721</a>:</p>
<blockquote>
<p>A wallet/broker/auction application MUST implement the wallet interface if it will accept safe transfers.</p>
</blockquote>
<p>Here is a code example from a <a target="_blank" href="https://solodit.xyz/issues/3593">contest with this issue</a><br /><code>mint(msg.sender, tokenId);</code></p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_stakeToken</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _tokenId, <span class="hljs-keyword">uint</span> _unlockTime</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">if</span> (_unlockTime <span class="hljs-operator">&gt;</span> <span class="hljs-number">0</span>) {
      unlockTime[_tokenId] <span class="hljs-operator">=</span> _unlockTime;
      <span class="hljs-keyword">uint</span> fullStakedTimeBonus <span class="hljs-operator">=</span> ((_unlockTime <span class="hljs-operator">-</span> <span class="hljs-built_in">block</span>.<span class="hljs-built_in">timestamp</span>) <span class="hljs-operator">*</span> stakingSettings.maxStakeBonusAmount) <span class="hljs-operator">/</span> stakingSettings.maxStakeBonusTime;
      stakedTimeBonus[_tokenId] <span class="hljs-operator">=</span> _tokenId <span class="hljs-operator">&lt;</span> <span class="hljs-number">10000</span> ? fullStakedTimeBonus : fullStakedTimeBonus <span class="hljs-operator">/</span> <span class="hljs-number">2</span>;
    }

    <span class="hljs-comment">// Transfer the underlying token from the owner to this contract</span>
    IERC721 collection;
    <span class="hljs-keyword">if</span> (_tokenId <span class="hljs-operator">&lt;</span> <span class="hljs-number">10000</span>) {
      collection <span class="hljs-operator">=</span> frankenpunks;
      stakedFrankenPunks<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    } <span class="hljs-keyword">else</span> {
      collection <span class="hljs-operator">=</span> frankenmonsters;
      stakedFrankenMonsters<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }

    <span class="hljs-keyword">address</span> owner <span class="hljs-operator">=</span> collection.ownerOf(_tokenId);
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span> <span class="hljs-operator">!</span><span class="hljs-operator">=</span> owner <span class="hljs-operator">&amp;</span><span class="hljs-operator">&amp;</span> <span class="hljs-operator">!</span>collection.isApprovedForAll(owner, <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>) <span class="hljs-operator">&amp;</span><span class="hljs-operator">&amp;</span> <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span> <span class="hljs-operator">!</span><span class="hljs-operator">=</span> collection.getApproved(_tokenId)) <span class="hljs-keyword">revert</span> NotAuthorized();
    collection.transferFrom(owner, <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>), _tokenId);

    <span class="hljs-comment">// Mint the staker a new ERC721 token representing their staked token</span>
    _mint(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, _tokenId);

    <span class="hljs-comment">// Return the voting power for this token based on staked time bonus and evil score</span>
    <span class="hljs-keyword">return</span> getTokenVotingPower(_tokenId);
  }
</code></pre>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/8739">1</a> <a target="_blank" href="https://solodit.xyz/issues/3595">2</a> <a target="_blank" href="https://solodit.xyz/issues/3593">3</a> <a target="_blank" href="https://solodit.xyz/issues/2943">4</a> <a target="_blank" href="https://solodit.xyz/issues/6339">5</a> <a target="_blank" href="https://solodit.xyz/issues/3373">6</a> <a target="_blank" href="https://solodit.xyz/issues/2298">7</a> <a target="_blank" href="https://solodit.xyz/issues/1858">8</a> <a target="_blank" href="https://solodit.xyz/issues/1862">9</a></p>
<h3 id="heading-onerc721received-reenterency">onERC721Received reenterency</h3>
<p>Whenever you see that nft is transfering, check if that code is following checks-effects pattern or had reentrency guard otherwise project might be vulnrabale to reentrency exploit due to NFT <a target="_blank" href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721Receiver.sol#L21">onERC721Received hook</a>. Let's examine code from a <a target="_blank" href="https://solodit.xyz/issues/6203">recent issue</a></p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mint</span>(<span class="hljs-params">
        MintTrade <span class="hljs-keyword">memory</span> _mintTrade
    </span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title">onlyMinter</span> </span>{
        <span class="hljs-keyword">uint</span> newTokenID <span class="hljs-operator">=</span> _tokenIds.current();

        Trade <span class="hljs-keyword">storage</span> newTrade <span class="hljs-operator">=</span> _trades[newTokenID];
        newTrade.margin <span class="hljs-operator">=</span> _mintTrade.margin;
        newTrade.leverage <span class="hljs-operator">=</span> _mintTrade.leverage;
        newTrade.asset <span class="hljs-operator">=</span> _mintTrade.asset;
        newTrade.direction <span class="hljs-operator">=</span> _mintTrade.direction;
        newTrade.price <span class="hljs-operator">=</span> _mintTrade.price;
        newTrade.tpPrice <span class="hljs-operator">=</span> _mintTrade.tp;
        newTrade.slPrice <span class="hljs-operator">=</span> _mintTrade.sl;
        newTrade.orderType <span class="hljs-operator">=</span> _mintTrade.orderType;
        newTrade.id <span class="hljs-operator">=</span> newTokenID;
        newTrade.tigAsset <span class="hljs-operator">=</span> _mintTrade.tigAsset;

        _safeMint(_mintTrade.account, newTokenID);   <span class="hljs-comment">// make external call because of safeMint() usage</span>
        <span class="hljs-keyword">if</span> (_mintTrade.orderType <span class="hljs-operator">&gt;</span> <span class="hljs-number">0</span>) { <span class="hljs-comment">// update the values of some storage functions</span>
            _limitOrders[_mintTrade.asset].<span class="hljs-built_in">push</span>(newTokenID);
            _limitOrderIndexes[_mintTrade.asset][newTokenID] <span class="hljs-operator">=</span> _limitOrders[_mintTrade.asset].<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>;
        } <span class="hljs-keyword">else</span> {
            initId[newTokenID] <span class="hljs-operator">=</span> accInterestPerOi[_mintTrade.asset][_mintTrade.tigAsset][_mintTrade.direction]<span class="hljs-operator">*</span><span class="hljs-keyword">int256</span>(_mintTrade.margin*_mintTrade.leverage/<span class="hljs-number">1e18</span>)<span class="hljs-operator">/</span><span class="hljs-number">1e18</span>;
            _openPositions.<span class="hljs-built_in">push</span>(newTokenID);
            _openPositionsIndexes[newTokenID] <span class="hljs-operator">=</span> _openPositions.<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>;

            _assetOpenPositions[_mintTrade.asset].<span class="hljs-built_in">push</span>(newTokenID);
            _assetOpenPositionsIndexes[_mintTrade.asset][newTokenID] <span class="hljs-operator">=</span> _assetOpenPositions[_mintTrade.asset].<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>;
        }
        _tokenIds.increment();
    }
</code></pre>
<p>As you can see code make external call to attacker specified address(<code>sendTo</code>) by <code>collateral.addr.safeTransferFrom(address(this), sendTo,</code> <a target="_blank" href="http://collateral.id"><code>collateral.id</code></a><code>)</code> which would call <code>sendTo</code> address <code>onERC721Received()</code> function and then code checks to make sure that debt is lower than max debt and code retrieves the updated debt after the call to give user a chance to pay some of the debt in the hook function.</p>
<p>Other examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/6326">1</a> <a target="_blank" href="https://solodit.xyz/issues/6776">2</a> <a target="_blank" href="https://solodit.xyz/issues/16246">3</a> <a target="_blank" href="https://solodit.xyz/issues/2777">4</a> <a target="_blank" href="https://solodit.xyz/issues/1255">5</a></p>
<h3 id="heading-owner-control-nft-after-transferring-burning-etc">Owner control NFT after transferring, burning etc</h3>
<p>Sometimes, developers forget that the owner of an NFT can still utilize the NFT's power even after transferring it. Similar issues can arise when OpenSea fails to cancel sale offers upon NFT transfer. If the NFT is eventually transferred back to the original owner, all previous approvals remain intact, which may come as an unexpected outcome for the owner. Let's examine code from a <a target="_blank" href="https://github.com/sherlock-audit/2023-04-footium-judging/issues/289">recent issue in the Sherlock contest</a>. In this case, there is a football club (represented as an NFT) that can have multiple players (also represented as NFTs) and funds (ERC-20 tokens). The owner has the ability to set the football club's players and funds to themselves using these functions.</p>
<pre><code class="lang-solidity"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setApprovalForERC20</span>(<span class="hljs-params">
    IERC20 erc20Contract,
    <span class="hljs-keyword">address</span> to,
    <span class="hljs-keyword">uint256</span> amount
</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title">onlyClubOwner</span> </span>{
    erc20Contract.approve(to, amount);
}

<span class="hljs-comment">/**
 * @notice Sets approval for ERC721 tokens.
 * @param erc721Contract ERC721 contract address.
 * @param to The address of token spender.
 * @param approved Boolean flag indicating whether approved or not.
 * @dev only the club owner address allowed.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setApprovalForERC721</span>(<span class="hljs-params">
    IERC721 erc721Contract,
    <span class="hljs-keyword">address</span> to,
    <span class="hljs-keyword">bool</span> approved
</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title">onlyClubOwner</span> </span>{
    erc721Contract.setApprovalForAll(to, approved);
}
</code></pre>
<p>but there is no implementation of removing those approvals on transfer, which means that the previous owner can still transfer the club's players and funds to themselves after selling the club.</p>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/7254">1</a> <a target="_blank" href="https://github.com/sherlock-audit/2023-04-footium-judging/issues/289">2</a> <a target="_blank" href="https://solodit.xyz/issues/191">3</a> <a target="_blank" href="https://solodit.xyz/issues/3306">4</a> <a target="_blank" href="https://solodit.xyz/issues/7261">5</a> <a target="_blank" href="https://solodit.xyz/issues/6287">6</a> <a target="_blank" href="https://solodit.xyz/issues/8747">7</a> <a target="_blank" href="https://solodit.xyz/issues/1481">8</a> <a target="_blank" href="https://solodit.xyz/issues/190">9</a></p>
<h3 id="heading-missing-ownerof-whenever-function-arguments-contain-arbitrary-nft-id">Missing ownerOf whenever function arguments contain arbitrary NFT ID</h3>
<p>Whenever there is an arbitrary NFT ID inside function arguments, there should be checks to ensure that the caller is the owner of the NFT on which this function operates. Let's examine code from a <a target="_blank" href="https://solodit.xyz/issues/5691">recent issue in the Sherlock contest</a></p>
<pre><code class="lang-solidity"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withdrawFromGauge</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> _NFTId, <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> _tokens</span>)  <span class="hljs-title"><span class="hljs-keyword">public</span></span>  </span>{
    <span class="hljs-keyword">uint256</span> amount <span class="hljs-operator">=</span> depositReceipt.pooledTokens(_NFTId);
    depositReceipt.burn(_NFTId);
    gauge.getReward(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>), _tokens);
    gauge.withdraw(amount);
    <span class="hljs-comment">//AMMToken adheres to ERC20 spec meaning it reverts on failure, no need to check return</span>
    <span class="hljs-comment">//slither-disable-next-line unchecked-transfer</span>
    AMMToken.<span class="hljs-built_in">transfer</span>(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, amount);
}
</code></pre>
<p>This function is used to withdraw funds from the gauge by burning the deposit receipt NFT. After the NFT is either transferred or approved, a malicious user could withdraw the NFT for themselves.</p>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/7256">1</a> <a target="_blank" href="https://solodit.xyz/issues/5690">2</a></p>
<h3 id="heading-the-protocol-doesnt-follow-the-eip-standard">The protocol doesn't follow the EIP standard</h3>
<p>Smart people wrote EIP standards with a lot of effort on the security, projects that are not compliant with standards might be vulnerable. Let's examine code from a <a target="_blank" href="https://solodit.xyz/issues/5612">contest</a></p>
<pre><code class="lang-solidity">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">approve</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint256</span> tokenId</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
    <span class="hljs-keyword">address</span> tokenOwner <span class="hljs-operator">=</span> _tokenOwner[tokenId];
    <span class="hljs-built_in">require</span>(to <span class="hljs-operator">!</span><span class="hljs-operator">=</span> tokenOwner, <span class="hljs-string">"ERC721: cannot approve self"</span>);
    <span class="hljs-built_in">require</span>(_isApproved(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, tokenId), <span class="hljs-string">"ERC721: not approved sender"</span>);
    <span class="hljs-keyword">if</span> (_isEventRegistered(HolographERC721Event.beforeApprove)) {
      <span class="hljs-built_in">require</span>(SourceERC721().beforeApprove(tokenOwner, to, tokenId));
    }
    _tokenApprovals[tokenId] <span class="hljs-operator">=</span> to;
    <span class="hljs-keyword">emit</span> Approval(tokenOwner, to, tokenId);
    <span class="hljs-keyword">if</span> (_isEventRegistered(HolographERC721Event.afterApprove)) {
      <span class="hljs-built_in">require</span>(SourceERC721().afterApprove(tokenOwner, to, tokenId));
    }
  }
</code></pre>
<p>Bob calls <code>approve</code> to approve Alice on token ID 42 (which is owned by Bob). One week later, Bob sees that a malicious address was approved for his token ID 42 (e.g. because Alice got phished) and stole his token. Bob wonders how this is possible because Alice should not have permission to approve other addresses. However, because the protocol did not follow EIP-721, it was possible.</p>
<p>Other similar examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/3005">1</a> <a target="_blank" href="https://solodit.xyz/issues/5597">2</a> <a target="_blank" href="https://solodit.xyz/issues/16468">3</a> <a target="_blank" href="https://solodit.xyz/issues/1594">4</a> <a target="_blank" href="https://solodit.xyz/issues/16472">5</a> <a target="_blank" href="https://solodit.xyz/issues/16485">6</a></p>
<h3 id="heading-counter-for-nft-is-not-being-trackedupdated-correctly">Counter for NFT is not being tracked/updated correctly</h3>
<p>There should be some symmetries for different functions. For instance, a <code>burn</code> function should (usually) undo all the state changes of a <code>mint</code> function. Asymmetries in these function pairs (e.g., forgetting to unset a field or to subtract from a value) will to overwriting/assigning existing nft to other users. Let's examine code from <a target="_blank" href="https://solodit.xyz/issues/8870">this issue</a></p>
<pre><code class="lang-solidity">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">register</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> _cidNFTID</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
<span class="hljs-number">43</span>:         <span class="hljs-keyword">if</span> (ERC721(cidNFT).ownerOf(_cidNFTID) <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>)
<span class="hljs-number">44</span>:             <span class="hljs-comment">// We only guarantee that a CID NFT is owned by the user at the time of registration</span>
<span class="hljs-number">45</span>:             <span class="hljs-comment">// ownerOf reverts if non-existing ID is provided</span>
<span class="hljs-number">46</span>:             <span class="hljs-keyword">revert</span> NFTNotOwnedByUser(_cidNFTID, <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>);
<span class="hljs-number">47</span>:         cidNFTs[<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>] <span class="hljs-operator">=</span> _cidNFTID;
<span class="hljs-number">48</span>:         <span class="hljs-keyword">emit</span> CIDNFTAdded(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, _cidNFTID);
<span class="hljs-number">49</span>:     }
</code></pre>
<p>The same CID NFT can be registered under multiple accounts at the same time.</p>
<p>This happens because the registration process doesn't check if the CID NFT has been previously registered in order to blank the previous association. This may lead to having the same CID NFT registered for multiple accounts at the same time, as the CID NFT can be registered, then transferred, then registered again, and this process can be repeated any number of times.</p>
<p>Other examples from Solodit: <a target="_blank" href="https://solodit.xyz/issues/8869">1</a> <a target="_blank" href="https://solodit.xyz/issues/6367">2</a> <a target="_blank" href="https://solodit.xyz/issues/3289">3</a></p>
<h3 id="heading-erc-777-tokens">ERC-777 tokens</h3>
<p>In all sherlock's contests, there is a question for project owners</p>
<blockquote>
<p>Which ERC777 tokens do you expect will interact with the smart contracts?</p>
</blockquote>
<p>This means that if the project supports it then there will be definitely some issues with it. ERC-777 tokens have been exploited multiple times in the past. I suggest searching for ERC-777 on <a target="_blank" href="https://solodit.xyz/auth">Solodit</a> to see why.</p>
<h3 id="heading-about-0xvolodya">About <strong>0xVolodya</strong></h3>
<p>0xVolodya is an independent smart contract security researcher. Ranked #2 out of 343 on the 60-day leaderboard and ranked #5 out of 530 on the 90-day leaderboard at <a target="_blank" href="https://code4rena.com/">code4rena</a>. Currently available for projects. dm me on Twitter <a target="_blank" href="https://twitter.com/0xVolodya">0xVolodya</a>.</p>
]]></content:encoded></item></channel></rss>