Skip to content

[SEC] Implement Reentrancy Guard for Payout Execution #18

Description

@Queenode

🔴 Priority: Critical
Difficulty: Hard
Estimated Effort: 2-3 days
Relevant Files: contracts/src/lib.rs, contracts/src/test.rs
Labels: security, priority:critical, soroban

Requirements

  1. Reentrancy Vulnerability Remediation

    • The current payout() function updates state (HasReceivedPayout) after executing the token transfer:
      token_client.transfer(&env.current_contract_address(), &recipient, &pool_size);
      env.storage().persistent().set(&DataKey::HasReceivedPayout(recipient.clone()), &true);
    • While Soroban currently limits reentrancy via standard cross-contract calls, future protocol upgrades or specific token implementations (like custom wrapped tokens) might introduce reentrancy vectors.
    • Refactor payout() to strictly follow the Checks-Effects-Interactions (CEI) pattern.
  2. State Update Relocation

    • Move the set(&DataKey::HasReceivedPayout, &true) state update to happen before token_client.transfer().
    • Ensure that the TTL extensions also occur before the external token transfer call.
  3. Reentrancy Guard Mutex (Optional but Recommended)

    • Implement a simple lock in instance storage: DataKey::IsExecutingPayout.
    • Set it to true at the start of payout(), and clear it at the end.
    • Assert !IsExecutingPayout at the beginning of both contribute() and payout().
  4. Testing

    • Unit test: Verify the CEI pattern is implemented (the state changes even if the transfer fails in a mock).
    • Write a mock malicious token contract that attempts to call payout() again during the transfer() callback.
    • Verify that the malicious reentrant call panics with "Already received payout" or "Reentrancy detected".
    • Target: 100% coverage on the payout function.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third Campaign

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions