Skip to content
Business logic Audit finding Intermediate

is_permissioned() may underflow

A permission check function decrements a limited-use permission even when the action no longer requires permission, causing an underflow if the uses are already zero. An attacker with a limited permission could be blocked from executing an action after the administrator changes the action's permission requirement.

Kow Follow with bin2chen and g

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-andromeda-ado-judging/issues/46

Found by

Kow, bin2chen, g

Summary

is_permissioned(), if permissioned_action is changed from true to false. Users who were previously set to Permission::Limited may be underflowed.

Vulnerability Detail

is_permissioned() is used to implement the permission check, which is implemented as follows

Rust
    pub fn is_permissioned(
...
        let permission = Self::get_permission(store, action_string.clone(), actor_string.clone())?;
@>      let permissioned_action = self
            .permissioned_actions
            .may_load(store, action_string.clone())?
            .unwrap_or(false);
        match permission {
            Some(mut permission) => {
                ensure!(
                    permission.is_permissioned(&env, permissioned_action),
                    ContractError::Unauthorized {}
                );

                // Consume a use for a limited permission
@>              if let Permission::Limited { .. } = permission {
@>                  permission.consume_use()?;
                    permissions().save(
                        store,
                        (action_string.clone() + actor_string.as_str()).as_str(),
                        &PermissionInfo {
                            action: action_string,
                            actor: actor_string,
                            permission,
                        },
                    )?;
                }

                Ok(())
            }

From the above code, we know that if the user has Permission::Limited , it will be reduced by 1 regardless of whether the action needs permission or not. This permission.consume_use() can be underflow in the following cases

  1. action1 needs permission at first, i.e. permissioned_action=true
  2. the administrator grants alice Permission::Limited permission and Limited.uses = 3.
  3. alice used up 3 times, Limited.uses = 0
  4. the administrator adjusts the action1 permissions configuration to not require permissions, i.e. permissioned_action=false
  5. at this point alice wants to execute action1, but is_permissioned(Alice,action1) will revert,

because permission.consume_use() will be executed, resulting in underflow (Limited.uses ==0 ,Limited.uses-=1)

Impact

is_permissioned() may underflow, causing the permission check to fail and the corresponding action to can't be executed

Code Snippet

https://github.com/sherlock-audit/2024-05-andromeda-ado/blob/main/andromeda-core/packages/std/src/ado_contract/permissioning.rs#L71

Tool used

Manual Review

Recommendation

only permissioned_action==true, then execute permission.consume_use().

Diff
    pub fn is_permissioned(
...
            Some(mut permission) => {
                ensure!(
                    permission.is_permissioned(&env, permissioned_action),
                    ContractError::Unauthorized {}
                );

                // Consume a use for a limited permission
+          if permissioned_action {
                if let Permission::Limited { .. } = permission {
                    permission.consume_use();
                    permissions().save(
                        store,
                        (action_string.clone() + actor_string.as_str()).as_str(),
                        &PermissionInfo {
                            action: action_string,
                            actor: actor_string,
                            permission,
                        },
                    )?;
                }
+           }
                Ok(())
            }

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/andromedaprotocol/andromeda-core/pull/553

bin2chen66

fix-reviews note: https://github.com/sherlock-audit/2024-05-andromeda-ado-judging/issues/46 PR changed so that permission.consume_use() is executed only if permissioned_action==true. Also changed so that consume_use() no longer returns an error. (Secured by local_permission.is_permissioned().) Fixed this issue

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

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 permission check unconditionally consumes a use for limited permissions, even when the action is not permissioned.

Impact

The permission check may underflow, causing the action to fail and preventing the user from executing it.

What the attacker needed

A limited permission with zero uses; administrator changes action to not require permission

The fix

The issue was fixed by only consuming a use when the action is permissioned.

How it was found

  1. An action initially requires permission.
  2. The administrator grants alice a limited permission with three uses.
  3. Alice uses the permission three times, leaving zero uses.
  4. The administrator changes the action to not require permission.
  5. Alice tries to execute the action, but the permission check reverts due to underflow.

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 gives users a limited number of times they can perform an action. The code that checks permission always subtracts one use, even when the action no longer needs permission. If the user has already used all their tries, subtracting one more causes an error and blocks them.

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 Kow

View all

8 more disclosed reports, best first.

More from Andromeda – Validator Staking ADO and Vesting ADO

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.