Skip to content

Commit 23c6fcd

Browse files
Elias Naurkaralabe
authored andcommitted
mobile: don't retain transient []byte in CallMsg.SetData (#14804)
* mobile: don't retain transient []byte in CallMsg.SetData Go mobile doesn't copy []byte parameters, for performance and to allow writes to the byte array be reflected in the native byte array. Unfortunately, that means []byte arguments are only valid during the call it is being passed into. CallMsg.SetData retains such a byte array. Copy it instead Fixes #14675 * mobile: copy all []byte arguments from gomobile To avoid subtle errors when accidentially retaining an otherwise transient byte slice coming from gomobile, copy all byte slices before use. * mobile: replace copySlice with common.CopyBytes
1 parent cf5d4b5 commit 23c6fcd

7 files changed

Lines changed: 21 additions & 17 deletions

File tree

‎mobile/accounts.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/ethereum/go-ethereum/accounts"
2727
"github.com/ethereum/go-ethereum/accounts/keystore"
28+
"github.com/ethereum/go-ethereum/common"
2829
"github.com/ethereum/go-ethereum/crypto"
2930
)
3031

@@ -111,7 +112,7 @@ func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error {
111112
// SignHash calculates a ECDSA signature for the given hash. The produced signature
112113
// is in the [R || S || V] format where V is 0 or 1.
113114
func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) {
114-
return ks.keystore.SignHash(accounts.Account{Address: address.address}, hash)
115+
return ks.keystore.SignHash(accounts.Account{Address: address.address}, common.CopyBytes(hash))
115116
}
116117

117118
// SignTx signs the given transaction with the requested account.
@@ -130,7 +131,7 @@ func (ks *KeyStore) SignTx(account *Account, tx *Transaction, chainID *BigInt) (
130131
// be decrypted with the given passphrase. The produced signature is in the
131132
// [R || S || V] format where V is 0 or 1.
132133
func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) {
133-
return ks.keystore.SignHashWithPassphrase(account.account, passphrase, hash)
134+
return ks.keystore.SignHashWithPassphrase(account.account, passphrase, common.CopyBytes(hash))
134135
}
135136

136137
// SignTxPassphrase signs the transaction if the private key matching the
@@ -189,7 +190,7 @@ func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string
189190

190191
// ImportKey stores the given encrypted JSON key into the key directory.
191192
func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) {
192-
acc, err := ks.keystore.Import(keyJSON, passphrase, newPassphrase)
193+
acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase)
193194
if err != nil {
194195
return nil, err
195196
}
@@ -198,7 +199,7 @@ func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string)
198199

199200
// ImportECDSAKey stores the given encrypted JSON key into the key directory.
200201
func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Account, _ error) {
201-
privkey, err := crypto.ToECDSA(key)
202+
privkey, err := crypto.ToECDSA(common.CopyBytes(key))
202203
if err != nil {
203204
return nil, err
204205
}
@@ -212,7 +213,7 @@ func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Acco
212213
// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
213214
// a key file in the key directory. The key file is encrypted with the same passphrase.
214215
func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) {
215-
account, err := ks.keystore.ImportPreSaleKey(keyJSON, passphrase)
216+
account, err := ks.keystore.ImportPreSaleKey(common.CopyBytes(keyJSON), passphrase)
216217
if err != nil {
217218
return nil, err
218219
}

‎mobile/big.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package geth
2121
import (
2222
"errors"
2323
"math/big"
24+
25+
"github.com/ethereum/go-ethereum/common"
2426
)
2527

2628
// A BigInt represents a signed multi-precision integer.
@@ -52,7 +54,7 @@ func (bi *BigInt) GetInt64() int64 {
5254
// SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets
5355
// the big int to that value.
5456
func (bi *BigInt) SetBytes(buf []byte) {
55-
bi.bigint.SetBytes(buf)
57+
bi.bigint.SetBytes(common.CopyBytes(buf))
5658
}
5759

5860
// SetInt64 sets the big int to x.

‎mobile/bind.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client
119119
if err != nil {
120120
return nil, err
121121
}
122-
addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, bytecode, client.client, args.objects...)
122+
addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
123123
if err != nil {
124124
return nil, err
125125
}

‎mobile/common.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Hash struct {
3535
// NewHashFromBytes converts a slice of bytes to a hash value.
3636
func NewHashFromBytes(binary []byte) (hash *Hash, _ error) {
3737
h := new(Hash)
38-
if err := h.SetBytes(binary); err != nil {
38+
if err := h.SetBytes(common.CopyBytes(binary)); err != nil {
3939
return nil, err
4040
}
4141
return h, nil
@@ -136,7 +136,7 @@ type Address struct {
136136
// NewAddressFromBytes converts a slice of bytes to a hash value.
137137
func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
138138
a := new(Address)
139-
if err := a.SetBytes(binary); err != nil {
139+
if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
140140
return nil, err
141141
}
142142
return a, nil

‎mobile/ethereum.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (msg *CallMsg) SetFrom(address *Address) { msg.msg.From = address.address
6464
func (msg *CallMsg) SetGas(gas int64) { msg.msg.Gas = big.NewInt(gas) }
6565
func (msg *CallMsg) SetGasPrice(price *BigInt) { msg.msg.GasPrice = price.bigint }
6666
func (msg *CallMsg) SetValue(value *BigInt) { msg.msg.Value = value.bigint }
67-
func (msg *CallMsg) SetData(data []byte) { msg.msg.Data = data }
67+
func (msg *CallMsg) SetData(data []byte) { msg.msg.Data = common.CopyBytes(data) }
6868
func (msg *CallMsg) SetTo(address *Address) {
6969
if address == nil {
7070
msg.msg.To = nil

‎mobile/interface.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (i *Interface) SetBool(b bool) { i.object = &b }
4646
func (i *Interface) SetBools(bs []bool) { i.object = &bs }
4747
func (i *Interface) SetString(str string) { i.object = &str }
4848
func (i *Interface) SetStrings(strs *Strings) { i.object = &strs.strs }
49-
func (i *Interface) SetBinary(binary []byte) { i.object = &binary }
49+
func (i *Interface) SetBinary(binary []byte) { b := common.CopyBytes(binary); i.object = &b }
5050
func (i *Interface) SetBinaries(binaries [][]byte) { i.object = &binaries }
5151
func (i *Interface) SetAddress(address *Address) { i.object = &address.address }
5252
func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses }

‎mobile/types.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"errors"
2424
"fmt"
2525

26+
"github.com/ethereum/go-ethereum/common"
2627
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/rlp"
2829
)
@@ -68,7 +69,7 @@ func NewHeaderFromRLP(data []byte) (*Header, error) {
6869
h := &Header{
6970
header: new(types.Header),
7071
}
71-
if err := rlp.DecodeBytes(data, h.header); err != nil {
72+
if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
7273
return nil, err
7374
}
7475
return h, nil
@@ -145,7 +146,7 @@ func NewBlockFromRLP(data []byte) (*Block, error) {
145146
b := &Block{
146147
block: new(types.Block),
147148
}
148-
if err := rlp.DecodeBytes(data, b.block); err != nil {
149+
if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
149150
return nil, err
150151
}
151152
return b, nil
@@ -212,15 +213,15 @@ type Transaction struct {
212213

213214
// NewTransaction creates a new transaction with the given properties.
214215
func NewTransaction(nonce int64, to *Address, amount, gasLimit, gasPrice *BigInt, data []byte) *Transaction {
215-
return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, gasLimit.bigint, gasPrice.bigint, data)}
216+
return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, gasLimit.bigint, gasPrice.bigint, common.CopyBytes(data))}
216217
}
217218

218219
// NewTransactionFromRLP parses a transaction from an RLP data dump.
219220
func NewTransactionFromRLP(data []byte) (*Transaction, error) {
220221
tx := &Transaction{
221222
tx: new(types.Transaction),
222223
}
223-
if err := rlp.DecodeBytes(data, tx.tx); err != nil {
224+
if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
224225
return nil, err
225226
}
226227
return tx, nil
@@ -285,7 +286,7 @@ func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Tra
285286
if chainID != nil {
286287
signer = types.NewEIP155Signer(chainID.bigint)
287288
}
288-
rawTx, err := tx.tx.WithSignature(signer, sig)
289+
rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
289290
return &Transaction{rawTx}, err
290291
}
291292

@@ -315,7 +316,7 @@ func NewReceiptFromRLP(data []byte) (*Receipt, error) {
315316
r := &Receipt{
316317
receipt: new(types.Receipt),
317318
}
318-
if err := rlp.DecodeBytes(data, r.receipt); err != nil {
319+
if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
319320
return nil, err
320321
}
321322
return r, nil

0 commit comments

Comments
 (0)