Details
+ return fmt.Errorf("transaction %d (%s) does not match its raw form (%s)", i,
+ base64.StdEncoding.EncodeToString(expected), base64.StdEncoding.EncodeToString(rawTx))
+ }
+ }
+
+ return nil
+}
diff --git a/protoutil/blockutils_test.go b/protoutil/blockutils_test.go
index b2159da9f..2871483f1 100644
--- a/protoutil/blockutils_test.go
+++ b/protoutil/blockutils_test.go
@@ -489,3 +489,109 @@ func TestBlockSignatureVerifierByCreator(t *testing.T) {
require.Len(t, signatureSet, 1)
require.Equal(t, []byte("creator1"), signatureSet[0].Identity)
}
+
+func TestVerifyTransactionsAreWellFormed(t *testing.T) {
+ originalBlock := &cb.Block{
+ Data: &cb.BlockData{
+ Data: [][]byte{
+ marshalOrPanic(&cb.Envelope{
+ Payload: []byte{1, 2, 3},
+ Signature: []byte{4, 5, 6},
+ }),
+ marshalOrPanic(&cb.Envelope{
+ Payload: []byte{7, 8, 9},
+ Signature: []byte{10, 11, 12},
+ }),
+ },
+ },
+ }
+
+ forgedBlock := proto.Clone(originalBlock).(*cb.Block)
+ tmp := make([]byte, len(forgedBlock.Data.Data[0])+len(forgedBlock.Data.Data[1]))
+ copy(tmp, forgedBlock.Data.Data[0])
+ copy(tmp[len(forgedBlock.Data.Data[0]):], forgedBlock.Data.Data[1])
+ forgedBlock.Data.Data = [][]byte{tmp} // Replace transactions {0,1} with transaction {0 || 1}
+
+ for _, tst := range []struct {
+ name string
+ expectedError string
+ block *cb.Block
+ }{
+ {
+ name: "empty block",
+ },
+ {
+ name: "no block data",
+ block: &cb.Block{},
+ },
+ {
+ name: "no transactions",
+ block: &cb.Block{Data: &cb.BlockData{}},
+ },
+ {
+ name: "single transaction",
+ block: &cb.Block{Data: &cb.BlockData{Data: [][]byte{marshalOrPanic(&cb.Envelope{
+ Payload: []byte{1, 2, 3},
+ Signature: []byte{4, 5, 6},
+ })}}},
+ },
+ {
+ name: "good block",
+ block: originalBlock,
+ },
+ {
+ name: "forged block",
+ block: forgedBlock,
+ expectedError: "transaction 0 has 10 trailing bytes",
+ },
+ {
+ name: "no signature",
+ expectedError: "transaction 0 has no signature",
+ block: &cb.Block{
+ Data: &cb.BlockData{
+ Data: [][]byte{
+ marshalOrPanic(&cb.Envelope{
+ Payload: []byte{1, 2, 3},
+ }),
+ },
+ },
+ },
+ },
+ {
+ name: "no payload",
+ expectedError: "transaction 0 has no payload",
+ block: &cb.Block{
+ Data: &cb.BlockData{
+ Data: [][]byte{
+ marshalOrPanic(&cb.Envelope{
+ Signature: []byte{4, 5, 6},
+ }),
+ },
+ },
+ },
+ },
+ {
+ name: "transaction invalid",
+ expectedError: "transaction 0 is invalid: proto: cannot parse invalid wire-format data",
+ block: &cb.Block{
+ Data: &cb.BlockData{
+ Data: [][]byte{
+ marshalOrPanic(&cb.Envelope{
+ Payload: []byte{1, 2, 3},
+ Signature: []byte{4, 5, 6},
+ })[9:],
+ },
+ },
+ },
+ },
+ } {
+ t.Run(tst.name, func(t *testing.T) {
+ err := protoutil.VerifyTransactionsAreWellFormed(tst.block)
+ if tst.expectedError == "" {
+ require.NoError(t, err)
+ } else {
+ require.EqualError(t, err, tst.expectedError)
+ }
+ })
+ }
+}
The idea is as follows:
When we validate that the block's transactions match the hash in the header, we not only hash the transactions are earlier,
but also ensure that if the transactions in the block are encoded into bytes, they re-create the exact split in the original block: `|$$$$$$$$$$$$|***********|@@@@@@@@@|%%%%%%%%%%%|`
More specifically, each transaction in the block is parsed and then re-encoded to bytes, and we check that the original encoding of a transaction is as the second encoding after parsing the original bytes of the transaction.
This fix keeps the legacy way of hashing transactions to create the block data hash, but also aims to check if some manipulation was done.
To understand why the fix works, we need to understand how protobuf, the wire protocol that Fabric uses to encode transactions (and almost anything it sends over the wire or writes to disk) encodes a transaction.
A transaction is a protobuf message with two fields of bytes: (1) Payload and (2) Signature.
When encoding a field of bytes, protobuf first writes a tag for the field (a byte) and then writes the length of the field in variable-length encoding, and then the bytes themselves.
For example, to encode a transaction, protobuf writes 10 (the tag for payload), then two bytes for the length of the payload, then the payload, and then 18, the tag for the signature, and then a single byte for the length of the signature, and finally the signature.
Now, we can understand a proof sketch of why my solution works:
Assume in contradiction that an adversary takes a block of transactions and changes the split of the concatenation in a way that changes the transactions for a peer:
From `|$$$$$$$$$$$$|************|@@@@@@@@@@@|...|%%%%%%%|` to (for example): From `|$$$$$$$$$$$$************|@@@@@@@@@@@|...|%%%%%%%|`
Since this split is not identical to the original split, there exists at least one transaction index of different size between the two splits. Let's look at the first transaction that is of different size.
For example, for the split:
`|$$$$$$$$$$$$|************|@@@@@@@@@@@|...|%%%%%%%|` we have two options:
1. The first transaction of different size is smaller in the new split: `|$$$$$$$$$$$$|*****|*******|@@@@@@@@@@@|...|%%%%%%%|` In such a case, it must contain both a payload and a signature, so it needs two fields (we can say we will return an error if one of the two is missing). If the protobuf parser detects it lacks bytes to parse a payload, it will fail with an error. Else, it has enough bytes to parse the payload, and then the signature is parsed. If the signature field is too short then we also error similarly.
2. The first transaction of different size is bigger in the new split: `|$$$$$$$$$$$$|************@@@@|@@@@@@@|...|%%%%%%%|`
In that case, once this transaction is parsed, the extra bytes are skipped, so encoding the transaction to bytes yields a shorter byte array, and we detect a tempering.