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.
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
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
action1needs permission at first, i.e.permissioned_action=true- the administrator grants
alicePermission::Limitedpermission andLimited.uses = 3. - alice used up 3 times,
Limited.uses = 0 - the administrator adjusts the
action1permissions configuration to not require permissions, i.e.permissioned_action=false - at this point
alicewants to executeaction1, butis_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
Tool used
Manual Review
Recommendation
only permissioned_action==true, then execute permission.consume_use().
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
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
- An action initially requires permission.
- The administrator grants alice a limited permission with three uses.
- Alice uses the permission three times, leaving zero uses.
- The administrator changes the action to not require permission.
- 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.