-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwithdraw_vulnerable.rs
More file actions
57 lines (48 loc) · 1.75 KB
/
Copy pathwithdraw_vulnerable.rs
File metadata and controls
57 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use anchor_lang::prelude::*;
use crate::state::Vault;
/// Withdraw lamports from vault - VULNERABLE version
///
/// ⚠️ VULNERABLE: This instruction allows withdrawing lamports without
/// checking if the remaining balance stays above rent-exempt minimum.
///
/// If the vault's lamports fall below rent-exemption threshold:
/// 1. The account becomes eligible for garbage collection
/// 2. The runtime may purge the account after the transaction
/// 3. All account data (including ownership info) is LOST
/// 4. The remaining lamports are LOST
#[derive(Accounts)]
pub struct WithdrawVulnerable<'info> {
#[account(
mut,
seeds = [b"vault", owner.key().as_ref()],
bump = vault.bump,
has_one = owner
)]
pub vault: Account<'info, Vault>,
#[account(mut)]
pub owner: Signer<'info>,
}
pub fn handler(ctx: Context<WithdrawVulnerable>, amount: u64) -> Result<()> {
let vault = &ctx.accounts.vault;
let vault_info = vault.to_account_info();
// Get current balance
let vault_balance = vault_info.lamports();
// ⚠️ VULNERABLE: Only checks if we have enough lamports
// Does NOT check if remaining balance stays rent-exempt!
require!(
vault_balance >= amount,
crate::error::ErrorCode::InsufficientFunds
);
// Transfer lamports out
**vault_info.try_borrow_mut_lamports()? -= amount;
**ctx.accounts.owner.try_borrow_mut_lamports()? += amount;
let remaining = vault_info.lamports();
msg!(
"VULNERABLE: Withdrew {} lamports. Remaining: {} (may be below rent-exempt!)",
amount,
remaining
);
// ⚠️ If remaining < rent_exempt_minimum, this account could be purged!
// The data and any remaining lamports would be LOST
Ok(())
}