<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Oz Kashi on Medium]]></title>
        <description><![CDATA[Stories by David Oz Kashi on Medium]]></description>
        <link>https://medium.com/@kdavidoz?source=rss-36e4234e19ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I9z3b4D09uGy9RLwl8VwsA.png</url>
            <title>Stories by David Oz Kashi on Medium</title>
            <link>https://medium.com/@kdavidoz?source=rss-36e4234e19ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 06:58:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kdavidoz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Reentrancy Strikes Again — The Case of Lendf.Me]]></title>
            <link>https://medium.com/@kdavidoz/the-reentrancy-strikes-again-the-case-of-lendf-me-4359016776f3?source=rss-36e4234e19ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/4359016776f3</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[smart-contract-security]]></category>
            <category><![CDATA[defi]]></category>
            <dc:creator><![CDATA[David Oz Kashi]]></dc:creator>
            <pubDate>Fri, 24 Apr 2020 16:26:31 GMT</pubDate>
            <atom:updated>2020-04-24T16:26:31.147Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Reentrancy Strikes Again — The Case of Lendf.Me</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*wmHG_iGV2_iMuKQ5_eSzdA.jpeg" /></figure><p>DeFi or decentralized finance is a growing sector in the blockchain and cryptocurrency space that defines an ecosystem of decentralized applications providing financial services with no governing authority.</p><p><a href="https://www.lendf.me/">Lendf.me</a> is a DeFi app utilizing smart contracts in order to provide instant, decentralized lending. The platform suffered an attack causing the loss of $25m in cryptocurrency on the day of April 19, 2020.</p><p>Thus, joining the list of other DeFi protocols exploited recently:</p><p><a href="https://atozmarkets.com/news/37-million-seth-drain-synthetix-hack/">Synthetix hack</a> — 37M sETH stolen</p><p><a href="https://www.cryptoground.com/a/ethereum-based-defi-platform-bzx-loses-645000-in-second-exploit">bZx hack</a> — $900k stolen</p><p>Lendf.me’s current vulnerability is a unique instance of the reentrancy bug. Reentrancy is a well-known issue in the field of computing, referring to the ability of a subroutine to be interrupted in the middle of its execution and (then safely) be called again.</p><p>Other reentrancy bugs have been exploited in the past, causing massive damage, including:</p><p><a href="https://hackingdistributed.com/2016/06/18/analysis-of-the-dao-exploit/">The DAO hack</a> — $150m stolen</p><p><a href="https://medium.com/spankchain/we-got-spanked-what-we-know-so-far-d5ed3a0f38fe">Spank Chain hack</a> — $30k stolen</p><p>The Lendf.me attack took place on the Ethereum main-net smart contract named<a href="https://etherscan.io/address/0x0eee3e3828a45f7601d5f54bf49bb01d1a9df5ea#code"> MoneyMarket</a>, which implements the core logic of the Lendf.me app.</p><h3>Understanding the Lendf.me vulnerability</h3><p>In order to best understand the underlying cause of the vulnerability, we should consider the contents of the various functions in the MoneyMarket contract.</p><p>First, we will consider the <em>MoneyMarket.supply() </em>function (line 1508).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*EEfia0-LtQtCnVLsSKeSWA.png" /><figcaption><em>MoneyMarket.supply() part 1</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*9bPmX8X04Zt0o7SEP6JkOg.png" /><figcaption><em>MoneyMarket.supply() part 2</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*i4lTX3ZVbTaQ1NffE7oYQg.png" /><figcaption><em>MoneyMarket.doTransferIn()</em></figcaption></figure><p>The main purpose of the<em> MoneyMarket.supply()</em> function is to handle token deposits. The function takes two arguments, the asset (the asset that the user wishes to deposit), and the amount (the number of tokens he wishes to deposit).</p><p>The main logic flow of the <em>MoneyMarket.supply()</em> function is as follows:</p><p>First, we read the balance variable that represents the user’s deposited asset balance in MoneyMarket storage (line 1514), then, <em>MoneyMarket.checkTransferIn()</em> function is invoked (line 1526). This function (externally) calls the asset contract in order to figure if the user has the number of tokens he wishes to deposit and that he approved the MoneyMarket contract to withdraw this amount on his behalf.</p><p>Later <em>MoneyMarket.doTransferIn()</em> function is invoked (line 1583) which (externally) calls the asset contract’s <em>transferFrom()</em> function (line 405) that in turn transfers the amount from the user to the MoneyMarket contract. After the return from the external call, the <em>MoneyMarket.supply() </em>function is updating the user’s deposited balance (lines 1599–1600).</p><p>Let’s go over the <em>MoneyMarket.withdraw()</em> function’s logic briefly. In a simplified manner, this function gets the requested amount of tokens to withdraw, checks that the user holds at least this amount of tokens then transfers these tokens to the user by (externally) calling the token contract <em>transfer()</em> function.</p><blockquote><strong>Can you spot the vulnerability by now?</strong></blockquote><p>The issue here is that <em>MoneyMarket.supply()</em> function is actually updating the user’s asset balance <strong>after</strong> the external call to <em>asset.transferFrom() </em>(lines 1599–1600), but based on a value that was read <strong>before </strong>the external call (line 1514), which means that the update potentially ignores any updates that were made within the external call. In many terms, we can consider this anomaly to be a “Lost Update”.</p><p><strong>But why is Lendf.me’s vulnerability exploitable?</strong></p><p>In order to understand this, we will have a look at the imBTC contract (or any other ERC-777 compliant contract)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*TQgyblkFYU3x6sV7bYAJhQ.png" /><figcaption>imBTC._transferFrom()</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*IFEtDE7ZS_5uAljAFgtDbQ.png" /><figcaption>imBTC._callTokensToSend()</figcaption></figure><p>The attacker took advantage of the fact that some of the assets implement ERC-777 standard, which means that the <em>imBTC._callTokensToSend() </em>function and thus, <em>attackerContract.tokensToSend()</em> function are invoked (lines 866, 1056 respectively) before the actual transfer of value between the two parties. This way, the attacker’s contract gets a chance to call <em>MoneyMarket.withdraw()</em> function <strong>before </strong>the invocation of MoneyMarket.supply() is finished!</p><h3>Attack Strategy</h3><p>The only prerequisite for attempting the exploit is for an attacker to deploy an attacker contract that holds some amount of any asset that is ERC-777 compliant, let’s assume for example that the attacker holds 10 tokens of <a href="https://etherscan.io/address/0x3212b29E33587A00FB1C83346f5dBFA69A458923#code">imBTC</a>.</p><p>Now,</p><ol><li>The attacker would place the first transaction that invokes <em>MoneyMarket.supply(asset = imBTCAddress, amount = 9)</em>. At this point, the attacker holds a supply of 9 imBTC in the MoneyMarket contract, and a balance of 1 imBTC in the imBTC token contract.</li><li>The attacker would place the second transaction that invokes <em>MoneyMarket.supply(asset = imBTCAddress, amount = 1)</em> , but now with an external call to <em>MoneyMarket.withdraw(asset = imBTCAddress, requestedAmount = 9)</em> inside the <em>attackerContract.tokensToSend()</em> callback. By the end of this transaction, the attacker’s imBTC balance in the imBTC token contract is 9, but the imBTC supply in the MoneyMarket contract is 10! This unwanted state occurred as the <em>MoneyMarket.supply()</em> function increases the supply for the attacker (lines 1599–1600) it uses stale data. Therefore, the function doesn’t “know” at this point, that the attacker has already withdrawn some of his supply.</li><li>Now, the attacker holds a deposit on the MoneyMarket contract, backed by nothing. The attacker can use this to (falsely) borrow or withdraw assets deposited by other users. Furthermore, these two steps could be potentially performed, again and again, thus draining MoneyMarket’s liquidity.</li></ol><h3>Mitigation</h3><p>When writing the smart contract code,</p><ol><li>Try not to update <strong>any</strong> storage variables after an external call.</li><li>If not possible, deploy some locking mechanism, like the commonly known<a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuard.sol"> ReentrancyGuard</a> instead. Make sure that any pair of code paths that have a possible read/write conflict for a variable will be “reentrancy guarded”. For example, in this case, deploying a reentrancy guard only for the <em>MoneyMarket.supply()</em> function would not solve the problem, it should be deployed for the <em>MoneyMarket.withdraw()</em> function as well. Valid network’s automated tools can help identify locations where these guards are missing, or incorrectly implemented.</li></ol><h3>About Valid Network</h3><p><a href="https://valid.network/">Valid Network</a> provides a comprehensive cybersecurity platform for DApps (Decentralized Applications). Valid Network’s unique value proposition is well defined in its DevSecOps approach that protects organizations from the first line of code developed, through supplying assurance and governance for live business transactions. Valid Network’s experience in distributed systems brought the team to exhibit several breakthrough technologies for detection, monitoring, and enforcement.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4359016776f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 Ways to maliciously manipulate storage variables in Solidity]]></title>
            <link>https://medium.com/@kdavidoz/3-ways-to-maliciously-manipulate-storage-variables-in-solidity-d297c92c30e6?source=rss-36e4234e19ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/d297c92c30e6</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[David Oz Kashi]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 06:52:01 GMT</pubDate>
            <atom:updated>2020-06-24T11:23:06.293Z</atom:updated>
            <content:encoded><![CDATA[<h4>Smart contracts may not be as transparent as they seem to be</h4><p>Smart contracts, in general, offer the ability to determine factors and expectations set out by the contract. In the field of programming, factors and expectations can be described as ‘variables’ and ‘conditions’ within the actual code. As such, state changes throughout a smart contract should adhere only to the contract’s logic. While it may seem straight forward, we will examine a couple of cases that prove a capable attacker could potentially violate the terms set by the smart contract in the first place.</p><p>Our overall intentions for this blog are to promote cybersecurity awareness within the blockchain community and demonstrate best practices to help smart contract developers write more robust code.</p><p>To better understand the method of the attack, let’s assume we have a safe that’s inside a mansion. Also, the guards outside the mansion are instructed to allow only red vehicles. However, an attacker already has that information and therefore, they simply climb the wall on the opposite end of the mansion to get into the safe. Moreover, imagine the guards are understood as upholding the smart contract terms (allow only red cars), and the safe inside that mansion as the actual monetary balance (considered a variable within the code) of the attacker.</p><p>This kind of attack damages the concept of <strong>trust</strong>. The transparency and self-explanatory aspect, commonly known as “what you see it’s what you get”, is irreparably damaged as a result.</p><p>In a worst-case scenario, the attacker can cause even greater and more direct damage; balance modification.</p><p>Let’s dive deeper into the attack itself, so we may understand arithmetic overflows/underflows of EVM.</p><p>In Solidity, the size of a storage slot is 32 bytes, which means statically sized variables can take 32 bytes at most. ‘uint256’ type, for example, represents a non-negative number in the range of [0,2²⁵⁶-1]. Respectively, the ‘uint8’ type represents a non-negative number in the range of [0,2⁸ -1]. The number presented after ‘uint’ represents the number of bits needed to store the variable. Arithmetic operations are supposed to act as under modulo(2**number_of_bits). For example, the arithmetic addition of a+b (two ‘uint256’ variables) is implemented as (a+b) modulo 2²⁵⁶.</p><p>An arithmetic overflow occurs when the product of an arithmetic operation is more than the largest number inside its type range. For example, let’s take uint8 x = 255, and then x = x + 1. After these two operations x will hold the value of 0, causing an overflow.</p><p>An arithmetic underflow occurs when the product of an arithmetic operation is less than the smallest number inside its type range. For example, let’s take uint8 x = 0, and then x = x -1. After these two operations x will hold the value of 255, causing an underflow.</p><p>At this point, it’s worth mentioning EVM enables a programmer to use arithmetic over/under-flows within the program, without throwing an exception.</p><p>Arithmetic over/under-flows caused a lot of troubles in the past. <a href="https://cryptoslate.com/batchoverflow-exploit-creates-trillions-of-ethereum-tokens/">BEC token hack</a>, for example, enabled the attacker to mint and steal trillions of Ethereum tokens, thus devaluing the token’s price to effectively zero. Therefore, it is imperative developers consider using a ‘SafeMath’ like libraries that reverts over/under-flows.</p><p>Now that we have a firmer understanding of arithmetic over/under-flows attacks, let’s explore the equally devastating vulnerability of storage variables manipulation. This can be achieved by these three methods:</p><h3><strong>Array length underflow</strong></h3><p>Dynamically-sized arrays in solidity store their length in a slot, and the values of the array are stored sequentially starting at the hash of the slot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*SSjY8rLnaoK2PvIqoHeY0g.png" /><figcaption>The location of an element of a dynamically-sized array</figcaption></figure><p>The array length property can be manipulated programmatically, which means one can modify the length of the array without changing the array’s values. The Solidity compiler adds a restriction that prevents access to slots that are not part of the array, by checking that index &lt; array.length. Which means that theoretically, an attacker can modify <strong>any storage slot</strong> he wishes to just by choosing an index that satisfies:</p><ol><li>index = (slotToModify - keccak256(arraySlot)) toTwosCompliment(256)</li><li>Index &lt; array.length</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*4ZWqC2d-l0iw8vB307AmGQ.png" /><figcaption>Array length underflow example</figcaption></figure><p>Using the example above, the attacker holds a balance of 2. However, using less than 5 lines of code, he/she can effectively modify the balance and steal tokens by launching the attack below:</p><ol><li>call ‘decreaseSize’ function (line 16), which changes the array length property from 0 to ((2**256) -1).</li><li>compute the storage slot that holds the balance(slotToModify), by calling ‘mapLocation’ function with slot = 1 and key = attackerAddress. This computes to 40188104427714482242681265279535408119688761634073773811122280228267054098286.</li><li>find the index that satisfies:</li></ol><p>index = (slotToModify - keccak256(arraySlot)) toTwosCompliment(256) = 21618673952608599655092999141927839583015649660180456411662060369447791395339</p><ol><li>call ‘setValue’ function (line 12) with index = 21618673952608599655092999141927839583015649660180456411662060369447791395339 and value = hacker’s desired balance 😈</li></ol><p><em>Note that this vulnerability has been mitigated for solidity compiler versions &gt;= 0.6.0</em></p><h3><strong>Uninitialized storage pointer</strong></h3><p>Storage pointers are very useful. Intended for non-scalar variables, their purpose is to avoid unnecessary copies from storage to memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*rK4uOd2SQK78ZPxLkTD4IA.png" /><figcaption>Storage pointer usage example</figcaption></figure><p>The above example shows the usage of storage pointer to modify properties inside a struct.</p><p>Now let’s consider the following example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*a2vBnArvFbT0XrDLd4wPmw.png" /><figcaption>Uninitialized storage pointer example</figcaption></figure><p>As you can see here, p is not initialized, which means it refers to slot 0 in storage. It also means that line 14 will overwrite the value inside of slot 0. In order for the attacker to modify its balance, all he has to do is call the ‘setHeight’ function(line 12) with height = hacker’s desired balance 😈</p><p><em>Note that this vulnerability has been mitigated for solidity compiler versions &gt;= 0.5.0</em></p><h3><strong>Inside slot overflow</strong></h3><p>As mentioned before, in Solidity, the size of a storage slot is 32 bytes. Statically-sized variables that need less than 32 bytes are packed into a single storage slot. Developers utilize this technique to save gas by combining multiple reads or writes into a single operation, whenever possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*GpLeWhljstxUfy5Q1pxqNQ.png" /><figcaption>Inside slot overflow example</figcaption></figure><p>What do you think might happen after invoking ‘incrementX’ function (line7)?</p><p>Normally, we would expect ‘x’ to turn ‘0’ without impacting ‘attackerBalance’. However, a compiler bug caused ‘x’ to turn ‘0’ but ‘attackerBalance’ had turned to ‘1’. Therefore, increasing the attacker’s original balance.</p><p><em>Note that this vulnerability has been mitigated for solidity compiler versions &gt;= 0.4.4</em></p><p>To recap, we have reviewed three separate ways to maliciously modify one’s balance: Array length underflow, uninitialized storage pointer, and inside slot overflow. As we’ve demonstrated throughout, using our examples, one wrong line of code, can have a potentially devastating impact on a project. At Valid Network, we’ve handcrafted an intuitive platform which can identify these sort of vulnerabilities, among many others not mentioned.</p><p>Enjoying our content? Follow us on <a href="https://www.linkedin.com/company/valid-network/">LinkedIn </a>for more updates</p><h3>About Valid.Network</h3><p><a href="https://valid.network/">Valid.Network</a> provides a comprehensive cybersecurity platform for DApps (Decentralized Applications). Valid Network’s unique value proposition is well defined in its DevSecOps approach that protects organizations from the first line of code developed, through supplying assurance and governance for live business transactions. Valid Network’s experience in distributed systems brought the team to exhibit several breakthrough technologies for detection, monitoring, and enforcement.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d297c92c30e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>