Events emitted by L2 bridge hooks are discarded
The L2 deposit function executes bridge hook messages but discards the events they emit, so applications and bots never see them. In the worst case, a withdrawal initiated through a hook burns tokens on L2 but its event is lost, meaning the withdrawal is never bridged to L1 and the funds are permanently locked.
Originally disclosed on Code4rena
Submitted by RadiantLabs
Finding description and impact
The L1 deposit function allows a depositor to send a signed payload to be executed atomically with the deposited tokens, via bridge hooks.
These are executed in L2 by routing messages to the default message handler from the app’s router:
File: deposit.go
54: for _, msg := range tx.GetMsgs() {
55: handler := k.router.Handler(msg)
56: if handler == nil {
57: reason = fmt.Sprintf("Unrecognized Msg type: %s", sdk.MsgTypeURL(msg))
58: return
59: }
60:
61: _, err = handler(cacheCtx, msg)
62: if err != nil {
63: reason = fmt.Sprintf("Failed to execute Msg: %s", err)
64: return
65: }
66: }
From the above code, we can see that the sdk.Result returned by the handler() call is discarded. This is incorrect because the returned sdk.Result is where the events emitted by handler are stored, because in the handler call the EventManager passed with cacheCtx is immediately discarded:
File: ../../../../../go/pkg/mod/github.com/cosmos/[email protected]/baseapp/msg_service_router.go
171: msr.routes[requestTypeName] = func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
172: ctx = ctx.WithEventManager(sdk.NewEventManager())
Also by comparison, we can see that another place that similarly handles messages directly (the MsgExecuteMessages handler), does copy events over to the parent context:
File: opchild/keeper/msg_server.go
126: res, err = handler(cacheCtx, msg)
127: if err != nil {
128: return nil, err
129: }
130:
131: events = append(events, res.GetEvents()...)
132: }
133:
134: writeCache()
135:
136: // TODO - merge events of MsgExecuteMessages itself
137: sdkCtx.EventManager().EmitEvents(events)
As a consequence, events emitted by L2 hooks are discarded and not propagated to the transactions, and cannot be picked up by apps, and most importantly, bots.
Proof of Concept
In the below PoC we show the worst case scenario, where a withdrawal is initiated through hooks, and because the EventTypeInitiateTokenWithdrawal event is what bridges tokens to L1, tokens are permanently locked in the bridge.
This is a reasonable scenario where a user could, for example, bridge from L1 gas tokens that are necessary to initiate in L2 a withdrawal of a different token.
The test can be run if added to the OPinit/x/opchild/keeper/msg_server_test.go test file.
func Test_MsgServer_Deposit_HookNoEvents(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(&input.OPChildKeeper)
bz := sha3.Sum256([]byte("test_token"))
denom := "l2/" + hex.EncodeToString(bz[:])
require.Equal(t, math.ZeroInt(), input.BankKeeper.GetBalance(ctx, addrs[1], denom).Amount)
// empty deposit to create account
priv, _, addr := keyPubAddr()
msg := types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addr.String(), sdk.NewCoin(denom, math.ZeroInt()), 1, 1, "test_token", nil)
_, err := ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)
// create hook data
acc := input.AccountKeeper.GetAccount(ctx, addr)
require.NotNil(t, acc)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv}, []uint64{acc.GetAccountNumber()}, []uint64{0}
from, _ := input.AccountKeeper.AddressCodec().BytesToString(addr)
to, _ := input.AccountKeeper.AddressCodec().BytesToString(addrs[2])
signedTxBz, err := input.EncodingConfig.TxConfig.TxEncoder()(generateTestTx(
t, input,
[]sdk.Msg{
types.NewMsgInitiateTokenWithdrawal(from, to, sdk.NewCoin(denom, math.NewInt(50))),
},
privs, accNums, accSeqs, sdk.UnwrapSDKContext(ctx).ChainID(),
))
require.NoError(t, err)
// valid deposit
ctx = sdk.UnwrapSDKContext(ctx).WithEventManager(sdk.NewEventManager())
msg = types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addr.String(), sdk.NewCoin(denom, math.NewInt(100)), 2, 1, "test_token", signedTxBz)
_, err = ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)
withdrawalEventFound := false
for _, event := range sdk.UnwrapSDKContext(ctx).EventManager().Events() {
if event.Type == types.EventTypeInitiateTokenWithdrawal {
withdrawalEventFound = true
}
}
// but no event was emitted by hooks
require.False(t, withdrawalEventFound)
}
Recommended Mitigation Steps
Consider adding manual emission of generated events, as done for MsgExecuteMessages.
beer-1 (Initia) confirmed, but disagreed with severity and commented:
Seems the problem exists and events are dropped. We will do patch to emit the event. However, would like to lower severity, as events are not that critical part (not consensus) and we can re-index the chain after fix.
Mitigation here.
Downgrading to medium, but given that the impact is limited to events not being emitted, I can also see this as a low and am open to reasoned arguments during Post Judging QA.
For this codebase, lost events mean lost funds, because L2 events, and in particular the one proven to be lost in the PoC, are the one vector that bridges funds to L1.
In other words, a lost event means tokens burned in L2 and not ported to L1
->permanently lost funds. We believe High is appropriate.
- On L2, user initiates a withdrawal by sending the
MsgInitiateTokenWithdrawalmessage whose handling logic is here- The handling logic burns the L2 tokens and emits an
InitiateTokenWithdrawalevent, and that’s where the message handling stops- The events are picked up by a bot and update the L2 state with the withdrawal
- Funds can be rescued on L1 via the
MsgFinalizeTokenWithdrawalmessage that grant them after verifying the withdrawal from L2 state.If the event emitted at point 2 is lost, point 3 is not executed, and point 4 can never be executed because the withdrawal is not found in the L2 state tree, so funds are lost.
I agree with this assessment, but these are side effects that require a bunch of things to line up. The attack vector is not direct. Per the supreme court decision, the report does show “broader level impacts”. However, they are medium in nature and fairly unlikely in practice (which is why I said I could be potentially talked into low risk).
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 deposit handler calls the message handler but ignores the returned result, so the events collected in the handler's temporary event manager are never copied back to the parent context.
Impact
Events from L2 hooks are lost, so apps and bots cannot react; if a withdrawal is started through a hook, its event is missing, the withdrawal is never bridged to L1, and the tokens are permanently locked.
What the attacker needed
A deposit that carries a signed hook transaction; the hook starts a withdrawal
The fix
The project patched it to emit the generated events, as is already done for the other message-execution handler.
How it was found
- Create an account on L2 with an empty deposit.
- Build a signed transaction containing a withdrawal message to use as hook data.
- Call the deposit function with that signed transaction as the hook.
- Check the context's event manager for a withdrawal event and observe that none is present.
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.
When a deposit is made on one chain, it can carry a small program (a hook) that runs on the other chain. That program may do something important, like start a withdrawal, and it announces what it did by writing an 'event' to a log. Here the code ran the program but threw away its log, so the system that watches logs never learned the withdrawal happened, and the money got stuck.
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.