From 546f1f5db6b73f4e021ae3eddc53e8a7e7ff0074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 10 Oct 2020 13:06:02 +0200 Subject: [PATCH] ledgerwallet: Validate signing bytes --- chain/wallet/ledger/ledger.go | 21 +++++++++++++++++++-- cmd/lotus-wallet/main.go | 3 +-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/chain/wallet/ledger/ledger.go b/chain/wallet/ledger/ledger.go index 36b0fb0402a..71ec067c8a1 100644 --- a/chain/wallet/ledger/ledger.go +++ b/chain/wallet/ledger/ledger.go @@ -1,9 +1,12 @@ package ledgerwallet import ( + "bytes" "context" "encoding/json" "fmt" + "github.com/ipfs/go-cid" + "golang.org/x/xerrors" "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/crypto" @@ -27,7 +30,7 @@ type LedgerKeyInfo struct { Path []uint32 } -var _ (api.WalletAPI) = (*LedgerWallet)(nil) +var _ api.WalletAPI = (*LedgerWallet)(nil) func (lw LedgerWallet) WalletSign(ctx context.Context, signer address.Address, toSign []byte, meta api.MsgMeta) (*crypto.Signature, error) { ki, err := lw.getKeyInfo(signer) @@ -44,7 +47,21 @@ func (lw LedgerWallet) WalletSign(ctx context.Context, signer address.Address, t return nil, fmt.Errorf("ledger can only sign chain messages") } - // TODO: assert meta matches the 'toSign' bits + { + var cmsg types.Message + if err := cmsg.UnmarshalCBOR(bytes.NewReader(meta.Extra)); err != nil { + return nil, xerrors.Errorf("unmarshalling message: %w", err) + } + + _, bc, err := cid.CidFromBytes(toSign) + if err != nil { + return nil, xerrors.Errorf("getting cid from signing bytes: %w", err) + } + + if !cmsg.Cid().Equals(bc) { + return nil, xerrors.Errorf("cid(meta.Extra).bytes() != toSign") + } + } sig, err := fl.SignSECP256K1(ki.Path, meta.Extra) if err != nil { diff --git a/cmd/lotus-wallet/main.go b/cmd/lotus-wallet/main.go index 2d87998a415..3814bc7b391 100644 --- a/cmd/lotus-wallet/main.go +++ b/cmd/lotus-wallet/main.go @@ -63,7 +63,7 @@ var runCmd = &cli.Command{ Value: "0.0.0.0:1777", }, &cli.BoolFlag{ - Name: "ledger", + Name: "ledger", Usage: "use a ledger device instead of an on-disk wallet", }, }, @@ -115,7 +115,6 @@ var runCmd = &cli.Command{ w = ledgerwallet.NewWallet(ds) } - address := cctx.String("listen") mux := mux.NewRouter()