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 potential crash in unixfs directory #798

Merged
merged 3 commits into from
Jan 21, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The following emojis are used to highlight certain changes:

- `gateway` Fix redirect URLs for subdirectories with characters that need escaping. [#779](https://github.com/ipfs/boxo/pull/779)
- `ipns` Fix `ipns` protobuf namespace conflicts by using full package name `github.com/ipfs/boxo/ipns/pb/record.proto` instead of the generic `record.proto` [#794](https://github.com/ipfs/boxo/pull/794)
- `unixfs` Fix possible crash when modifying directory [#798](https://github.com/ipfs/boxo/pull/798)

### Security

Expand Down
29 changes: 21 additions & 8 deletions ipld/unixfs/io/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,33 +489,46 @@
// until we either reach a value above the threshold (in that case no need
// to keep counting) or an error occurs (like the context being canceled
// if we take too much time fetching the necessary shards).
func (d *HAMTDirectory) sizeBelowThreshold(ctx context.Context, sizeChange int) (below bool, err error) {
func (d *HAMTDirectory) sizeBelowThreshold(ctx context.Context, sizeChange int) (bool, error) {
if HAMTShardingSize == 0 {
panic("asked to compute HAMT size with HAMTShardingSize option off (0)")
}

// We don't necessarily compute the full size of *all* shards as we might
// end early if we already know we're above the threshold or run out of time.
partialSize := 0
var err error
below := true

// We stop the enumeration once we have enough information and exit this function.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
linkResults := d.EnumLinksAsync(ctx)

for linkResult := range d.EnumLinksAsync(ctx) {
for linkResult := range linkResults {
if linkResult.Err != nil {
return false, linkResult.Err
below = false
err = linkResult.Err
break

Check warning on line 511 in ipld/unixfs/io/directory.go

View check run for this annotation

Codecov / codecov/patch

ipld/unixfs/io/directory.go#L509-L511

Added lines #L509 - L511 were not covered by tests
}

partialSize += linksize.LinkSizeFunction(linkResult.Link.Name, linkResult.Link.Cid)
if partialSize+sizeChange >= HAMTShardingSize {
// We have already fetched enough shards to assert we are
// above the threshold, so no need to keep fetching.
return false, nil
// We have already fetched enough shards to assert we are above the
// threshold, so no need to keep fetching.
below = false
break
}
}
cancel()

// We enumerated *all* links in all shards and didn't reach the threshold.
if !below {
// Wait for channel to close so links are not being read after return.
for range linkResults {
}

Check warning on line 527 in ipld/unixfs/io/directory.go

View check run for this annotation

Codecov / codecov/patch

ipld/unixfs/io/directory.go#L527

Added line #L527 was not covered by tests
return false, err
}

// Enumerated all links in all shards before threshold reached.
return true, nil
}

Expand Down
Loading