Adding extra data check on DecodeBlockMsg

fix: types: error out on decoding BlockMsg with extraneous data
Fixes OSS-fuzz issue 48208: lotus:fuzz_block_msg

Signed-off-by: Yolan Romailler <anomalroil@users.noreply.github.com>
This commit is contained in:
Yolan Romailler 2023-05-12 21:26:26 +02:00
parent a0ebd8bb16
commit 0d7d906abf
No known key found for this signature in database
GPG Key ID: 36B611F9C2139052
2 changed files with 46 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package types
import (
"bytes"
"fmt"
"github.com/ipfs/go-cid"
)
@ -14,10 +15,13 @@ type BlockMsg struct {
func DecodeBlockMsg(b []byte) (*BlockMsg, error) {
var bm BlockMsg
if err := bm.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
data := bytes.NewReader(b)
if err := bm.UnmarshalCBOR(data); err != nil {
return nil, err
}
if l := data.Len(); l != 0 {
return nil, fmt.Errorf("extraneous data in BlockMsg CBOR encoding: got %d unexpected bytes", l)
}
return &bm, nil
}

View File

@ -0,0 +1,40 @@
package types
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecodeBlockMsg(t *testing.T) {
type args struct {
b []byte
}
tests := []struct {
name string
data []byte
want *BlockMsg
wantErr bool
}{
{"decode empty BlockMsg with extra data at the end", []byte{0x83, 0xf6, 0x80, 0x80, 0x20}, new(BlockMsg), true},
{"decode valid empty BlockMsg", []byte{0x83, 0xf6, 0x80, 0x80}, new(BlockMsg), false},
{"decode invalid cbor", []byte{0x83, 0xf6, 0x80}, nil, true},
}
for _, tt := range tests {
data := tt.data
want := tt.want
wantErr := tt.wantErr
t.Run(tt.name, func(t *testing.T) {
got, err := DecodeBlockMsg(data)
if wantErr {
assert.Errorf(t, err, "DecodeBlockMsg(%v)", data)
return
}
assert.NoErrorf(t, err, "DecodeBlockMsg(%v)", data)
assert.Equalf(t, want, got, "DecodeBlockMsg(%v)", data)
serialized, err := got.Serialize()
assert.NoErrorf(t, err, "DecodeBlockMsg(%v)", data)
assert.Equalf(t, serialized, data, "DecodeBlockMsg(%v)", data)
})
}
}