Prastut Kumar
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: "Jet Protocol - $25 million vulnerability" description: "Learn how rust memory layout works" pubDate: "Sep 14 2022" year: 2022 active: true tags: Superteam Security --- Jet - a protocol that builds a decentralised lending platform - suffered from a critical vulnerability where a hacker could have siphoned off $25 million! Fortunately, [Jayne](https://twitter.com/0xjayne/status/1478357088468234243?s=20&t=7dK4vfaYXsmfG7xKq9AtFA) - a dev in the Solana ecosystem - who happened to be going through the Jet's codebase, found the vulnerability and then privately informed the Jet protocol team about it. The core team promptly redeployed the mainnet program with the bug patched. Nevertheless, it will still be interesting to learn how the hack could have occurred and glean some insights. ![](https://i.imgur.com/D7qgrb1.png) <br> ## Why would you use Jet protocol? Jet Protocol is a decentralised, non-custodial protocol that uses a collection of smart contracts that provides lending and borrowing functions. Users can: 1. lend assets to earn interest. 2. borrow assets against the collateral they have posted. These lending or borrowing activities are collectively called opening a "**position**". Whenever you come across the word position in the article below, think of them as a user doing a lend or a borrow activity with Jet. So when you open a "position" with Jet, inside the hood the protocol tracks these two values: - **total collateral value** - the sum of all the collateral the user has supplied to the protocol - **total loans value** - the sum of all the loans users owes to the protocol. The above two values calculate the collateral-to-loan ratio (short form C-ratio). :::warning :bulb: What is collateral-to-loan ratio? It is a ratio to determine whether the collateral backing your loan is in a healthy condition or not. If it's not, then the protocol sells some/all your collateral to cover for the loan. What is collateral-to-loan ratio? It is a ratio to determine whether the collateral backing your loan is in a healthy condition or not. If it's not, then the protocol sells some/all your collateral to cover the loan. ***Formula for C-ratio = collateral value / borrowed value*** Example: * Assume the current market price of 1 SOL = $12.5 * Suppose: * you want to lend 10 SOL i.e $125 worth of SOL * and then borrow 100 USDC. * The current C-ratio would come out to be: * = ($125 worth of SOL) / ($100 worth of USDC) * = 125% * After a while: * suppose the price of 1 SOL drops to $8.5. * then the value of your collateral effectively becomes $85. * Now any rational person who has borrowed $100 worth of USDC, will not want to return this to the protocol since they will get back only $85 worth of SOL! * To paraphrase, they are incentivized to take off with the loaned asset. This situation is not ideal hence: * Jet protocol instils a guardrail where the user's position C-ratio should always be >= 125%. * If the user's position C-ratio drops below 125%, then Jet protocol sells off part of the collateral to get the position above 125%. ::: <br> ![](https://i.imgur.com/VXWz11l.png) <br> ## How could the hacker have stolen the funds? Now let's understand what precisely the vulnerability entailed at a high level: 1. Jet used an array to store users' positions. - Suppose I have four positions in the array. - `c` stands for supplying collateral/lending position - `b` stands for borrowing/loan position - So the array tracking user's positions would look like this: ![](https://i.imgur.com/cXvGN2S.png) 3. Whenever a user tried to take loans: - Jet calculates the C-ratio by looping through the user's positions array. - If the calculated C-ratio was >=125%, then the user was allowed to take loans. - From the above example, C-ratio becomes: ![](https://i.imgur.com/0mlyQnD.png) 4. The recover rent upgrade introduced a vulnerability that if a user closed any loan positions, the positions after it got ignored in the C-ratio calculation. - Let's assume that in the above example, the user closed `b1` loan. - Due to the vulnerability, all positions after `b1` got ignored and the C-ratio calculation became: - ![](https://i.imgur.com/XehuVaw.png) 6. So even if the user takes more loans, the wonky C-ratio will not account for that position, as all positions after `b1` get ignored! <br> ## How could the exploit work? Let's take a real-world example: 1. deposit `x` SOL as collateral 2. borrow `0.2*x` USDC 3. deposit `y' BONK as collateral 4. borrow `0.2*y` USDC Then the array of positions becomes: - [ `x` SOL Collateral, `0.2*x` USDC loan, `y` BONK collateral, `0.2*y` USDC loan] Now to trigger the vulnerability, repay `0.2*x` USDC loan: - [ `x` SOL Collateral, ~~0.2*x USDC loan~~, `y` BONK collateral, `0.2*y` USDC loan] Due to closing this account, loan value beyond index 1 is ignored in all calculations from here on. And voila, you can steal money now! 1. You can borrow any amount of USDC. 2. You don't even need to provide any collateral to borrow! Now that we have understood the hack at a high level, let's dive deep into technical details. <br> ## Technical Details There are 3 functions that are responsible for calculating C-Ratio: - [**collateral_value**](https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L293-L299) - responsible for calculating total market value of all the collateral user has supplied - [**loan_value**](https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L301-L307) - responsible for calculating total market value of all the loans the user owes to the protocol. - [**_market_value**](https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/state/obligation.rs#L483-L496) - called by the above functions to calculate market value of the assets. ![](https://i.imgur.com/Xfy6dfj.png) Focus on the `for` loop in the above code. * The break statement breaks the loop when `Pubkey::default()` is encountered. - What is `Pubkey::default()`? - In Solana, every account has a public address represented by it's public key. - [`Pubkey` struct](https://docs.rs/solana-program/1.4.9/solana_program/pubkey/struct.Pubkey.html) offers developers various methods around public keys like public key generation for a new account. - One such method is `default`. - The default value for a Pubkey Solana is an all-zero value. - The accounts that have a `default` Pubkey means that either they are not created or they are closed. - So this method helps developers check whether a particular account exists or not. - **Hence encountering `Pubkey::default()` means a blank account which meant that there are no positions after this iteration** This wasn't an issue before Jet added the feature to close accounts to recover rent for users. <br> The close account feature: - introduced 2 new instructions: - [`close_loan_account`](https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/instructions/close_loan_account.rs) and - [`close_collateral_account`](https://github.com/jet-lab/jet-v1/blob/e9cfe1810a6614b78934704ac46dffef67a8d794/programs/jet/src/instructions/close_collateral_account.rs) - which when closing accounts set the account value to `Pubkey::default`. <br> Now when a user tries to borrow funds after closing any loan position: - `_market_value` get's called to calculate user's C-ratio to check whether the user can borrow more funds or not. - As soon as the `for` loop encounters the `Pubkey` for a closed account, it breaks. - **leading to ignoring the rest of the loans and collateral remaining in the array!** <br> This incorrect breaking of `for` loop results in: - inaccurate calculation for: - total market value of user's collateral - and total market value of user's loans! - resulting in calculating an inaccurate C-ratio. --- Another function `is_healthy` also became vulnerable: ![](https://i.imgur.com/DL9N5rr.png) Here also `take_while` statement only iterates only till a `Pubkey::default()` is encountered which introduces a bug in determining if a user’s collateral-to-loan ratio is healthy or not. ## How to fix the vulnerability? * Inside `_market_value` function, replace the `break` statement with `continue` so that loop still runs after encountering `Pubkey::default` - [patch here](https://github.com/jet-lab/jet-v1/commit/e4617be106449faed319feeadca18887ab97f9da#diff-4a3d8fc5d1304a2a6e54d9201bf8bcd038ad1adea84dc1d83be6e574039f1ca4R479). * Inside, `is_healthy`, replace `take_while` with `filter` so instead of breaking while loop on `Pubkey::default` we just filter the array where accounts exist - [patch here.](https://github.com/jet-lab/jet-v1/commit/f6951a454d049304ef3af0654751dffea652e205#diff-4a3d8fc5d1304a2a6e54d9201bf8bcd038ad1adea84dc1d83be6e574039f1ca4R470) ## Official Response Jet protocol publicly disclosed the bug [here](https://medium.com/jetprotocol/jet-protocol-upgrade-bug-patch-disclosure-6eb74cf7149).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully