diff --git a/accounts/usbwallet/ledger.go b/accounts/usbwallet/ledger.go index b54cde016a..caaed99e4a 100644 --- a/accounts/usbwallet/ledger.go +++ b/accounts/usbwallet/ledger.go @@ -512,6 +512,12 @@ func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHas // APDU length | 1 byte // Optional APDU data | arbitrary func (w *ledgerDriver) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 ledgerParam2, data []byte) ([]byte, error) { + // max safe length check + const maxDataLength = 128 * 1024 * 1024 // 128 MB + if len(data) > maxDataLength { + return nil, errors.New("data too large") + } + // Construct the message payload, possibly split into multiple chunks apdu := make([]byte, 2, 7+len(data)) diff --git a/trie/encoding.go b/trie/encoding.go index 8d98f67464..cb54ad2a29 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -16,6 +16,8 @@ package trie +import "math" + // Trie keys are dealt with in three distinct encodings: // // KEYBYTES encoding contains the actual key and nothing else. This encoding is the @@ -104,6 +106,9 @@ func compactToHex(compact []byte) []byte { } func keybytesToHex(str []byte) []byte { + if len(str) > math.MaxInt/2 { + panic("input too large") + } l := len(str)*2 + 1 var nibbles = make([]byte, l)