Rent Epoch Validation Remediation
How to fix improper rent epoch validation.
Rent Epoch Validation Remediation
Overview
Detector Reference: Rent Epoch Validation
This guide explains how to properly validate rent-exempt status before modifying accounts, particularly relevant for programs handling account lifecycle operations.
Recommended Fix
Query the Rent sysvar dynamically before modifying account state:
let rent = Rent::get()?;
require!(
account.lamports() >= rent.minimum_balance(account.data_len()),
ErrorCode::NotRentExempt
);
Alternative Mitigations
- Ensure rent exemption at creation: use
SystemProgram::create_accountwith sufficient lamports from the start. - Anchor init:
#[account(init, payer = user, space = ...)]ensures rent exemption at creation. - Skip for rent-exempt-only programs: if your program only creates and operates on rent-exempt accounts, this finding is informational and can be acknowledged.
Common Mistakes
- Caching the rent-exempt minimum: storing the value in account data creates a stale reference. Always query
Rent::get()dynamically. - Checking rent before realloc but not after: reallocation changes the minimum balance. Check again after resizing.
- Ignoring epoch transitions: while rare, rent parameter changes across epochs can affect the minimum balance.