Skip to content

Commit

Permalink
Apply wasmd-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
iKapitonau committed Aug 29, 2024
1 parent 71fa2ad commit 2c9f1c7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

# 1.14.0

- Fix CWA-2024-005
- Bump ledger-cosmos-go from v0.12.2 to v0.12.4

# 1.13.0

- Support DCAP attestation
Expand Down
15 changes: 15 additions & 0 deletions x/compute/internal/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,21 @@ func (h SDKMessageHandler) handleSdkMessage(ctx sdk.Context, msg sdk.Msg) (*sdk.
return nil, sdkerrors.ErrUnknownRequest.Wrapf("can't route message %+v", msg)
}

// callDepthMessageHandler is a wrapper around a Messenger that checks the call depth before dispatching a message.
type callDepthMessageHandler struct {
Messenger
MaxCallDepth uint32
}

func (h callDepthMessageHandler) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg v1wasmTypes.CosmosMsg) (events []sdk.Event, data [][]byte, err error) {
ctx, err = checkAndIncreaseCallDepth(ctx, h.MaxCallDepth)
if err != nil {
return nil, nil, err
}

return h.Messenger.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg)
}

// convertWasmIBCTimeoutHeightToCosmosHeight converts a wasm type ibc timeout height to ibc module type height
func convertWasmIBCTimeoutHeightToCosmosHeight(ibcTimeoutBlock *v1wasmTypes.IBCTimeoutBlock) ibcclienttypes.Height {
if ibcTimeoutBlock == nil {
Expand Down
23 changes: 23 additions & 0 deletions x/compute/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Keeper struct {
messenger Messenger
// queryGasLimit is the max wasm gas that can be spent on executing a query with a contract
queryGasLimit uint64
maxCallDepth uint32
HomeDir string
// authZPolicy AuthorizationPolicy
// paramSpace subspace.Subspace
Expand Down Expand Up @@ -149,9 +150,13 @@ func NewKeeper(
cdc,
),
queryGasLimit: wasmConfig.SmartQueryGasLimit,
maxCallDepth: types.DefaultMaxCallDepth,
HomeDir: homeDir,
LastMsgManager: lastMsgManager,
}
// always wrap the messenger, even if it was replaced by an option
keeper.messenger = callDepthMessageHandler{keeper.messenger, keeper.maxCallDepth}

keeper.queryPlugins = DefaultQueryPlugins(govKeeper, distKeeper, mintKeeper, bankKeeper, stakingKeeper, queryRouter, &keeper, channelKeeper).Merge(customPlugins)

return keeper
Expand Down Expand Up @@ -812,6 +817,24 @@ func (k Keeper) querySmartImpl(ctx sdk.Context, contractAddress sdk.AccAddress,
return queryResult, nil
}

func checkAndIncreaseCallDepth(ctx sdk.Context, maxCallDepth uint32) (sdk.Context, error) {
var callDepth uint32
if size, ok := types.CallDepth(ctx); ok {
callDepth = size
}

// increase
callDepth++

// did we go too far?
if callDepth > maxCallDepth {
return sdk.Context{}, types.ErrExceedMaxCallDepth
}

// set updated stack size
return types.WithCallDepth(ctx, callDepth), nil
}

// We don't use this function since we have an encrypted state. It's here for upstream compatibility
// QueryRaw returns the contract's state for give key. For a `nil` key a empty slice result is returned.
func (k Keeper) QueryRaw(ctx sdk.Context, contractAddress sdk.AccAddress, key []byte) []types.Model {
Expand Down
16 changes: 16 additions & 0 deletions x/compute/internal/types/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const contextKeyCallDepth contextKey = iota

func WithCallDepth(ctx sdk.Context, counter uint32) sdk.Context {
return ctx.WithValue(contextKeyCallDepth, counter)
}

func CallDepth(ctx sdk.Context) (uint32, bool) {
val, ok := ctx.Value(contextKeyCallDepth).(uint32)
return val, ok
}
3 changes: 3 additions & 0 deletions x/compute/internal/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ var (

// ErrMaxIBCChannels error for maximum number of ibc channels reached
ErrMaxIBCChannels = errors.Register(DefaultCodespace, 22, "max transfer channels")

// ErrExceedMaxCallDepth error if max message stack size is exceeded
ErrExceedMaxCallDepth = errors.Register(DefaultCodespace, 30, "max call depth exceeded")
)

func IsEncryptedErrorCode(code uint32) bool {
Expand Down
1 change: 1 addition & 0 deletions x/compute/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

const (
DefaultMaxCallDepth = uint32(500)
defaultLRUCacheSize = uint64(0)
defaultEnclaveLRUCacheSize = uint16(100)
defaultQueryGasLimit = uint64(10_000_000)
Expand Down

0 comments on commit 2c9f1c7

Please sign in to comment.