Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3 #3

Open
Edward-Lo opened this issue Sep 26, 2020 · 1 comment
Open

Issue 3 #3

Edward-Lo opened this issue Sep 26, 2020 · 1 comment

Comments

@Edward-Lo
Copy link
Collaborator

Description

There is a lack of sanity check in the Upgrade() method of FlamingoSwapPairContract which might lead to unwanted behaviour.

public static byte[] Upgrade(byte[] newScript, byte[] paramList, byte returnType, ContractPropertyState cps, string name, string version, string author, string email, string description)
{
    Assert(Runtime.CheckWitness(GetAdmin()), "upgrade: CheckWitness failed!");

    var me = ExecutionEngine.ExecutingScriptHash;
    byte[] newContractHash = Hash160(newScript);

    var r = ReservePair;
    SafeTransfer(Token0, me, newContractHash, r.Reserve0);
    SafeTransfer(Token1, me, newContractHash, r.Reserve1);

    Contract newContract = Contract.Migrate(newScript, paramList, returnType, cps, name, version, author, email, description);

    Runtime.Notify("upgrade", ExecutionEngine.ExecutingScriptHash, newContractHash);
    return newContractHash;
}

The Upgrade function first transfers the tokens to the new contract, then calls Contract.Migrate() to upgrade the contract. Contract.Migrate() migrates everything in the persistent storage of the current contract to the new contract when executed. For Migrate() method, it will only transfer the contract storages when the target contract has not been deployed yet.

Specifically, one can frontrun the deployment of the new contract so the migration won’t transfer the storages to the new contract. Though what an attacker can do still depends on the new contract, this might not be the operator’s expectation.

Recommendation

Check whether the contract already exists before calling Contract.Migrate(). And transfer the tokens after the contract migration has succeeded.

@Ashuaidehao
Copy link
Contributor

Updated,thanks for your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants