This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new bb6c6e80 fix(arrow/ipc): return errors instead of panicking on
malformed file metadata (#1651)
bb6c6e80 is described below
commit bb6c6e806ca97c9e2745230c8ab95b561bd71b77
Author: Ayaan <[email protected]>
AuthorDate: Tue Nov 24 22:24:34 2026 +0530
fix(arrow/ipc): return errors instead of panicking on malformed file
metadata (#1651)
### Rationale for this change
Three paths in `ipc.NewFileReader` panic on malformed input instead of
returning an error: unknown union mode, unknown TimeUnit, and an
out-of-range dictionary compression offset (6 ClusterFuzz inputs from
arrow-testing).
### What changes are included in this PR?
- Return an error for unknown union modes in `concreteTypeFromFB`.
- `unitFromFB` returns an error instead of panicking.
- `readDictionary` recovers flatbuffer out-of-range panics, like
`loadRecordBatch`.
### Are these changes tested?
Yes: regression test runs the 6 fuzz inputs through `NewFileReader`,
checks it returns an error, and checks nothing leaks (checked allocator;
needs `ARROW_TEST_DATA`).
### Are there any user-facing changes?
These three paths now return errors for the 6 inputs. No API changes.
Fixes #1568
---------
Signed-off-by: Ayaan <[email protected]>
---
arrow/ipc/file_reader.go | 9 ++-
arrow/ipc/ipc_file_panics_test.go | 114 ++++++++++++++++++++++++++++++++++++++
arrow/ipc/metadata.go | 26 ++++++---
3 files changed, 139 insertions(+), 10 deletions(-)
diff --git a/arrow/ipc/file_reader.go b/arrow/ipc/file_reader.go
index 61230fe4..53a813d0 100644
--- a/arrow/ipc/file_reader.go
+++ b/arrow/ipc/file_reader.go
@@ -914,7 +914,14 @@ func (ctx *arrayLoaderContext) loadUnion(dt
arrow.UnionType) arrow.ArrayData {
return array.NewData(dt, int(field.Length()), buffers, subs, 0, 0)
}
-func (d *ipcDecoder) readDictionary(meta, body *memory.Buffer)
(dictutils.Kind, error) {
+func (d *ipcDecoder) readDictionary(meta, body *memory.Buffer) (kind
dictutils.Kind, err error) {
+ // flatbuffer accessors panic on out-of-range offsets in malformed
metadata
+ defer func() {
+ if pErr := recover(); pErr != nil {
+ kind = 0
+ err = utils.FormatRecoveredError("arrow/ipc: error
reading dictionary batch", pErr)
+ }
+ }()
var (
msg = flatbuf.GetRootAsMessage(meta.Bytes(), 0)
md flatbuf.DictionaryBatch
diff --git a/arrow/ipc/ipc_file_panics_test.go
b/arrow/ipc/ipc_file_panics_test.go
new file mode 100644
index 00000000..7f352b65
--- /dev/null
+++ b/arrow/ipc/ipc_file_panics_test.go
@@ -0,0 +1,114 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ipc_test
+
+import (
+ "bytes"
+ "encoding/binary"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow/internal/flatbuf"
+ "github.com/apache/arrow-go/v18/arrow/ipc"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ flatbuffers "github.com/google/flatbuffers/go"
+)
+
+// Fuzz inputs that used to panic in NewFileReader (apache/arrow-go#1568).
+func TestFileReaderFuzzPanics(t *testing.T) {
+ dataDir := os.Getenv("ARROW_TEST_DATA")
+ if dataDir == "" {
+ t.Skip("ARROW_TEST_DATA is not set")
+ }
+ for _, name := range []string{
+
"clusterfuzz-testcase-minimized-arrow-ipc-file-fuzz-5577412021190656",
+
"clusterfuzz-testcase-minimized-arrow-ipc-file-fuzz-5693640900083712",
+
"clusterfuzz-testcase-minimized-arrow-ipc-file-fuzz-5864855240835072.fuzz",
+
"clusterfuzz-testcase-minimized-arrow-ipc-file-fuzz-5749190446153728",
+
"clusterfuzz-testcase-minimized-arrow-ipc-file-fuzz-5768644156194816",
+ "clusterfuzz-testcase-arrow-ipc-file-fuzz-5768644156194816",
+ } {
+ t.Run(name, func(t *testing.T) {
+ data, err := os.ReadFile(filepath.Join(dataDir,
"arrow-ipc-file", name))
+ if err != nil {
+ t.Fatal(err)
+ }
+ mem :=
memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+ r, err := ipc.NewFileReader(bytes.NewReader(data),
ipc.WithAllocator(mem))
+ if err == nil {
+ r.Close()
+ t.Fatal("expected an error, got success")
+ }
+ })
+ }
+}
+
+// unknownTypeSchema builds a one-field schema whose type id is unknown.
+func unknownTypeSchema(b *flatbuffers.Builder) flatbuffers.UOffsetT {
+ flatbuf.NullStart(b)
+ typ := flatbuf.NullEnd(b)
+ name := b.CreateString("f")
+ flatbuf.FieldStart(b)
+ flatbuf.FieldAddName(b, name)
+ flatbuf.FieldAddTypeType(b, flatbuf.Type(200))
+ flatbuf.FieldAddType(b, typ)
+ field := flatbuf.FieldEnd(b)
+ flatbuf.SchemaStartFieldsVector(b, 1)
+ b.PrependUOffsetT(field)
+ fields := b.EndVector(1)
+ flatbuf.SchemaStart(b)
+ flatbuf.SchemaAddFields(b, fields)
+ return flatbuf.SchemaEnd(b)
+}
+
+// A field with an unknown flatbuf type id must yield an error, not a panic.
+func TestReadersUnknownTypeID(t *testing.T) {
+ // stream: continuation marker, padded length, then a Message wrapping
the schema.
+ b := flatbuffers.NewBuilder(0)
+ schema := unknownTypeSchema(b)
+ flatbuf.MessageStart(b)
+ flatbuf.MessageAddVersion(b, flatbuf.MetadataVersionV5)
+ flatbuf.MessageAddHeaderType(b, flatbuf.MessageHeaderSchema)
+ flatbuf.MessageAddHeader(b, schema)
+ b.Finish(flatbuf.MessageEnd(b))
+ msg := b.FinishedBytes()
+ msg = append(msg, make([]byte, -len(msg)&7)...)
+ stream := binary.LittleEndian.AppendUint32(nil, 0xFFFFFFFF)
+ stream = binary.LittleEndian.AppendUint32(stream, uint32(len(msg)))
+ stream = append(stream, msg...)
+ if _, err := ipc.NewReader(bytes.NewReader(stream)); err == nil ||
!strings.Contains(err.Error(), "200") {
+ t.Errorf("NewReader: expected an error mentioning 200, got %v",
err)
+ }
+
+ // file: magic, footer holding the same schema, footer size, magic.
+ b = flatbuffers.NewBuilder(0)
+ schema = unknownTypeSchema(b)
+ flatbuf.FooterStart(b)
+ flatbuf.FooterAddVersion(b, flatbuf.MetadataVersionV5)
+ flatbuf.FooterAddSchema(b, schema)
+ b.Finish(flatbuf.FooterEnd(b))
+ footer := b.FinishedBytes()
+ file := append([]byte("ARROW1\x00\x00"), footer...)
+ file = binary.LittleEndian.AppendUint32(file, uint32(len(footer)))
+ file = append(file, "ARROW1"...)
+ if _, err := ipc.NewFileReader(bytes.NewReader(file)); err == nil ||
!strings.Contains(err.Error(), "200") {
+ t.Errorf("NewFileReader: expected an error mentioning 200, got
%v", err)
+ }
+}
diff --git a/arrow/ipc/metadata.go b/arrow/ipc/metadata.go
index 7a3a2321..f7a63915 100644
--- a/arrow/ipc/metadata.go
+++ b/arrow/ipc/metadata.go
@@ -168,18 +168,18 @@ func (blk fileBlock) section() io.Reader {
return io.NewSectionReader(blk.r, blk.offset, int64(blk.meta)+blk.body)
}
-func unitFromFB(unit flatbuf.TimeUnit) arrow.TimeUnit {
+func unitFromFB(unit flatbuf.TimeUnit) (arrow.TimeUnit, error) {
switch unit {
case flatbuf.TimeUnitSECOND:
- return arrow.Second
+ return arrow.Second, nil
case flatbuf.TimeUnitMILLISECOND:
- return arrow.Millisecond
+ return arrow.Millisecond, nil
case flatbuf.TimeUnitMICROSECOND:
- return arrow.Microsecond
+ return arrow.Microsecond, nil
case flatbuf.TimeUnitNANOSECOND:
- return arrow.Nanosecond
+ return arrow.Nanosecond, nil
default:
- panic(fmt.Errorf("arrow/ipc: invalid flatbuf.TimeUnit(%d)
value", unit))
+ return 0, fmt.Errorf("arrow/ipc: invalid flatbuf.TimeUnit(%d)
value", unit)
}
}
@@ -814,6 +814,8 @@ func concreteTypeFromFB(typ flatbuf.Type, data
flatbuffers.Table, children []arr
mode = arrow.SparseMode
case flatbuf.UnionModeDense:
mode = arrow.DenseMode
+ default:
+ return nil, fmt.Errorf("arrow/ipc: invalid union mode:
%d", dt.Mode())
}
typeIDLen := dt.TypeIdsLength()
@@ -894,7 +896,7 @@ func concreteTypeFromFB(typ flatbuf.Type, data
flatbuffers.Table, children []arr
return ret, nil
default:
- panic(fmt.Errorf("arrow/ipc: type %v not implemented",
flatbuf.EnumNamesType[typ]))
+ return nil, fmt.Errorf("arrow/ipc: type %v not implemented",
typ)
}
}
@@ -992,7 +994,10 @@ func decimalFromFB(data flatbuf.Decimal) (arrow.DataType,
error) {
func timeFromFB(data flatbuf.Time) (arrow.DataType, error) {
bw := data.BitWidth()
- unit := unitFromFB(data.Unit())
+ unit, err := unitFromFB(data.Unit())
+ if err != nil {
+ return nil, err
+ }
switch bw {
case 32:
@@ -1019,7 +1024,10 @@ func timeFromFB(data flatbuf.Time) (arrow.DataType,
error) {
}
func timestampFromFB(data flatbuf.Timestamp) (arrow.DataType, error) {
- unit := unitFromFB(data.Unit())
+ unit, err := unitFromFB(data.Unit())
+ if err != nil {
+ return nil, err
+ }
tz := string(data.Timezone())
return &arrow.TimestampType{Unit: unit, TimeZone: tz}, nil
}