Skip to content

Commit 5cdb17d

Browse files
author
Jason Yellick
committed
[FAB-2616] Fix potential crash in cauthdsl
https://jira.hyperledger.org/browse/FAB-2616 There was an outstanding FIXME in the cauthdsl code which ignored an error. In the case that the supplied identity could not be deserialized, the cuathdsl would dereference a null pointer and crash. This CR fixes this issue and moves some mock code to the test. Change-Id: Ied5daa4d6f6f9961c8617a282590d3d79317f407 Signed-off-by: Jason Yellick <[email protected]>
1 parent 30a0e21 commit 5cdb17d

File tree

2 files changed

+84
-81
lines changed

2 files changed

+84
-81
lines changed

common/cauthdsl/cauthdsl.go

+7-81
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ package cauthdsl
1919
import (
2020
"fmt"
2121

22-
"bytes"
23-
2422
"github.com/hyperledger/fabric/msp"
2523
cb "github.com/hyperledger/fabric/protos/common"
2624
"github.com/op/go-logging"
27-
"github.com/syndtr/goleveldb/leveldb/errors"
2825
)
2926

3027
var cauthdslLogger = logging.MustGetLogger("cauthdsl")
@@ -73,13 +70,16 @@ func compile(policy *cb.SignaturePolicy, identities []*cb.MSPPrincipal, deserial
7370
if used[i] {
7471
continue
7572
}
76-
// FIXME: what should I do with the error below?
77-
identity, _ := deserializer.DeserializeIdentity(sd.Identity)
78-
err := identity.SatisfiesPrincipal(signedByID)
73+
identity, err := deserializer.DeserializeIdentity(sd.Identity)
74+
if err != nil {
75+
cauthdslLogger.Errorf("Principal deserialization failed: (%s) for identity %v", err, sd.Identity)
76+
continue
77+
}
78+
err = identity.SatisfiesPrincipal(signedByID)
7979
if err == nil {
8080
err := identity.Verify(sd.Data, sd.Signature)
8181
if err == nil {
82-
cauthdslLogger.Debugf("Principal evaluation succeeds: (%s)", t, used)
82+
cauthdslLogger.Debugf("Principal evaluation succeeds: (%s) (used %s)", t, used)
8383
used[i] = true
8484
return true
8585
}
@@ -92,77 +92,3 @@ func compile(policy *cb.SignaturePolicy, identities []*cb.MSPPrincipal, deserial
9292
return nil, fmt.Errorf("Unknown type: %T:%v", t, t)
9393
}
9494
}
95-
96-
// FIXME: remove the code below as soon as we can use MSP from the policy manager code
97-
var invalidSignature = []byte("badsigned")
98-
99-
type mockIdentity struct {
100-
idBytes []byte
101-
}
102-
103-
func (id *mockIdentity) SatisfiesPrincipal(p *cb.MSPPrincipal) error {
104-
if bytes.Compare(id.idBytes, p.Principal) == 0 {
105-
return nil
106-
} else {
107-
return errors.New("Principals do not match")
108-
}
109-
}
110-
111-
func (id *mockIdentity) GetIdentifier() *msp.IdentityIdentifier {
112-
return &msp.IdentityIdentifier{Mspid: "Mock", Id: "Bob"}
113-
}
114-
115-
func (id *mockIdentity) GetMSPIdentifier() string {
116-
return "Mock"
117-
}
118-
119-
func (id *mockIdentity) Validate() error {
120-
return nil
121-
}
122-
123-
func (id *mockIdentity) GetOrganizationalUnits() []string {
124-
return []string{"dunno"}
125-
}
126-
127-
func (id *mockIdentity) Verify(msg []byte, sig []byte) error {
128-
if bytes.Compare(sig, invalidSignature) == 0 {
129-
return errors.New("Invalid signature")
130-
} else {
131-
return nil
132-
}
133-
}
134-
135-
func (id *mockIdentity) VerifyOpts(msg []byte, sig []byte, opts msp.SignatureOpts) error {
136-
return nil
137-
}
138-
139-
func (id *mockIdentity) VerifyAttributes(proof []byte, spec *msp.AttributeProofSpec) error {
140-
return nil
141-
}
142-
143-
func (id *mockIdentity) Serialize() ([]byte, error) {
144-
return id.idBytes, nil
145-
}
146-
147-
func toSignedData(data [][]byte, identities [][]byte, signatures [][]byte) ([]*cb.SignedData, []bool) {
148-
signedData := make([]*cb.SignedData, len(data))
149-
for i := range signedData {
150-
signedData[i] = &cb.SignedData{
151-
Data: data[i],
152-
Identity: identities[i],
153-
Signature: signatures[i],
154-
}
155-
}
156-
return signedData, make([]bool, len(signedData))
157-
}
158-
159-
type mockDeserializer struct {
160-
}
161-
162-
func NewMockDeserializer() msp.IdentityDeserializer {
163-
return &mockDeserializer{}
164-
}
165-
166-
func (md *mockDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
167-
return &mockIdentity{idBytes: serializedIdentity}, nil
168-
}

common/cauthdsl/cauthdsl_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,89 @@ limitations under the License.
1717
package cauthdsl
1818

1919
import (
20+
"bytes"
21+
"errors"
2022
"testing"
2123

24+
"github.com/hyperledger/fabric/msp"
25+
2226
"github.com/golang/protobuf/proto"
2327
cb "github.com/hyperledger/fabric/protos/common"
2428
)
2529

30+
var invalidSignature = []byte("badsigned")
31+
32+
type mockIdentity struct {
33+
idBytes []byte
34+
}
35+
36+
func (id *mockIdentity) SatisfiesPrincipal(p *cb.MSPPrincipal) error {
37+
if bytes.Compare(id.idBytes, p.Principal) == 0 {
38+
return nil
39+
} else {
40+
return errors.New("Principals do not match")
41+
}
42+
}
43+
44+
func (id *mockIdentity) GetIdentifier() *msp.IdentityIdentifier {
45+
return &msp.IdentityIdentifier{Mspid: "Mock", Id: "Bob"}
46+
}
47+
48+
func (id *mockIdentity) GetMSPIdentifier() string {
49+
return "Mock"
50+
}
51+
52+
func (id *mockIdentity) Validate() error {
53+
return nil
54+
}
55+
56+
func (id *mockIdentity) GetOrganizationalUnits() []string {
57+
return []string{"dunno"}
58+
}
59+
60+
func (id *mockIdentity) Verify(msg []byte, sig []byte) error {
61+
if bytes.Compare(sig, invalidSignature) == 0 {
62+
return errors.New("Invalid signature")
63+
} else {
64+
return nil
65+
}
66+
}
67+
68+
func (id *mockIdentity) VerifyOpts(msg []byte, sig []byte, opts msp.SignatureOpts) error {
69+
return nil
70+
}
71+
72+
func (id *mockIdentity) VerifyAttributes(proof []byte, spec *msp.AttributeProofSpec) error {
73+
return nil
74+
}
75+
76+
func (id *mockIdentity) Serialize() ([]byte, error) {
77+
return id.idBytes, nil
78+
}
79+
80+
func toSignedData(data [][]byte, identities [][]byte, signatures [][]byte) ([]*cb.SignedData, []bool) {
81+
signedData := make([]*cb.SignedData, len(data))
82+
for i := range signedData {
83+
signedData[i] = &cb.SignedData{
84+
Data: data[i],
85+
Identity: identities[i],
86+
Signature: signatures[i],
87+
}
88+
}
89+
return signedData, make([]bool, len(signedData))
90+
}
91+
92+
type mockDeserializer struct {
93+
}
94+
95+
func NewMockDeserializer() msp.IdentityDeserializer {
96+
return &mockDeserializer{}
97+
}
98+
99+
func (md *mockDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
100+
return &mockIdentity{idBytes: serializedIdentity}, nil
101+
}
102+
26103
var validSignature = []byte("signed")
27104
var signers = [][]byte{[]byte("signer0"), []byte("signer1")}
28105
var msgs = [][]byte{nil, nil}

0 commit comments

Comments
 (0)