Jet Protocol 任意提款漏洞

慢雾科技Pubblicato 2022-04-02Pubblicato ultima volta 2022-04-02

Introduzione

目前在 Solana 上发生过多起黑客攻击事件均与账号校验问题有关,慢雾安全团队提醒广大 Solana 开发者,注意对账号体系进行严密的审查。

据 Jet Protocol 官方博客披露,他们近期修复了一个赏金漏洞,这个漏洞会导致恶意用户可以提取任意用户的存款资金,慢雾安全团队对此漏洞进行了简要分析,并将分析结果分享如下。

账号

(来源:https://www.jetprotocol.io/posts/jet-bug-disclosure)
相关信息
Jet Protocol 是运行在 Solana 上的一个借贷市场,用户可将账号里的代币(如:USDC、SOL)存入金库,赚取年化收益,同时也可以按一定的比例借出另一种代币。在这个过程中合约会给用户一个 note 凭证,作为用户未来的提款凭证,用我们熟悉的字眼来说就是 LP,而本次漏洞发生的原因也和这个 LP 的设计有关。
我们知道和以太坊合约相比,Solana 合约没有状态的概念,取而代之的是账号机制,合约数据都存储在相关联的账号中,这种机制极大提升了 Solana 的区块链性能,但也给合约编写带来了一些困难,最大的困难就是需要对输入的账号进行全面的验证。Jet Protocol 在开发时使用了 Anchor 框架进行开发,Anchor 是由 Solana 上的知名项目 Serum 团队开发的,可以精简很多账号验证及跨合约调用逻辑。
Anchor 是如何工作的呢?我们可以从 Jet Protocol 的一段代码说起:
programs/jet/src/instructions/init_deposit_account.rs

账号

这里的 deposit_account 账号就是用于存储 LP 代币数据的账号,用户在首次使用时,需要调用合约生成该账号,并支付一定的存储费用。
而这里的 #[account] 宏定义限定了这个账号的生成规则:
规则 1:#[account(init, payer = <target_account>, space = <num_bytes>)]
这个约束中,init 是指通过跨合约调用系统合约创建账号并初始化,payer=depositor 意思是 depositor 为新账号支付存储空间费用。
规则 2:#[account(seeds = <seeds>, bump)]
这个约束中将检查给定帐户是否是当前执行程序派生的 PDA,PDA (Program Derived Address) 账号是一个没有私钥、由程序派生的账号,seed 和 bump 是生成种子,如果 bump 未提供,则 Anchor 框架默认使用 canonical bump,可以理解成自动赋予一个确定性的值。
使用 PDA,程序可以以编程方式对某些地址进行签名,而无需私钥。同时,PDA 确保没有外部用户也可以为同一地址生成有效签名。这些地址是跨程序调用的基础,它允许 Solana 应用程序相互组合。这里用的是 "deposits" 字符 + reserve 账号公钥 + depositor 账号公钥作为 seeds,bump 则是在用户调用时传入。
规则 3:#[account(token::mint = <target_account>, token::authority = <target_account>)]
这是一个 SPL 约束,用于更简便地验证 SPL 账号。这里指定 deposit_account 账号是一个 token 账号,它的 mint 权限是 deposit_note_mint 账号,authority 权限是 market_authority。
Account 的宏定义还有很多,这里略表不提,详细可以考虑文档:
https://docs.rs/anchor-lang/latest/anchor_lang/derive.Accounts.html
有了这些前置知识,我们就可以直接来看漏洞代码:
programs/jet/src/instructions/withdraw_tokens.rs

账号

正常情况下,用户调用函数 withdraw_tokens 提币时,会传入自己的 LP 账号,然后合约会销毁他的 LP 并返还相应数量的代币。但这里我们可以看到 deposit_note_account 账号是没有进行任何约束的,用户可以随意传入其他用户的 LP 账号。难道使用别人的 LP 账号不需要他们的签名授权吗?
通过前面分析宏定义代码,我们已经知道了 market_authority 账号拥有 LP 代币的操作权限,确实不需要用户自己的签名。那么 market_authority 又是一个怎么样的账号呢?我们可以看这里:

  • programs/jet/src/instructions/init_market.rs
账号

这个 market_authority 也是一个 PDA 账号。也就是说合约通过自身的调用就可以销毁用户的 LP 代币。那么对于恶意用户来说,要发起攻击就很简单了,只要简单地把 deposit_note_account 账号设置为想要窃取的目标账号,withdraw_account 账号设置为自己的收款账号,就可以销毁他的 LP,并把他的存款本金提现到自己的账号上。
最后我们看一下官方的修复方法:

账号

补丁中并未直接去约束 deposit_note_account 账号,而是去除了 burn 操作的 PDA 签名,并将 authority 权限改成了 depositor,这样的话用户将无法直接调用这里的函数进行提现,而是要通过另一个函数 withdraw() 去间接调用,而在 withdraw() 函数中账号宏定义已经进行了严密的校验,恶意用户如果传入的是他人的 LP 账号,将无法通过宏规则的验证,将无法通过宏规则的验证,因为 depositor 需要满足 signer 签名校验,无法伪造成他人的账号。

programs/jet/src/instructions/withdraw.rs

账号

总结
本次漏洞的发现过程比较有戏剧性,漏洞的发现人 @charlieyouai 在他的个人推特上分享了漏洞发现的心路历程,当时他发现 burn 的权限是 market_authority,用户无法进行签名,认为这是一个 bug,会导致调用失败且用户无法提款,于是给官方提交了一个赏金漏洞,然后就去吃饭睡觉打豆豆了。
而后官方开发者意识到了问题的严重性,严格地说,他们知道这段代码没有无法提现的漏洞,而是人人都可以提现啊,老铁,一个能良好运行的 bug 你知道意味着什么吗?!所幸的是没有攻击事件发生。
目前在 Solana 上发生过多起黑客攻击事件均与账号校验问题有关,慢雾安全团队提醒广大 Solana 开发者,注意对账号体系进行严密的审查。

Letture associate

Super Storage Week, How to Value It?

"Super Memory Week: How to Value It?" The week of July 27th marked a pivotal moment for the global memory chip market. On Monday, ChangXin Memory Technology (CXMT, 688825.SH) debuted on the Shanghai STAR Market, with its share price experiencing high volatility and settling around 49 yuan, resulting in a massive market capitalization of over 3.2 trillion yuan. This valuation far exceeds traditional models, as the market priced in CXMT's future potential as China's first large-scale DRAM producer rather than just current fundamentals. This event signaled that China's memory industry is transitioning from a cycle follower to a significant variable in global market valuations. The week also saw major earnings reports from South Korean giants SK Hynix and Samsung Electronics, which highlighted strong profits driven by AI memory demand but also raised questions about future capacity expansion and the cycle's sustainability. The core debate centers on CXMT's true worth: is it based on immediate profits, the current DRAM boom, or China's strategic potential in semiconductors over the next decade? While CXMT has rapidly grown to become the world's fourth major DRAM player, gaps remain with incumbents in profitability, advanced products (like HBM), and technology ecosystems. The divergent market reactions underscored the valuation challenge. Chinese markets priced CXMT for its scarcity and national strategic importance, while overseas markets began to reprice global chip stocks, factoring in potential future competition from Chinese capacity expansion. Ultimately, CXMT's valuation hinges on its ability to convert its massive financing and market-assigned "option value" into tangible technological progress, yield improvements, capacity, and sustained profits in the coming quarters and years.

marsbit35 min fa

Super Storage Week, How to Value It?

marsbit35 min fa

Trading

Spot
活动图片