Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix oppushdata overflow bug #22

Merged
merged 3 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions bscript/oppushdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,51 @@ func DecodeParts(b []byte) ([][]byte, error) {
if len(b) < 2 {
return r, errors.New("not enough data")
}
l := uint64(b[1])
if len(b) < int(2+l) {

l := int(b[1])
b = b[2:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[2 : 2+l]

part := b[:l]
r = append(r, part)
b = b[2+l:]
b = b[l:]

case OpPUSHDATA2:
if len(b) < 3 {
return r, errors.New("not enough data")
}
l := binary.LittleEndian.Uint16(b[1:])
if len(b) < int(3+l) {

l := int(binary.LittleEndian.Uint16(b[1:]))
caevv marked this conversation as resolved.
Show resolved Hide resolved

b = b[3:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[3 : 3+l]

part := b[:l]
r = append(r, part)
b = b[3+l:]
b = b[l:]

case OpPUSHDATA4:
if len(b) < 5 {
return r, errors.New("not enough data")
}
l := binary.LittleEndian.Uint32(b[1:])
if len(b) < int(5+l) {

l := int(binary.LittleEndian.Uint32(b[1:]))

b = b[5:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[5 : 5+l]

part := b[:l]
r = append(r, part)
b = b[5+l:]
b = b[l:]

default:
if b[0] >= 0x01 && b[0] <= OpPUSHDATA4 {
Expand Down
28 changes: 27 additions & 1 deletion bscript/oppushdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/hex"
"testing"

"github.com/libsv/go-bt/bscript"
"github.com/stretchr/testify/assert"

"github.com/libsv/go-bt/bscript"
)

func TestDecodeParts(t *testing.T) {
Expand Down Expand Up @@ -98,6 +99,31 @@ func TestDecodeParts(t *testing.T) {
assert.Equal(t, 0, len(decoded))
})

t.Run("invalid decode using OP_PUSHDATA2 - overflow", func(t *testing.T) {

b := make([]byte, 0)
b = append(b, bscript.OpPUSHDATA2)
b = append(b, 0xff)
b = append(b, 0xff)

bigScript := make([]byte, 0xffff)

b = append(b, bigScript...)

t.Logf("Script len is %d", len(b))

defer func() {
if r := recover(); r != nil {
t.Errorf("Panic detected: %v", r)
}
}()

_, err := bscript.DecodeParts(b)
if err != nil {
t.Error(err)
}
})

t.Run("invalid decode using OP_PUSHDATA4 - payload too small", func(t *testing.T) {

data := "testing the code OP_PUSHDATA4"
Expand Down