Skip to content

tarball: return error instead of panicking on missing rootfs.diff_ids - #2304

Merged
Subserial merged 1 commit into
google:mainfrom
iahsanGill:fix/tarball-empty-diffids-panic
May 19, 2026
Merged

Subserial merged 1 commit into
google:mainfrom
iahsanGill:fix/tarball-empty-diffids-panic

Conversation

@iahsanGill

Copy link
Copy Markdown
Contributor

Summary

Fixes #2192.

compressedImage.Manifest() indexed cfg.RootFS.DiffIDs[i] inside the layer loop without checking the slice length. When the embedded config has fewer diff_ids than the tarball manifest has layers, the index goes out of range and the process panics. The canonical case reported in #2192 is a buildkit cacheconfig image (application/vnd.buildkit.cacheconfig.v0) loaded via tarball.ImageFromPath and then walked with Layers().

Stack from the report:

panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/google/go-containerregistry/pkg/v1/tarball.(*compressedImage).Manifest(...)
    pkg/v1/tarball/image.go:368

Fix

Add a length check before the index and return a descriptive error. Callers (e.g. partial.FSLayers, Validate, user code) then surface the failure cleanly instead of crashing.

if i >= len(cfg.RootFS.DiffIDs) {
    return nil, fmt.Errorf("tarball manifest references %d layer(s) but config has %d rootfs.diff_ids; ...", ...)
}

This intentionally does not try to support cacheconfig as an image, only to fail safely. Supporting non-image config media types is a separate design discussion.

Test plan

  • New TestManifestEmptyDiffIDs in pkg/v1/tarball/image_test.go builds an in-memory docker-save tarball whose config has rootfs.diff_ids: [] and a single (gzip-compressed) layer, then asserts Manifest() returns a descriptive error rather than panicking.
  • go test ./pkg/v1/tarball/... -count=1 — all existing tests still pass.
  • go vet ./pkg/v1/tarball/... — clean.

Fixes google#2192.

compressedImage.Manifest() indexed cfg.RootFS.DiffIDs[i] without
checking length, panicking with "index out of range" when the embedded
config has fewer diff_ids than the manifest has layers (e.g. a buildkit
cacheconfig: application/vnd.buildkit.cacheconfig.v0).

Add the bounds check and return a descriptive error so callers can
handle the case (validate, surface to user) instead of crashing.

Signed-off-by: iahsanGill <[email protected]>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.92%. Comparing base (53f7e39) to head (df5e494).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2304   +/-   ##
=======================================
  Coverage   56.92%   56.92%           
=======================================
  Files         165      165           
  Lines       11333    11335    +2     
=======================================
+ Hits         6451     6453    +2     
  Misses       4119     4119           
  Partials      763      763           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Subserial
Subserial merged commit a3a74df into google:main May 19, 2026
17 checks passed
Subserial pushed a commit that referenced this pull request Jul 25, 2026
The compressed side got this guard in #2304. LayerByDiffID walks the
config's rootfs.diff_ids and indexes imgDescriptor.Layers with the same
index, so a config declaring more diff_ids than manifest.json lists
layers panics with an index out of range instead of returning an error.

Signed-off-by: Arpit Jain <[email protected]>
Subserial pushed a commit that referenced this pull request Jul 29, 2026
createAddendums walks a config's history and advances a separate layer
index only for non-empty history entries, then indexes layers[layerIndex]
with no bounds check. A config whose history advertises more non-empty
entries than the image has layers panics with an index out of range.

Rebase feeds both newConfig.History/newBaseLayers and
origConfig.History/origLayers straight in, and both configs come from
pulled images, so a crafted base image crashes crane rebase and anything
else embedding mutate.Rebase. Return an error describing the mismatch
instead, which matches how #2304 handled the same class on the tarball
side.

Also fix the leftover-layer loop, which appended layers[layerIndex]
instead of layers[i]. layerIndex is frozen after the history walk, so a
base image with layers but no history had its first leftover layer
repeated in place of the rest.

Adds a test for each case against the exported Rebase entry point.

Signed-off-by: Arpit Jain <[email protected]>
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
crane 0.21.7

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* tarball: return error instead of panicking on missing rootfs.diff_ids by @iahsanGill in google/go-containerregistry#2304
* gcrane: honor --platform flag in copy by @iahsanGill in google/go-containerregistry#2307
* mutate: verify layer digests in Extract and Time by @momenashrafff in google/go-containerregistry#2303
* tarball: close layer readers during Write by @nandbhat in google/go-containerregistry#2308
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2311
* build(deps): bump github.com/docker/cli from 29.4.3+incompatible to 29.5.2+incompatible in the go-deps group across 1 directory by @dependabot[bot] in google/go-containerregistry#2312
* BUGFIX: Fail with error when read exceeds maximum by @inteon in google/go-containerregistry#2328
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2327
* fix(name): anchor loopback registry detection by @rohan-patnaik in google/go-containerregistry#2314
* Reject symlinks in OCI layout blobs by @mosskappa in google/go-containerregistry#2306
* fix(crane): avoid creating export tar on pull failure by @Haihan-Jiang in google/go-containerregistry#2318
* feat(kubernetes): allow ignoring pull secrets by @rohan-patnaik in google/go-containerregistry#2315
* fix(name): preserve localhost registry references by @rohan-patnaik in google/go-containerregistry#2316
* pkg/registry: export ErrNotFound by @malt3 in google/go-containerregistry#2176
* pkg/registry: export RedirectError by @malt3 in google/go-containerregistry#2177
* build(deps): bump the go-deps group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2343
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2344
* fix: prevent SSRF in google.List() pagination by @tufstraka in google/go-containerregistry#2332
* internal/gzip: fix goroutine leak in ReadCloserLevel by @amarkdotdev in google/go-containerregistry#2347
* fix(transport): apply refreshed bearer token after cross-host redirect by @64johnlee in google/go-containerregistry#2337
* build(deps): bump the go-deps group across 3 directories with 4 updates by @dependabot[bot] in google/go-containerregistry#2348
* fix(tarball): normalize paths when matching files by @bstoll in google/go-containerregistry#2334
* transport: do not re-attach bearer token after cross-host redirect by @evilgensec in google/go-containerregistry#2349
* Bump CI go version to 1.26.4 by @Subserial in google/go-containerregistry#2350

## New Contributors
* @momenashrafff made their first contribution in google/go-containerregistry#2303
* @nandbhat made their first contribution in google/go-containerregistry#2308
* @inteon made their first contribution in google/go-containerregistry#2328
* @rohan-patnaik made their first contribution in google/go-containerregistry#2314
* @mosskappa made their first contribution in google/go-containerregistry#2306
* @Haihan-Jiang made their first contribution in google/go-containerregistry#2318
* @tufstraka made their first contribution in google/go-containerregistry#2332
* @amarkdotdev made their first contribution in google/go-containerregistry#2347
* @64johnlee made their first contribution in google/go-containerregistry#2337
* @bstoll made their first contribution in google/go-containerregistry#2334

**Full Changelog**: https://github.com/google/go-containerregistry/compare/v0.21.6...v0.21.7</pre>
  <p>View the full release notes at <a href="https://github.com/google/go-containerregistry/releases/tag/v0.21.7">https://github.com/google/go-containerregistry/releases/tag/v0.21.7</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ggcr: panic on getting image layers with config application/vnd.buildkit.cacheconfig.v0 from tar

3 participants