verify_origin() previous_sender may be forged
The AMP packet verification in Andromeda's ADO contracts did not check that the previous_sender field matched the actual sender when a user submitted an AMPReceive message directly. An attacker could forge previous_sender to impersonate a privileged address, potentially bypassing permission checks such as the minter restriction in the CW721 contract.
Source: https://github.com/sherlock-audit/2024-05-andromeda-ado-judging/issues/45
Found by
bin2chen, g
Summary
in AMPPkt.verify_origin() does not verify the legitimacy of the previous_sender and can be specified at will, leading to security risks.
Vulnerability Detail
The execute() method of most current ado's can handle two types of ExecuteMsg
ExecuteMsg::AMPReceive or other Msg
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
let ctx = ExecuteContext::new(deps, info, env);
match msg {
ExecuteMsg::AMPReceive(pkt) => {
@> ADOContract::default().execute_amp_receive(ctx, pkt, handle_execute)
}
_ => handle_execute(ctx, msg),
}
}
If the request is for an AMPReceive it checks the legality of the AMPCtx at execute_amp_receive().
execute_amp_receive()->verify_origin()
pub fn verify_origin(&self, info: &MessageInfo, deps: &Deps) -> Result<(), ContractError> {
let kernel_address = ADOContract::default().get_kernel_address(deps.storage)?;
@> if info.sender == self.ctx.origin || info.sender == kernel_address {
Ok(())
} else {
let adodb_address: Addr =
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: kernel_address.to_string(),
msg: to_json_binary(&KernelQueryMsg::KeyAddress {
key: ADO_DB_KEY.to_string(),
})?,
}))?;
// Get the sender's Code ID
let contract_info: ContractInfoResponse =
deps.querier
.query(&QueryRequest::Wasm(WasmQuery::ContractInfo {
contract_addr: info.sender.to_string(),
}))?;
let sender_code_id = contract_info.code_id;
// We query the ADO type in the adodb, it will return an error if the sender's Code ID doesn't exist.
AOSQuerier::verify_code_id(&deps.querier, &adodb_address, sender_code_id)
}
}
The main task is to check the legitimacy of AMPCtx.origin and AMPCtx.previous_sender
There are three cases
- sender == kernel_address -> pass (trusted by default, not malicious)
- sender == ADO type in the adodb -> pass (trusted by default, not malicious)
- sender == user (user submits AMPReceive directly) -> check `AMPCtx.origin == sender
In the third case, only AMPCtx.origin == user is checked and there is no restriction on AMPCtx.previous_sender == user.
So the user can submit ExecuteMsg::AMPReceive and specify previous_sender as they wish.
Impact
If AMPCtx.previous_sender can be specified arbitrarily, security checks that depend on it will have security implications
Example: ExecuteContext.contains_sender()
pub fn contains_sender(&self, addr: &str) -> bool {
if self.info.sender == addr {
return true;
}
match &self.amp_ctx {
None => false,
@> Some(ctx) => ctx.ctx.get_origin() == addr || ctx.ctx.get_previous_sender() == addr,
}
}
The one that currently has the ability to determine permissions using this method is andromeda-cw721.
fn execute_mint(
ctx: ExecuteContext,
token_id: String,
token_uri: Option<String>,
owner: String,
extension: TokenExtension,
) -> Result<Response, ContractError> {
let minter = ANDR_MINTER
.load(ctx.deps.storage)?
.get_raw_address(&ctx.deps.as_ref())?;
ensure!(
@> ctx.contains_sender(minter.as_str())
| is_context_permissioned_strict(
ctx.deps.storage,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
MINT_ACTION
)?,
ContractError::Unauthorized {}
);
Code Snippet
Tool used
Manual Review
Recommendation
pub fn verify_origin(&self, info: &MessageInfo, deps: &Deps) -> Result<(), ContractError> {
let kernel_address = ADOContract::default().get_kernel_address(deps.storage)?;
- if info.sender == self.ctx.origin || info.sender == kernel_address {
+ if (info.sender == self.ctx.origin && info.sender == self.ctx.previous_sender) || info.sender == kernel_address {
Ok(())
} else {
let adodb_address: Addr =
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: kernel_address.to_string(),
msg: to_json_binary(&KernelQueryMsg::KeyAddress {
key: ADO_DB_KEY.to_string(),
})?,
}))?;
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/andromedaprotocol/andromeda-core/pull/552
bin2chen66
fix-reviews note:
https://github.com/andromedaprotocol/andromeda-core/pull/552
This PR adds judgmentinfo.sender == self.ctx.origin && info.sender == self.ctx.previous_sender
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 verify_origin function only checked that the origin matched the sender, but did not verify that previous_sender also matched, allowing it to be set arbitrarily.
Impact
If previous_sender can be set arbitrarily, security checks that rely on it, such as contains_sender used in the CW721 mint function, could be bypassed, potentially allowing unauthorised minting.
What the attacker needed
Attacker can submit an AMPReceive message; target contract uses contains_sender for permission
The fix
The protocol team fixed this issue by requiring that info.sender equals both origin and previous_sender when the sender is a user.
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.
Smart contracts on some blockchains can call each other, and they pass messages that include a 'previous sender' field. This report found that a user could set that field to any value they liked, so a contract that trusted it might think the message came from someone important. That could let the user do things they should not be allowed to do, like mint tokens.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.