fix: zero base token bytes before init to prevent IDL buffer attack#2248
fix: zero base token bytes before init to prevent IDL buffer attack#2248ananas-block merged 2 commits intomainfrom
Conversation
Audit issue #17 (CRITICAL): Token::new_zero_copy only sets mint, owner, and state fields without zeroing amount, delegate, delegated_amount, is_native, or close_authority. An attacker could pre-set the amount field via IDL buffer. Zero all 165 base token bytes before initialization.
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughThis change adds a pre-initialization step in the CToken account setup that zeros the first 165 bytes of token account data before zero-copy deserialization. This prevents unintended field values from the IDL buffer from persisting into the newly initialized state, ensuring a clean slate. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@programs/compressed-token/program/src/shared/initialize_ctoken_account.rs`:
- Around line 210-215: Replace the magic number 165 with a well-named constant
and fail on short buffers: add a new constant BASE_TOKEN_ACCOUNT_LEN: usize =
165 near the top of the file (alongside T22_ACCOUNT_TYPE_OFFSET) to document the
base SPL token account length, then change the zeroing block that currently
checks token_account_data.len() >= 165 to check token_account_data.len() >=
BASE_TOKEN_ACCOUNT_LEN and call
token_account_data[..BASE_TOKEN_ACCOUNT_LEN].fill(0); if the length is smaller
return an explicit error (propagate a suitable error/result from the surrounding
function) instead of silently skipping; keep references to token_account_data
and new_zero_copy intact.
Summary
new_zero_copyinitialization to prevent pre-set values (e.g.amountfield via IDL buffer) from persisting, sincenew_zero_copyonly writesmint,owner, andstate.