Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/processor/validator_claim_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use borsh::BorshDeserialize;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey, rent::Rent,
sysvar::Sysvar,
};

use crate::{
Expand Down Expand Up @@ -49,18 +50,26 @@ pub fn process_validator_claim_fees(
true,
)?;

// Calculate the amount to transfer
let min_rent = Rent::default().minimum_balance(8);
let amount = args
.amount
.unwrap_or(validator_fees_vault.lamports() - min_rent);
// Use the on-chain Rent sysvar (not `Rent::default()`) and the actual
// account data length so the minimum-balance threshold is always accurate.
let min_rent =
Rent::get()?.minimum_balance(validator_fees_vault.data_len());
Comment on lines +53 to +56
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

protocol_claim_fees.rs retains the same unfixed pattern.

src/processor/protocol_claim_fees.rs:53-60 still uses Rent::default().minimum_balance(8) with unchecked subtraction — the exact vulnerability this PR was raised to fix. Consider applying the same Rent::get()?.minimum_balance(fees_vault.data_len()) + checked_sub treatment there before the next release.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/processor/validator_claim_fees.rs` around lines 53 - 56, The
protocol_claim_fees.rs code still uses Rent::default().minimum_balance(8) and
performs an unchecked subtraction; replace that pattern by retrieving the
on-chain rent with Rent::get()? and computing minimum_balance using the actual
account data length (e.g., fees_vault.data_len()), assign to a min_rent
variable, and use a checked subtraction (checked_sub) when subtracting min_rent
from balances to avoid underflow—locate the logic around fees_vault and the
current minimum_balance/subtraction and update it to mirror the
validator_claim_fees.rs approach.


// Guard against underflow: if the vault somehow holds fewer lamports than
// the rent-exempt minimum, return an error instead of wrapping around.
let available = validator_fees_vault
.lamports()
.checked_sub(min_rent)
.ok_or(ProgramError::InsufficientFunds)?;

let amount = args.amount.unwrap_or(available);

// Ensure vault has enough lamports
if validator_fees_vault.lamports() - min_rent < amount {
if available < amount {
msg!(
"Vault ({}) has insufficient funds: {} < {}",
validator_fees_vault.key,
validator_fees_vault.lamports() - min_rent,
available,
amount
);
return Err(ProgramError::InsufficientFunds);
Expand Down