Rust Fund

AI First Flight #9
Beginner FriendlyRust
EXP
View results
Submission Details
Severity: medium
Valid

The unset dealine_set flag allows the creator to replace the campaign deadline indefinitely

The unset dealine_set flag allows the creator to replace the campaign deadline indefinitely

Summary

set_deadline() checks a one-time-set flag but never flips it. The creator can repeatedly move the deadline, including extending it after contributors deposited or after the original deadline elapsed.

Vulnerability Details

fund_create() initializes dealine_set to false. set_deadline() rejects only when that flag is true, but updates only deadline:

pub fn set_deadline(ctx: Context<FundSetDeadline>, deadline: u64) -> Result<()> {
let fund = &mut ctx.accounts.fund;
if fund.dealine_set {
return Err(ErrorCode::DeadlineAlreadySet.into());
}
fund.deadline = deadline;
Ok(())
}

No instruction ever sets dealine_set = true, making the DeadlineAlreadySet error unreachable.

Because the deadline controls both when contributions stop and when refunds become eligible, a creator can change the campaign terms after users have deposited. In particular, moving an elapsed deadline back into the future prevents contributors from calling refund() until the replacement deadline.

Proof of Concept

Add the following test to the Anchor test suite:

it("allows the creator to replace a supposedly immutable deadline", async () => {
const creator = anchor.web3.Keypair.generate();
const signature = await provider.connection.requestAirdrop(
creator.publicKey,
2 * anchor.web3.LAMPORTS_PER_SOL,
);
await provider.connection.confirmTransaction(signature, "confirmed");
const name = "mutable-deadline";
const [fund] = anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from(name), creator.publicKey.toBuffer()],
program.programId,
);
const first = new anchor.BN(Math.floor(Date.now() / 1000) + 3_600);
const second = new anchor.BN(Math.floor(Date.now() / 1000) + 7_200);
await program.methods
.fundCreate(
name,
"PoC",
new anchor.BN(anchor.web3.LAMPORTS_PER_SOL),
)
.accountsStrict({
fund,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.signers([creator])
.rpc();
await program.methods
.setDeadline(first)
.accountsStrict({ fund, creator: creator.publicKey })
.signers([creator])
.rpc();
await program.methods
.setDeadline(second)
.accountsStrict({ fund, creator: creator.publicKey })
.signers([creator])
.rpc();
const state = await program.account.fund.fetch(fund);
expect(state.deadline.eq(second)).to.equal(true);
expect(state.dealineSet).to.equal(false);
});

The test passes, proving that the second deadline replaces the first while the guard remains false:

✔ allows the creator to replace a supposedly immutable deadline

Impact

The creator can unilaterally change the condition governing when contributions stop and refunds become available. Once contribution accounting is corrected, extending the deadline delays every contributor's refund and temporarily freezes their SOL. It also violates the fixed campaign terms contributors relied upon before depositing.

The current zero-refund bug is a separate root cause that already prevents nonzero refunds. Both issues must be fixed for the advertised refund mechanism to work.

Tools Used

  • Manual source review

  • Anchor 0.30.1

  • solana-test-validator 1.18.17

  • Bun and ts-mocha

Recommended Mitigation

Validate that the supplied deadline is in the future and activate the one-time guard in the same instruction:

let now: u64 = Clock::get()?.unix_timestamp
.try_into()
.map_err(|_| ErrorCode::CalculationOverflow)?;
require!(!fund.dealine_set, ErrorCode::DeadlineAlreadySet);
require!(deadline > now, ErrorCode::InvalidDeadline);
fund.deadline = deadline;
fund.dealine_set = true;
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 3 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-02] The set_deadline function does not set the dealine_set flag to true

The `set_deadline()` function in the `rustfund` program contains a vulnerability that allows campaign creators to manipulate deadlines indefinitely. While the function correctly checks if `fund.dealine_set` is true before allowing the deadline to be changed, it never sets this flag to true after setting the deadline. ```rust pub fn set_deadline(ctx: Context<FundSetDeadline>, deadline: u64) -> Result<()> { let fund = &mut ctx.accounts.fund; if fund.dealine_set { return Err(ErrorCode::DeadlineAlreadySet.into()); } fund.deadline = deadline; Ok(()) } ``` The function is missing a crucial line to update the flag: `fund.dealine_set = true;` This oversight bypasses a key safeguard intended to prevent creators from manipulating deadlines after they've been set. According to the project documentation, this flag is meant to enforce deadline immutability, which is an essential part of the platform's trust model. ### Impact 1. **Refund evasion**: Creators can prevent users from obtaining refunds by continually extending the deadline whenever it approaches. This directly undermines the project's advertised "Refund Mechanism" which promises that "Contributors can get refunds if deadlines are reached and goals aren't met." 2. **Fund locking**: Contributors' funds can be effectively locked indefinitely, as the refund function is contingent upon the deadline being reached: ```rust if ctx.accounts.fund.deadline != 0 && ctx.accounts.fund.deadline > Clock::get().unwrap().unix_timestamp.try_into().unwrap() { return Err(ErrorCode::DeadlineNotReached.into()); } ``` ### Proof of Concept (PoC) The following test demonstrates how a creator can set the deadline multiple times, effectively bypassing the intended deadline immutability: ```javascript import * as anchor from "@coral-xyz/anchor"; import { Program } from "@coral-xyz/anchor"; import { Rustfund } from "../target/types/rustfund"; import { assert } from "chai"; describe("VULN-02: set_deadline vulnerability", () => { // Configures the provider to use the local cluster const provider = anchor.AnchorProvider.env(); anchor.setProvider(provider); const program = anchor.workspace.Rustfund as Program<Rustfund>; // Test variables const fundName = "TestFund"; const description = "Testing deadline vulnerability"; const goal = new anchor.BN(1000000); let fundPda: anchor.web3.PublicKey; it("Allows you to modify the deadline several times", async () => { // Derivation of PDA address for financing account [fundPda] = await anchor.web3.PublicKey.findProgramAddress( [Buffer.from(fundName), provider.wallet.publicKey.toBuffer()], program.programId ); // Fund creation await program.rpc.fundCreate(fundName, description, goal, { accounts: { fund: fundPda, creator: provider.wallet.publicKey, systemProgram: anchor.web3.SystemProgram.programId, }, }); // First deadline assignment const deadline1 = new anchor.BN(Math.floor(Date.now() / 1000) + 3600); // 1 hour in the future await program.rpc.setDeadline(deadline1, { accounts: { fund: fundPda, creator: provider.wallet.publicKey, }, }); // Second deadline assignment (which should not be possible if the flag is set to true) const deadline2 = new anchor.BN(Math.floor(Date.now() / 1000) + 7200); // 2 hours into the future await program.rpc.setDeadline(deadline2, { accounts: { fund: fundPda, creator: provider.wallet.publicKey, }, }); // Check that the deadline has been updated to the second value const fundAccount = await program.account.fund.fetch(fundPda); assert.ok( fundAccount.deadline.eq(deadline2), "The deadline may have been modified several times, but vulnerability presents" ); }); }); ``` Save the above test as, for example, tests/02.ts in your project's test directory and run the test : ```Solidity anchor test ``` ### Concrete Impact Example To illustrate the real-world impact of this vulnerability, consider this scenario: - A creator launches a campaign to fund a project with a goal of 100 SOL - The creator sets an initial deadline of 30 days - Contributors collectively deposit 80 SOL (below the goal) - As the deadline approaches, the creator realizes they won't reach the goal - Instead of allowing refunds as promised, the creator extends the deadline by another 30 days - This pattern can repeat indefinitely, effectively locking contributor funds - Even if contributors try to request refunds, they'll be rejected with "DeadlineNotReached" errors ### Recommendation The fix for this vulnerability is straightforward. The `set_deadline()` function should be modified to set the `dealine_set` flag to true after setting the deadline: ```rust pub fn set_deadline(ctx: Context<FundSetDeadline>, deadline: u64) -> Result<()> { let fund = &mut ctx.accounts.fund; if fund.dealine_set { return Err(ErrorCode::DeadlineAlreadySet.into()); } fund.deadline = deadline; fund.dealine_set = true; // Add this line to fix the vulnerability Ok(()) } ```

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!