GHSA-mjfq-3qr2-6g84: Github.com/cosmos/evm Cosmos EVM Allows Partial Precompile Vulnerability

HIGH
CWE-94
Published
Advisory Summary

GHSA-mjfq-3qr2-6g84 is a HIGH cosmos evm allows partial precompile vulnerability (CWE-94: Improper Control of Generation of Code ('Code Injection')) affecting Github.com/cosmos/evm libraries in the Go ecosystem. The issue involves cosmos evm allows partial precompile state writes. Remediation is available in the latest vendor maintenance releases.

GHSA-mjfq-3qr2-6g84 at a Glance

CVE Identifier N/A
GHSA Identifier GHSA-mjfq-3qr2-6g84
Severity HIGH
CVSS v3.1 Base Score N/A
CWE Classification CWE-94: Improper Control of Generation of Code ('Code Injection')
Affected Product Github.com/cosmos/evm
Package Ecosystem Go
Published Date
Last Updated

How to Fix GHSA-mjfq-3qr2-6g84

To remediate this vulnerability, upgrade all affected Github.com/cosmos/evm dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

github.com/cosmos/evm fixed: Latest secure release
Upgrade github.com/cosmos/evm to Latest secure release
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for github.com/cosmos/evm

How to Check if You Are Affected

To determine whether your application is affected by GHSA-mjfq-3qr2-6g84, inspect your project dependencies for Github.com/cosmos/evm:

inspect dependencies for github.com/cosmos/evm

Analyze the terminal output to locate any version of Github.com/cosmos/evm in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than the patched release), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-94: Improper Control of Generation of Code ('Code Injection'), where the product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment. In Github.com/cosmos/evm, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Impact Setting lower EVM call gas allows users to partially execute precompiles and error at specific points in the precompile code without reverting the partially written state. If executed on the distribution precompile when claiming funds, it could cause funds to be transferred to a user without resetting the claimable rewards to 0. The vulnerability could also be used to cause indeterministic execution by failing at other points in the code, halting validators. Any evmOS or Cosmos EVM chain using precompiles is affected. ### Patches The vulnerability was patched by wrapping each precompile execution into an atomic function that reverts any partially committed state on error. - [evmos/os](https://github.com/evmos/os) patch file: https://drive.google.com/file/d/1LfC0WSrQOqwTOW3qfaE6t8Jqf1PLVtS_/ For chains using a different file structure, you must manually apply the diff: ### **In `x/evm/statedb.go`:** Add the following function: ```go func (s *StateDB) RevertMultiStore(cms storetypes.CacheMultiStore, events sdk.Events) { s.cacheCtx = s.cacheCtx.WithMultiStore(cms) s.writeCache = func() { // rollback the events to the ones // on the snapshot s.ctx.EventManager().EmitEvents(events) cms.Write() } } ``` ### **In `x/evm/statedb/journal.go`:** Replace the `Revert` function with the following: ```go func (pc precompileCallChange) Revert(s *StateDB) { // rollback multi store from cache ctx to the previous // state stored in the snapshot s.RevertMultiStore(pc.multiStore, pc.events) } ``` ### **In `precompiles/common/precompile.go`:** Change the function signature in `HandleGasError` to: ```go func HandleGasError(ctx sdk.Context, contract *vm.Contract, initialGas storetypes.Gas, err *error, stateDB *statedb.StateDB, snapshot snapshot) func() { ... } ``` In the `HandleGasError` function, add the following line in the switch statement in the `case storetypes.ErrorOutOfGas:` case: ```go stateDB.RevertMultiStore(snapshot.MultiStore, snapshot.Events) ``` Add the following function: ```go // RunAtomic is used within the Run function of each Precompile implementation. // It handles rolling back to the provided snapshot if an error is returned from the core precompile logic. // Note: This is only required for stateful precompiles. func (p Precompile) RunAtomic(s snapshot, stateDB *statedb.StateDB, fn func() ([]byte, error)) ([]byte, error) { bz, err := fn() if err != nil { // revert to snapshot on error stateDB.RevertMultiStore(s.MultiStore, s.Events) } return bz, err } ``` ### **All Precompiles:** Finally, in each precompile, locate the `Run` function, and wrap each switch statement and return values into `p.RunAtomic`. For example: ```go // Run executes the precompiled contract IBC transfer methods defined in the ABI. func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error) { ctx, stateDB, snapshot, method, initialGas, args, err := p.RunSetup(evm, contract, readOnly, p.IsTransaction) if err != nil { return nil, err } // This handles any out of gas errors that may occur during the execution of a precompile tx or query. // It avoids panics and returns the out of gas error so the EVM can continue gracefully. defer cmn.HandleGasError(ctx, contract, initialGas, &err, stateDB, snapshot)() // === WRAP HERE === return p.RunAtomic(snapshot, stateDB, func() ([]byte, error) { switch method.Name { // TODO Approval transactions => need cosmos-sdk v0.46 & ibc-go v6.2.0 // Authorization Methods: case exampleCase: bz, err = p.example(ctx, evm.Origin, stateDB, method, args) default: return nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name) } if err != nil { return nil, err } cost := ctx.GasMeter().GasConsumed() - initialGas if !contract.UseGas(cost) { return nil, vm.ErrOutOfGas } if err := p.AddJournalEntries(stateDB, snapshot); err != nil { return nil, err } return bz, nil }) } ``` ### Workarounds There are no workarounds for chains that make use of precompiles. A coordinated upgrade is necessary to patch the issue. ### Testing A test was introduced in the distribution precompile to ensure that partial state writes no longer occur when a lower gas amount is set.

GHSA-mjfq-3qr2-6g84 Affected Github.com/cosmos/evm Versions

1 component(s)
Product / Package Ecosystem Affected Versions Fixed Version
github.com/cosmos/evm
Go
0.1.0
All 1 versions
0.1.0
None specified

References

Frequently Asked Questions

What is GHSA-mjfq-3qr2-6g84?
GHSA-mjfq-3qr2-6g84 is a security vulnerability in Github.com/cosmos/evm involving Cosmos EVM Allows Partial Precompile State Writes under CWE-94: Improper Control of Generation of Code ('Code Injection').
Which Github.com/cosmos/evm versions are affected?
Affected versions include releases prior to current release patches across affected packages: github.com/cosmos/evm.
What version fixes GHSA-mjfq-3qr2-6g84?
Upgrading to current release patches (or the latest patched release for your respective branch) resolves the vulnerability.
Is github.com/cosmos/evm affected?
Yes, github.com/cosmos/evm is affected in releases prior to Latest secure release. Upgrade your dependency to Latest secure release or later.
What CWE is associated with GHSA-mjfq-3qr2-6g84?
GHSA-mjfq-3qr2-6g84 is classified under CWE-94: Improper Control of Generation of Code ('Code Injection') (The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.).
How can I check Go dependencies for GHSA-mjfq-3qr2-6g84?
Run the dependency tree command: 'inspect dependencies for github.com/cosmos/evm'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-mjfq-3qr2-6g84",
  "modified" : "2025-05-14T17:35:54Z",
  "published" : "2025-05-14T17:35:54Z",
  "aliases" : [ ],
  "summary" : "Cosmos EVM Allows Partial Precompile State Writes",
  "details" : "### Impact\nSetting lower EVM call gas allows users to partially execute precompiles and error at specific points in the precompile code without reverting the partially written state. \n\nIf executed on the distribution precompile when claiming funds, it could cause funds to be transferred to a user without resetting the claimable rewards to 0. The vulnerability could also be used to cause indeterministic execution by failing at other points in the code, halting validators.\n\nAny evmOS or Cosmos EVM chain using precompiles is affected.\n\n### Patches\nThe vulnerability was patched by wrapping each precompile execution into an atomic function that reverts any partially committed state on error.\n\n- [evmos/os](https://github.com/evmos/os) patch file: https://drive.google.com/file/d/1LfC0WSrQOqwTOW3qfaE6t8Jqf1PLVtS_/\n\nFor chains using a different file structure, you must manually apply the diff:\n\n### **In `x/evm/statedb.go`:**\n\nAdd the following function:\n```go\nfunc (s *StateDB) RevertMultiStore(cms storetypes.CacheMultiStore, events sdk.Events) {\n\ts.cacheCtx = s.cacheCtx.WithMultiStore(cms)\n\ts.writeCache = func() {\n\t\t// rollback the events to the ones\n\t\t// on the snapshot\n\t\ts.ctx.EventManager().EmitEvents(events)\n\t\tcms.Write()\n\t}\n}\n```\n\n### **In `x/evm/statedb/journal.go`:**\n\nReplace the `Revert` function with the following:\n```go\nfunc (pc precompileCallChange) Revert(s *StateDB) {\n\t// rollback multi store from cache ctx to the previous\n\t// state stored in the snapshot\n\ts.RevertMultiStore(pc.multiStore, pc.events)\n}\n```\n\n### **In `precompiles/common/precompile.go`:**\n\nChange the function signature in `HandleGasError` to:\n```go\nfunc HandleGasError(ctx sdk.Context, contract *vm.Contract, initialGas storetypes.Gas, err *error, stateDB *statedb.StateDB, snapshot snapshot) func() {\n...\n}\n```\n\nIn the `HandleGasError` function, add the following line in the switch statement in the `case storetypes.ErrorOutOfGas:` case:\n```go\nstateDB.RevertMultiStore(snapshot.MultiStore, snapshot.Events)\n```\n\nAdd the following function:\n```go\n// RunAtomic is used within the Run function of each Precompile implementation.\n// It handles rolling back to the provided snapshot if an error is returned from the core precompile logic.\n// Note: This is only required for stateful precompiles.\nfunc (p Precompile) RunAtomic(s snapshot, stateDB *statedb.StateDB, fn func() ([]byte, error)) ([]byte, error) {\n\tbz, err := fn()\n\tif err != nil {\n\t\t// revert to snapshot on error\n\t\tstateDB.RevertMultiStore(s.MultiStore, s.Events)\n\t}\n\treturn bz, err\n}\n```\n\n### **All Precompiles:**\nFinally, in each precompile, locate the `Run` function, and wrap each switch statement and return values into `p.RunAtomic`. For example:\n```go\n// Run executes the precompiled contract IBC transfer methods defined in the ABI.\nfunc (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error) {\n\tctx, stateDB, snapshot, method, initialGas, args, err := p.RunSetup(evm, contract, readOnly, p.IsTransaction)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t// This handles any out of gas errors that may occur during the execution of a precompile tx or query.\n\t// It avoids panics and returns the out of gas error so the EVM can continue gracefully.\n\tdefer cmn.HandleGasError(ctx, contract, initialGas, &err, stateDB, snapshot)()\n\n        // === WRAP HERE ===\n\treturn p.RunAtomic(snapshot, stateDB, func() ([]byte, error) {\n\t\tswitch method.Name {\n\t\t// TODO Approval transactions => need cosmos-sdk v0.46 & ibc-go v6.2.0\n\t\t// Authorization Methods:\n\t\tcase exampleCase:\n\t\t\tbz, err = p.example(ctx, evm.Origin, stateDB, method, args)\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name)\n\t\t}\n\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tcost := ctx.GasMeter().GasConsumed() - initialGas\n\n\t\tif !contract.UseGas(cost) {\n\t\t\treturn nil, vm.ErrOutOfGas\n\t\t}\n\n\t\tif err := p.AddJournalEntries(stateDB, snapshot); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\treturn bz, nil\n\t})\n}\n```\n\n\n### Workarounds\nThere are no workarounds for chains that make use of precompiles. A coordinated upgrade is necessary to patch the issue.\n\n### Testing\nA test was introduced in the distribution precompile to ensure that partial state writes no longer occur when a lower gas amount is set.",
  "severity" : [ {
    "type" : "CVSS_V4",
    "score" : "CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:H/VA:L/SC:N/SI:N/SA:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "Go",
      "name" : "github.com/cosmos/evm"
    },
    "versions" : [ "0.1.0" ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/cosmos/evm/security/advisories/GHSA-mjfq-3qr2-6g84"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/cosmos/evm/commit/0fff8c144b24effbcb3addd666150ba5989d631c"
  }, {
    "type" : "WEB",
    "url" : "https://drive.google.com/file/d/1LfC0WSrQOqwTOW3qfaE6t8Jqf1PLVtS_"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/cosmos/evm"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-94" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2025-05-14T17:35:54Z",
    "nvd_published_at" : null
  }
}