Skip to content

fix(oci): guard scanBootFiles short namePrefix slice (#63)#64

Merged
CMGS merged 1 commit into
masterfrom
fix/scanbootfiles-short-label-panic
May 26, 2026
Merged

fix(oci): guard scanBootFiles short namePrefix slice (#63)#64
CMGS merged 1 commit into
masterfrom
fix/scanbootfiles-short-label-panic

Conversation

@CMGS
Copy link
Copy Markdown
Contributor

@CMGS CMGS commented May 26, 2026

Closes #63.

Problem

cocoon image import panics on any tar layer containing /boot/vmlinuz* or /boot/initrd.img*:

panic: runtime error: slice bounds out of range [:12] with length 8
  images/oci.scanBootFiles  boot.go:144
  images/oci.processTarReader import.go:145

scanBootFiles logged digestHex[:12], assuming a 64-char sha256. But the import path (import.go:145) passes a short placeholder fmt.Sprintf("import-%d", j.idx) — the real layer digest isn't known until the stream is fully hashed, and renameBootFiles relabels the extracted files to the real digest afterward. "import-0" is 8 bytes → [:12] panics, but only once a layer actually carries a boot file (the extract-log is the first slice reached).

Fix

  • Rename the param digestHexnamePrefix to reflect its real contract: a filename prefix that is a digest on the heal path but a short placeholder on import.
  • Bound the log slice with min(len(namePrefix), 12).

Scope is correct: every other [:12] in images/oci (import.go:166/176, process.go:47/88/94/97, boot.go:60) operates on a real 64-char hash (hex.EncodeToString(...) / digest.Hex()) and cannot receive a short label — verified.

Test

New images/oci/boot_test.go:

  • TestScanBootFilesNamePrefixLengths — table over empty / 8-char / 12-char / 64-char prefixes; asserts no panic + correct <prefix>.{vmlinuz,initrd.img} extraction. Reverting the slice guard reproduces the exact [:12] length 8 panic on the 8-char case.
  • TestScanBootFilesSkipsNonBoot — confirms non-boot/ dirs and .old suffixes are skipped.

Test plan

  • go test -race ./... — 24/24 packages green
  • make fmt-check && make lint (darwin + linux) — 0 issues
  • AST layout audit — 0 violations
  • Verified the new test panics pre-fix, passes post-fix

Follow-up (not in this PR)

The [:12] short-prefix idiom repeats ~11× across images/oci and images/cloudimg (all currently safe, fed real digests). A shared utils.ShortHex(s) would make the whole class panic-proof — worth a separate cleanup PR if desired.

The import path passes a short placeholder ("import-0") to scanBootFiles
since the layer digest isn't known until the stream is hashed, but the
extract-log sliced [:12] assuming a digest — panicking on any layer that
carries /boot/vmlinuz* or /boot/initrd.img*. Rename the param to namePrefix
(it's a filename prefix, not always a digest) and bound the slice with min().
Other [:12] sites are safe — they all receive the real 64-char hash.
@CMGS CMGS merged commit e56fa36 into master May 26, 2026
4 checks passed
@CMGS CMGS deleted the fix/scanbootfiles-short-label-panic branch May 26, 2026 06:41
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.

panic: slice bounds out of range [:12] in scanBootFiles when 'cocoon image import' processes a layer with /boot/vmlinuz

1 participant