Anchor contest - IllIllI's results

The Benchmark DeFi Yield.

General Information

Platform: Code4rena

Start Date: 24/02/2022

Pot Size: $170,000 UST

Total HM: 15

Participants: 16

Period: 14 days

Judge: Albert Chon

Total Solo HM: 11

Id: 82

League: COSMOS

Anchor

Findings Distribution

Researcher Performance

Rank: 13/16

Findings: 2

Award: $853.53

🌟 Selected for report: 0

🚀 Solo Findings: 0

Findings Information

🌟 Selected for report: hickuphh3

Also found by: 0xliumin, 0xwags, BondiPestControl, IllIllI, WatchPug, broccoli, cccz, cmichel, defsec, gzeon, hubble, robee

Labels

bug
QA (Quality Assurance)

Awards

507.2979 USDC - $507.30

External Links

Lines of code

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-token-contracts/contracts/staking/src/contract.rs#L331-L343

Vulnerability details

Impact

Distribution schedules can have s.0 and s.0 be in the wrong order leading to negative numbers, and thus negative distribution amounts per second

Proof of Concept

    for s in config.distribution_schedule.iter() {
        if s.0 > block_time || s.1 < state.last_distributed {
            continue;
        }

        // min(s.1, block_time) - max(s.0, last_distributed)
        let passed_time =
            std::cmp::min(s.1, block_time) - std::cmp::max(s.0, state.last_distributed);

        let time = s.1 - s.0;
        let distribution_amount_per_second: Decimal = Decimal::from_ratio(s.2, time);
        distributed_amount += distribution_amount_per_second * Uint128::from(passed_time as u128);
    }

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-token-contracts/contracts/staking/src/contract.rs#L331-L343

Tools Used

Code inspection

The vesting schedules don't have this issue because they check the order:

fn assert_vesting_schedules(vesting_schedules: &[(u64, u64, Uint128)]) -> StdResult<()> {
    for vesting_schedule in vesting_schedules.iter() {
        if vesting_schedule.0 >= vesting_schedule.1 {
            return Err(StdError::generic_err(
                "end_time must bigger than start_time",
            ));
        }
    }

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-token-contracts/contracts/vesting/src/contract.rs#L91-L98

so the distribution schedules should have similar checks

#0 - GalloDaSballo

2022-08-06T20:25:58Z

There seems to be no check for schedule.1 > schedule.0 in assert_new_schedules: https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-token-contracts/contracts/staking/src/contract.rs#L420

So assuming admin mistake this seems valid

#1 - albertchon

2022-09-19T02:40:42Z

Downgrading to QA since it relies on botched initialization, which is unlikely

Findings Information

🌟 Selected for report: csanuragjain

Also found by: 0v3rf10w, IllIllI, WatchPug, defsec, gzeon, hickuphh3, robee

Labels

bug
G (Gas Optimization)

Awards

346.2336 USDC - $346.23

External Links

Don't parse and generate strings to get the fractional part of Decimals

fn get_decimals(value: Decimal) -> StdResult<Decimal> { let stringed: &str = &*value.to_string(); let parts: &[&str] = &*stringed.split('.').collect::<Vec<&str>>(); match parts.len() { 1 => Ok(Decimal::zero()), 2 => { let decimals = Decimal::from_str(&*("0.".to_owned() + parts[1]))?; Ok(decimals) } _ => Err(StdError::generic_err("Unexpected number of dots")), } }

https://github.com/code-423n4/2022-02-anchor/blob/main/contracts/anchor-bAsset-contracts/contracts/anchor_basset_reward/src/user.rs#L227-L238 Do what normal rust Decimal does instead: https://docs.rs/rust_decimal/0.7.2/src/rust_decimal/decimal.rs.html#353-357 https://docs.rs/rust_decimal/0.7.2/src/rust_decimal/decimal.rs.html#315-332

Refactor common code

These four blocks do the same creation of increase/decrease messages. They should be refactored to a common function to save on deployment gas https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_token/src/handler.rs#L33-L50 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_token/src/handler.rs#L122-L145 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_token/src/handler.rs#L168-L185 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_token/src/handler.rs#L236-L256

Move most frequently used messages to beginning of match statements

These are some examples where the most frequently matched message isn't checked first. Rearranging should save gas https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bAsset-contracts/contracts/anchor_basset_token/src/contract.rs#L55-L87 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bAsset-contracts/contracts/anchor_basset_reward/src/contract.rs#L51-L61 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bAsset-contracts/contracts/anchor_basset_reward/src/contract.rs#L66-L73 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bAsset-contracts/contracts/anchor_basset_hub/src/contract.rs#L115-L185 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bAsset-contracts/contracts/anchor_basset_hub/src/contract.rs#L435-L447 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_token/src/contract.rs#L50-L83 https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/anchor-bEth-contracts/contracts/anchor_beth_reward/src/contract.rs#L59-L80

#0 - GalloDaSballo

2022-08-04T23:45:35Z

Minor Rust Gas Savings

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter