Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit dbcc43e

Browse files
committed
fix: correctly handle degenerate hamts while reading data
Fixes GHSA-q264-w97q-q778
1 parent 323bb63 commit dbcc43e

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ipfs/go-unixfs
33
require (
44
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a
55
github.com/gogo/protobuf v1.3.2
6-
github.com/ipfs/go-bitfield v1.0.0
6+
github.com/ipfs/go-bitfield v1.1.0
77
github.com/ipfs/go-block-format v0.0.3
88
github.com/ipfs/go-blockservice v0.2.1
99
github.com/ipfs/go-cid v0.3.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
244244
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
245245
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
246246
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
247-
github.com/ipfs/go-bitfield v1.0.0 h1:y/XHm2GEmD9wKngheWNNCNL0pzrWXZwCdQGv1ikXknQ=
248-
github.com/ipfs/go-bitfield v1.0.0/go.mod h1:N/UiujQy+K+ceU1EF5EkVd1TNqevLrCQMIcAEPrdtus=
247+
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
248+
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
249249
github.com/ipfs/go-bitswap v0.5.1 h1:721YAEDBnLIrvcIMkCHCdqp34hA8jwL9yKMkyJpSpco=
250250
github.com/ipfs/go-bitswap v0.5.1/go.mod h1:P+ckC87ri1xFLvk74NlXdP0Kj9RmWAh4+H78sC6Qopo=
251251
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=

hamt/hamt.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ func makeShard(ds ipld.DAGService, size int, key string, val *ipld.Link) (*Shard
106106
if err != nil {
107107
return nil, err
108108
}
109+
childer, err := newChilder(ds, size)
110+
if err != nil {
111+
return nil, err
112+
}
109113
maxpadding := fmt.Sprintf("%X", size-1)
110114
s := &Shard{
111115
tableSizeLg2: lg2s,
112116
prefixPadStr: fmt.Sprintf("%%0%dX", len(maxpadding)),
113117
maxpadlen: len(maxpadding),
114-
childer: newChilder(ds, size),
118+
childer: childer,
115119
tableSize: size,
116120
dserv: ds,
117121

@@ -765,11 +769,21 @@ type childer struct {
765769
children []*Shard
766770
}
767771

768-
func newChilder(ds ipld.DAGService, size int) *childer {
772+
const maximumHamtWidth = 1 << 10 // FIXME: Spec this and decide of a correct value
773+
774+
func newChilder(ds ipld.DAGService, size int) (*childer, error) {
775+
if size > maximumHamtWidth {
776+
return nil, fmt.Errorf("hamt witdh (%d) exceed maximum allowed (%d)", size, maximumHamtWidth)
777+
}
778+
bf, err := bitfield.NewBitfield(size)
779+
if err != nil {
780+
return nil, err
781+
}
782+
769783
return &childer{
770784
dserv: ds,
771-
bitfield: bitfield.NewBitfield(size),
772-
}
785+
bitfield: bf,
786+
}, nil
773787
}
774788

775789
func (s *childer) makeChilder(data []byte, links []*ipld.Link) *childer {

hamt/hamt_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,10 @@ func BenchmarkHAMTSet(b *testing.B) {
737737
}
738738

739739
func TestHamtBadSize(t *testing.T) {
740-
_, err := NewShard(nil, 7)
741-
if err == nil {
742-
t.Fatal("should have failed to construct hamt with bad size")
740+
for _, size := range [...]int{-8, 7, 2, 1337, 1024 + 8, -3} {
741+
_, err := NewShard(nil, size)
742+
if err == nil {
743+
t.Error("should have failed to construct hamt with bad size: %d", size)
744+
}
743745
}
744746
}

0 commit comments

Comments
 (0)