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

bam,sam: fix zero and negativish value handling #110

Merged
merged 2 commits into from
Oct 17, 2017
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
69 changes: 69 additions & 0 deletions bam/bam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2356,3 +2356,72 @@ func (s *S) TestIndexRoundtrip(c *check.C) {
c.Check(got, check.DeepEquals, expect, check.Commentf("Test %d", i))
}
}

var fuzzCrashers = []string{
// lText
"BAM\x01000\x86",
"BAM\x01\x00\x00\x00v",
"BAM\x01000\xef",

// nRef
"BAM\x01\x00\x00\x00\x00000\xbd",
"BAM\x01\x00\x00\x00\x000000",
"BAM\x01\x00\x00\x00\x000000\x00\x00\x00\x000",
"BAM\x01\x00\x00\x00\x00000\xbf",

// lName
"BAM\x01\x00\x00\x00\x000000\x00\x00\x00\x000",
"BAM\x01\x00\x00\x00\x00000\xbf",

//reader.go 412
"BAM\x01\xbf\xbdC_t\x0efRRUAe\x80t.V",
"BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00000\xbf" +
"e.Ce&Cell",
"BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00000\xe9",

// Zero size buffer.
"BAM\x01\x00\x00\x00\x00\x00\x9f7R",
// Negative size buffer
"BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",

// Short reads.
"BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x000",
"BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\b\x00\x00\x000000" +
"0000",
}

func TestFuzzCrashers(t *testing.T) {
for i, test := range fuzzCrashers {
func() {
i := i
defer func() {
r := recover()
if r != nil {
t.Errorf("unexpected panic for crasher %d: %v", i, r)
panic(r)
}
}()
var buf bytes.Buffer
w := bgzf.NewWriter(&buf, 1)
_, err := w.Write([]byte(test))
if err != nil {
t.Errorf("unexpected error preparing buffer: %v", err)
}
err = w.Close()
if err != nil {
t.Errorf("unexpected error closing buffer: %v", err)
}

r, err := NewReader(&buf, 1)
if err != nil {
return
}
for {
_, err := r.Read()
if err != nil {
break
}
}
}()
}
}
49 changes: 49 additions & 0 deletions bam/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,17 @@ func parseAux(aux []byte) []sam.Aux {
type buffer struct {
off int
data []byte
err error
}

func (b *buffer) bytes(n int) []byte {
if b.err != nil {
return nil
}
if b.len() < n {
b.err = io.ErrUnexpectedEOF
return nil
}
s := b.off
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to check that b.len() < n + b.off ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b.len() is the number of remaining bytes in the buffer (see here).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. right.

b.off += n
return b.data[s:b.off]
Expand All @@ -368,23 +376,58 @@ func (b *buffer) len() int {
}

func (b *buffer) discard(n int) {
if b.err != nil {
return
}
if b.len() < n {
b.err = io.ErrUnexpectedEOF
return
}
b.off += n
}

func (b *buffer) readUint8() uint8 {
if b.err != nil {
return 0
}
if b.len() < 1 {
b.err = io.ErrUnexpectedEOF
return 0
}
b.off++
return b.data[b.off-1]
}

func (b *buffer) readUint16() uint16 {
if b.err != nil {
return 0
}
if b.len() < 2 {
b.err = io.ErrUnexpectedEOF
return 0
}
return binary.LittleEndian.Uint16(b.bytes(2))
}

func (b *buffer) readInt32() int32 {
if b.err != nil {
return 0
}
if b.len() < 4 {
b.err = io.ErrUnexpectedEOF
return 0
}
return int32(binary.LittleEndian.Uint32(b.bytes(4)))
}

func (b *buffer) readUint32() uint32 {
if b.err != nil {
return 0
}
if b.len() < 4 {
b.err = io.ErrUnexpectedEOF
return 0
}
return binary.LittleEndian.Uint32(b.bytes(4))
}

Expand All @@ -406,6 +449,12 @@ func newBuffer(br *Reader) (*buffer, error) {
}
b := &buffer{data: br.buf[:4]}
size := int(b.readInt32())
if size == 0 {
return nil, io.EOF
}
if size < 0 {
return nil, errors.New("bam: invalid record: invalid block size")
}
b.off, b.data = 0, make([]byte, size)
n, err = io.ReadFull(br.r, b.data)
if err != nil {
Expand Down
26 changes: 23 additions & 3 deletions sam/parse_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (bh *Header) DecodeBinary(r io.Reader) error {
if err != nil {
return err
}
if lText < 0 {
return errors.New("sam: invalid text length")
}
text := make([]byte, lText)
n, err := r.Read(text)
if err != nil {
Expand All @@ -65,6 +68,9 @@ func (bh *Header) DecodeBinary(r io.Reader) error {
if err != nil {
return err
}
if nRef < 0 {
return errors.New("sam: invalid reference count field")
}
refs, err := readRefRecords(r, nRef)
if err != nil {
return err
Expand All @@ -79,17 +85,24 @@ func (bh *Header) DecodeBinary(r io.Reader) error {
}

func readRefRecords(r io.Reader, n int32) ([]*Reference, error) {
rr := make([]*Reference, n)
// bootstrapSize is the maximum number of
// reference records to pre-allocate.
const bootstrapSize = 1000

rr := make([]*Reference, 0, min(n, bootstrapSize))
var (
lName int32
err error
)
for i := range rr {
rr[i] = &Reference{id: int32(i)}
for i := 0; i < int(n); i++ {
rr = append(rr, &Reference{id: int32(i)})
err = binary.Read(r, binary.LittleEndian, &lName)
if err != nil {
return nil, err
}
if lName < 1 {
return nil, errors.New("sam: invalid name length")
}
name := make([]byte, lName)
n, err := r.Read(name)
if err != nil {
Expand All @@ -107,6 +120,13 @@ func readRefRecords(r io.Reader, n int32) ([]*Reference, error) {
return rr, nil
}

func min(a, b int32) int32 {
if a < b {
return a
}
return b
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (bh *Header) UnmarshalText(text []byte) error {
if bh.seenRefs == nil {
Expand Down