Skip to content
Business logic Audit finding Beginner

Incorrect use of u64 for arg amount in withdrawtolandlord can cause withdrawal failure

The withdrawtolandlord function uses a u64 for the token amount, which can only hold about 18 tokens with 18 decimals. If a landlord's owed amount exceeds this, they cannot withdraw in one call and must split into many transactions.

nnez Follow

Originally disclosed on Code4rena

Submitted by nnez

https://github.com/code-423n4/2024-10-coded-estate/blob/main/contracts/codedestate/src/execute.rs#L1786-L1796

https://github.com/code-423n4/2024-10-coded-estate/blob/main/contracts/codedestate/src/msg.rs#L156-L162

Impact

The use of u64 for token amount in the withdrawtolandlord function can lead to failed withdrawals when handling tokens with 18 decimals, limiting the landlord’s ability to withdraw their entitled funds if the amount exceeds the maximum value of u64.

Proof-of-Concept

In the withdrawtolandlord function, the token amount is defined as a u64 value. However, this can cause issues when handling tokens with 18 decimals, as the u64 data type can only store values up to approximately 1.8446744e+19 ~18 token of token with 18 decimals. This limit is significantly lower than what is supported by u128, which is used in other parts of the contract to handle token amount, such as info.funds[0].amount is a U128 type.

File: contracts/codedestate/src/execute.rs
pub fn withdrawtolandlord(
    &self,
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    token_id: String,
    tenant: String,
    renting_period: Vec<String>,
    amount:u64,
    address:String
) -> Result<Response<C>, ContractError> { ... snipped ...}

File: contracts/codedestate/src/msg.rs
pub enum ExecuteMsg<T, E> {
    ...snipped...
    ...
    WithdrawToLandlord {
        token_id: String,
        tenant: String,
        renting_period: Vec<String>,
        amount: u64,
        address: String,
    },
    ...snipped...
    ...

This discrepancy between the data types can create an issue. If the token amount owed to the landlord exceeds the maximum value supported by u64, the landlord will not be able to withdraw their entitled funds through the withdrawtolandlord function.

This is problematic as the Nibiru chain, the chain on which Coded Estate is deployed, supports custom denominated tokens, and users can specify tokens with 18 decimals as their payment currency. For example, if a large payment is made in such a token, the landlord would be unable to withdraw the full amount due to the limitations of the u64 type.

See here.

Example Scenario:

  1. A user pays deposit using a token with 18 decimals.
  2. The total deposit amount exceeds the maximum value of u64 (~18 tokens for 18-decimal tokens).
  3. When the landlord tries to withdraw their funds via the withdrawtolandlord function, the function fails because the u64 type cannot accommodate the large token amount.
  4. As a result, the landlord is unable to withdraw their funds, leading to loss of access to legitimate payments.

Recommended Mitigation

Change type of amount to u128 for consistency with other parts in the system.

Assessed type

Context

Lambda (judge) commented:

Does not seem to be a significant problem to me at first sight, if such a scenario would ever happen, withdrawal should be possible with multiple calls.

blockchainstar12 (Coded Estate) acknowledged

Lambda (judge) decreased severity to Low and commented:

Unlike #29, this does not impact the functionality of the protocol significantly. While a larger data type could still be a good idea here, the owner can still withdraw funds by splitting up the withdrawals into multiple calls.

nnez (warden) commented:

@Lambda - I might have overstated the impact in the report (unable to withdraw funds). However, I still believe that this issue should be classified as Medium severity. This bug does impact the protocol’s functionality.

Consider the scenario wherein the required deposit is 5_000e18 tokens. In this scenario, the token owner would have to split their transaction into 5_000e18 / (2^64-1) = 271.05 → 272 separate transactions in order to withdraw all the funds.

That’s a lot of transactions and this is just for one long-term rental. An individual token owner’s can have more than one property and they can have more than one active long-term rental with deposit to withdraw.

Instead of paying gas for one transaction, users unnecessarily have to pay 200x+ more of gas in order to withdraw the full amount.

Additionally, 5_000e18 is just an arbitrary reasonable number for a 18 decimals token worth $1; the problem could get worse with a larger amount of tokens. For example, 50_000e18 of $0.1 would take 2711 transactions to withdraw the full amount.

Lambda (judge) increased severity to Medium and commented:

That’s true, $5,000 is a reasonable amount for such a protocol to handle. Potentially even low, with business apartments in cities like Zurich that often cost $5,000 per month, so you could easily have $30,000 for a longer rental. 18 decimal stable coins are also very common.

So it is not that unlikely that a landlord would have to perform ~1,632 calls for one withdrawal. On the one hand, this would be of course very cumbersome (especially if the UI did not support this), but it can also become pretty expensive (if one call were roughly $1, this would be an almost 5% fee on top). So based on that, Medium is indeed more appropriate.


The original report as published on Code4rena, by nnez. 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 amount parameter in withdrawtolandlord is typed as u64 while other parts of the contract use u128 for token amounts, so values above the u64 maximum cannot be passed.

Impact

Landlords cannot withdraw large amounts in a single call and must split withdrawals into many transactions, which is cumbersome and costly; the judge noted the owner can still withdraw by splitting into multiple calls.

What the attacker needed

Token with 18 decimals used as payment; owed amount exceeds u64 maximum

How it was found

  1. A user pays a deposit using a token with 18 decimals.
  2. The total deposit amount exceeds the maximum value of u64.
  3. The landlord tries to withdraw funds via withdrawtolandlord.
  4. The function fails because u64 cannot hold the large amount.

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.

A smart contract stores numbers in fixed-size types. A u64 can only hold up to about 18.4 quintillion, which sounds large but is only about 18 tokens if each token has 18 decimal places. So if a landlord is owed more than that, the withdrawal function cannot accept the full amount and the landlord must make many smaller withdrawals.

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 nnez

View all

32 more disclosed reports, best first.

More from Coded Estate Invitational

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.