Cross-Program Invocation (CPI) allows a Solana program to call the public instructions of another program within the same atomic transaction. This composability is the foundation of DeFi legos, enabling a single user action to orchestrate a complex sequence of operations across a lending protocol, an AMM, and a liquid staking pool without counterparty risk between steps. However, this power comes with a critical security implication: the calling program temporarily delegates its program identity (program_id) and signer privileges to the callee, creating a privilege escalation surface that is the root cause of many high-severity exploits on Solana.

Cross-Program Invocation (CPI) Security Best Practices
Introduction
Cross-Program Invocation (CPI) is the core mechanism enabling composability between on-chain programs, but it introduces a distinct class of security risks that are unique to Solana's atomic execution model.
The primary threats in CPI design are privilege de-escalation failures and re-entrancy. A program that invokes an unknown, unverified, or upgradeable external program without properly validating its identity and expected behavior risks having its own Program Derived Address (PDA) signer seeds abused to drain vaults or manipulate state. Unlike the EVM, Solana's runtime does not allow recursive re-entrancy into the same instruction, but cross-program re-entrancy—where Program A calls Program B, which then calls back into a different instruction of Program A—is a live and exploited attack vector. The classic checks-effects-interactions pattern must be adapted for Solana's account model, where state mutations must be committed before any CPI to an untrusted program.
For protocol developers building composable systems and security auditors reviewing them, a rigorous CPI security model is non-negotiable. This includes enforcing allowlists of known program IDs, validating that invoked programs are not upgradeable unless governed by a trusted multi-sig, and structuring instruction logic so that all account state transitions are finalized before any external call. Chainscore Labs helps teams audit these cross-program trust boundaries, review upgrade authority configurations, and implement CPI safety patterns that prevent privilege escalation and re-entrancy attacks before code reaches mainnet.
Quick Facts
Key security properties and operational risks of Cross-Program Invocation on Solana that every builder and auditor must verify.
| Area | What changes | Who is affected | Action |
|---|---|---|---|
Re-entrancy | Solana's runtime prevents recursive CPI into the same program, but multi-program circular invocation is possible | DeFi protocol developers, security auditors | Design invocation graphs that avoid circular dependencies; verify with integration tests |
Privilege de-escalation | Callee programs inherit the caller's signer privileges unless explicitly dropped | Smart contract developers, wallet engineers | Use |
Unknown program invocation | Invoking an unverified or upgradeable program can introduce arbitrary code execution risk | Protocol architects, DeFi integrators | Verify program IDs against canonical sources; consider immutable deployment |
Account ownership | CPI can modify any account owned by the callee program, potentially including accounts the caller did not intend to modify | Smart contract developers, security reviewers | Validate account ownership and data in every instruction handler |
Atomic composability | All CPIs in a transaction succeed or fail atomically, but partial state changes within a single instruction can persist if not handled | DeFi protocol developers, MEV searchers | Apply checks-effects-interactions pattern adapted for Solana's account model |
Compute budget | CPI depth and instruction complexity consume compute units from the caller's budget | Protocol developers, RPC providers | Profile CPI-heavy transactions; set appropriate compute unit limits |
Upgradeable program risk | Programs invoked via CPI can be upgraded to malicious code if upgrade authority is not burned or governed | Protocol architects, governance delegates | Verify upgrade authority status; prefer immutable programs for critical dependencies |
Cross-program account confusion | Programs may misinterpret account data formats when invoking programs with different serialization conventions | DeFi integrators, wallet engineers | Validate account discriminators and data schema versions before deserialization |
The CPI Security Model
A technical analysis of the security model for Cross-Program Invocations on Solana, focusing on re-entrancy, privilege de-escalation, and the risks of invoking untrusted programs.
Cross-Program Invocation (CPI) is the fundamental composability primitive on Solana, enabling a program to call the instruction handler of another program within the same atomic transaction. While this synchronous execution model unlocks powerful DeFi integrations like flash loans and multi-hop swaps, it introduces a distinct security model that differs significantly from asynchronous, message-passing architectures. The core challenge is that a callee program executes with the full signer privileges of the original transaction, meaning a malicious or compromised program can potentially manipulate shared state, re-enter the caller, or abuse delegated authority in ways that are not possible in isolated execution environments.
The primary security pattern for CPI is the checks-effects-interactions model, adapted for Solana's account-centric design. A program must complete all validation checks and state mutations on its own accounts before making an external CPI. This prevents re-entrancy attacks where a malicious callee recursively invokes the calling program before its state is finalized. However, Solana's runtime enforces a maximum call depth of 4 and a linear call stack (no recursive self-CPI), which provides a structural mitigation against unbounded re-entrancy. The more subtle risk is privilege de-escalation failure: when a program invokes an unknown or upgradeable program, it must manually inspect the callee's program ID and cannot rely on the runtime to enforce security boundaries. Invoking an upgradeable program whose authority has been compromised is a leading vector for wallet-draining attacks.
For protocol developers, a rigorous CPI security review must map every external invocation to a trust assumption. Invoking a well-known, immutable program like the System Program or Token Program carries minimal risk. Invoking an upgradeable DeFi protocol requires verifying the upgrade authority's governance structure and timelock parameters. The most dangerous pattern is a program that blindly forwards user-provided instruction data to an arbitrary program ID, effectively acting as a proxy for the user's signature. Chainscore Labs' security reviews for composable protocols focus on these trust boundaries, ensuring that CPI flows are explicitly authorized, re-entrancy is structurally prevented, and the blast radius of a compromised dependency is contained.
Affected Actors
Protocol Developers
Program authors who expose instructions for cross-program invocation must treat every incoming CPI as a potential re-entrancy vector. The primary risk is state inconsistency when an external program calls back into your program before the first invocation completes.
Required actions:
- Apply a strict checks-effects-interactions pattern: perform all account validation and state mutations before invoking any external program via CPI.
- Implement re-entrancy mutexes using a dedicated account or PDA flag to block recursive calls into sensitive instructions.
- Never trust that a CPI caller has performed its own validation. Your program must independently verify all account ownership, signer privileges, and data invariants on every entry point.
- Audit all upgradeable program dependencies. A dependency that upgrades can silently introduce malicious CPI behavior into your protocol.
Chainscore Labs can review your program's CPI surface for re-entrancy vulnerabilities and privilege escalation paths before mainnet deployment.
Key Security Patterns and Anti-Patterns
Cross-Program Invocation is Solana's primary composability primitive but also its most common attack surface. These patterns and anti-patterns define safe CPI design for production programs.
Unknown Program Invocation Risk
Invoking a program passed as an account argument is a critical anti-pattern unless you explicitly verify its Program ID against a known allowlist. An attacker can supply a malicious program that mimics expected behavior while draining accounts. If your program must support pluggable callees, require governance-approved program registries stored on-chain and validate the callee's ID against that registry. Never trust a user-supplied program ID without cryptographic verification.
Upgradeable Program Trust Assumptions
When you CPI into an upgradeable program, you inherit all future code changes that program's upgrade authority may deploy. A previously safe callee can become malicious after an upgrade. Mitigate this by: (1) verifying the callee's upgrade authority is a trusted multisig or governance, (2) checking the program's ProgramData account to confirm immutability if required, and (3) architecting your program to minimize damage even if a callee becomes hostile. Consider forking critical dependencies into immutable programs.
Account Mismatch During CPI
When you CPI into another program, you pass accounts from your context. The callee may interpret account roles differently than you expect. Always verify that the accounts you're forwarding match the callee's expected schema—especially writable accounts. A common exploit: a program passes a vault PDA as a writable account to a callee that treats it as a fee receiver, draining funds. Use Anchor's #[account(constraint = ...)] or manual checks to validate account roles before forwarding.
CPI Risk Matrix
Evaluates the primary security, operational, and integration risks introduced by cross-program invocations, mapping failure modes to affected actors and recommended actions.
| Risk Area | Failure Mode | Severity | Affected Actors | Mitigation and Action |
|---|---|---|---|---|
Re-entrancy via CPI Loops | A malicious or buggy callee program re-invokes the caller program before the first invocation completes, violating state consistency assumptions. | Critical | Protocol developers, security auditors, DeFi protocols | Strictly apply a checks-effects-interactions pattern. Use a re-entrancy guard mutex flag on the account state. Audit CPI call graphs for circular invocation paths. |
Privilege Escalation via Signer Seeds | A program invokes a CPI with PDA seeds that match the caller's authority, allowing the callee to perform privileged actions (e.g., token transfers) as the caller. | Critical | Wallet engineers, custody providers, protocol developers | Validate the signer seeds and program ID of all invoked programs. Never trust a callee to act on behalf of the caller without explicit, validated PDA authorization. Review |
Invoking an Unverified Upgradeable Program | A CPI target is an upgradeable program whose code can be changed unilaterally by a deployer, introducing malicious logic after the caller's audit. | High | Protocol developers, DeFi protocols, governance delegates | Verify the upgrade authority of all CPI targets. Prefer immutable programs for critical security dependencies. Monitor upgrade authorities via on-chain governance or multi-sig. Chainscore Labs can perform a dependency upgrade-risk review. |
Account Data Deserialization Mismatch | The caller and callee programs use different serialization formats or schema versions for the same account, leading to corrupted or misinterpreted state. | High | Protocol architects, DeFi teams, auditors | Establish a strict schema versioning policy for shared accounts. Use a discriminator prefix on all account data. Validate the account owner and data length before deserialization in the callee. |
Arbitrary CPI Destination Injection | A user-supplied program ID is used as the CPI destination without validation, allowing an attacker to redirect execution to a malicious program. | High | Smart contract developers, wallet engineers | Never accept a program ID as a user-provided argument for CPI. Maintain a hardcoded or verified on-chain registry of allowed program IDs. Validate the destination program's owner. |
Lamport Drain via Malicious Callee | A callee program exploits its writable access to the caller's accounts to drain lamports or manipulate token balances beyond the intended scope. | Critical | Custody providers, DeFi protocols, wallet teams | Minimize the number of accounts passed with |
Compute Unit Exhaustion in CPI | A callee program consumes an unexpectedly large number of compute units, causing the entire transaction to fail and potentially locking user funds or state. | Medium | Infrastructure teams, DeFi protocol developers | Set a reasonable |
Developer Security Checklist
A practical checklist for developers building or auditing programs that use Cross-Program Invocation (CPI). Each item identifies a specific risk vector, explains why it matters in Solana's atomic execution model, and provides the signal or artifact that confirms readiness.
What to check: For every program you invoke via CPI, confirm the program owner (BPFLoaderUpgradeable vs. BPFLoader) and whether an upgrade authority exists.
Why it matters: Invoking an upgradeable program means its logic can change without notice. A trusted program today could introduce malicious behavior tomorrow if its upgrade key is compromised or controlled by a hostile party.
Confirmation signal: Your deployment documentation or audit report includes a pinned list of target program IDs with their loader type and upgrade authority status. If the authority is not burned or controlled by a reputable multi-sig, document the trust assumption explicitly.
Source Resources
Use these resources to verify CPI execution semantics, signer propagation, PDA signing, and common Solana program security failure modes before shipping composable programs.
Looking to build on a specific blockchain?
We build smart contracts, DeFi applications, wallets, tokenization platforms, and blockchain infrastructure across the major ecosystems teams choose today. That includes Ethereum, Arbitrum, Optimism, Polygon, Avalanche, Solana, Sui, Aptos, Hedera, Stellar, and NEAR, with support for additional EVM and non-EVM networks based on your product requirements.
EVM ecosystems
- Ethereum
- Arbitrum
- Optimism
- Polygon
- Avalanche
- Cronos

Non-EVM ecosystems
- Solana
- Sui
- Aptos
- Hedera
- Stellar
- NEAR
Additional ecosystems
- Polkadot
- Cosmos
- TON
- Cardano
- Algorand
- Tempo
Also available for Base, appchains, custom EVM networks, and cross-chain product architecture.
Frequently Asked Questions
Common questions from protocol developers and auditors about Cross-Program Invocation safety, re-entrancy, privilege management, and composability risks on Solana.
No. While Solana's runtime processes instructions sequentially within a transaction, self-re-entrancy (a program calling itself) and circular re-entrancy (Program A → Program B → Program A) are both possible. The runtime does not enforce a re-entrancy guard by default.
What to check:
- Does your program invoke itself via CPI after modifying account state?
- Does your program invoke an external program that could call back into your program?
- Are you relying on the runtime to block re-entrant calls? (It won't.)
Mitigation: Implement an explicit re-entrancy guard using a boolean flag in a transient account or the instruction data. Anchor's #[account(init_if_needed)] pattern does not protect against re-entrancy. Teams should verify their guard covers all CPI exit points.
Delivering blockchain solutions for 5+ years.
We have partnered with 50+ leading DeFi protocols, NFT ecosystems, and fintech innovators to build secure, scalable, and capital-efficient blockchain products.
Selected Partners & Clients
“I've been working with Chainscore Labs for last 3+ years, they've consistently delivered with strong ownership across multiple projects. The team is reliable and detail-oriented.”
How to get started?
If you're looking for blockchain integration, ChainScore Labs has 5+ years of experience helping teams build and integrate exchanges, wallets, smart contracts, tokenization solutions, and protocol-connected products, we can help you choose the right path, integrate securely, and get to production faster. Our team consists of experienced blockchain developers and architects who can help you with your blockchain integration needs.
Exploration & Strategy
Define your product goals and choose the right blockchain architecture for your use case.
Architecture & Design
Design the smart contracts, tokenomics, and security parameters of your system.
Development & Integration
Build and integrate with wallets, oracles, and front-end dApps for a seamless experience.
Security & Launch
Comprehensive audits followed by a risk-managed mainnet deployment to protect your users.
Discover our
blockchain development services.
We build production-grade blockchain solutions for top-tier projects across DeFi and Web3.
Need a blockchain engineering team?
Send the project context and we will respond with next steps, scope questions, and a practical path to delivery.


