<?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 jayne on Medium]]></title>
        <description><![CDATA[Stories by jayne on Medium]]></description>
        <link>https://medium.com/@0xjayne?source=rss-e4f43a1a4ab3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iCd05SVkS84hSwsg76EaVQ.jpeg</url>
            <title>Stories by jayne on Medium</title>
            <link>https://medium.com/@0xjayne?source=rss-e4f43a1a4ab3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 19:24:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@0xjayne/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[How to freely borrow all the TVL from the Jet Protocol]]></title>
            <link>https://medium.com/@0xjayne/how-to-freely-borrow-all-the-tvl-from-the-jet-protocol-25d40e35920e?source=rss-e4f43a1a4ab3------2</link>
            <guid isPermaLink="false">https://medium.com/p/25d40e35920e</guid>
            <category><![CDATA[solana-network]]></category>
            <category><![CDATA[bug-bounty]]></category>
            <dc:creator><![CDATA[jayne]]></dc:creator>
            <pubDate>Tue, 04 Jan 2022 13:04:34 GMT</pubDate>
            <atom:updated>2022-01-04T13:11:55.701Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I discovered a critical vulnerability that could possibly lead to the loss of funds in the smart contract of <a href="https://www.jetprotocol.io/">Jet Protocol</a>, a decentralized lending-borrowing protocol on Solana. A huge thank you to the team at Jet Protocol for their quick response in deploying a fix and also for awarding me a bug bounty through <a href="https://immunefi.com/bounty/jetprotocol/">Immunefi</a>.</p><h3>The Bug</h3><p>In a recent mainnet upgrade to the Jet program, a feature to close accounts to recover rent was added. This introduced an inaccuracy in the calculation of a user’s total loan and total collateral values and thereby the calculation of a user’s collateral-to-loan ratio.</p><p>The functions collateral_value (<a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L293">https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L293</a>) and loan_value (<a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L301">https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L301</a>) are used to calculate the total market value of all the collateral a user has supplied and the total market value of all the loans owed by the user respectively. Both these functions are used further at multiple places to calculate if the collateral-to-loan ratio is <a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L157">healthy</a> or if the user is at risk of <a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L188">liquidation</a> and use the below function _market_value for the market value calculation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*5G2LK3Vi7zRMSx7GS5prVQ.png" /></figure><p>The break statement in the above code breaks this calculation when Pubkey::default() is encountered. This wasn’t an issue earlier as encountering Pubkey::default() meant that there were no loans or collateral beyond this point. However with the addition of the new feature to close accounts especially the instructions <a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/instructions/close_loan_account.rs">close_loan_account</a> and <a href="https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/instructions/close_collateral_account.rs">close_collateral_account</a>, since these set the pos.account value to Pubkey::default() after closing the account, it now leads to the ignoring of loans and collateral after the first Pubkey::default() is encountered resulting in the inaccuracy in the loan and collateral value calculation.</p><p>Additionally, the below function is_healthy is used to check if the user’s collateral-to-loan ratio is healthy before letting a user borrow more funds from the protocol’s reserve and if the collateral-to-loan ratio is unhealthy before liquidating a user’s collateral.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*7_bUgICFR8bm3DPq_3neDg.png" /></figure><p>The use of the take_while statement that iterates only till a Pubkey::default() is encountered introduces an inaccuracy in determining if a user’s collateral-to-loan ratio is healthy in a similar idea to that of the break statement in the _market_value function. Basically, if the loan account in the first position is closed which sets the corresponding p.account value to Pubkey::default() , the loans beyond this point is ignored and a user’s collateral-to-loan ratio is always considered as healthy.</p><h3>How to exploit</h3><p>Following a certain steps to exploit this slight inaccuracy in calculation can be used to borrow a large sum from the protocol without ever getting liquidated or having to provide the sufficient collateral in the first place.</p><p>A rough outline of steps to exploit the bug can be as follows:</p><ul><li>deposit<em> x</em> SOL as collateral</li><li>borrow <em>0.2x</em> SOL loan</li><li>deposit <em>y</em> USDC as collateral</li><li>borrow <em>0.2y</em> USDC loan</li><li>repay the <em>0.2x</em> SOL loan call the close_loan_account instruction to close this loan account. This sets the position.account value to Pubkey::default() in the first position and all loan value beyond this point is ignored in all calculations here on</li><li>Now, borrow any amount of USDC you want to from the protocol without getting liquidated or having to even provide sufficient collateral in the first place!</li></ul><p>This is possible as the protocol checks if the user’s collateral-to-loan ratio will be healthy before letting the user borrow the requested funds. Since the p.account value in the first position is set to Pubkey::default() , the is_healthy function returns true all the time here on.</p><h3>Impact</h3><p>While this vulnerability can be exploited by anyone through the above steps to borrow any amount the please without any liability from their end, it is also important to note that several users can also wrongly get liquidated despite providing sufficient collateral.</p><h3>The Fix</h3><p>Funnily, the fix to this bug is simply replacing the <em>break</em> statement with a <em>continue</em> statement and replacing <em>take_while</em> to <em>filter</em>. The fix to the bug was deployed in the following two commits: <a href="https://github.com/jet-lab/jet-v1/commit/e4617be106449faed319feeadca18887ab97f9da">fix#1</a> and <a href="https://github.com/jet-lab/jet-v1/commit/f6951a454d049304ef3af0654751dffea652e205">fix#2</a>.</p><h3>Takeaways</h3><p>Honestly, when I first came across the bug and things didn’t seemed to add up in the calculations, I thought that I was missing something and wasn’t very confident that something was actually wrong with program itself. Only when I was filling up the report in Immunefi did I realize how critical the issue was and how it could be exploited. So the three biggest takeaways for me are as follows:</p><ul><li>code isn’t always perfect and neither are smart contracts</li><li>how powerful open source is in that it can take your code one step closer to being safer</li><li>while most critical vulnerabilities that I have seen in Solana programs arise from missing account verifications or rounding/overflow issues, it was interesting to see one arise from the use of a break statement</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25d40e35920e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>