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:
parent
a0ebd8bb16
commit
0d7d906abf
@ -2,6 +2,7 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
)
|
)
|
||||||
@ -14,10 +15,13 @@ type BlockMsg struct {
|
|||||||
|
|
||||||
func DecodeBlockMsg(b []byte) (*BlockMsg, error) {
|
func DecodeBlockMsg(b []byte) (*BlockMsg, error) {
|
||||||
var bm BlockMsg
|
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
|
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
|
return &bm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
chain/types/blockmsg_test.go
Normal file
40
chain/types/blockmsg_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user