LLMSkillHub
mirroredCopied from PlamenTSV/plamen under MIT. PlamenTSV has not claimed this page, and is not affiliated with LLM SkillHub.

plamentsv / cpi-security

Trigger Pattern CPI flag detected (invoke/invoke_signed/CpiContext usage) - Inject Into Breadth agents, depth agents

0
SKILL.md
5.2 KB

CPI_SECURITY Skill

Trigger Pattern: CPI flag detected (invoke/invoke_signed/CpiContext usage)
Inject Into: Breadth agents, depth agents
Finding prefix: [CPI-N]
Rules referenced: S3, S5, R1, R4

For every Cross-Program Invocation (CPI) in the Solana program:

1. CPI Target Inventory

Enumerate ALL CPI calls:

#LocationCPI TypeTarget ProgramTarget InstructionAccounts ForwardedSigners Forwarded
1{file:line}invoke/invoke_signed/CpiContext{program_id}{instruction}{list}{list}

2. Target Program Validation

For each CPI call, verify the target program ID:

CPIProgram ID SourceHardcoded/Validated?Spoofable?
{cpi}{where program_id comes from}Hardcoded constant / Validated against known / FROM USER INPUT{if from input: CRITICAL finding}

Attack (S3): If program ID comes from user input without validation, attacker substitutes a malicious program that mimics the expected interface but steals funds.
Defense: Always validate program ID against a hardcoded constant or well-known program address.

3. Signer Privilege Tracing

For each CPI that forwards signers:

CPISigner SourcePrivilege LevelShould Forward?Over-Privileged?
{cpi}{PDA / user wallet / authority}{what the signer can do in target}YES/NO{if forwarding more privilege than needed}

Attack: Forwarding user's wallet as signer to a CPI target that uses it for unintended operations.
Pattern: PDA signers via invoke_signed are safe (program-controlled). User wallet forwarding needs careful scoping.

4. Account Reload Audit (S5 - CRITICAL)

For each CPI call, check what accounts are read AFTER the CPI returns:

CPIAccounts Modified by TargetRead After CPI?reload() Called?Owner Re-checked?
{cpi}{list accounts CPI can modify}YES/NOYES/NOYES/NO

Attack (S5): CPI target modifies account data. Caller reads stale cached data without reload().
CRITICAL: Anchor's reload() refreshes data but does NOT re-verify the account owner. A CPI target could assign the account to a different program. After reload(), the data is fresh but the owner may have changed.
Full defense: After CPI, both reload() AND re-check account.owner.

5. Lamport Balance Conservation

For each CPI that transfers SOL:

CPIExpected Lamport ChangeActual Change Verified?Drain Risk?
{cpi}{expected delta}YES/NO{if NO: CPI could drain more than expected}

Attack: CPI target drains more lamports than expected from accounts owned by the caller's program.
Defense: Check lamport balances before and after CPI; verify delta matches expected amount.

6. Account Owner Check Post-CPI

For each CPI that could modify account ownership:

CPICan Target Call system_program::assign?Owner Checked After CPI?Risk
{cpi}YES/NOYES/NO{if YES and NO: account hijacking}

Attack: CPI target uses system_program::assign to change account owner to attacker-controlled program. Caller continues using the account assuming it's still owned by its program.

7. CPI Depth Analysis

For each code path with nested CPIs:

Entry PointCPI ChainMax DepthCU Cost EstimateRisk
{instruction}{A → B → C}{depth}{estimate}{CU exhaustion?}

Solana limit: CPI depth capped at 4 levels. Check if deep chains approach this limit.

Finding Template

**ID**: [CPI-N]
**Severity**: [based on impact: fund theft via program spoofing = Critical, stale data = High]
**Step Execution**: ✓1,2,3,4,5,6,7 | ✗(reasons) | ?(uncertain)
**Rules Applied**: [S3:✓, S5:✓, R1:✓, R4:✓/✗]
**Location**: program/src/{file}.rs:LineN
**Title**: [CPI issue type] in [instruction] enables [attack]
**Description**: [Specific CPI vulnerability with call chain trace]
**Impact**: [Fund theft / state corruption / privilege escalation]

Step Execution Checklist (MANDATORY)

SectionRequiredCompleted?Notes
1. CPI Target InventoryYES✓/✗/?For every CPI
2. Target Program ValidationYES✓/✗/?For every CPI
3. Signer Privilege TracingYES✓/✗/?For every CPI with signers
4. Account Reload AuditYES✓/✗/?CRITICAL - most common CPI bug
5. Lamport Balance ConservationIF SOL transfers✓/✗(N/A)/?
6. Account Owner Check Post-CPIYES✓/✗/?assign attack prevention
7. CPI Depth AnalysisIF nested CPIs✓/✗(N/A)/?CU exhaustion risk